2017-11-13 4 views
0

私は、Javaアプリケーションの実行時にクラス(名前はhaan.java)を作成しています。 このクラスのオブジェクトはOracle Coherenceキャッシュに挿入されます キャッシュからオブジェクト(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のクラスローダ=(URLClassLoaderの)currentThreadClassLoader.getSystemClassLoader(); \t \t \t \t \t \t \t cls = URLClassLoader.class; \t \t \t \t \t \t \tメソッドm = cls.getDeclaredMethod( "addURL"、new Class [] {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です。呼び出し(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オブジェクト[] {ur});
cls = Class.forName(rtomProperties.getPropertyValue( "pojoPackageLocation")。concat( "。haan")、 true、currentThreadClassLoader);

関連する問題