多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # 创建测试类 ~~~ public class Person implements Serializable { private long id; public String name; public Person() { this.id = 100L; this.name = "jdxia"; } public Person(Long id, String name) { this.id = id; this.name = name; } public Person(long id) { this.id = id; } private Person(String name) { super(); this.name = name+"======="; System.out.println(name+"======="); } private String getSomeThing() { return "sdsadasdsasd"; } //getter/setter ~~~ # 反射 ~~~ package reflect; import com.sun.org.apache.xpath.internal.operations.String; import org.junit.Before; import org.junit.Test; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class MyReflect { private java.lang.String className = null; private Class personClass = null; //反射Person类 @Before public void init() throws ClassNotFoundException { className = "reflect.Person"; //把这个类文件载入jvm的内存 personClass = Class.forName(className); } //获取某个class文件对象 @Test public void getClassName() { System.out.println(personClass); } //获取某个class文件对象的另一种方式 @Test public void getClassName2() { System.out.println(Person.class); } //创建一个class文件表示的实例对象,底层会调用空参数的构造方法 @Test public void getNewInstance() throws IllegalAccessException, InstantiationException { System.out.println(personClass.newInstance()); } //获取非私有的构造函数 @Test public void getPublicConstructor() throws Exception { Constructor constructor = personClass.getConstructor(Long.class, String.class); Person person = (Person) constructor.newInstance(100L, "张三"); System.out.println(person.getId()); System.out.println(person.getName()); } //访问私有的构造函数 @Test public void getPrivateConstructor() throws Exception { Constructor constructor = personClass.getDeclaredConstructor(String.class); //强制取消java的权限审查 constructor.setAccessible(true); Person person = (Person) constructor.newInstance("张三"); System.out.println("**" + person.getName()); } //访问非私有成员变量 @Test public void getNotPrivateField() throws Exception { Constructor constructor = personClass.getConstructor(Long.class, String.class); Object obj = constructor.newInstance(100L, "zhangsan"); Field field = personClass.getField("name"); field.set(obj, "lisi"); System.out.println(field.get(obj)); } //访问私有成员变量 @Test public void getPrivateMethod() throws Exception { //获取空参构造函数 Object obj = personClass.newInstance(); Method method = personClass.getDeclaredMethod("getSomeThing"); method.setAccessible(true); Object value = method.invoke(obj); System.out.println(value); } @Test public void otherMethod() throws Exception { //当前加载这个class文件的那个类加载器对象 System.out.println(personClass.getClassLoader()); //获取某个类实现的所有接口 Class[] interfaces = personClass.getInterfaces(); for (Class class1 : interfaces) { System.out.println(class1); } //反射当前这个类的直接父类 System.out.println(personClass.getGenericSuperclass()); /** * getResourceAsStream这个方法可以获取到一个输入流,这个输入流会关联到name所表示的那个文件上。 */ //path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。 System.out.println(personClass.getResourceAsStream("/log4j.properties")); System.out.println(personClass.getResourceAsStream("log4j.properties")); //判断当前文件的class对象表示是否是数组 System.out.println(personClass.isArray()); System.out.println(new String[3].getClass().isArray()); //判断当前的class对象表示是否是枚举类 System.out.println(personClass.isEnum()); System.out.println(Class.forName("reflect.Person").isEnum()); System.out.println(personClass.isInterface()); System.out.println(Class.forName("reflect.Person").isInterface()); } } ~~~