Java注释单独获取声明的方法

标签 java annotations

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

以下是我的代码:

编辑:

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

在此之后,我才得到我在类中声明的方法。上述值是什么以及如何避免它们。

最诚挚的问候

最佳答案

试试这个:

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()

关于Java注释单独获取声明的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16144277/

相关文章:

java - 使用 Canvas android拉伸(stretch)图像

java - Android房间,实体内的列表?

java - 是否可以在java中读取注释的值?

java - Swagger @ApiModelProperty 示例值为 Long 为 null

java - 如何增加JavaFX中ScrollPane的滚动效果?

java - 将基于 EMF/GMF 的 eclipse 3 插件迁移到 RCP

java - JPQL IS NOT NULL 返回带有 NULL 的对象

spring - 为什么 BeanDefinition.getPropertyValues 返回一个空列表

spring - 为什么 spring @schedule 不能与 @Lazy 一起使用

java - Spring 3 注释配置选择@Configuration 和@Component 但不是@Controller