Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java反射不工作,正在获取Java.lang.NoSuchMethodException_Java_Tomcat_Reflection_Tomcat7 - Fatal编程技术网

Java反射不工作,正在获取Java.lang.NoSuchMethodException

Java反射不工作,正在获取Java.lang.NoSuchMethodException,java,tomcat,reflection,tomcat7,Java,Tomcat,Reflection,Tomcat7,我有一个Servlet类,它使用反射进行一些动态类初始化和方法调用。它有以下代码来实现这一点 @Override protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { try { String reqActionContext = PageContextHelper.getFunction

我有一个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();
    }
}
当我运行代码时,得到以下跟踪: 痕迹

它说NoSuchMethod,但该方法在类
com.proj.context.AuthenticationContextHelper

我是否遗漏了任何基本信息或代码中有什么错误

在您的
doGet()
方法中,我使用的环境是Java1.6.0\u32Tomcat7

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


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

它看起来像是返回一个
对象:

classOBJ = Class.forName( CONTEXT_CLASS_PACKAGE + basePath + CONTEXT_CLASS_SUFFIX);
当您尝试创建一个方法时,您正在类
class
上创建一个方法,因为您调用了
class.getClass()
。这就是为什么你会有例外。查看您的输出:

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

它们都是类对象。

您正在从
getContextBoundedClass()
返回一个
Class
对象,并将其放入
对象中,如下所示:

Object contextClass = PageContextHelper.getContextBoundedClass( "authentication");
相反,请说:

 Class contextClass = PageContextHelper.getContextBoundedClass( "authentication");
您不再调用
contextClass.getClass()
。而是获取调用的类的实例
contextClass.newInstance()
,以创建该对象的实例,并在调用该方法时使用该实例。阅读本教程:


PageContextHelper.getContextBoundedClass
方法通过调用
class.forName
方法返回类
AuthenticationContextHelper
。@ShanthaKumara没有发生这种情况,请参见我的回答中的编辑您的观点是正确的,@sethu的答案解决了我的问题。@ShanthaKumara或者您可以将该方法设置为静态,您不必创建新实例来调用方法,除非您需要。谢谢@Jon,我忘了添加注释我是如何解决问题的。答案中添加了注释。将
contextClass
引用更改为type
Class
并调用
contextClass.newInstance()
解决了我的问题。
Contextclass : class com.proj.context.AuthenticationContextHelper
thisClass : class java.lang.Class
Object contextClass = PageContextHelper.getContextBoundedClass( "authentication");
 Class contextClass = PageContextHelper.getContextBoundedClass( "authentication");