Class AClass = objA.getClass();
Method m = AClass.getMethod(...);
m.invoke(objA,new Object[]{str});

解决方案 »

  1.   

    public class AppJavaEventHandler implements AppEventHandler {    public static final String module = AppJavaEventHandler.class.getName();    private Map eventClassMap = new HashMap();    /** 
         * Invoke the web event
         * @param eventPath The path or location of this event
         * @param eventMethod The method to invoke
         * @param request The servlet request object
         * @param response The servlet response object
         * @return String Result code
         * @throws EventHandlerException
         */
        public Map invoke(String eventPath, String eventMethod ,Class[] paramTypes,Object[] params) throws EventHandlerException {
            Class eventClass = (Class) this.eventClassMap.get(eventPath);        if (eventClass == null) {
                synchronized (this) {
                    eventClass = (Class) this.eventClassMap.get(eventPath);
                    if (eventClass == null) {
                        try {
                            ClassLoader loader = Thread.currentThread().getContextClassLoader();
                            eventClass = loader.loadClass(eventPath);
                        } catch (ClassNotFoundException e) {
                            Debug.logError(e, "Error loading class with name: " + eventPath + ", will not be able to run event...");
                        }
                        if (eventClass != null) {
                            eventClassMap.put(eventPath, eventClass);
                        }
                    }
                }
            }
            System.out.println("[Set path/method]: " + eventPath + " / " + eventMethod);        //Class[] paramTypes = new Class[] {HttpServletRequest.class, HttpServletResponse.class};        System.out.println("*[[Event invocation]]*");
            //Object[] params = new Object[] {request, response};        return invoke(eventPath, eventMethod, eventClass, paramTypes, params);
        }    private Map invoke(String eventPath, String eventMethod, Class eventClass, Class[] paramTypes, Object[] params) throws EventHandlerException {
            if (eventClass == null) {
                throw new EventHandlerException("Error invoking event, the class " + eventPath + " was not found");
            }
            if (eventPath == null || eventMethod == null) {
                throw new EventHandlerException("Invalid event method or path; call initialize()");
            }        System.out.println("[Processing]: JAVA Event");
            try {
                Method m = eventClass.getMethod(eventMethod, paramTypes);
                Map eventReturn = (Map) m.invoke(null, params);            //if (Debug.verboseOn()) Debug.logVerbose("[Event Return]: " + eventReturn, module);
                return eventReturn;
            } catch (java.lang.reflect.InvocationTargetException e) {
                Throwable t = e.getTargetException();            if (t != null) {
                    System.out.println("Problems Processing Event");
                    throw new EventHandlerException("Problems processing event: " + t.toString(), t);
                } else {
                    System.out.println("Problems Processing Event");
                    throw new EventHandlerException("Problems processing event: " + e.toString(), e);
                }
            } catch (Exception e) {
                System.out.println("Problems Processing Event");
                throw new EventHandlerException("Problems processing event: " + e.toString(), e);
            }
        }
        public static void main(String[] args){
         String eventPath = "com.service.app.JavaTestEvent";
         String eventMethod = "exportToFile";
         Class[] paramTypes = null;
         Object[] params = null;
         Map eventReturn=null;
         AppJavaEventHandler tempHandler  = new AppJavaEventHandler();
         try{    
            eventReturn = tempHandler.invoke(eventPath,eventMethod,paramTypes,params);
         }catch(EventHandlerException e){
            System.out.println("Exception:"+e.getMessage());
         }
         System.out.println("eventReturn="+eventReturn);
        }
    }
      

  2.   

    核心是
    Method m = eventClass.getMethod(eventMethod, paramTypes);
    Map eventReturn = (Map) m.invoke(null, params);