0

Basic Issue

I want to inherit annotations so I can declare abstract classes including annotations for configuration. Then you only have to extend this abstract class and implement all abstract methods without configuring anything.

public class A {

}

@MyAnno
public class B extends A {
    public abstract void foo();
}

public class C extends B {
    public void foo() {
        Magic.bar();
    }
}

@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnno {

}

But if I check with reflections, class C are not annotated with MyAnno. If I add @MyAnno to class C, it works. This seems strange for me.

Jersey

In addition to my own annotations, I want to use Java RESTful Annotations like @GET, @Path(""where/I/am) and so on. This should work! But it doesn't and the subclass of the annotated superclass does not have ANY RESTful annotations or own annotations, nothing.

Community
  • 1
  • 1
  • How are you checking for the annotation on class C? (I assume you didn't really intend to write `class C extends C`.) – VGR Sep 09 '15 at 13:53
  • new Reflections(MyServer.class.getPackage().getName()).getTypesAnnotatedWith(MyAnno.class) - This works if I annotate the subclass itself. (Gradle: compile 'org.reflections:reflections:0.9.10') –  Sep 09 '15 at 14:00
  • 1
    I don't know what kind of reflection that library does, but simply doing `MyAnno anno = C.class.getAnnotation(MyAnno.class)` return the annotation. You can see the difference if you remove the `@Inherited`, the previous code will result in null – Paul Samsotha Sep 09 '15 at 15:02
  • The code in your comment works fine for me, returns both `C` and `B`. Check your package. – Sotirios Delimanolis Sep 09 '15 at 15:08
  • Yeah... reading javadocs sometimes makes life easier. xD Any suggestions for libraries, to query all annotated class in a specific package? –  Sep 09 '15 at 15:25

1 Answers1

0

Reflections

I was using org.reflections which does not output subclasses of annotated superclasses if these superclasses are not in the package path of the type search.

Jersey

I had used 2.9.x while it was not running. Works fine in 2.21!
Update: Remove @PathParam from implemented methods!