Java 如何列出在“中使用特定注释注释的所有方法”;src/测试“;使用反射的路径?
我有两个注释:@Feature和@Scenario 我在控制器方法中使用的@Feature注释(路径“src/main/java…”中的GET/PUT/POST…) 我在单元测试中使用的@Scenario注释(路径“src/test/java…”中的控制器或服务测试) 我需要列出所有用这两个注释注释的方法,所以我在“src/main/java…”路径中创建了一个类来完成这项工作。 我可以找到所有用@Feature注释的方法,但找不到用@Scenario注释的方法。 我该怎么做 我试着用图书馆做这件事Java 如何列出在“中使用特定注释注释的所有方法”;src/测试“;使用反射的路径?,java,annotations,Java,Annotations,我有两个注释:@Feature和@Scenario 我在控制器方法中使用的@Feature注释(路径“src/main/java…”中的GET/PUT/POST…) 我在单元测试中使用的@Scenario注释(路径“src/test/java…”中的控制器或服务测试) 我需要列出所有用这两个注释注释的方法,所以我在“src/main/java…”路径中创建了一个类来完成这项工作。 我可以找到所有用@Feature注释的方法,但找不到用@Scenario注释的方法。 我该怎么做 我试着用图书馆做这
final Set features=new reflection(“com.myprefix”,new methodannotationsCanner()).getMethodsAnnotatedWith(Feature.class);
最终设置场景=新反射(“com.myprefix”,new methodAnnotationsCanner()).getMethodsAnnotatedWith(Scenario.class);
“features”返回所有带@Feature注释的方法,但“scenarios”返回空的您是否尝试编写JUnit测试来解决您的问题?以下是解决您问题的方法:
@Test
public void findMethodsannotatedWith(){
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()
, new MethodAnnotationsScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("com.myprefix"))));
final Set<Method> features = reflections.getMethodsAnnotatedWith(Feature.class);
final Set<Method> scenarios = reflections.getMethodsAnnotatedWith(Scenario.class);
}
@测试
public void findMethodsannotatedWith(){
List ClassLoaderList=新建LinkedList();
添加(ClasspathHelper.contextClassLoader());
添加(ClasspathHelper.staticClassLoader());
反射反射=新反射(新配置生成器()
.setScanners(新的子类扫描程序(false/*不排除Object.class*/),新的ResourcesCanner()
,新方法AnnotationsCanner())
.setURL(ClasspathHelper.forClassLoader(classLoaderList.toArray(新类加载器[0]))
.filterInputsBy(新的FilterBuilder().include(FilterBuilder.prefix(“com.myprefix”));
最终设置features=reflections.getMethodsAnnotatedWith(Feature.class);
最终设置场景=reflections.getMethodsAnnotatedWith(Scenario.class);
}
@Test
public void findMethodsannotatedWith(){
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()
, new MethodAnnotationsScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("com.myprefix"))));
final Set<Method> features = reflections.getMethodsAnnotatedWith(Feature.class);
final Set<Method> scenarios = reflections.getMethodsAnnotatedWith(Scenario.class);
}