Java 反射不工作并获取 java.lang.NoSuchMethodException

标签 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_32Tomcat 7

最佳答案

您正在从 getContextBoundedClass() 返回一个 Class 对象,并将其放入此处的 Object 中:

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

而是这样说:

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

您不要再次调用 contextClass.getClass()。要获取类的实例,您可以调用 contextClass.newInstance() 来创建该对象的实例并在调用该方法时使用它。阅读本教程:

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

关于Java 反射不工作并获取 java.lang.NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11999982/

相关文章:

matlab - 在反射线上反射 RGB 图像中的点 (MATLAB)

java空引用复制

java - SwingWorker 从不返回任何东西? (java)

java - 从 mac 应用程序包中的 jar 捕获错误/堆栈跟踪

java - TOMCAT如何在系统中定位JRE?

mysql - 基于 J2EE 表单的登录错误

java - 调用返回通用列表类类型对象的方法会抛出 java.lang.IllegalArgumentException

java - RMI:多个请求,一个数据

tomcat - Lua脚本从apache tomcat服务器获取日期和时间

反射时Java参数类型不匹配