ASM-14-reflectASM 性能更高的反射框架
2018年7月20日大约 1 分钟
reflectasm
reflectasm is a very small Java library that provides high performance reflection by using code generation.
An access class is generated to set/get fields, call methods, or create a new instance.
The access class uses bytecode rather than Java's reflection, so it is much faster.
It can also access primitive fields via bytecode to avoid boxing.
使用例子
Maven 引入
com.esotericsoftware
reflectasm
1.11.9
测试代码
对象定义
public class UserService {
private String name;
public UserService() {
this("default");
}
public UserService(String name) {
this.name = name;
}
public void showName() {
System.out.println("Name is: " + name);
}
}
方法验证
UserService someObject = new UserService("ryo");
MethodAccess access = MethodAccess.get(UserService.class);
access.invoke(someObject, "showName");
构造器验证
发现不支持有参构造器,这点我们可以进行提升。
ConstructorAccess access = ConstructorAccess.get(UserService.class);
UserService someObject = access.newInstance();
someObject.showName();
字段构造器
字段默认也无法反射私有变量,这也非常的麻烦。
UserService someObject = new UserService();
FieldAccess access = FieldAccess.get(UserService.class);
for(int i = 0; i [bean-mapping](https://github.com/houbb/bean-mapping)
# 参考文档
[https://asm.ow2.io/asm4-guide.pdf](https://asm.ow2.io/asm4-guide.pdf)
[reflectasm](https://github.com/EsotericSoftware/reflectasm)
贡献者
binbin.hou