JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
JAVA反射(放射)机制:“程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言”。从这个观点看,Perl,Python,Ruby是动态语言,C++,Java,C#不是动态语言。但是JAVA有着一个非常突出的动态相关机制:Reflection,用在Java身上指的是我们可以于运行时加载、探知、使用编译期间完全未知的classes。换句话说,Java程序可以加载一个运行时才得知名称的class,获悉其完整构造(但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods。
Class<?> cls1 = Class.forName("java.lang.String");
Class<?> cls2 = String.class;
String str = "";
Class<?> cls3 = str.getClass();
//相当于String str = new String();
Class<?> cls = Class.forName("java.lang.String");
String str1 = (String)cls.newInstance();
//以下相当于String str2 = new String("hello");
Class<?> cls2 = Class.forName("java.lang.String");
Constructor<?> constructor = cls2.getConstructor(String.class);
String str2 = (String) constructor.newInstance("hello");
Method[] methods = cls.getMethods();
Method method = cls.getMethod("substring", int.class, int.class);
Field[] fields = cls.getDeclaredFields();
Field field = cls.getDeclaredField("value");
格式如下,obj为要调用的实例对象,args是要调用方法要传入的实际参数。
public Object invoke(Object obj, Object... args)
示例:
Method meth = cls2.getMethod("substring", int.class, int.class);
String subString = (String)meth.invoke(str2, 0, 3);
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException,
InvocationTargetException, NoSuchFieldException {
// 创建对象方式一:通过Class.forName(ClassName).newInstance()调用无参构造函数.注意ClassName必须是全名。
Class<?> cls1 = Class.forName("java.lang.String");
String str1 = (String) cls1.newInstance(); // 相当于String str = new String();
// 创建Class对象的其它方式
// Class<?> cls2 = String.class;
// String str = "";
// Class<?> cls3 = str.getClass();
// 创建对象方式二:通过Constructor对象创建
// 相当于String str2 = new String("hello");
Class<?> cls2 = Class.forName("java.lang.String");
Constructor<?> constructor = cls2.getConstructor(String.class);
String str2 = (String) constructor.newInstance("hello");
//调用方法
Method meth = cls2.getMethod("substring", int.class, int.class);
String subString = (String)meth.invoke(str2, 0, 3);
System.out.println(subString);
Class<?> cls = Class.forName("java.lang.String");
// 获得String类的所有public构造函数
Constructor<?>[] constructors = cls.getConstructors();
for (Constructor c : constructors) {
System.out.println(c);
}
System.out
.println("-----------------------------------------------------------------");
// 获得String类的所有public方法
Method[] methods = cls.getMethods();
for (Method m : methods) {
System.out.println(m);
}
System.out
.println("-----------------------------------------------------------------");
// 获取指定方法
Method method = cls.getMethod("substring", int.class, int.class);
System.out.println(method);
System.out
.println("-----------------------------------------------------------------");
// 获取全部的成员变量
Field[] fields = cls.getDeclaredFields();
for (Field f : fields) {
System.out.println(f);
}
System.out
.println("-----------------------------------------------------------------");
// 获取指定的成员变量
Field field = cls.getDeclaredField("value");
System.out.println(field);
System.out
.println("-----------------------------------------------------------------");
}
}
原文:http://blog.csdn.net/csujiangyu/article/details/44628761