简介
百度百科定义:JAVA反射机制是在运行状态中,对于任意一个实体类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。
通俗理解:所谓反射其实是获取类的字节码文件,也就是.class文件。.class文件可以理解为所有对象的正本,反射机制允许通过Class类获取正本。并由正本实例化生成副本,副本可进行相应的操作。
特性
- 在运行时判断任意一个对象所属的类;
- 在运行时构造任意一个类的对象;
- 在运行时判断任意一个类所具有的成员变量和方法(通过反射甚至可以调用private方法);
- 在运行时调用任意一个对象的方法
- 生成动态代理
用途
基于反射机制特性,反射被广泛地应用于那些需要在运行时检测或修改程序行为的程序中。比如常用的Spring框架:Spring的核心模块实现了IoC的功能,它将类和类之间的依赖从代码中脱离出来,用配置的方式进行依赖关系描述,由IoC容器负责依赖类之间的创建、拼接、管理、获取等工作。而此依赖类的创建便是依赖反射机制来完成的。
用法
Class类
Class代表类的实体,在运行的Java应用程序中表示类和接口。
- 获得类相关的方法
方法 | 用途 |
---|---|
asSubclass(Class<U> clazz) | 把传递的类的对象转换成代表其子类的对象 |
Cast | 把对象转换成代表类或是接口的对象 |
getClassLoader() | 获得类的加载器 |
getClasses() | 返回一个数组,数组中包含该类中所有公共类和接口类的对象 |
getDeclaredClasses() | 返回一个数组,数组中包含该类中所有类和接口类的对象 |
forName(String className) | 根据类名返回类的对象 |
getName() | 获得类的完整路径名字 |
newInstance() | 创建类的实例 |
getPackage() | 获得类的包 |
getSimpleName() | 获得类的名字 |
getSuperclass() | 获得当前类继承的父类的名字 |
getInterfaces() | 获得当前类实现的类或是接口 |
- 获得类中属性相关的方法
方法 | 用途 |
---|---|
getField(String name) | 获得某个公有的属性对象 |
getFields() | 获得所有公有的属性对象 |
getDeclaredField(String name) | 获得某个属性对象 |
getDeclaredFields() | 获得所有属性对象 |
- 获得类中注解相关的方法
方法 | 用途 |
---|---|
getAnnotation(Class<A> annotationClass) | 返回该类中与参数类型匹配的公有注解对象 |
getAnnotations() | 返回该类所有的公有注解对象 |
getDeclaredAnnotation(Class<A> annotationClass) | 返回该类中与参数类型匹配的所有注解对象 |
getDeclaredAnnotations() | 返回该类所有的注解对象 |
- 获得类中构造器相关的方法
方法 | 用途 |
---|---|
getConstructor(Class...<?> parameterTypes) | 获得该类中与参数类型匹配的公有构造方法 |
getConstructors() | 获得该类的所有公有构造方法 |
getDeclaredConstructor(Class...<?> parameterTypes) | 获得该类中与参数类型匹配的构造方法 |
getDeclaredConstructors() | 获得该类所有构造方法 |
- 获得类中方法相关的方法
方法 | 用途 |
---|---|
getMethod(String name, Class...<?> parameterTypes) | 获得该类某个公有的方法 |
getMethods() | 获得该类所有公有的方法 |
getDeclaredMethod(String name, Class...<?> parameterTypes) | 获得该类某个方法 |
getDeclaredMethods() | 获得该类所有方法 |
- 类中其他重要的方法
方法 | 用途 |
---|---|
isAnnotation() | 如果是注解类型则返回true |
isAnnotationPresent(Class<? extends Annotation> annotationClass) | 如果是指定类型注解类型则返回true |
isAnonymousClass() | 如果是匿名类则返回true |
isArray() | 如果是一个数组类则返回true |
isEnum() | 如果是枚举类则返回true |
isInstance(Object obj) | 如果obj是该类的实例则返回true |
isInterface() | 如果是接口类则返回true |
isLocalClass() | 如果是局部类则返回true |
isMemberClass() | 如果是内部类则返回true |
Field类
Field代表类的成员变量(成员变量也称为类的属性)。
方法 | 用途 |
---|---|
equals(Object obj) | 属性与obj相等则返回true |
get(Object obj) | 获得obj中对应的属性值 |
set(Object obj, Object value) | 设置obj中对应属性值 |
Method类
Method代表类的方法。
方法 | 用途 |
---|---|
invoke(Object obj, Object... args) | 传递object对象及参数调用该对象对应的方法 |
Constructor类
Constructor代表类的构造方法。
方法 | 用途 |
---|---|
newInstance(Object... initargs) | 根据传递的参数创建类的对象 |
获取Class类对象的三种方式
- 通过类名获取
package com.back.web;
import com.back.entity.User;
public class ReflectionTest {
public static void main(String[] args) {
Class<?> c = User.class;
}
}
- 通过对象获取
package com.back.web;
import com.back.entity.User;
public class ReflectionTest {
public static void main(String[] args) {
User user = new User();
Class<?> c = user.getClass();
}
}
- 通过全类名获取
package com.back.web;
public class ReflectionTest {
public static void main(String[] args) {
try {
Class<?> c = Class.forName("com.back.entity.User");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
获取成员变量
1.先定义被反射类
package com.back.entity;
public class User {
public int userId = 1;
private String userName = "test";
private String userAcc;
private String userPwd;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAcc() {
return userAcc;
}
public void setUserAcc(String userAcc) {
this.userAcc = userAcc;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", userAcc=" + userAcc + ", userPwd=" + userPwd
+ "]";
}
}
2、利用反射获取User类的私有成员变量userName和公有成员变量userId
package com.back.web;
import java.lang.reflect.Field;
public class ReflectionTest {
public static void main(String[] args) throws Exception {
ReflectionTest test = new ReflectionTest();
Class<?> c = test.getMyClass();
System.err.println(test.getUserId(c));
System.err.println(test.getUserName(c));
}
public Class<?> getMyClass() throws ClassNotFoundException {
return Class.forName("com.back.entity.User");
}
/**
* 获取私有成员变量
*
*/
public String getUserName(Class<?> c) throws InstantiationException, IllegalAccessException {
Object obj = c.newInstance();
Field[] fields = c.getDeclaredFields();
fields[1].setAccessible(true);// 私有成员变量开启可见。否则会报错“can not access a member of class com.back.entity.User with modifiers "private"”
return String.valueOf(fields[1].get(obj));
}
/**
* 获取公有成员变量
*
*/
public String getUserId(Class<?> c) throws InstantiationException, IllegalAccessException {
Object obj = c.newInstance();
Field[] fields = c.getFields();
return String.valueOf(fields[0].get(obj));
}
}
获取方法
1、先定义被反射类
package com.back.entity;
public class User {
public int userId = 0;
private String userName = "test0";
private String userAcc;
private String userPwd;
public User() {
}
public User(int userId, String userName) {
this.userId = userId;
this.userName = userName;
}
private User(int userId, String userName, String userAcc) {
this.userId = userId;
this.userName = userName;
this.userAcc = userAcc;
}
public void setUserInfo(int userId, String userName) {
this.userId = userId;
this.userName = userName;
}
private void setUserInfo(int userId, String userName, String userAcc) {
this.userId = userId;
this.userName = userName;
this.userAcc = userAcc;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", userAcc=" + userAcc + ", userPwd=" + userPwd
+ "]";
}
}
2、通过反射进入方法
package com.back.web;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class ReflectionTest {
public static void main(String[] args) throws Exception {
ReflectionTest test = new ReflectionTest();
Class<?> c = test.getMyClass();
System.err.println(test.reflectPublicConstructor(c));
System.err.println(test.reflectPrivateConstructor(c));
System.err.println(test.reflectPublicMethod(c));
System.err.println(test.reflectPrivateMethod(c));
}
public Class<?> getMyClass() throws ClassNotFoundException {
return Class.forName("com.back.entity.User");
}
/**
* 反射公有构造方法
*
*/
public String reflectPublicConstructor(Class<?> c) throws Exception {
// 基本数据类型与包装类不自动转化。int.class换成Integer.class会报错。
// 获取公有特定参数的构造方法
Constructor<?> constructor = c.getConstructor(int.class, String.class);
// 通过实例化对象,完成给构造方法传参
Object object = constructor.newInstance(1, "test1");
// 获取所有公有构造方法
// Constructor<?>[] constructors = c.getConstructors();
return object.toString();
}
/**
* 反射私有构造方法
*
*/
public String reflectPrivateConstructor(Class<?> c) throws Exception {
// 基本数据类型与包装类不自动转化。int.class换成Integer.class会报错。
// 获取所有特定参数的构造方法
Constructor<?> constructor = c.getDeclaredConstructor(int.class, String.class, String.class);
constructor.setAccessible(true);
// 通过实例化对象,完成给构造方法传参
Object object = constructor.newInstance(2, "test2", "test2");
// 获取所有构造方法
// Constructor<?>[] constructors = c.getDeclaredConstructors();
return object.toString();
}
/**
* 反射公有方法
*/
public String reflectPublicMethod(Class<?> c) throws Exception {
Object obj = c.newInstance();
// 基本数据类型与包装类不自动转化。int.class换成Integer.class会报错。
Method method = c.getMethod("setUserInfo", int.class, String.class);
// 作用于哪个对象及传递相应的参数
method.invoke(obj, 3, "test3");
return obj.toString();
}
/**
* 反射私有方法
*/
public String reflectPrivateMethod(Class<?> c) throws Exception {
Object obj = c.newInstance();
// 基本数据类型与包装类不自动转化。int.class换成Integer.class会报错。
Method method = c.getDeclaredMethod("setUserInfo", int.class, String.class, String.class);
method.setAccessible(true);// 开启私有可见
// 作用于哪个对象及传递相应的参数
method.invoke(obj, 4, "test4", "test4");
return obj.toString();
}
}
本文由 Administrator 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站部分文章采集自互联网,因某些原因未注明出处,如有侵权,请留言告知。