2011-12-01 40 views
1

我想通過反射調用類中的方法。該方法在ejb類中。我一直在找不到方法。我編寫了下面的代碼來調試問題。 其中一個參數(在jar中定義的接口)似乎是相同的,但是存在散列碼不同,導致問題。我已經在調用和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; 
} 

回答

1

看起來你有不同的類加載器,它們都包含該類的副本。雖然這些類可能完全相同,但除非您序列化之間的對象(如EJB遠程調用),否則不能互換使用它們。

當您的客戶端和服務器運行在不同的JVM中或應用程序服務器引入了多個類加載器來提供應用程序隔離時,通常會發生這種情況。