dimanche 12 novembre 2017

reflection method invocation for a dynamic object

I am creating a class (named haan.java) at runtime of my java application. The object of this class is inserted in a Oracle Coherence Cache When I get an object(tempHaan) from the cache, it is of type haan. But when I try to invoke the method of this object(tempHaan), I am getting the error "java.lang.IllegalArgumentException: object is not an instance of declaring class"

Error occurs at the line "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;
                        }





Aucun commentaire:

Enregistrer un commentaire