1.åå°ç±»ç详ç»ç®ä»
åå°ç±»ç详ç»ç®ä»
åå¦è ç类使ç¨åå°çå¯ç¹æ»æ¯ java.lang.Class å®ä¾ãå¦ææ¨å¸æä¸é¢å å®ä¹çç±»åä½ï¼é£ä¹Javaè¯è¨æä¾ä¸ç§ç´æ¥è·å¾ Class å®ä¾çç®ä¾¿å¿«æ·æ¹å¼ï¼
Class clas = MyClass.class;
å½æ¨ä½¿ç¨è¿ä¸é¡¹ææ¯æ¶ï¼è£ å ¥ç±»æ¶åçææå·¥ä½å¨å¹åè¿è¡ãä½æ¯ï¼å¦ææ¨éè¦å¨è¿è¡æ¶ä»æäºå¤é¨æºè¯»åç±»åï¼è¿ç§æ¹æ³å¹¶ä¸éåãå®é ä¸ï¼æ¨éè¦ä½¿ç¨ä¸ä¸ªç±»è£ å ¥å¨æ¥æ¥æ¾ç±»ä¿¡æ¯ã以ä¸ä»ç»ä¸ç§æ¹æ³ï¼
// name is 卡蜜出售源码the class name to loadClass clas = null;try { clas = Class.forName(name);} catch (ClassNotFoundException ex) { // handle exception case}// use the loaded class
å¦æå·²ç»è£ å ¥äºç±»ï¼æ¨å°å¾å°ç°æç Class ä¿¡æ¯ãå¦æç±»æªè¢«è£ å ¥ï¼ç±»è£ å ¥å¨å°ç°å¨è£ å ¥å¹¶è¿åæ°å建çç±»å®ä¾ã
ç±»çåå°
Class 对象为æ¨æä¾æ¥å ¥ç±»å æ°æ®çåå°çææåºæ¬hookãè¿ç±»å æ°æ®å æ¬å ³äºç±»èªèº«çä¿¡æ¯ï¼å¦å åç±»çç¶ç±»ï¼ä»¥å该类å®æ½çæ¥å£ãå®è¿å æ¬è¯¥ç±»å®ä¹çæé å½æ°ãå段åæ¹æ³ç详ç»ä¿¡æ¯ãè¿äºæåç项ç®é½æ¯ç¼ç¨ä¸æç»å¸¸ä½¿ç¨ç项ç®ï¼å æ¤æå°å¨æ¬å°èçç¨åé¨åç»åºä¸äºä¸å®ä»¬åä½çå®ä¾ã
对äºä»¥ä¸ä¸ç±»ç»ä»¶ä¸çä»»ä½ä¸ç±»æ¥è¯´ -- æé å½æ°ãå段åæ¹æ³ -- java.lang.Class æä¾åç§ç¬ç«çåå°è°ç¨ï¼ä»¥ä¸åçæ¹å¼æ¥è·å¾ä¿¡æ¯ãè°ç¨é½éµå¾ªä¸ç§æ åæ ¼å¼ã以ä¸æ¯ç¨äºæ¥æ¾æé å½æ°çä¸ç»åå°è°ç¨ï¼
Constructor getConstructor(Class[] params) -- è·å¾ä½¿ç¨ç¹æ®çåæ°ç±»åçå ¬å ±æé å½æ°ï¼
Constructor[] getConstructors() -- è·å¾ç±»çææå ¬å ±æé å½æ°
Constructor getDeclaredConstructor(Class[] params) -- è·å¾ä½¿ç¨ç¹å®åæ°ç±»åçæé å½æ°(ä¸æ¥å ¥çº§å«æ å ³)
Constructor[] getDeclaredConstructors() -- è·å¾ç±»çæææé å½æ°(ä¸æ¥å ¥çº§å«æ å ³)
æ¯ç±»è¿äºè°ç¨é½è¿åä¸ä¸ªæå¤ä¸ª java.lang.reflect.Constructor å½æ°ãè¿ç§ Constructor ç±»å®ä¹ newInstance æ¹æ³ï¼å®éç¨ä¸ç»å¯¹è±¡ä½ä¸ºå ¶å¯ä¸çåæ°ï¼ç¶åè¿åæ°å建çåå§ç±»å®ä¾ã该ç»å¯¹è±¡æ¯ç¨äºæé å½æ°è°ç¨çåæ°å¼ãä½ä¸ºè§£éè¿ä¸å·¥ä½æµç¨çå®ä¾ï¼å设æ¨æä¸ä¸ª TwoString ç±»åä¸ä¸ªä½¿ç¨ä¸å¯¹ String sçæé å½æ°ï¼å¦æ¸ å1æ示ï¼
æ¸ å1:ä»ä¸å¯¹å符串å建çç±»
public class TwoString { private String m_s1, m_s2; public TwoString(String s1, String s2) { m_s1 = s1; m_s2 = s2; }}
æ¸ å2ä¸ç代ç è·å¾æé å½æ°å¹¶ä½¿ç¨å®æ¥åå»ºä½¿ç¨ String s a å b ç TwoString ç±»çä¸ä¸ªå®ä¾:
æ¸ å2ï¼æé å½æ°çåå°è°ç¨
Class[] types = new Class[] { String.class, String.class }; Constructor cons = TwoString.class.getConstructor(types); Object[] args = new Object[] { a, b }; TwoString ts = cons.newInstance(args);
æ¸ å2ä¸ç代ç 忽ç¥äºä¸ååå°æ¹æ³æåºçå¤ç§å¯è½éä¸çä¾å¤ç±»åãä¾å¤å¨ Javadoc API æè¿°ä¸è¯¦ç»è®°å½ï¼å æ¤ä¸ºäºç®æèµ·è§ï¼æå°å¨ææç¨åºå®ä¾ä¸å¿½ç¥å®ä»¬ã
尽管æå¨è®¨è®ºæé å½æ°ä¸»é¢ï¼Javaç¼ç¨è¯è¨è¿å®ä¹äºä¸ç§æ¨å¯ä»¥ç¨æ¥ä½¿ç¨ æ åæ°ï¼æ缺çï¼æé å½æ°å建类çä¸ä¸ªå®ä¾çç¹æ®å¿«æ·æ¹å¼ãè¿ç§å¿«æ·æ¹å¼åµå ¥å° Class å®ä¹ä¸ï¼å¦ä¸ï¼
Object newInstance() -- 使ç¨ç¼ºçå½æ°å建æ°çå®ä¾
å³ä½¿è¿ç§æ¹æ³åªå 许æ¨ä½¿ç¨ä¸ç§ç¹æ®çæé å½æ°ï¼å¦æè¿æ£æ¯æ¨éè¦çï¼é£ä¹å®å°æä¾ä¸ç§é常æ¹ä¾¿çå¿«æ·æ¹å¼ãå½ä¸JavaBeansåä½æ¶è¿é¡¹ææ¯å°¤å ¶æç¨ï¼JavaBeanséè¦å®ä¹å ¬å ±ãæ åæ°æé å½æ°ã
éè¿åå°å¢å å段
è·å¾å段信æ¯ç Class åå°è°ç¨ä¸åäºé£äºç¨äºæ¥å ¥æé å½æ°çè°ç¨ï¼å¨åæ°ç±»åæ°ç»ä¸ä½¿ç¨äºå段åï¼
Field getField(String name) -- è·å¾å½åçå ¬å ±å段
Field[] getFields() -- è·å¾ç±»çææå ¬å ±å段
Field getDeclaredField(String name) -- è·å¾ç±»å£°æçå½åçå段
Field[] getDeclaredFields() -- è·å¾ç±»å£°æçææå段
尽管ä¸æé å½æ°è°ç¨ç±»ä¼¼ï¼å¨å段æ¹é¢ä»åå¨ä¸ä¸ªéè¦çåºå«ï¼å两个åéè¿åå¯ä»¥éè¿ç±»æ¥å ¥çå ¬å ±å段çä¿¡æ¯ -- å³ä½¿å®ä»¬æ¥èªäºç¥å ç±»ãå两个åéè¿åç±»ç´æ¥å£°æçå段çä¿¡æ¯ -- ä¸å段çæ¥å ¥ç±»åæ å ³ã
è°ç¨è¿åç java.lang.reflect.Field å®ä¾å®ä¹ææ主类åç getXXX å setXXX æ¹æ³ï¼
æç« åºå¤ï¼/course/3_program/java/javashl//.html
以åä¸å¯¹è±¡å¼ç¨åä½çéç¨ get å set æ¹æ³ãæ¨å¯ä»¥æ ¹æ®å®é çå段类åèªè¡éæ©ä¸ç§éå½çæ¹æ³ï¼è getXXX æ¹æ³å°èªå¨å¤çæ©å±è½¬æ¢(å¦ä½¿ç¨ getInt æ¹æ³æ¥æ£ç´¢ä¸ä¸ªåèå¼)ã
æ¸ å3æ¾ç¤ºä½¿ç¨å段åå°æ¹æ³çä¸ä¸ªå®ä¾ï¼ä»¥æ¹æ³çæ ¼å¼æ ¹æ®å称å¢å 对象ç int å段 ï¼
æ¸ å3ï¼éè¿åå°å¢å ä¸ä¸ªå段
public int incrementField(String name, Object obj) throws... { Field field = obj.getClass().getDeclaredField(name); int value = field.getInt(obj) + 1; field.setInt(obj, value); return value;}
è¿ç§æ¹æ³å¼å§å±ç¤ºäºåå°å¸¦æ¥çæäºçµæ´»æ§ãä¸ç¹å®çç±»åä½ä¸åï¼ incrementField 使ç¨ä¼ å ¥ç对象ç getClass æ¹æ³æ¥æ¥æ¾ç±»ä¿¡æ¯ï¼ç¶åç´æ¥å¨è¯¥ç±»ä¸æ¥æ¾å½åçå段ã
éè¿åå°å¢å æ¹æ³
è·å¾æ¹æ³ä¿¡æ¯ç Class åå°è°ç¨ä¸ç¨äºæé å½æ°åå段çè°ç¨é常类似ï¼
Method getMethod(String name, Class[] params) -- 使ç¨ç¹å®çåæ°ç±»åï¼è·å¾å½åçå ¬å ±æ¹æ³
Method[] getMethods() -- è·å¾ç±»çææå ¬å ±æ¹æ³
Method getDeclaredMethod(String name, Class[] params) -- 使ç¨ç¹åçåæ°ç±»åï¼è·å¾ç±»å£°æçå½åçæ¹æ³
Method[] getDeclaredMethods() -- è·å¾ç±»å£°æçæææ¹æ³
ä¸å段è°ç¨ä¸æ ·ï¼å两个åéè¿åå¯ä»¥éè¿ç±»æ¥å ¥çå ¬å ±æ¹æ³çä¿¡æ¯ -- å³ä½¿å®ä»¬æ¥èªäºç¥å ç±»ãå两个åéè¿å类声æçæ¹æ³çä¿¡æ¯ï¼ä¸æ¹æ³çæ¥å ¥ç±»åæ å ³ã
è°ç¨è¿åç java.lang.reflect.Method å®ä¾å®ä¹ä¸ç§ invoke æ¹æ³ï¼æ¨å¯ä»¥ç¨æ¥å¨æ£å¨å®ä¹çç±»çä¸ä¸ªå®ä¾ä¸è°ç¨æ¹æ³ãè¿ç§ invoke æ¹æ³ä½¿ç¨ä¸¤ä¸ªåæ°ï¼ä¸ºè°ç¨æä¾ç±»å®ä¾ååæ°å¼æ°ç»ã
æ¸ å4è¿ä¸æ¥éè¿°å段å®ä¾ï¼æ¾ç¤ºåå°æ£å¨è¿è¡çæ¹æ³çä¸ä¸ªå®ä¾ãè¿ç§æ¹æ³å¢å ä¸ä¸ªå®ä¹æ get å set æ¹æ³ç int JavaBeanå±æ§ãä¾å¦ï¼å¦æ对象为ä¸ä¸ªæ´æ° count å¼å®ä¹äº getCount å setCount æ¹æ³ï¼æ¨å¯ä»¥å¨ä¸æ¬¡è°ç¨ä¸å该æ¹æ³ä¼ éâcountâä½ä¸º name åæ°ï¼ä»¥å¢å 该å¼ã
æ¸ å4ï¼éè¿åå°å¢å ä¸ä¸ªJavaBean å±æ§
public int incrementProperty(String name, Object obj) { String prop = Character.toUpperCase(name.charAt(0)) + name.substring(1); String mname = get + prop; Class[] types = new Class[] { }; Method method = obj.getClass().getMethod(mname, types); Object result = method.invoke(obj, new Object[0]); int value = ((Integer)result).intValue() + 1; mname = set + prop; types = new Class[] { int.class }; method = obj.getClass().getMethod(mname, types); method.invoke(obj, new Object[] { new Integer(value) }); return value;}
为äºéµå¾ªJavaBeansæ¯ä¾ï¼ææå±æ§åçé¦åæ¯æ¹ä¸ºå¤§åï¼ç¶åé¢å èè get æ¥å建读æ¹æ³åï¼ set æ¥å建åæ¹æ³åãJavaBeans读æ¹æ³ä» è¿åå¼ï¼èåæ¹æ³ä½¿ç¨å¼ä½ä¸ºå¯ä¸çåæ°ï¼å æ¤æè§å®æ¹æ³çåæ°ç±»å以è¿è¡å¹é ãæåï¼è¯¥æ¯ä¾è¦æ±æ¹æ³ä¸ºå ¬å ±ï¼å æ¤æ使ç¨æ¥æ¾æ ¼å¼ï¼æ¥æ¾ç±»ä¸å¯è°ç¨çå ¬å ±æ¹æ³ã
è¿ä¸å®ä¾æ¯ç¬¬ä¸ä¸ªæ使ç¨åå°ä¼ é主å¼çå®ä¾ï¼å æ¤ç°å¨æ们æ¥ççå®æ¯å¦ä½å·¥ä½çãåºæ¬åçå¾ç®åï¼æ 论ä»ä¹æ¶åæ¨éè¦ä¼ é主å¼ï¼åªéç¨ç¸åºå°è£ ç±»çä¸ä¸ªå®ä¾ï¼å¨ java.lang å ä¸å®ä¹ï¼æ¥æ¿æ¢è¯¥ç±»ä¸»å¼ãè¿å¯ä»¥åºç¨äºè°ç¨åè¿åãå æ¤ï¼å½æå¨å®ä¾ä¸è°ç¨ get æ¹æ³æ¶ï¼æé¢è®¡ç»æ为å®é int å±æ§å¼ç java.lang.Integer å°è£ ã
åå°æ°ç»
æ°ç»æ¯Javaç¼ç¨è¯è¨ä¸ç对象ãä¸ææ对象ä¸æ ·ï¼å®ä»¬é½æç±»ãå¦ææ¨æä¸ä¸ªæ°ç»ï¼ä½¿ç¨æ å getClass æ¹æ³ï¼æ¨å¯ä»¥è·å¾è¯¥æ°ç»çç±»ï¼å°±è±¡ä»»ä½å ¶å®å¯¹è±¡ä¸æ ·ãä½æ¯ï¼ ä¸éè¿ç°æçå®ä¾æ¥è·å¾ç±»ä¸åäºå ¶å®ç±»åç对象ãå³ä½¿æ¨æä¸ä¸ªæ°ç»ç±»ï¼æ¨ä¹ä¸è½ç´æ¥å¯¹å®è¿è¡å¤ªå¤çæä½ -- åå°ä¸ºæ åç±»æä¾çæé å½æ°æ¥å ¥ä¸è½ç¨äºæ°ç»ï¼èä¸æ°ç»æ²¡æä»»ä½å¯æ¥å ¥çå段ï¼åªæåºæ¬ç java.lang.Object æ¹æ³å®ä¹ç¨äºæ°ç»å¯¹è±¡ã
æ°ç»çç¹æ®å¤çä½¿ç¨ java.lang.reflect.Array ç±»æä¾çéææ¹æ³çéåã该类ä¸çæ¹æ³ä½¿æ¨è½å¤å建æ°æ°ç»ï¼è·å¾æ°ç»å¯¹è±¡çé¿åº¦ï¼è¯»ååæ°ç»å¯¹è±¡çç´¢å¼å¼ã
æ¸ å5æ¾ç¤ºäºä¸ç§éæ°è°æ´ç°ææ°ç»å¤§å°çæææ¹æ³ãå®ä½¿ç¨åå°æ¥å建ç¸åç±»åçæ°æ°ç»ï¼ç¶åå¨è¿åæ°æ°ç»ä¹åï¼å¨èæ°ç»ä¸å¤å¶æææ°æ®ã
æ¸ å 5ï¼éè¿åå°æ¥æ©å±ä¸ä¸ªæ°ç»
public Object growArray(Object array, int size) { Class type = array.getClass().getComponentType(); Object grown = Array.newInstance(type, size); System.arraycopy(array, 0, grown, 0, Math.min(Array.getLength(array), size)); return grown;}
å®å ¨æ§ååå°
å¨å¤çåå°æ¶å®å ¨æ§æ¯ä¸ä¸ªè¾å¤æçé®é¢ãåå°ç»å¸¸ç±æ¡æ¶å代ç 使ç¨ï¼ç±äºè¿ä¸ç¹ï¼æ¨å¯è½å¸ææ¡æ¶è½å¤å ¨é¢æ¥å ¥æ¨ç代ç ï¼æ éèè常è§çæ¥å ¥éå¶ãä½æ¯ï¼å¨å ¶å®æ åµä¸ï¼ä¸åæ§å¶çæ¥å ¥ä¼å¸¦æ¥ä¸¥éçå®å ¨æ§é£é©ï¼å¦å½ä»£ç å¨ä¸å¼å¾ä¿¡ä»»ç代ç å ±äº«çç¯å¢ä¸è¿è¡æ¶ã
ç±äºè¿äºäºç¸çç¾çéæ±ï¼Javaç¼ç¨è¯è¨å®ä¹ä¸ç§å¤çº§å«æ¹æ³æ¥å¤çåå°çå®å ¨æ§ãåºæ¬æ¨¡å¼æ¯å¯¹åå°å®æ½ä¸åºç¨äºæºä»£ç æ¥å ¥ç¸åççéå¶ï¼
ä»ä»»æä½ç½®å°ç±»å ¬å ±ç»ä»¶çæ¥å ¥
ç±»èªèº«å¤é¨æ ä»»ä½å°ç§æç»ä»¶çæ¥å ¥
åä¿æ¤åæå ï¼ç¼ºçæ¥å ¥ï¼ç»ä»¶çæéæ¥å ¥
ä¸è¿ï¼è³å°æäºæ¶åï¼å´ç»è¿äºéå¶æä¸ç§ç®åçæ¹æ³ãæå¨åé¢å®ä¾ä¸ä½¿ç¨ç Constructor ã Field å Method ç±»é½æ©å±äºä¸ä¸ªæ®éçåºæ¬ç±»-- java.lang.reflect.AccessibleObject ç±»ã该类å®ä¹ä¸ç§ setAccessible æ¹æ³ï¼ä½¿æ¨è½å¤å¯å¨æå ³é对è¿äºç±»ä¸å ¶ä¸ä¸ä¸ªç±»çå®ä¾çæ¥å ¥æ£æµãå¯ä¸çé®é¢å¨äºå¦æ使ç¨äºå®å ¨æ§ç®¡çå¨ï¼å®å°æ£æµæ£å¨å ³éæ¥å ¥æ£æµç代ç æ¯å¦è®¸å¯äºè¿æ ·åãå¦ææªè®¸å¯ï¼å®å ¨æ§ç®¡çå¨æåºä¸ä¸ªä¾å¤ã
æ¸ å6å±ç¤ºäºä¸ä¸ªç¨åºï¼å¨ æ¸ å 1 TwoString ç±»çä¸ä¸ªå®ä¾ä¸ä½¿ç¨åå°æ¥æ¾ç¤ºå®å ¨æ§æ£å¨è¿è¡ï¼
æç« åºå¤ï¼/course/3_program/java/javashl//_2.html