1,什么事反射?
反射: Java的反射(reflection)机制是指在程序的运行状态中,可以构造任意一个类的对象,可以了解任意一个对象所属的类,可以了解任意一个类的成员变量和方法,可以调用任意一个对象的属性和方法。这种动态获取程序信息以及动态调用对象的功能称为Java语言的反射机制。反射被视为动态语言的关键 —百度百科
注意:反射是jdk1.5加入的性特性
2,准备
User.java
package com.qijian.reflect; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class User { public String name; public Integer id; }
Student.java
package com.qijian.reflect; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.transaction.annotation.Transactional; @Data @AllArgsConstructor @NoArgsConstructor public class Student { private String name; private Integer id; @Transactional public String show(String str){ System.out.println("我最帅!!!"); return str; } }
pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.9.RELEASE</version> </dependency> </dependencies>
3,利用反射来创建对象
@Test public void testReflect() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { Class<?> aclass = Class.forName("com.qijian.reflect.User"); //通过无参构造函数构造对象 User user = (User)aclass.newInstance(); System.out.println(user.toString()); //通过有参构造函数创建对象 Constructor<?> constructor= aclass.getConstructor(String.class,Integer.class); User user1 = (User) constructor.newInstance("qijian",20182206); System.out.println(user1.toString()); //值得注意的是此处的User类的属性的public User user2 = (User) aclass.newInstance(); Field userId = aclass.getDeclaredField("id"); userId.set(user2,20182206); Field username = aclass.getDeclaredField("name"); username.set(user2,"qijian"); System.out.println(user2.toString()); //此处的student类的属性都是私有的 Class<?> sclass = Class.forName("com.qijian.reflect.Student"); Student student = (Student) sclass.newInstance(); Field sid = sclass.getDeclaredField("id"); sid.setAccessible(true);//若没有这个设置将不能对该属性进行设置 sid.set(student,20182206); Field sname = sclass.getDeclaredField("name"); sname.setAccessible(true); sname.set(student,"qijian"); System.out.println(student.toString()); }
4,使用反射的机制给调用方法
@Test//使用反射的机制调用方法 public void test2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> sclass = Class.forName("com.qijian.reflect.Student"); Student student = (Student) sclass.newInstance(); Method show = sclass.getDeclaredMethod("show",String.class); show.invoke(student,"666"); }
5,利用反射机制查看方法是否有注解
@Test//利用反射机制查看方法是否有注解 public void test3() throws ClassNotFoundException, NoSuchMethodException { Class<?> sclass = Class.forName("com.qijian.reflect.Student"); Method show = sclass.getDeclaredMethod("show",String.class); Transactional declaredAnnotation = show.getDeclaredAnnotation(Transactional.class); System.out.println(declaredAnnotation); }