2017-11-13 5 views
0

내 Java 어플리케이션의 런타임에 (haan.java라는 이름의) 클래스를 생성 중입니다. 이 클래스의 객체는 Oracle Coherence Cache에 삽입됩니다. 캐시에서 객체 (tempHaan)를 가져올 때 유형은 haan입니다.동적 객체에 대한 반사 메소드 호출

오류 라인 "개체 발생 :"개체 선언 클래스의 인스턴스가 아닌 java.lang.IllegalArgumentException가 "나는이 객체 (tempHaan)의 메소드를 호출 할 때, 나는 오류 가 무엇입니까 invoke = methodgetHash.invoke (tempHaan, null); "

PFB the code: 

NamedCache cacheConn; 
CacheFactory.ensureCluster(); 
NamedCache cacheConnHaan = CacheFactory.getCache("Haan"); 
Class cls = null; 
File f = new File(rtomProperties.getPropertyValue("pojoToBuild")); 

try { 
    ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader(); 
    URLClassLoader cl = new URLClassLoader(new URL[] { f.toURI().toURL() }, 
     currentThreadClassLoader); 
    Object tempHaan; 
    cls = Class.forName(rtomProperties.getPropertyValue("pojoPackageLocation").concat(".haan"), true, cl); 
    System.out.println("*************** the class is **********" 
     + cls.newInstance().getClass().toString()); 
    System.out.println("******DONE LOADING"); 

    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); 
    Class clazz = URLClassLoader.class; 

    // Use reflection 
    Method m = clazz.getDeclaredMethod("addURL", new Class[] { URL.class }); 
    m.setAccessible(true); 
    URL ur = f.toURI().toURL(); 
    m.invoke(classLoader, new Object[] { ur }); 

    Thread.currentThread().getContextClassLoader().getResourceAsStream("context.xml"); 

    tempHaan = cacheConnHaan.get(aan); 

    System.out.println("*************AFTER" + tempHaan.getClass().toString()); 

    System.out.println("*************tempHaan: "+tempHaan.toString()); 
    Class[] paramObject = new Class[1]; 
    paramObject[0] = Object.class; 
    Method methodgetHash = null;        
    Class noparams[] = {}; 
    methodgetHash = cls.getDeclaredMethod("getHash", noparams); 
    Object temp = cls.newInstance(); 

    Object invoke = methodgetHash.invoke(tempHaan, null); 
    key = (String) invoke; 

    System.out.println("key for the record: " + key); 
} catch (ClassNotFoundException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} catch (InstantiationException e) { 
    // TODO Auto-generated catch block 
    System.out.println("******not instantiated"); 
    e.printStackTrace(); 
} catch (IllegalAccessException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (NullPointerException npe) { 
    System.out.println("Hash-Aan mapping not found for aan " + aan); 
    continue; 
} 
+0

이 모든 이유는 무엇입니까? 지나치게 복잡하고 무의미한 것처럼 보입니다. – Kayaman

답변

1

동적 클래스, 즉 캐시 된 인스턴스의 클래스와이 오류로 이어지는 다른 클래스 로더에서 온에서 메소드가 호출 할 수 클래스를로드에 대한 ULRClassLoader을 만들 수 있습니다. 당신은 클래스와 함께 방법을 캐싱

Method methodgetHash = tempHaan.getDeclaredMethod("getHash", noparams); 

를 호출하여 호출 될 캐시 할 인스턴스에서 선언 된 방법을 얻을 인스턴스와 함께 캐시하거나 캐시에서 인스턴스를 검색하는 방법을 얻을 수 있습니다 성능 향상 BTW가 캐시 될 것입니다.

+0

고마워요 @Lothar – Anjani

+0

저에게 도움이되는 코드 : ClassLoader currentThreadClassLoader = Thread.currentThread(). getContextClassLoader(); \t \t \t \t \t \t \t URLClassLoader의 classLoader가 = (URLClassLoader의) currentThreadClassLoader.getSystemClassLoader(); \t \t \t \t \t \t \t cls = URLClassLoader.class; \t \t \t \t \t \t \t 방법 m = cls.getDeclaredMethod ("addURL"새로운 클래스 [{URL.class}); \t \t \t \t \t \t \t m.setAccessible (true); \t \t \t \t \t \t \t URL ur = f.toURI(). toURL(); \t \t \t \t \t \t \tm.invoke (classLoader, new Object [] {ur}); \t \t \t \t \t \t \t \t \t \t \t \t CLS = Class.forName을 (rtomProperties.getPropertyValue ("pojoPackageLocation"). CONCAT (". 하안") \t \t \t \t \t \t \t \t 진정한 \t, currentThreadClassLoader) ; – Anjani

+0

@Anjani이 질문을 답이없는 질문 목록에서 없애기 위해 다른 대답으로 추가하고 동의 할 수 있습니까? – Lothar

0

나를 위해 작동 한 코드는 다음과 같습니다. ClassLoader currentThreadClassLoader = Thread.currentThread(). getContextClassLoader(); URLClassLoader classLoader = (URLClassLoader) currentThreadClassLoader.getSystemClassLoader(); cls = URLClassLoader.class; 메소드 m = cls.getDeclaredMethod ("addURL", 새 클래스 [] {URL.class}); m.setAccessible (true); URL ur = f.toURI(). toURL(); m.invoke (classLoader, new Object [] {ur});
cls = Class.forName (rtomProperties.getPropertyValue ("pojoPackageLocation"). concat (". haan"), true, currentThreadClassLoader);

관련 문제