我有一个 Servlet 类,它使用反射进行一些动态类初始化和方法调用。它有以下代码来做到这一点。
@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();
}
}
}
当我运行代码时,得到以下跟踪: Trace
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