pointerness
pointerness

Reputation: 323

reflection and polymorphism

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; 
}

Upvotes: 1

Views: 1230

Answers (1)

Thomas
Thomas

Reputation: 88727

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.

Upvotes: 1

Related Questions