Java Reflection无法正常工作并获得java.lang.NoSuchMethodException

时间:2012-08-17 04:53:56

标签: java tomcat reflection tomcat7

我有一个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();
        }
    }
}

当我运行代码时,获取以下跟踪: 微量

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

2 个答案:

答案 0 :(得分:1)

使用doGet()方法:

contextMethod.invoke ( contextClass, request, response );

您正在传递类型为Class的contextClass。您需要传递一个您希望调用该方法的该类的实例,而不是该类的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对象。

答案 1 :(得分: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