【问题标题】:reflection method invocation for a dynamic object动态对象的反射方法调用
【发布时间】:2017-11-14 19:22:41
【问题描述】:

我正在我的 java 应用程序运行时创建一个类(名为 haan.java)。 此类的对象被插入到 Oracle Coherence 缓存中 当我从缓存中获取一个对象(tempHaan)时,它的类型是 haan。但是当我尝试调用这个对象(tempHaan)的方法时,我收到错误 "java.lang.IllegalArgumentException: object is not an instance of declaring class"

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

【问题讨论】:

  • 这一切的原因是什么?这似乎过于复杂和毫无意义。

标签: java reflection


【解决方案1】:

您动态创建一个ULRClassLoader 来加载类,即缓存实例的类和您从中获取方法调用的类来自不同的类加载器,从而导致此错误。您可以从要缓存的实例中获取声明的方法并将其与实例一起缓存,或者您从缓存中检索实例并通过调用获取要调用的方法

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

将方法与要缓存的类一起缓存会带来性能提升顺便说一句。

【讨论】:

  • 谢谢@Lothar
  • 对我有用的代码:ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader(); URLClassLoader classLoader = (URLClassLoader) currentThreadClassLoader.getSystemClassLoader(); cls = URLClassLoader.class;方法 m = cls.getDeclaredMethod("addURL", new Class[] { URL.class }); m.setAccessible(true);网址 ur = f.toURI().toURL(); m.invoke(classLoader, new Object[] { ur }); cls = Class.forName(rtomProperties.getPropertyValue("pojoPackageLocation").concat(".haan"), true, currentThreadClassLoader);
  • @Anjani 您能否将其添加为另一个答案并接受它,以便将此问题从未回答的问题列表中删除?
【解决方案2】:

对我有用的代码: 类加载器 currentThreadClassLoader = Thread.currentThread().getContextClassLoader(); URLClassLoader classLoader = (URLClassLoader) currentThreadClassLoader.getSystemClassLoader(); cls = URLClassLoader.class; 方法 m = cls.getDeclaredMethod("addURL", new Class[] { URL.class }); m.setAccessible(true); 网址 ur = f.toURI().toURL(); m.invoke(classLoader, new Object[] { ur });
cls = Class.forName(rtomProperties.getPropertyValue("pojoPackageLocation").concat(".haan"), true, currentThreadClassLoader);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 2010-11-30
    • 2013-03-23
    • 2011-02-15
    • 2013-07-23
    相关资源
    最近更新 更多