0

我使用以下代码来获取类中声明的方法。但是代码也返回了不必要的值以及声明的方法。

以下是我的代码:

编辑 :

Class cls;
List classNames = hexgenClassUtils.findMyTypes("com.hexgen.*");
            Iterator<Class> it = classNames.iterator();
            while(it.hasNext())
            {

                Class obj = it.next(); 
                System.out.println("Methods available in : "+obj.getName());
                System.out.println("===================================");
                cls = Class.forName(obj.getName());
                Method[] method = cls.getDeclaredMethods();
                int i=1;
    Method[] method = cls.getDeclaredMethods();
     int i=1;
     for (Method method2 : method) {
    System.out.println(+i+":"+method2.getName());
    }
}

我也试过getMethods()

以下是我的输出:

1:ajc$get$validator
2:ajc$set$validator
3:ajc$get$requestToEventTranslator
4:ajc$set$requestToEventTranslator
5:ajc$interMethodDispatch2$com_hexgen_api_facade_HexgenWebAPIValidation$validate
6:handleValidationException

在此之后,只有我得到我在课堂上声明的方法。什么是上述值以及如何避免它们。?

此致

4

1 回答 1

1

尝试这个:

Method[] method = cls.getClass().getDeclaredMethods();

代替

Method[] method = cls.getDeclaredMethods();

看这个例子:

import java.lang.reflect.Method;

public class Example {
    private void one() {
    }

    private void two() {
    }

    private void three() {
    }

    public static void main(String[] args) {
        Example program = new Example();
        Class progClass = program.getClass();

        // Get all the methods associated with this class.
        Method[] methods = progClass.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println("Method " + (i + 1) + " :"
                    + methods[i].toString());
        }
    }
}

输出:

Method 1 :public static void Example.main(java.lang.String[])
Method 2 :private void Example.one()
Method 3 :private void Example.two()
Method 4 :private void Example.three()
于 2013-04-22T10:13:19.530 回答