我有一個Servlet類,它使用反射做一些動態類初始化和方法調用。它有以下代碼來做到這一點。Java反射不工作和獲取java.lang.NoSuchMethodException
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String reqActionContext = PageContextHelper.getFunctionName("login");
// Trace reqActionContext
System.out .println("reqActionContext : " + reqActionContext);
// Get the page context related class object
Object contextClass = PageContextHelper.getContextBoundedClass("authentication");
Class <?>[ ] paramTypes = new Class <?>[2];
paramTypes[0] = HttpServletRequest.class ;
paramTypes[1] = HttpServletResponse.class ;
// Is there a class associated with the current context
if (contextClass != null) {
// Trace contextClassSystem. out .println("Contextclass : " + contextClass);
// get the Class
Class <?> thisClass = contextClass.getClass();
// Trace thisClass
System.out .println("thisClass : " + thisClass);
// get the method
Method contextMethod = thisClass.getDeclaredMethod(reqActionContext, paramTypes);
if (contextMethod != null) {
contextMethod.invoke (contextClass, request, response);
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
其中獲得上下文界類具有以下代碼的類:
PageContextHelper.java
public class PageContextHelper {
private static final String CONTEXT_CLASS_SUFFIX = "ContextHelper" ;
private static final String CONTEXT_CLASS_PACKAGE = "com.proj.context." ;
/**
* Get the base path related context class object.
*
*@param basePath
*@return
*/
public static Object getContextBoundedClass (String basePath) {
Class <?> classOBJ = null ;
try{
if(basePath != null && !basePath.isEmpty() && basePath.length() > 1) {
basePath = basePath .substring(0,1).toUpperCase() + basePath.substring(1).toLowerCase();
}
// Get the class object
classOBJ = Class.forName(CONTEXT_CLASS_PACKAGE + basePath + CONTEXT_CLASS_SUFFIX);
} catch (ClassNotFoundException e) {
// Do nothing and return null object
}
return classOBJ;
}
.....
}
這是尋找類是com.proj.context.AuthenticationContextHelper
,它具有以下內容:
com.proj.context.AuthenticationContextHelper
package com.proj.context;
public class AuthenticationContextHelper{
public void loginProcess (HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
// Do some processing here
} catch (Exception e) {
// TODO : Remove this trace line and do something meaningful
e.printStackTrace();
}
}
}
當我運行代碼得到下面的跟蹤:跟蹤
reqActionContext :loginProcess
Contextclass : class com.proj.context.AuthenticationContextHelper
thisClass : class java.lang.Class
java.lang.NoSuchMethodException: java.lang.Class.loginProcess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
at java.lang.Class.getDeclaredMethod(Class.java:1937)
at com.proj.servlet.ContentServlet.doGet(ContentServlet.java:81)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
...
它說NoSuchMethod但該方法存在的類com.proj.context.AuthenticationContextHelper
。
我是否在代碼中缺少任何基本的或錯誤的東西?
我使用的環境是的Java 1.6.0_32和的Tomcat 7
將'contextClass'引用更改爲類'Class'並調用'contextClass.newInstance()'解決了我的問題。 – 2012-08-17 06:31:41