| Annotations and com.sun.mirror API [message #636866] |
Wed, 03 November 2010 04:06  |
Fabien Giquel Messages: 134 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
4, rue du Château de l'Eraudiere
44324 NANTES CEDEX 03
----------------------------------------------------
|
|
|
| Re: Annotations and com.sun.mirror API [message #636988 is a reply to message #636866] |
Wed, 03 November 2010 12: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.
|
|
|
| Re: Annotations and com.sun.mirror API [message #637139 is a reply to message #636988] |
Thu, 04 November 2010 06:45   |
Fabien Giquel Messages: 134 Registered: July 2009 |
Senior Member |
|
|
Yes, i forget the "extends A".
Thanks for your explanation. In my case, both A and B .java files are in
the same project. I use incremental build and did not try some clean.
Never mind. I understand your explanation about typesystem information,
and finally the provided information will fit my needs.
Thanks again.
Fabien.
----------------------------------------------------
Fabien GIQUEL
R&D Engineer
Mia-Software
4, rue du Château de l'Eraudiere
44324 NANTES CEDEX 03
----------------------------------------------------
|
|
|
| Re: Annotations and com.sun.mirror API [message #637234 is a reply to message #637139] |
Thu, 04 November 2010 12:40  |
Walter Harley Messages: 847 Registered: July 2009 |
Senior Member |
|
|
On 11/4/10 3:45 AM, Fabien wrote:
> Yes, i forget the "extends A".
>
> Thanks for your explanation. In my case, both A and B .java files are in
> the same project. I use incremental build and did not try some clean.
> Never mind. I understand your explanation about typesystem information,
> and finally the provided information will fit my needs.
The com.sun.mirror.apt (Java 5) APT API was designed for command-line
compilation with the apt tool, and did not take incremental compilation
(as in an IDE) into account. When the javax.annotation.processing API
was standardized in Java 6, with JSR269, we were able to get them to
make some changes to make it a little better for incremental compilation
- it is still not ideal but it is a little more predictable and usable.
|
|
|
Powered by
FUDForum. Page generated in 0.01585 seconds