Annotation Processing [message #207217] |
Sat, 18 June 2005 22:03  |
Eclipse User |
|
|
|
Originally posted by: rohitlodha.gmail.com
Hi
I want to do some annotation processing.
Source files are converted to AST and then to class files. I want to
hook the process after the completion AST and before the generation of
class files so that I can change AST.
How can I do that?
-Rohit
|
|
|
|
|
|
|
Re: Annotation Processing [message #207750 is a reply to message #207655] |
Thu, 23 June 2005 19:34   |
Eclipse User |
|
|
|
Originally posted by: mkaufman.nospam.bea.com
Hi Rohit -
As Tim mentioned, modifying the source of the file containing the
annotations is not permitted with APT. If you want to do something like
that, you could write to eclipse-specific APIs to update the AST (although
these will update the source code of your file as well. I think you want to
look at the ASTRewrite class)
Regarding the @Property example, you could try something like the following:
public class FooTemplate{ @Property String name; }
And then have an AnnotationProcessor that would generate the following type:
public class Foo {
String name;
public String getName() { return name; }
public void setName( String s ) { name = s; }
}
Here, clients would reference the Foo type instead of FooTemplate. However,
this seems like a lot of complexity to go through, just to avoid adding
getters & setters. Particularly considering that most IDEs will easily
generate getters & setters for you.
Anyway, I agree with Tim's comments on why modifying the AST based on
annotations is a bad thing. I suspect that if you reconsider your problem,
you will find a solution with less complexity. If you keep coming back to
why you want to modify the AST, and why other solutions won't work, I'd love
to hear about it.
Good luck!
Mike Kaufman
BEA Systems, Inc.
"Rohit Lodha" <rohitlodha@gmail.com> wrote in message
news:d9dgk9$8uf$1@news.eclipse.org...
> Tim Hanson wrote:
>> There are many reasons to not allow modifications.
>>
>> Consistency. Allowing modifications makes it impossible to know what a
>> program will do without know what annotation processors will run.
>> Tooling. Modifications make find/usages & refactoring difficult if not
>> impossible.
>> Debugging. At worst, the modifications break the line number table. At
>> best, the code that is introduced is not debuggable.
>>
> Suppose I want to use @Property for generating getters and setters. I will
> not be able to use them until the get..() and set..() methods are
> generated. I would like them to be generated during compilation.
>
> Is this has to be prohibited?
>
> -Rohit
|
|
|
Re: Annotation Processing [message #208005 is a reply to message #207217] |
Mon, 27 June 2005 03:43   |
Eclipse User |
|
|
|
Originally posted by: plankton.softwitch.net
Rohit Lodha wrote:
> Hi
>
> I want to do some annotation processing.
>
> Source files are converted to AST and then to class files. I want to
> hook the process after the completion AST and before the generation of
> class files so that I can change AST.
>
> How can I do that?
>
> -Rohit
Think in ease.
Annotations are static properties. because it made on source. So it
can't be changed on run-time. this is natural.
java.awt.Color.red must contain (255, 0, 0). it cant be change in same
way. I know, you have to do modify these things sometimes. But that is
too dangerous and causes so many problems on logical view.
Anyway, I couldn't find accessing method to annotaiton member field.
Does Apt support that? Apt works on source(RETENTION.SOURCE). An I think
that's just all feature of apt.
So i developed Java Annotation Reader package, It can extract exact
refernce of Enum Object, and Arrays of primtive or Enum from annotation
string. and It can handle all stuffs defined by JSR.
Am I doing usless stuff? I cant sure. I tried to find tools works like
that I made. but I couldn't. If mine is useful, notify me, I'll release.
Mine works likes:
Annotation annotaion = m.getAnnotation(Note.class);
AnnotationReader ar = new AnnotationReader(annotaion);
Progress progress = (Progress) ar.readObject("progress");
int[] version = (int[]) ar.readObject("version");
Annotation Note defined as:
public @interface Note {
public Progress progress();
public int[] version() default { 0, 0, 1 };
}
public Enum Progress{
ASSIGNED, TESTED, DONE
}
|
|
|
Re: Annotation Processing [message #208794 is a reply to message #208005] |
Tue, 05 July 2005 18:05  |
Eclipse User |
|
|
|
> Anyway, I couldn't find accessing method to annotaiton member field.
> Does Apt support that? Apt works on source(RETENTION.SOURCE). An I think
> that's just all feature of apt.
>
> Mine works likes:
> Annotation annotaion = m.getAnnotation(Note.class);
> AnnotationReader ar = new AnnotationReader(annotaion);
> Progress progress = (Progress) ar.readObject("progress");
> int[] version = (int[]) ar.readObject("version");
>
> Annotation Note defined as:
> public @interface Note {
> public Progress progress();
> public int[] version() default { 0, 0, 1 };
> }
> public Enum Progress{
> ASSIGNED, TESTED, DONE
> }
There is a much easier way to do this:
Note note = m.getAnnotation(Note.class);
Progress progress = note.progress();
int[] version = progress.version();
Tim
|
|
|
Powered by
FUDForum. Page generated in 0.30805 seconds