Java 反射调用的一种优化
写一些Java框架的时候,经常需要通过反射get或者set某个bean的field,比较普通的做法是获取field后调用java.lang.reflect.Field.get(Object),但每次都这样调用,能否有优化的空间呢?
答案是有。
第一种:
由于每次都是重复的调用,所以想到了缓存每个bean的field,但这样做还是不够,所以想到了写一个code generator。通过生成代码的方式,get或者set每个bean的时候直接调用该bean的getter或者setter,这个实现听起来很牛逼,其实就是用asm生成一个类在用一个classloader加载进来每次调用直接invoke就可以了。
可单纯为了一个反射调用做这么多,总感觉是大炮打了蚊子。
第二种:
多谢@RednaxelaFX 的指点,找到了更简单的做法:sun.misc.Unsafe
使用也非常的简单:首先通过sun.misc.Unsafe.objectFieldOffset(Field) 获取field的offset,然后使用sun.misc.Unsafe.getObject(Object, long)获取某个实例上的field的值。
简单的测试代码如下:
- import java.io.Serializable;
- import java.lang.reflect.Field;
- import sun.misc.Unsafe;
- /**
- * @author haitao.yao Dec 14, 2010
- */
- public class ReflectionCompare {
- private static final int count = 10000000;
- /**
- * @param args
- */
- public static void main(String[] args) {
- long duration = testIntCommon();
- System.out.println("int common test for " + count
- + " times, duration: " + duration);
- duration = testUnsafe();
- System.out.println("int unsafe test for " + count
- + " times, duration: " + duration);
- }
- private static long testUnsafe() {
- long start = System.currentTimeMillis();
- sun.misc.Unsafe unsafe = getUnsafe();
- int temp = count;
- Field field = getIntField();
- long offset = unsafe.objectFieldOffset(field);
- while (temp-- > 0) {
- unsafe.getInt(new TestBean(), offset);
- }
- return System.currentTimeMillis() - start;
- }
- private static long testIntCommon() {
- long start = System.currentTimeMillis();
- int temp = count;
- getIntField().setAccessible(true);
- while (temp-- > 0) {
- TestBean bean = new TestBean();
- try {
- getIntField().get(bean);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return System.currentTimeMillis() - start;
- }
- private static final sun.misc.Unsafe unsafe;
- static {
- sun.misc.Unsafe value = null;
- try {
- Class<?> clazz = Class.forName("sun.misc.Unsafe");
- Field field = clazz.getDeclaredField("theUnsafe");
- field.setAccessible(true);
- value = (Unsafe) field.get(null);
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException("error to get theUnsafe", e);
- }
- unsafe = value;
- }
- public static final sun.misc.Unsafe getUnsafe() {
- return unsafe;
- }
- private static final Field intField;
- private static final Field stringField;
- static {
- try {
- intField = TestBean.class.getDeclaredField("age");
- stringField = TestBean.class.getDeclaredField("name");
- } catch (Exception e) {
- e.printStackTrace();
- throw new IllegalStateException("failed to init testbean field", e);
- }
- }
- public static final Field getIntField() {
- return intField;
- }
- public static final Field getStringField() {
- return stringField;
- }
- /**
- * @author haitao.yao
- * Dec 14, 2010
- */
- static class TestBean implements Serializable{
- /**
- *
- */
- private static final long serialVersionUID = -5994966479456252766L;
- private String name;
- private int age;
- /**
- * @return the name
- */
- public String getName() {
- return name;
- }
- /**
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * @return the age
- */
- public int getAge() {
- return age;
- }
- /**
- * @param age the age to set
- */
- public void setAge(int age) {
- this.age = age;
- }
- }
- }
import java.io.Serializable;
import java.lang.reflect.Field;
import sun.misc.Unsafe;
/**
* @author haitao.yao Dec 14, 2010
*/
public class ReflectionCompare {
private static final int count = 10000000;
/**
* @param args
*/
public static void main(String[] args) {
long duration = testIntCommon();
System.out.println("int common test for " + count
+ " times, duration: " + duration);
duration = testUnsafe();
System.out.println("int unsafe test for " + count
+ " times, duration: " + duration);
}
private static long testUnsafe() {
long start = System.currentTimeMillis();
sun.misc.Unsafe unsafe = getUnsafe();
int temp = count;
Field field = getIntField();
long offset = unsafe.objectFieldOffset(field);
while (temp-- > 0) {
unsafe.getInt(new TestBean(), offset);
}
return System.currentTimeMillis() - start;
}
private static long testIntCommon() {
long start = System.currentTimeMillis();
int temp = count;
getIntField().setAccessible(true);
while (temp-- > 0) {
TestBean bean = new TestBean();
try {
getIntField().get(bean);
} catch (Exception e) {
e.printStackTrace();
}
}
return System.currentTimeMillis() - start;
}
private static final sun.misc.Unsafe unsafe;
static {
sun.misc.Unsafe value = null;
try {
Class<?> clazz = Class.forName("sun.misc.Unsafe");
Field field = clazz.getDeclaredField("theUnsafe");
field.setAccessible(true);
value = (Unsafe) field.get(null);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("error to get theUnsafe", e);
}
unsafe = value;
}
public static final sun.misc.Unsafe getUnsafe() {
return unsafe;
}
private static final Field intField;
private static final Field stringField;
static {
try {
intField = TestBean.class.getDeclaredField("age");
stringField = TestBean.class.getDeclaredField("name");
} catch (Exception e) {
e.printStackTrace();
throw new IllegalStateException("failed to init testbean field", e);
}
}
public static final Field getIntField() {
return intField;
}
public static final Field getStringField() {
return stringField;
}
/**
* @author haitao.yao
* Dec 14, 2010
*/
static class TestBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5994966479456252766L;
private String name;
private int age;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}
}
通过测试发现,效率是普通java.lang.reflect.Field.get(Object)的3倍,当然,性能这个东西,还是自己测试了放心。
这样做有一个不好的地方:sun.misc.Unsafe在sun的包里,默认情况下,eclipse编译会报错,在Window->Preference->Java->Compiler->Errors/Warnings->Deprecated and restricted API -> Forbidden Reference 修改成Warning或者Ignore就可以了。由于Unsafe在JDK中很多的类库中都在使用,框架代码中使用还是很安全的,如果需要api变动,JDK源代码的修改工作量比我们的大多了 :-0
至于第一种方法,虽然麻烦,有时间还是可以尝试一下的,有时间了写一下。
Java 反射调用的一种优化的更多相关文章
- Java 反射 调用私有域和方法(setAccessible)
Java 反射 调用私有域和方法(setAccessible) @author ixenos AccessibleObject类 Method.Field和Constructor类共同继承了Acces ...
- 利用java反射调用类的的私有方法--转
原文:http://blog.csdn.net/woshinia/article/details/11766567 1,今天和一位朋友谈到父类私有方法的调用问题,本来以为利用反射很轻松就可以实现,因为 ...
- JAVA反射调用方法
1.用户类 package com.lf.entity; import com.lf.annotation.SetProperty; import com.lf.annotation.SetTable ...
- 通过Java反射调用方法
这是个测试用的例子,通过反射调用对象的方法. TestRef.java import java.lang.reflect.Method; import java.lang.reflect.In ...
- java反射调用dubbo接口
需求:项目增加幂等 场景:1.三个项目:a .b.c2.a项目加幂等3.b项目dubbo调用项目a的时候超时没有获取返回结果,增加重试机制(非立即重试,3min or 5min 后重试)4.c项目是一 ...
- Java 反射 调用私有构造方法
单例类: package singleton; public class SingletonTest { // 私有构造方法 private SingletonTest(){ System.out.p ...
- java黑魔法-反射机制-02-通过Java反射调用其他类方法
package com.aaron.reflect; import java.lang.reflect.Method; import java.lang.reflect.InvocationTarge ...
- java反射调用api
cglib的fastmethod 简单示例: FastClass serviceFastClass = FastClass.create(Person.class); Person p = new P ...
- Java 反射调用方法 - 不跳过安全检查、跳过安全检查和普通方法性能比较测试
java中反射提供灵活性同时,给运行效率带来了一定影响.写个代码测试一下 package com.xzlf.reflectTest; import java.lang.reflect.Method; ...
随机推荐
- vue的nextTick的实现
vue的nextTick是用浏览器支持的方法模拟nodejs的process.nextTick 老版本的vue用如下方法来模拟 Promise.thenMutationObserver(Mutatio ...
- [COGS 2089.] 平凡的测试数据 带权并查集
差点就撸上LCT了....... 带权并查集就是在并查集的基础上稍作修改,我的用穿址实现的有人用记录原父亲来实现. #include<cstdio> #define N 300010 us ...
- yaf的安装
http://kenby.iteye.com/blog/1979899 yaf源码分析学习网站 # wget https://github.com/laruence/php-yaf/archive/m ...
- [POJ2777] Count Color
\[Count Color\] Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50865 Accepted: 15346 Des ...
- POJ1797:Heavy Transportation(改造Dijkstra)
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 52728 Accepted: ...
- 通用adapter
http://blog.csdn.net/lmj623565791/article/details/38902805/
- 对Office文档进行授权
Microsoft.Office.Interop.Word.ApplicationClass app = new Microsoft.Office.Interop.Word.ApplicationCl ...
- IDEA 用maven创建web项目编译时不能发布resources中的文件
1.在pom.xml加入 <build> <resources> <resource> <directory>${basedir}/src/main/j ...
- js实现2048小游戏
这是学完javascript基础,编写的入门级web小游戏 游戏规则:在玩法规则也非常的简单,一开始方格内会出现2或者4等这两个小数字,玩家只需要上下左右其中一个方向来移动出现的数字,所有的数字就会想 ...
- 计算机网络中七层,五层,四层协议;IP 地址子网划分
七层协议: 7 应用层(http) 6 表示层(上层用户可以相互识别的数据:jpg) 5 会话层(不同主机不同线程间的通信) 4 运输层(tcp/ip:传输层提供端到端的透明数据服务)/差错控制和流量 ...