java反射
反射是java的重要特性之一,java.lang.reflect 是jdk支持反射的重要包,我下面可能会对构造器Constructor,属性Filed,方法Method会用到。反射其实很常见,在java源码、spring就有很多运用,Spring AOP就应用到了反射。设计模式中代理模式就可以用java反射实现。
反射的例子
创建一个Bean
package com.java.javabase.reflect;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
/**
* @author
*/
@Slf4j
@Data
//@AllArgsConstructor
@ToString
@NoArgsConstructor
public class Book {
private final String tag="tag";
private String auther;
private String name;
private Book(String auther, String name) {
this.auther = auther;
this.name = name;
}
private void printAlarm(String args){
log.info("alarm occured! "+args);
}
}
创建测试程序
从获取Class、创建Book的对象Obect、构造器、私有属性(Field)、私有方法(Method)四个维度测试
package com.java.javabase.reflect;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author
*/
@Slf4j
public class BookReflectTest {
//创建对象测试
public static void reflectNewInstance() {
try {
Class bookClass = Class.forName("com.java.javabase.reflect.Book");
//需要一个无参构造器,否则会报错:Caused by: java.lang.NoSuchMethodException: com.java.javabase.reflect.Book.<init>()
Book bookObject = (Book) bookClass.newInstance();
bookObject.setName("English");
bookObject.setAuther("jack");
log.info("NewInstance test :{}", bookObject.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void reflcectConstructor() {
Class bookClass = null;
try {
bookClass = Class.forName("com.java.javabase.reflect.Book");
//共有的构造器用getConstructor和getDeclaredConstructor不需要constructor.setAccessible(true);
//私有的构造器constructor.setAccessible(true);
//Constructor constructor = bookClass.getConstructor(String.class, String.class);
Constructor constructor = bookClass.getDeclaredConstructor(String.class, String.class);
constructor.setAccessible(true);
Book bookObject = (Book) constructor.newInstance("jack2", "English 2");
log.info("Constructor test :{}", bookObject.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public static void reflectfield() {
Class bookClass = null;
try {
bookClass = Class.forName("com.java.javabase.reflect.Book");
// Book bookObject = null;
// bookObject = (Book) bookClass.newInstance();
Book bookObject = new Book();
Field field = bookClass.getDeclaredField("tag");
Field nameField = bookClass.getDeclaredField("name");
field.setAccessible(true);
nameField.setAccessible(true);
bookObject.setName("English3");
bookObject.setAuther("jack3");
//public Object get(Object obj)
Object object = field.get(bookObject);
Object nameObject = nameField.get(bookObject);
log.info("reflectfield tag :{}", object);
log.info("reflectfield name :{}", nameObject);
log.info("reflectfield test :{}", bookObject.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// catch (InstantiationException e) {
// e.printStackTrace();
// }
}
public static void reflectMethod() {
Class bookClass = Book.class;
// Book bookObject = null;
// bookObject = (Book) bookClass.newInstance();
Book bookObject = null;
try {
bookObject = (Book) bookClass.newInstance();
//public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
Method method = bookClass.getDeclaredMethod("printAlarm",String.class);
method.setAccessible(true);
try {
//public Object invoke(Object obj, Object... args)
method.invoke(bookObject,"end");
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
reflectNewInstance();
reflcectConstructor();
reflectfield();
reflectMethod();
}
}
结果
2019-08-26 18:54:34,573 [main] INFO BookReflectTest - NewInstance test :Book(tag=tag, auther=jack, name=English)
2019-08-26 18:54:34,588 [main] INFO BookReflectTest - Constructor test :Book(tag=tag, auther=jack2, name=English 2)
2019-08-26 18:54:34,588 [main] INFO BookReflectTest - reflectfield tag :tag
2019-08-26 18:54:34,588 [main] INFO BookReflectTest - reflectfield name :English3
2019-08-26 18:54:34,588 [main] INFO BookReflectTest - reflectfield test :Book(tag=tag, auther=jack3, name=English3)
2019-08-26 18:54:34,588 [main] INFO Book - alarm occured! end