Java Reflection
Reflection是被视为动态语言的关键,反射机制允许程序在执行期借助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及方法。
Java反射机制提供的功能:
1>在运行时判断任意一个对象所属的类。
2>在运行时构造任意一个类的对象。
3>在运行时判断任意一个类所具有的成员变量和方法。
4>在运行时调用任意一个对象的成员变量和方法。
5>生成动态代理。
public class TestReflection {
@Test
public void test1() throws Exception {
// 在反射以前,如何创建一个类的对象,并调用其中的方法和属性。
Person p = new Person();
p.setAge(20);
p.setName("TanWeiWei");
System.out.println(p);
p.show();
p.display("CHN");
}
@Test
public void test2() throws Exception {
// 有了反射,可以通过反射创建一个类的对象,并调用其中的结构。
Class clazz = Person.class;
// 1.创建clazz对应的运行时类Person类的对象。
Person p = (Person) clazz.newInstance();
System.out.println(p);
// 2.通过反射调用运行时类的指定属性
// 2.1
Field f1 = clazz.getField("name");
f1.set(p, "LiuDeHua");
System.out.println(p);
// 2.2
Field f2 = clazz.getDeclaredField("age");
f2.setAccessible(true);
f2.set(p, 20);
System.out.println(p);
// 3.通过反射调用运行时类指定方法。
Method m1 = clazz.getMethod("show");
m1.invoke(p);
Method m2 = clazz.getMethod("display", String.class);
m2.invoke(p, "CHN");
}
/**
* java.lang.Class:是反射的源头。
* 创建了一个类,通过编译(javac.exe),生成对应的.class文件。之后我们使用java.exe加载(JVM的classloader)此.class文件。
* 此.class文件加载到内存以后,就是一个运行时类,存放在缓存区。那么这个运行时类本身就是一个 Class的实例。
* 1.每一个运行时类只加载一次!
* 2.有了Class的实例以后,我们才可以进行如下操作:
* 1)*创建对应的运行时类的对象
* 2)获取对应的运行时类的完整结构(属性、方法、构造器、内部类、父类、所在的包、异常、注解、...)
* 3)*调用对应的运行时类的指定结构(属性、方法、构造器)
* 4)反射的应用:动态代理
*/
@Test
public void test3() {
Person p = new Person();
Class clazz = p.getClass();// 通过运行时类的对象调用其getClass(),返回其运行时类。
System.out.println(clazz);
}
// 如何获取Class的实例(3种)
@Test
public void test4() throws ClassNotFoundException {
// 1.调用运行时类本身的.class属性
Class clazz1 = Person.class;
System.out.println(clazz1.getName());
Class clazz2 = String.class;
System.out.println(clazz2.getName());
// 2.通过运行时类的对象获取。
Person p = new Person();
Class clazz3 = p.getClass();
System.out.println(clazz3.getName());
// 3.通过Class的静态方法获取,通过此方法,体会反射的动态性。
String className = "JavaBasic.day20.Person";
Class clazz4 = Class.forName(className);
System.out.println(clazz4.getName());
// 4.(了解)通过类的加载器
ClassLoader classLoader = this.getClass().getClassLoader();
Class clazz5 = classLoader.loadClass(className);
System.out.println(clazz5.getName());
}
@Test
public void test5() throws Exception {
ClassLoader loader1 = ClassLoader.getSystemClassLoader();
System.out.println(loader1);
ClassLoader loader2 = loader1.getParent();
System.out.println(loader2);
ClassLoader loader3 = loader2.getParent();
System.out.println(loader3);
Class clazz = Person.class;
ClassLoader classLoader = clazz.getClassLoader();
System.out.println(classLoader);
// String className = "java.lang.object";
// Class clazz1 = Class.forName(className);
// ClassLoader classLoader1 = clazz1.getClassLoader();
// System.out.println(classLoader1);
// 掌握如下:
ClassLoader classLoader2 = this.getClass().getClassLoader();
InputStream is = classLoader2.getResourceAsStream("JavaBasic\\day20\\jdbc.properties");
Properties props = new Properties();
props.load(is);
String user = props.getProperty("user");
String password = props.getProperty("password");
System.out.println("UserName = " + user + "\n" + "Password = " + password);
}
@Test
public void test6() throws Exception {
String className = "JavaBasic.day20.Person";
Class clazz = Class.forName(className);
// 创建对应的运行时类的对象。使用newInstance(),实际上就是调用了运行时类的空参的构造器。
// 要想能够创建成功:
// ①要求对应的运行时类要有空参的构造器。
// ②构造器的权限修饰符要足够。
Person p = (Person) clazz.newInstance();
System.out.println(p);
}
}
反射的使用:1、通过反射调用运行时类的构造器:
public class TestConstructor { @Test public void test1() throws ClassNotFoundException { String className = "JavaBasic.day20.Person"; Class clazz = Class.forName(className); Constructor[] constructors = clazz.getDeclaredConstructors(); for (Constructor cons : constructors) { System.out.println(cons); } } // 调用指定的构造器 @Test public void test2() throws Exception { String className = "JavaBasic.day20.Person"; Class clazz = Class.forName(className); Constructor constructor = clazz.getDeclaredConstructor(String.class, int.class); constructor.setAccessible(true); Person p = (Person) constructor.newInstance("张三", 30); System.out.println(p); } }
2、通过反射获取运行时类的属性:
public class TestField { // 获取对应的运行时类的属性 @Test public void test1() { Class clazz = Person.class; // 1.getFields():只能获取运行时类及其父类中声明为public的属性 Field[] fields = clazz.getFields(); for (Field field : fields) { System.out.println(field); } System.out.println(); // 2.getDeclaredFields():获取运行时类声明的所有属性。 Field[] fields1 = clazz.getDeclaredFields(); for (Field field : fields1) { System.out.println(field.getName()); } } // 权限修饰符 变量类型 变量名 // 获取属性的各个部分的内容 @Test public void test2() { Class clazz = Person.class; Field[] fields1 = clazz.getDeclaredFields(); for (Field field : fields1) { // 1.获取每个属性的权限修饰符 String modifier = Modifier.toString(field.getModifiers()); System.out.print(modifier + "\t"); // 2.获取属性的变量类型 Class type = field.getType(); System.out.print(type.getName() + "\t"); // 3.获取属性名 System.out.println(field.getName()); } } // 调用运行时类的指定属性 @Test public void test3() throws Exception { Class clazz = Person.class; // 1.获取指定的属性 // 1.1 getField(String fieldName):获取运行时类中声明为public的属性名为fieldName的属性 Field name = clazz.getField("name"); // 2.创建运行时类的对象 Person p = (Person) clazz.newInstance(); System.out.println(p); name.set(p, "LiuDeHua"); System.out.println(p); // 1.2 getDeclaredField(String fieldName):获取运行时类中指定的名为fieldName的属性 Field age = clazz.getDeclaredField("age"); // 由于属性权限修饰符的限制,为了保证可以给属性复制,需要在操作前使得此属性可被操作。 age.setAccessible(true); age.set(p, 20); System.out.println(p); } }
3、通过反射获取运行时类的方法:
public class TestMethod { // 1.获取运行时类的方法 @Test public void test1() { Class clazz = Person.class; // 1.1getMethods():获取运行时类及其父类中声明为public的方法。 Method[] methods = clazz.getMethods(); for (Method method : methods) { System.out.println(method.getName()); } System.out.println(); // 1.2 getDecalredMethods():获取运行时类自己声明的所有的方法。 Method[] methods1 = clazz.getDeclaredMethods(); for (Method method : methods1) { System.out.println(method.getName()); } } @Test public void test2() { Class clazz = Person.class; Method[] methods = clazz.getMethods(); for (Method method : methods) { // 1.注解 Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } // 2.权限修饰符 System.out.print(Modifier.toString(method.getModifiers()) + " "); // 3.返回值类型 System.out.print(method.getReturnType().getName() + " "); // 4.方法名 System.out.print(method.getName() + " "); // 5.形参列表 Class[] paramTypes = method.getParameterTypes(); for (int i = 0; i < paramTypes.length; i++) { System.out.print(paramTypes[i].getName() + " " + "args-" + i + " "); } // 6.异常类型 Class[] exceptionTypes = method.getExceptionTypes(); for (Class exception : exceptionTypes) { System.out.println(exception.getName()); } } } // 调用运行时类中指定的方法。 @Test public void test3() throws Exception { Class clazz = Person.class; // getMethod(string methodName, Class ... params):获取运行时类中声明为public的方法 Method m1 = clazz.getMethod("show"); Person p = (Person) clazz.newInstance(); // 调用指定的方法:invoke(Object obj, Object ... obj) System.out.println(m1.invoke(p)); Method m2 = clazz.getMethod("toString"); System.out.println(m2.invoke(p)); // 对于运行时类中静态方法的调用 Method m3 = clazz.getMethod("showInfo"); m3.invoke(Person.class); // getDeclaredMethod(String methodName,Class ... params):获取运行时类中声明的指定方法。 Method m4 = clazz.getDeclaredMethod("display", String.class, Integer.class); m4.setAccessible(true); Object value = m4.invoke(p, "CHN", 10); System.out.println(value); } }
4、通过反射获取运行时类的父类、带泛型的父类、父类的泛型、实现的接口、类的注解、所在包等等。
public class TestOthers { // 1.获取运行时类的父类 @Test public void test1() { Class clazz = Person.class; Class parent = clazz.getSuperclass(); System.out.println(parent.getName()); } // 2.获取带泛型的父类 @Test public void test2() { Class clazz = Person.class; Type type = clazz.getGenericSuperclass(); System.out.println(type); } // 3.获取父类的泛型 @Test public void test3() { Class clazz = Person.class; Type type = clazz.getGenericSuperclass(); ParameterizedType pt = (ParameterizedType) type; Type[] types = pt.getActualTypeArguments(); for (Type t : types) { System.out.println(t.getTypeName()); } } // 4.获取实现的接口 @Test public void test4() { Class clazz = Person.class; Class[] interfaces = clazz.getInterfaces(); for (Class i : interfaces) { System.out.println(i.getName()); } } // 5.获取所在的包 @Test public void test5() { Class clazz = Person.class; Package pack = clazz.getPackage(); System.out.println(pack.getName()); } // 6.获取类的注解 @Test public void test6() { Class clazz = Person.class; Annotation[] annotations = clazz.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } } }