反射reflection
反射:在程序运行期间,能够观察和修改类或类的对象的属性和行为的特性
反射机制提供了以下功能
在运行时:
1、获取类的修饰符,包名,类名,实现的接口,继承的父类
2、获取类的所有属性名,修饰符,属性类型
3、获取所有方法,方法的返回值类型,方法名,方法参数数量,方法参数类型
4、调用加载类的方法
Java反射API常用的类
java.lang.Class
java.lang.reflect.Method
java.lang.Constructor
java.lang.Field 属性
java.lang.Modifier 修饰符
Java反射的使用实例:
1、 Person extends FatherTest implements java.io.Serializable
建立
public class GetClassInfo{
public void test(String className) throws Exceptions
class classN=Class.forName(className);//获取Person类对象(不是实例),此处写全类名(包名.类名)
//获取类所在的包名
Package package=classN.getPackage();
System.out.println(classN.getSimpleName()+"定义在"+package.getName);
//获取对象所表示的实体(类、接口、基本类型或void)的超类Class
//如果对象是Object类、一个接口、一个基本类型或void,超类返回值null
//如果对象是一个数组类,返回表示Object类的Class对象
Class superClass=classN.getSuperClass();
System.out.println(classN.getSimpleName()+"的超类是:"+superClass);
//获取Person类所实现的接口
//如果Person类没有实现任何接口,返回一个长度为0的数组
//如果Person类是一个基本类型或者是void,也返回一个长度为0的数组
Class[] interfaces=classN.getInterfaces();
System.out.println("所实现的数组有:");
for(class:interfaces){
System.out.println(c.getName());
}
}
public static void main(String[] args) throws Exception{
GetClassInfo get ci=new GetClassInfo();
getci.test("entity.Person");
}
}
建立
public class GetClassConstructorInfo{
public void test1() throws Exception{
Class classM=Class.forName("entity.Person");
Object object=classM.newInstance(); 通过newInstance获取新实例
}
public void tesr2() throws Exception{
Class cls=Class.forName("entity.Person");
//获取类的构造函数(private时)
Constructor[] const=cls.getDeclaredConstructors();
System.out.println("构造函数展示:");
for(Constructor con:consts){
Class[] params=con.getParameterTypes();
if(params.length==0){
System.out.println("此构造函数没有参数");
}else{
System.out.println("此构造函数的列表为:[");
for(int i=0;i<params.length;i++){
if(i!=0){
System.out.println(",");
}
System.out.println(params[i].getName());
}
System.out.println("]");
}
}
//获取无参构造
Constructor c1[cls.gerConstructor();
//通过无参构造获取Person类的对象实例
Object obj=c1.newInstance();
System.out.println(obj);
//获取有参构造
Constructor c2=cls.getDeclaredConstructor(String.class,String.class,String.class);
//c2私有时
c2.setAccessible(true);//只有此处可以访问需要访问的private
Object obj=c2.newInstance("name","address","message");//和有参构造参数数量保持一致
System.out.println(obj);
}
public static void main(String[] args){
GetClassConstructorInfo cons=new GetClassConstructorInfo();
cons.test1();
cons.test2();//和test1二选一
}
}
package reflection;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import javax.lang.model.type.DeclaredType;
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
// Class<Reflect1> classR=Reflect1.class;
Class<?> class1 = Class.forName(null);
interfaceR(class1);
}
//[interface]
public static void interfaceR(Class class1) {
Class[] classes = class1.getInterfaces();
System.out.println("Interfaces:");
//traverse
for(Class class2:classes) {
System.out.print(class2.getSimpleName()+"\t");
}
}
//[modifier]
public static void modifierR(Class class1) {
// int modifiers = class1.getModifiers();
//int need to convert to String
System.out.println(Modifier.toString(class1.getModifiers())+" class "+class1.getSimpleName());
}
//[field]
public static void field(Class class1) throws NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException{
Field[] fields = class1.getDeclaredFields();
//traverse
for (Field field : fields) {
System.out.println(Modifier.toString(field.getModifiers())+" "+field.getType().getSimpleName()+" "+field.getName());
}
//set some
Field field = class1.getDeclaredField("some");
//new instance
Object obj=class1.newInstance();
//mask private feature
field.setAccessible(true);
field.set(obj, "***");
//type coercion
Reflect1 re1=(Reflect1)obj;
System.out.println(re1.getSome());
}
//[method]
public static void method(Class class1) {
Method[] methods = class1.getMethods();
System.out.println("All Methods,include sub:");
//traverse
for (Method method : methods) {
System.out.println(Modifier.toString(method.getModifiers())+" "+method.getReturnType()+" "+method.getName());
System.out.println(method.getName()+"Parameter:");
//traverse
for (Class classes : method.getParameterTypes()) {
System.out.print(classes.getSimpleName()+",");
}
}
Method[] declaredMethods = class1.getDeclaredMethods();
System.out.println("All Methods,barring sub:");
for (Method method:declaredMethods) {
System.out.println(Modifier.toString(method.getModifiers())+" "+method.getReturnType()+" "+method.getName());
System.out.println(method.getName()+"Parameter:");
//traverse
for (Class classes : method.getParameterTypes()) {
System.out.print(classes.getSimpleName()+",");
}
}
// Method method=class1.getDeclaredMethod();
}
//[constructor]
public static void constructor(Class class1) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Constructor constructor = class1.getConstructor(String.class,String.class,int.class);
Object object = constructor.newInstance("didi","dog",0);
Reflect1 ref=(Reflect1)object;
ref.show();
}
//[annotation]
public static void annotation(Class class1) {
Reflect1Annotation annotation =(Reflect1Annotation) class1.getAnnotation(Reflect1Annotation.class);
}
}