如何获取具有注释的方法名称? [英] How can I get the method name which has annotation?

查看:595
本文介绍了如何获取具有注释的方法名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个类例如考试有一些注释的方法。

A class for example Exam has some methods which has annotation.

@Override
public void add() {
    int c=12;
}

如何获取具有 @Override 注释使用 org.eclipse.jdt.core.IAnnotation

How can I get the method name (add) which has @Override annotation using org.eclipse.jdt.core.IAnnotation?

推荐答案

IAnnotation 有很强的误导性,请参阅文档。

The IAnnotation is strongly misleading, please see the documentation.

从类中检索具有一些注释的方法。要做到这一点,你必须遍历所有的方法,只产生那些有这样的注释。

To retrieve the Methods from Class that have some annotation. To do that you have to iterate through all methods and yield only those that have such annotation.

public static Collection<Method> methodWithAnnotation(Class<?> classType, Class<?  extends Annotation> annotationClass) {

  if(classType == null) throw new NullPointerException("classType must not be null");

  if(annotationClass== null) throw new NullPointerException("annotationClass must not be null");  

  Collection<Method> result = new ArrayList<Method>();
  for(Method method : classType.getMethods()) {
    if(method.isAnnotationPresent(annotationClass)) {
       result.add(method);
    }
  }
  return result;
}

这篇关于如何获取具有注释的方法名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆