Subtyping for Annotations

Last night, I managed to change the Java compiler to parse and encode annotations with inheritance. I’ve complained about this shortcoming many times already: Even though subtyping (inheritance) is one of the core concepts in Java, and even though annotations are represented using (slightly enhanced) interfaces, which support inheritance, Java disallows inheritance for annotations. I never quite understood why.

I’ve now created a modified compiler, based on the LAPT-javac compiler I wrote a while back. The new compiler is called xajavac, for “eXtended Annotation javac”, and supports annotations on local variables, like LAPT-javac, and subtyping for annotations.

Now I can write the following:

1
2
3
4
5
6
7
8
9
10
11
12
@interface InvariantAnnotation { }
@interface OnlyThreadWithName extends InvariantAnnotation {
  String value();
}
@interface OnlyEventThread extends InvariantAnnotation { }
@interface Or extends InvariantAnnotation {
  InvariantAnnotation value();
}

@Or({@OnlyThreadWithName("main"),
     @OnlyEventThread})
void foo() { ... }

I’m now working on providing an API that supports access to these annotations at runtime.

Share

About Mathias

Software development engineer. Principal developer of DrJava. Recent Ph.D. graduate from the Department of Computer Science at Rice University.
This entry was posted in Research, xajavac. Bookmark the permalink.

Leave a Reply