JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制
要想解剖一个类,必须先要获取到该类的字节码文件对象。而解剖使用的就是Class类中的方法.所以先要获取到每一个字节码文件对应的Class类型的对象
简单来说,反射就是把java类中的各种成分映射成一个个的Java对象
例如:一个类有:成员变量、方法、构造方法、包等等信息,利用反射技术可以对一个类进行解剖,把个个组成部分映射成一个个对象
(其实:一个类中这些成员方法、构造方法、在加入类中都有一个类来描述)
两个测试类:Person类、Hero类
package com.mengming;
public class Person {
private int id;
public String name;
public Person() {
}
public Person(int id, String name) {
this.id = id;
this.name = name;
}
private Person(int id){
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void test() {
System.out.println("Person test...");
}
public void test(int num) {
System.out.println("Person test..." + num);
}
private void privateFunc(String str) {
System.out.println("Person privateFunc..." + str);
}
@Override
public String toString() {
return "com.mengming.Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
package com.mengming;
import java.util.ArrayList;
public class Hero extends Person {
void q() {
System.out.println("放大招...");
}
}
1、获取类对象的方法
(1)获取方式
import com.mengming.Person;
public class App {
public static void main(String[] args) {
//获取类对象的三种方式
//1.使用类
Class personClass = Person.class;
//2.使用全类名
try {
Class> aClass = Class.forName("com.mengming.Person");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//3.使用对象
Person person = new Person();
Class extends Person> clazz = person.getClass();
}
}
(2)对类对象操作
import com.mengming.Person;
import java.net.URL;
public class App2 {
public static void main(String[] args) {
try {
Class> personClazz = Class.forName("com.mengming.Person");
//对类对象操作
//获取类名字
System.out.println(personClazz.getName());
//获取类加载器
ClassLoader personClazzClassLoader = personClazz.getClassLoader();
System.out.println(personClazzClassLoader);
//获取资源
URL resource = personClazz.getResource("");
//获取父类
Class> superclass = Class.forName("com.mengming.Hero").getSuperclass();
System.out.println(superclass);
//判断一个类是否是接口
boolean isInterface = personClazz.isInterface();
System.out.println(isInterface);
//使用Class对象实例化一个对象
Person person = (Person) personClazz.newInstance();
person.test();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
2、对成员变量的操作
在java中万物皆对象成员变量也是对象,它拥有操作一个对象的成员变量的能力。
(1)获取成员变量
import java.lang.reflect.Field;
public class App3 {
public static void main(String[] args) {
try {
Class> personClazz = Class.forName("com.mengming.Person");
//在Person类中id字段是私有的,name字段是公有的
//获取字段,getFields方法只能获取公共的字段
Field[] fields = personClazz.getFields();
for (Field field : fields) {
System.out.println(field.getName());
}
//getField方法获取指定字段(public的)
Field nameFiled = personClazz.getField("name");
System.out.println(nameFiled);
//getDeclaredFields方法可以获取所有字段(private和public的都可以获取到)
Field[] declaredFields = personClazz.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField.getName());
}
//getField方法获取指定字段(public的、private的都可)
Field idFiled = personClazz.getDeclaredField("id");
System.out.println(idFiled);
} catch (ClassNotFoundException | NoSuchFieldException e) {
e.printStackTrace();
}
}
}
(2)获取对象的属性
import com.mengming.Person;
import java.lang.reflect.Field;
public class App4 {
public static void main(String[] args) {
try {
Person person = new Person();
person.setName("mike");
Class> personClazz = Class.forName("com.mengming.Person");
Field nameFiled = personClazz.getDeclaredField("name");
//Object get(Object obj)
String name = (String) nameFiled.get(person);
System.out.println(name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
(3)设置对象的属性
import com.mengming.Person;
import java.lang.reflect.Field;
public class App5 {
public static void main(String[] args) {
try {
Person person = new Person();
person.setName("mike");
System.out.println(person.getName());
Class> personClazz = Class.forName("com.mengming.Person");
Field nameField = personClazz.getDeclaredField("name");
//void set(Object obj, Object value)
nameField.set(person,"mengming");
System.out.println(person.getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
3、对方法的操作
(1)获取方法
import java.lang.reflect.Method;
public class App6 {
public static void main(String[] args) {
try {
Class> personClazz = Class.forName("com.mengming.Person");
//getMethods方法来获取公有方法
Method[] methods = personClazz.getMethods();
for (Method method : methods) {
System.out.println(method);
}
System.out.println("------------------------------------");
//获取指定的public方法
//获取某个方法,名字,后边是参数类型
Method testMethod = personClazz.getMethod("test", null);
System.out.println(testMethod);
System.out.println("------------------------------------");
Method testWithNumMethod = personClazz.getMethod("test", int.class);
System.out.println(testWithNumMethod);
System.out.println("------------------------------------");
//getDeclaredMethods方法来获取所有方法
Method[] declaredMethods = personClazz.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println(declaredMethod);
}
System.out.println("------------------------------------");
//获取指定方法(public和private的都可)
Method privateFuncMethod = personClazz.getDeclaredMethod("privateFunc", String.class);
System.out.println(privateFuncMethod);
System.out.println("------------------------------------");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
(2)对方法的操作
import com.mengming.Person;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class App7 {
public static void main(String[] args) {
try {
Class> personClazz = Class.forName("com.mengming.Person");
Method testWithNumMethod = personClazz.getDeclaredMethod("test", int.class);
//方法参数个数
int parameterCount = testWithNumMethod.getParameterCount();
System.out.println(parameterCount);
System.out.println("--------------------");
//方法名字
String funcName = testWithNumMethod.getName();
System.out.println(funcName);
System.out.println("--------------------");
//参数类型数组
Class>[] parameterTypes = testWithNumMethod.getParameterTypes();
for (Class> parameterType : parameterTypes) {
System.out.println(parameterType);
}
System.out.println("--------------------");
//获取方法的返回值的类型
Class> returnType = testWithNumMethod.getReturnType();
System.out.println(returnType);
System.out.println("--------------------");
Person person = new Person();
person.setName("mengming");
System.out.println(person.getName());
Method setNameMethod = personClazz.getDeclaredMethod("setName", String.class);
//反射调用方法,传一个实例,和参数
//Object invoke(Object obj, Object... args)
setNameMethod.invoke(person,"mike");
System.out.println(person.getName());
System.out.println("--------------------");
Person person2 = (Person) personClazz.newInstance();
System.out.println(person2.getName());
setNameMethod.invoke(person2,"person2");
System.out.println(person2.getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
4、对构造器的操作
(1)获取并构建对象
import com.mengming.Person;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class App8 {
public static void main(String[] args) {
try {
Class> personClazz = Class.forName("com.mengming.Person");
Constructor>[] constructors = personClazz.getConstructors();
for (Constructor> constructor : constructors) {
System.out.println(constructor);
}
System.out.println("-----------------------");
Constructor> nonParaConstructor = personClazz.getConstructor();
System.out.println(nonParaConstructor);
System.out.println("-----------------------");
Constructor> constructor = personClazz.getConstructor(int.class, String.class);
System.out.println(constructor);
System.out.println("-----------------------");
Constructor>[] declaredConstructors = personClazz.getDeclaredConstructors();
for (Constructor> declaredConstructor : declaredConstructors) {
System.out.println(declaredConstructor);
}
System.out.println("-----------------------");
Constructor> declaredConstructor = personClazz.getDeclaredConstructor(int.class);
System.out.println(declaredConstructor);
System.out.println("-----------------------");
Person person = (Person) constructor.newInstance(666,"mike");
System.out.println(person);
System.out.println("-----------------------");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
5、对注解的操作
用于测试的注解和类
用于测试的自定义注解和添加了自定义注解的类
package com.mengming;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MAnnotation {
}
package com.mengming;
@MAnnotation
public class AnnotationTestClass {
}
(1)从方法、字段、类上获取注解
import com.mengming.MAnnotation;
import java.lang.annotation.Annotation;
public class App9 {
public static void main(String[] args) {
try {
Class> annotationTestClazz = Class.forName("com.mengming.AnnotationTestClass");
Annotation[] annotations = annotationTestClazz.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
System.out.println("----------------------");
//getAnnotation方法提取类上指定的注解
MAnnotation mAnnotation = annotationTestClazz.getAnnotation(MAnnotation.class);
System.out.println(mAnnotation);
System.out.println("----------------------");
Class> simpleTestClazz = Class.forName("com.mengming.SimpleTestClass");
MAnnotation simpleTestClazzAnnotation = simpleTestClazz.getAnnotation(MAnnotation.class);
System.out.println(simpleTestClazzAnnotation);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
方法和字段上获取注解的方式和类上获取注解的方式是一样的
//字段上
Annotation annotation = field.getAnnotation(XxxAnnotation.class);
Annotation[] annotations = field.getAnnotations();
//方法上
Annotation annotation = method.getAnnotation(XxxAnnotation.class);
Annotation[] annotations = method.getAnnotations();
(2)从方法、字段、类上获取注解中的值
用于测试的自定义注解和添加了自定义注解的类
package com.mengming;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MTestAnnotation {
int id() default 0;
String name() default "";
}
package com.mengming;
@MTestAnnotation(id = 24, name = "mengming")
public class AnnotationGetValueTest {
}
测试用的main方法
import com.mengming.MTestAnnotation;
public class AnnotationApp {
public static void main(String[] args) {
try {
Class> clazz = Class.forName("com.mengming.AnnotationGetValueTest");
MTestAnnotation annotation = clazz.getAnnotation(MTestAnnotation.class);
//获取到注解对象后,可以通过对象名.成员变量属性的方式来获取到对应注解属性字段的值
System.out.println(annotation.id());
System.out.println(annotation.name());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}