反射和多态
我试图通过反射调用类中的方法。该方法位于 ejb 类内部。我一直找不到方法。我编写了以下代码来调试该问题。 其中一个参数(罐子内定义的接口)似乎相同,但哈希码不同,这导致了问题。我已经验证了调用代码和 calee 代码中的 jar 版本。在调用代码中,哈希码在调用之间是相同的。但是在被调用者代码中,接口的哈希码不断更改任何指针?
private static Method findMethod( Class<?> encapsulatingClass, Class<?>[] paramArray, String methodName) {
Method[] methods = encapsulatingClass.getDeclaredMethods();
Method method = null;
for (int i = 0; i < methods.length; i++) {
method = methods[i];
Class<?>[] paramTypes = method.getParameterTypes();
if (method.getName().equals(methodName) ) {
if(paramTypes.length == paramArray.length){
for(int j = 0;j<paramTypes.length;j++){
if(!paramTypes[j].equals(paramArray[j])){
Debug.info("Method argument " + paramTypes[j].getName() + " hashcode = "
+ paramTypes[j].hashCode() + "Parameter " + paramArray[j].getName() + " Hashcode = " +
paramArray[j].hashCode());
if(paramTypes[j].getName().equals(paramArray[j].getName())){
Debug.info("Atleast Parameter name seems to be same");
}else{
Debug.info("Strange cant find a diff can u??");
Debug.info("String 1 = " + paramTypes[j].getName() + " String 2 = " + paramArray[j].getName());
}
}
}
}
break;
}
}
return method;
}
I am trying to invoke a method in a class via reflection. The method is inside an ejb class. I keep getting method not found. I wrote the following code to debug the issue.
One of the parameters(an interface defined inside a jar) seems to be same but there hash code differs which is causing the issue. I have verified the jar versions in both the calling and calee code. In the calling code the hashcode is same across calls. But in the callee code the hashcode for the interface keeps on changing any pointers??
private static Method findMethod( Class<?> encapsulatingClass, Class<?>[] paramArray, String methodName) {
Method[] methods = encapsulatingClass.getDeclaredMethods();
Method method = null;
for (int i = 0; i < methods.length; i++) {
method = methods[i];
Class<?>[] paramTypes = method.getParameterTypes();
if (method.getName().equals(methodName) ) {
if(paramTypes.length == paramArray.length){
for(int j = 0;j<paramTypes.length;j++){
if(!paramTypes[j].equals(paramArray[j])){
Debug.info("Method argument " + paramTypes[j].getName() + " hashcode = "
+ paramTypes[j].hashCode() + "Parameter " + paramArray[j].getName() + " Hashcode = " +
paramArray[j].hashCode());
if(paramTypes[j].getName().equals(paramArray[j].getName())){
Debug.info("Atleast Parameter name seems to be same");
}else{
Debug.info("Strange cant find a diff can u??");
Debug.info("String 1 = " + paramTypes[j].getName() + " String 2 = " + paramArray[j].getName());
}
}
}
}
break;
}
}
return method;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您有不同的类加载器,它们都包含该类的副本。尽管这些类可能完全相等,但您不能互换使用它们,除非您序列化它们之间的对象(如 EJB 远程调用所做的那样)。
当您的客户端和服务器在不同的 JVM 中运行或当您的应用程序服务器引入多个类加载器以提供应用程序隔离时,这种情况通常会发生。
It looks like you have different class loaders that both contain a copy of that class. Although the classes might be completely equal you can't use them interchangeably unless you serialize the objects in between (like EJB remote calls do).
This mostly happens when your client and server run in different JVMs or when your application server introduces multiple class loaders to provide application isolation.