版权声明:本文为作者原创,转载请注明出处,联系qq:32248827 https://blog.csdn.net/dataiyangu/article/details/88370206
背景
在java agent中抓取Resin的 某些方法,在invoke的时候出现错误
java.lang.IllegalArgumentException: object is not an instance of declaring class
出现这个问题的原因是通过clazz找到的method,在真正invoke的时候传进去的clazz是不一样的。
原因
代码重构后再插件中Method进行了缓存(static)导致
provite Static Method mmm = null
if(method == null){
methodFromCache = clazz.getMethod("methodname", parameterTypes);
}
method.invoke(clazz,parameterTypes)
//我们这里的methodname是request
我们这里的clazz是request
通过debug发现在resin刚刚启动的时候会通过某种机制(位置)发送一个request,com.xxxx.xxxx.httpreqest,在客户端再次访问,再次发送一次request,com.xxxx.xxx.dispetchrequest
第一次将request缓存了进去(com.xxxx.xxxx.httpreqest),第二次直接取,可是第二次的clazz变成了com.xxxx.xxx.dispetchrequest,所以出现了上面的错误。
解决办法
通过map缓存方法,map中的key是classname+method name作为唯一确定一个方法的标识,value是classname
private static Map<String, Method> methodMap = new HashMap<String, Method>();
String methodId = className + methodName;
Method method = methodMap.get(methodId);
if(method == null){
methodFromCache = clazz.getMethod("methodname", parameterTypes);
}
method.invoke(clazz,parameterTypes)
这样保证了方法名其实是classname+methodname,这样也就不会出现上面的问题了。