Annotations and com.sun.mirror API [message #636866] |
Wed, 03 November 2010 08:06 |
Fabien Giquel Messages: 147 Registered: July 2009 |
Senior Member |
|
|
Given two classes with some inheritance and annotations :
public class A {
@MyAnnotation
public String myField;
}
public class B {
@MyOtherAnnotation
public String myOtherField;
}
-> Writing an AnnotationProcessor impl, i encounter this little problem, and i do not know if it is a bug or a normal limitation :
I get a com.sun.mirror.declaration.TypeDeclaration instance for B
- I can retrieve a FieldDeclaration, and the AnnotationMirror isntance, and then the SourcePosition associated to the annotation "MyOtherAnnotation"
- I can retrieve a TypeDeclaration for A from B (using .getSuperclass()), and then the FieldDeclaration for "myField"', and then AnnotationMirror instance for "MyAnnotation" but then there is no SourcePosition associated to this last one. Meanwhile there is one SourcePosition associated for the FieldDeclaration.
Thanks in advance for anyone who has an explanation about this.
----------------------------------------------------
Fabien GIQUEL
R&D Engineer
Mia-Software
rue Nina Simone
44000 NANTES
----------------------------------------------------
|
|
|
Re: Annotations and com.sun.mirror API [message #636988 is a reply to message #636866] |
Wed, 03 November 2010 16:36 |
Walter Harley Messages: 847 Registered: July 2009 |
Senior Member |
|
|
On 11/3/10 1:06 AM, Fabien Giquel wrote:
> Given two classes with some inheritance and annotations :
>
> public class A {
> @MyAnnotation
> public String myField;
> }
>
> public class B {
> @MyOtherAnnotation
> public String myOtherField;
> }
>
>
> -> Writing an AnnotationProcessor impl, i encounter this little problem,
> and i do not know if it is a bug or a normal limitation :
>
> I get a com.sun.mirror.declaration.TypeDeclaration instance for B
> - I can retrieve a FieldDeclaration, and the AnnotationMirror isntance,
> and then the SourcePosition associated to the annotation
> "MyOtherAnnotation"
> - I can retrieve a TypeDeclaration for A from B (using
> .getSuperclass()), and then the FieldDeclaration for "myField"', and
> then AnnotationMirror instance for "MyAnnotation" but then there is no
> SourcePosition associated to this last one. Meanwhile there is one
> SourcePosition associated for the FieldDeclaration.
>
>
> Thanks in advance for anyone who has an explanation about this.
I assume you mean B extends A? Your code example doesn't say so.
If A has already been compiled, then it might be read from the .class
instead of from source. In that case there is no SourcePosition. Are
both classes in the same project? Do you get these results on a full
build (after a clean) or only on incremental builds?
Generally the best way to think about annotation processing is that you
are looking at a typesystem - not at source code - but that you get a
little bit of extra information for the particular file whose
annotations are being processed. So if you're writing a processor for
@MyOtherAnnotation, you will get source positions for files containing
that annotation, and you will get typesystem information only for the
other files. Sometimes you get a bit extra but that is not guaranteed.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03690 seconds