java·´ÉäµÄÀý×Ó
package cn.test;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Java Reflection Cookbook
*
* @author Michael Lee
* @since 2006-8-23
* @version 0.1a
*/
public class TestReflection {
/**
* µÃµ½Ä³¸ö¶ÔÏóµÄ¹«¹²ÊôÐÔ
*
* @param owner, fieldName
* @return ¸ÃÊôÐÔ¶ÔÏó
* @throws Exception
*
*/
public Object getProperty(Object owner, String fieldName) throws Exception {
Class ownerClass = owner.getClass();
Field field = ownerClass.getField(fieldName);
Object property = field.get(owner);
return property;
}
/**
* µÃµ½Ä³ÀàµÄ¾²Ì¬¹«¹²ÊôÐÔ
*
* @param className ˈ̞
* @param fieldName ÊôÐÔÃû
* @return ¸ÃÊôÐÔ¶ÔÏó
* @throws Exception
*/
public Object getStaticProperty(String className, String fieldName)
throws Exception {
Class ownerClass = Class.forName(className);
Field field = ownerClass.getField(fieldName);
Object property = field.get(ownerClass);
return property;
}
/**
* Ö´ÐÐij¶ÔÏó·½·¨
*
* @param owner
* ¶ÔÏó
* @param methodName
* ·½·¨Ãû
* @param args
* ²ÎÊý
* @return ·½·¨·µ»ØÖµ
* @throws Exception
*/
public Object invokeMethod(Object owner, String methodName, Object[] args)
throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass
Ïà¹ØÎĵµ£º
ÀàµÄ³õʼ»¯ºÍ¶ÔÏó³õʼ»¯ÊÇ JVM ¹ÜÀíµÄÀàÐÍÉúÃüÖÜÆÚÖзdz£ÖØÒªµÄÁ½¸ö»·½Ú£¬Google ÁËÒ»±éÍøÂ磬ÓйØÀà×°ÔØ»úÖƵÄÎÄÕµ¹ÊDz»ÉÙ£¬È»¶øÀà³õʼ»¯ºÍ¶ÔÏó³õʼ»¯µÄÎÄÕ²¢²»¶à£¬ÌرðÊÇ´Ó×Ö½ÚÂëºÍ JVM ²ã´ÎÀ´·ÖÎöµÄÎÄÕ¸üÊÇÏÊÓÐËù¼û¡£
±¾ÎÄÖ÷Òª¶ÔÀàºÍ¶ÔÏó³õʼ»¯È«¹ý³Ì½øÐзÖÎö£¬Í¨¹ýÒ»¸öʵ¼ÊÎÊÌâÒýÈ룬½«Ô´´úÂëת»»³É JVM ×Ö½ÚÂëºó£¬ ......
Java¼ÓÃÜÓë½âÃÜ£¨by quqi99£©
×÷ÕߣºÕÅ»ª ·¢±íÓÚ£º2009-12-31
°æ鬃 ......
......
¿´µ½Ò»ÆªÎÄÕ¸оõºÜºÃ£¬×ª¹ýÀ´ÁË... http://bbs.chinaitlab.com/thread-167286-1-2.html
Äã¾õµÃ×Ô¼ºÊÇÒ»¸öJavaר¼ÒÂð£¿ÊÇ·ñ¿Ï¶¨×Ô¼ºÒѾȫÃæÕÆÎÕÁËJavaµÄÒì³£´¦Àí»úÖÆ£¿ÔÚÏÂÃæÕâ¶Î´úÂëÖУ¬ÄãÄܹ»Ñ¸ËÙÕÒ³öÒì³£´¦ÀíµÄÁù¸öÎÊÌâÂð£¿
1 OutputStreamWri ......