java的反射机制的一个问题 Class AClass = objA.getClass();Method m = AClass.getMethod(...);m.invoke(objA,new Object[]{str}); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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); }} 核心是Method m = eventClass.getMethod(eventMethod, paramTypes);Map eventReturn = (Map) m.invoke(null, params); 关于public类 java怎么读取数据库里的所有表名? 请大家帮我看看一个用 ant 生成 javadoc的问题 恳求续:java中如何实现数字的集合的排列组合,并将各个排列存放到内存中待以后处理? 谁有jdk chm格式的书? 小弟建了一个Java GUI技术交流,望大家支技 不知是哪里设错了,急! jb7怎么注册啊~~ 各位兄弟姐妹谈谈关于JAVA连接数据库的方法怎么样??????来up也欢迎 3000元学scjd值不值? 请高手指点,从C++到Java必碰到的问题 帮我看看这个警告是什么意思,来者有分!!
* 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);
}
}
Method m = eventClass.getMethod(eventMethod, paramTypes);
Map eventReturn = (Map) m.invoke(null, params);