jeudi 22 septembre 2016

NoSuchMethodException with reflection in java to invoke Grails method

I have Grails class stored in db and i have to invoke it from java. I am using reflection for this.Please see below code: Getting instance of Grails class (which is in db) in java project:

  GroovyCodeSource groovySource = new GroovyCodeSource(script,name,"");
                GroovyClassLoader classLoader = new GroovyClassLoader(this.getClass().getClassLoader());

                // Load string as Groovy script class.
                Class scriptClass = classLoader.parseClass(groovySource);

                try {
                    Object classInstance  =  scriptClass.newInstance();

                    mapScriptClass.put(name, classInstance);
                    return classInstance;

                } 

In other class getting instance of grails class from above method, tried to use getDeclaredmethod() and getMethod() both are giving same error, code is:

  Object scriptClassObj = loadScriptService.getScriptClass("scriptBindingSubject"+template.getId(),
                                template.getScriptBindingSubject());
                        if (scriptClassObj != null) {
                            try{
                                Method[] methods = scriptClassObj.getClass().getMethods();
                                System.out.println(Arrays.toString(methods));

                                for (Method m : scriptClassObj.getClass().getMethods()) {
                                   System.err.println(m.getName());
                                   for (Class<?> p : m.getParameterTypes()) {
                                      System.err.println("  " + p.getName());
                                   }
                                }


                                Method method = scriptClassObj.getClass().getDeclaredMethod("process", Pub.class, List.class); //this line throws error
                                method.setAccessible(true);

                                Object returnValue = method.invoke(scriptClassObj, pub, listForMail);

                            }

When i print parameter types and method names , i didnt see any method with name process() .In above code println statements print this on console with more info

[public void com..bindingSubject.setProperty(java.lang.String,java.lang.Object), public java.lang.Object com..bindingSubject.getProperty(java.lang.String), public java.lang.Object com..bindingSubject.getLog(), public java.lang.Object com..bindingSubject.invokeMethod(java.lang.String,java.lang.Object), public groovy.lang.MetaClass com..bindingSubject.getMetaClass(), public void com..bindingSubject.setMetaClass(groovy.lang.MetaClass), public java.lang.Object com..bindingSubject.getProcess(), public void com..bindingSubject.setProcess(java.lang.Object), public void com..bindingSubject.setLog(java.lang.Object), public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public boolean java.lang.Object.equals(java.lang.Object), public java.lang.String java.lang.Object.toString(), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), public final native void java.lang.Object.notify(), public final native void

java.lang.Object.notifyAll()]
setProperty
  java.lang.String
  java.lang.Object
getProperty
  java.lang.String
getLog
invokeMethod
  java.lang.String
  java.lang.Object
getMetaClass
setMetaClass
  groovy.lang.MetaClass
getProcess
setProcess
  java.lang.Object
setLog
  java.lang.Object
wait
wait
  long
  int
wait
  long
equals
  java.lang.Object
toString
hashCode
getClass
notify
notifyAll

From db script class is

package com...

class bindingSubject{

    def log

    def process = { pub,listForMail ->

        def mapBinding = [:]


        def fu
        def perimeters = pub.sub.ent
        perimeters.each(){ entity ->
             if (fu == null){
                do something
             }
        } 
        mapBinding.entity = fu.name
        if (!mapBinding.entity.toUpperCase().contains("project")){
        do something
        }

        // mapBinding.date = String.format('%tm/%<td/%<tY',pub.plannedDate)
        mapBinding.date = String.format('%tm/%<td/%<tY',listForMail.first().valueDate)

        return mapBinding
    }

}

Please let me know, why reflection is not able to find method process() from java code.

Thanks.





Aucun commentaire:

Enregistrer un commentaire