[code]
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
public class TestRef {
public static void main(String[] args) throws Exception {
TestRef testRef = new TestRef();
Class clazz = TestRef. class ;
System.out.println( " getPackage() = " + clazz.getPackage().getName());
// getModifiers()的返回值可以包含类的种类信息。比如是否为public,abstract,static
int mod = clazz.getModifiers();
System.out.println( " Modifier.isAbstract(mod) = " + Modifier.isAbstract(mod));
System.out.println( " getName() = " + clazz.getName());
System.out.println( " getSuperclass() = " + clazz.getSuperclass().getName());
System.out.println( " getInterfaces() = " + clazz.getInterfaces()); // 实现了哪些Interface
System.out.println( " clazz.getDeclaredClasses() = " + clazz.getDeclaredClasses()); // 包含哪些内部类
System.out.println( " getDeclaringClass() = " + clazz.getDeclaringClass()); // 如果clazz是inner class 那么返回其outer class
System.out.println( " ---------- " );
Constructor[] constructor = clazz.getDeclaredConstructors(); // 返回一组构造函数 Constructor[]
if (constructor != null ) {
for ( int i = 0 ; i < constructor.length; i ++ ) {
System.out.println(constructor[i].getName());
}
}
System.out.println( " ---------- " );
Method[] method = clazz.getDeclaredMethods(); // Method[]
if (method != null ) {
for ( int i = 0 ; i < method.length; i ++ ) {
System.out.println(method[i].getName());
}
}
System.out.println( " ---------- " );
Field[] field = clazz.getDeclaredFields(); // Field[]
if (field != null ) {
for ( int i = 0 ; i < field.length; i ++ ) {
System.out.println(field[i].getName());
System.out.println(field[i].getType().getName());
System.out.println(field[i].get(testRef));
}
}
// 动态生成instance(无参数)
Class clz = Class.forName( " reflection.TestRef " );
Object obj = clz.newInstance();
System.out.println(((TestRef)obj).getStr());
// 动态生成instance(有参数)
Class[] params = new Class[] {String. class , int . class , double . class } ;
Constructor construct = clz.getConstructor(params);
// JDK1.5的情况下可以直接用{"haha",999,100.01}作为参数
Object obj2 = construct.newInstance( new Object[] { " haha " , new Integer( 999 ), new Double( 100.01 )} );
System.out.println(((TestRef)obj2).getStr());
// 动态调用method(public method)
Class[] params2 = new Class[] {String. class } ;
Method methods = clz.getMethod( " setStr " , params2);
methods.invoke(testRef, new Object[] { " invoke method " } );
System.out.println(testRef.getStr());
// 动态改变field内容(public field)
Field fields = clz.getField( " str " );
fields.set(testRef, " set field's value " );
System.out.println(testRef.getStr());
}
public TestRef() {
System.out.println( " --- complete TestRef() --- " );
}
public TestRef(String str, int i, double d) {
this .str = str;
this .i = i;
this .d = d;
System.out.println( " --- complete TestRef(String str, int i, double d) --- " );
}
public String str = " I'm a string " ;
int i = 1 ;
double d = 3.14 ;
HashMap map = new HashMap();
public double getD() {
return d;
}
public void setD( double d) {
this .d = d;
}
public int getI() {
return i;
}
public void setI( int i) {
this .i = i;
}
public HashMap getMap() {
return map;
}
public void setMap(HashMap map) {
this .map = map;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this .str = str;
}
} [/code]
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
public class TestRef {
public static void main(String[] args) throws Exception {
TestRef testRef = new TestRef();
Class clazz = TestRef. class ;
System.out.println( " getPackage() = " + clazz.getPackage().getName());
// getModifiers()的返回值可以包含类的种类信息。比如是否为public,abstract,static
int mod = clazz.getModifiers();
System.out.println( " Modifier.isAbstract(mod) = " + Modifier.isAbstract(mod));
System.out.println( " getName() = " + clazz.getName());
System.out.println( " getSuperclass() = " + clazz.getSuperclass().getName());
System.out.println( " getInterfaces() = " + clazz.getInterfaces()); // 实现了哪些Interface
System.out.println( " clazz.getDeclaredClasses() = " + clazz.getDeclaredClasses()); // 包含哪些内部类
System.out.println( " getDeclaringClass() = " + clazz.getDeclaringClass()); // 如果clazz是inner class 那么返回其outer class
System.out.println( " ---------- " );
Constructor[] constructor = clazz.getDeclaredConstructors(); // 返回一组构造函数 Constructor[]
if (constructor != null ) {
for ( int i = 0 ; i < constructor.length; i ++ ) {
System.out.println(constructor[i].getName());
}
}
System.out.println( " ---------- " );
Method[] method = clazz.getDeclaredMethods(); // Method[]
if (method != null ) {
for ( int i = 0 ; i < method.length; i ++ ) {
System.out.println(method[i].getName());
}
}
System.out.println( " ---------- " );
Field[] field = clazz.getDeclaredFields(); // Field[]
if (field != null ) {
for ( int i = 0 ; i < field.length; i ++ ) {
System.out.println(field[i].getName());
System.out.println(field[i].getType().getName());
System.out.println(field[i].get(testRef));
}
}
// 动态生成instance(无参数)
Class clz = Class.forName( " reflection.TestRef " );
Object obj = clz.newInstance();
System.out.println(((TestRef)obj).getStr());
// 动态生成instance(有参数)
Class[] params = new Class[] {String. class , int . class , double . class } ;
Constructor construct = clz.getConstructor(params);
// JDK1.5的情况下可以直接用{"haha",999,100.01}作为参数
Object obj2 = construct.newInstance( new Object[] { " haha " , new Integer( 999 ), new Double( 100.01 )} );
System.out.println(((TestRef)obj2).getStr());
// 动态调用method(public method)
Class[] params2 = new Class[] {String. class } ;
Method methods = clz.getMethod( " setStr " , params2);
methods.invoke(testRef, new Object[] { " invoke method " } );
System.out.println(testRef.getStr());
// 动态改变field内容(public field)
Field fields = clz.getField( " str " );
fields.set(testRef, " set field's value " );
System.out.println(testRef.getStr());
}
public TestRef() {
System.out.println( " --- complete TestRef() --- " );
}
public TestRef(String str, int i, double d) {
this .str = str;
this .i = i;
this .d = d;
System.out.println( " --- complete TestRef(String str, int i, double d) --- " );
}
public String str = " I'm a string " ;
int i = 1 ;
double d = 3.14 ;
HashMap map = new HashMap();
public double getD() {
return d;
}
public void setD( double d) {
this .d = d;
}
public int getI() {
return i;
}
public void setI( int i) {
this .i = i;
}
public HashMap getMap() {
return map;
}
public void setMap(HashMap map) {
this .map = map;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this .str = str;
}
} [/code]