0

我有一个 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_32Tomcat 7

4

2 回答 2

1

在您的doGet()方法中,您有:

contextMethod.invoke ( contextClass, request, response );

您正在传递一个contextClassClass 类型。您需要向它传递一个您希望调用该方法的类的实例,而不是该类的 Class 对象。不知道你在哪里有一个实例AuthenticationContextHelper,我在你的 Servlet 类的任何地方都看不到它。如果这不可能,您可以loginProcess()方法设为静态,然后将一个空对象传递给invoke(null, request, response).


PageContextHelper.getContextBoundedClass 方法通过调用 Class.forName 返回类 AuthenticationContextHelper

看起来它返回一个Class对象:

classOBJ = Class.forName( CONTEXT_CLASS_PACKAGE + basePath + CONTEXT_CLASS_SUFFIX);

当你尝试创建一个方法时,你是在类上创建一个方法Class,因为你调用了Class.getClass(). 这就是为什么你得到你的例外。看看你的输出:

Contextclass : class com.proj.context.AuthenticationContextHelper
thisClass : class java.lang.Class

它们都是 Class 对象。

于 2012-08-17T05:00:21.557 回答
1

您正在Class从这里返回一个对象getContextBoundedClass()并将其放入Object

Object contextClass = PageContextHelper.getContextBoundedClass( "authentication");

而是这样说:

 Class contextClass = PageContextHelper.getContextBoundedClass( "authentication");

你不要再打电话contextClass.getClass()了。而不是获取您调用的类contextClass.newInstance()的实例来创建该对象的实例并在调用方法时使用它。阅读本教程:

http://docs.oracle.com/javase/tutorial/reflect/index.html

于 2012-08-17T05:15:42.790 回答