annotationProcessorFactory extension and @Inherited annotations [message #259302] |
Thu, 02 April 2009 08:53  |
Eclipse User |
|
|
|
Hello!
I've created an AnnotationProcessorFactory to process some annotations I
have created, and pluged it in Eclipse IDE using
org.eclipse.jdt.apt.core.annotationProcessorFactory extension point.
My annotations gets processed, and everything is fine, except for
annotations with @Inherited meta-annotation. In this case, classes that
inherit the annotation don't get processed by my AnnotationProcessor,
since AbstractCompilationEnv.getAnnotationTypes() dont return the
inherited annotation.
Am I doing anything wrong?
Here is a little sample of what I'm doing:
The annotation -------------------------------
@Inherited
@Documented
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface MyAnnotation {
}
The base class with annotation ---------------
@MyAnnotation
public abstract class MyAbstractClass {
}
The sub-class that inherits the annotation, but don't get processed by
AnnotationProcessor --------------------------
public class MyClassImplementation extends MyAbstractClass {
void someMethod() {
}
}
PS: If I change the RetentionPolicy to RetentionPolicy.RUNTIME, I'm able
to retrieve the sub-class annotation through reflection.
Thank you in advance!
|
|
|
Re: annotationProcessorFactory extension and @Inherited annotations [message #259306 is a reply to message #259302] |
Thu, 02 April 2009 12:27   |
Eclipse User |
|
|
|
"Cezar Lenci" <cezar_cps@hotmail.com> wrote in message
news:d520ce6cde40427a50551442e01b7a50$1@www.eclipse.org...
> Hello!
>
> I've created an AnnotationProcessorFactory to process some annotations I
> have created, and pluged it in Eclipse IDE using
> org.eclipse.jdt.apt.core.annotationProcessorFactory extension point.
>
> My annotations gets processed, and everything is fine, except for
> annotations with @Inherited meta-annotation. In this case, classes that
> inherit the annotation don't get processed by my AnnotationProcessor,
> since AbstractCompilationEnv.getAnnotationTypes() dont return the
> inherited annotation.
>
> Am I doing anything wrong?
It sounds like you are using the Java 5 annotation processing API
(com.sun.mirror.apt) rather than the Java 6 API
(javax.annotation.processing). For the sake of future maintenance and
cross-compiler compatibility, you might want to use the Java 6 API.
That said, it sounds like you are running into
https://bugs.eclipse.org/bugs/show_bug.cgi?id=270754 . This bug affects the
Java 6 implementation but might also affect the Java 5 implementation; I
haven't tested that. If so, your processor should work in a full build (ie
after a clean) but it is broken in incremental builds. Note however that
not all the APT APIs include inherited annotations in their results;
generally the javadoc says whether it does or not, but @Inherited treatment
seems to have been something of an afterthought and the javadoc is
occasionally vague in that regard.
But also note that using @Inherited with a @Retention(SOURCE) annotation is
ambiguous: there is no guarantee that will work on any given compiler. In
fact, an argument can be made from the JLS that this should not even be
permitted to work: in section 9.6.1.2 ("Retention"), the JLS states "If m
has an element whose value is annotation.RetentionPolicy.SOURCE, then a Java
compiler must ensure that a is not present in the binary representation of
the class or interface in which a appears." But when compiling class Sub
against class Base, it is not supposed to matter whether Base is binary or
source (this is a central concept of the Java typesystem, and is what
permits incremental compilation to work in the first place). So if we allow
class Sub to compile differently depending on whether Base is source or
binary, that principle is violated and the JLS is violated.
This point could be argued, and in fact it's clear that at least for now,
the Eclipse compiler does provide cross-file visibility of SOURCE
annotations during a build where both the base and derived class are
compiled. But that might change in the future.
So, you should avoid relying on @Inherited for annotations with
@Retention(SOURCE). But even if you do, it still won't work in incremental
builds, because of https://bugs.eclipse.org/bugs/show_bug.cgi?id=270754 ,
which I am working on :-)
|
|
|
|
Powered by
FUDForum. Page generated in 0.07613 seconds