0

リフレクションを使用して動的なクラスの初期化とメソッドの呼び出しを行うサーブレット クラスがあります。それを行うための次のコードがあります。

@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と表示されていますが、メソッドは class にありcom.proj.context.AuthenticationContextHelperます。

ここに基本的なものが欠けていますか、それともコードに何か問題がありますか?

私が使用している環境はJava 1.6.0_32Tomcat 7です

4

2 に答える 2

1

あなたのdoGet()方法では:

contextMethod.invoke ( contextClass, request, response );

contextClass型クラスであるを渡しています。クラスのClassオブジェクトではなく、メソッドが呼び出されるクラスのインスタンスを渡す必要があります。のインスタンスがAuthenticationContextHelperどこにあるかわからないので、サーブレットクラスのどこにも表示されません。それが不可能な場合は、メソッドを静的にしてから、nullオブジェクトをに渡すことができます。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 に答える