Java 反射API不显示具有注释的接口

Java 反射API不显示具有注释的接口,java,annotations,reflections,Java,Annotations,Reflections,我正在使用Reflections API扫描我的java项目,并获取具有特定注释的所有类/接口。 然而,它只是返回类,而不是接口 我正在使用以下工具: Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(Path.class); Set您可以尝试以下方法: @Documented @Target(ElementType.METHOD) @Inherited @Retention(Retenti

我正在使用Reflections API扫描我的java项目,并获取具有特定注释的所有类/接口。 然而,它只是返回类,而不是接口

我正在使用以下工具:

Set<Class<?>> annotated = 
    reflections.getTypesAnnotatedWith(Path.class);
Set您可以尝试以下方法:

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodInfo{
    String author() default "Kuntal";
    String date();
    int revision() default 1;
    String comments();
}


public class AnnotationExample {

    public static void main(String[] args) {
    }

    @Override
    @MethodInfo(author = "Kuntal", comments = "Main method", date = "Nov 17 2012", revision = 1)
    public String toString() {
        return "Overriden toString method";
    }

    @Deprecated
    @MethodInfo(comments = "deprecated method", date = "Nov 17 2012")
    public static void oldMethod() {
        System.out.println("old method, don't use it.");
    }

    @SuppressWarnings({ "unchecked", "deprecation" })
    @MethodInfo(author = "Kuntal", comments = "Main method", date = "Nov 17 2012", revision = 10)
    public static void genericsTest() throws FileNotFoundException {
        List l = new ArrayList();
        l.add("abc");
        oldMethod();
    }

}
然后,您可以使用反射来解析类中的java注释。请注意,注释保留策略应该是运行时的,否则它的信息在运行时将不可用,我们将无法从中获取任何数据

public class AnnotationParsing {

    public static void main(String[] args) {
        try {
            for (Method method : AnnotationParsing.class
                    .getClassLoader()
                    .loadClass(("com.kuntal.annotations.AnnotationExample"))
                    .getMethods()) {
                // checks if MethodInfo annotation is present for the method
                if (method
                        .isAnnotationPresent(com.kuntal.annotations.MethodInfo.class)) {
                    try {
                        // iterates all the annotations available in the method
                        for (Annotation anno : method.getDeclaredAnnotations()) {
                            System.out.println("Annotation in Method '"
                                    + method + "' : " + anno);
                        }
                        MethodInfo methodAnno = method
                                .getAnnotation(MethodInfo.class);
                        if (methodAnno.revision() == 1) {
                            System.out.println("Method with revision no 1 = "
                                    + method);
                        }

                    } catch (Throwable ex) {
                        ex.printStackTrace();
                    }
                }
            }
        } catch (SecurityException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

可以看出,这应该是可行的


也许你没有扫描所有相关的URL?在这种情况下,请尝试正确构建反射对象(使用ClasspathHelper.forClasspath()将扫描所有对象,尽管它可能太宽了…)

反射的类型是什么?反射从哪里扫描类和接口?这是谷歌反射API。它正在从我的类路径扫描类。我在eclipse中运行这个程序。谢谢Kuntal,但我的问题是我无法使用反射API扫描带有注释的接口。(不是注释本身就是接口)这意味着如果我正在搜索所有具有“Path”注释的类和接口,那么API也应该在接口cls下面返回我@Path(value=“xyz”)公共接口cls{}`该接口不在我的类路径中,尽管我这么认为。我刚刚加上了丢失的罐子。