Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » annotationProcessorFactory extension and @Inherited annotations
annotationProcessorFactory extension and @Inherited annotations [message #259302] Thu, 02 April 2009 08:53 Go to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
"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 :-)
Re: annotationProcessorFactory extension and @Inherited annotations [message #259310 is a reply to message #259306] Thu, 02 April 2009 12:56 Go to previous message
Eclipse UserFriend
In fact, I am using Java 5 annotation processing API, and moving to Java 6
API.

I agree about the @Retention vs. @Inherited ambiguity. I'll change
retention to CLASS to avoid this.

Thanks for your help!
Previous Topic:Can one selectively format java code?
Next Topic:TextEdit
Goto Forum:
  


Current Time: Wed May 07 18:53:43 EDT 2025

Powered by FUDForum. Page generated in 0.04535 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top