Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Annotation Processing
Annotation Processing [message #207217] Sat, 18 June 2005 22:03 Go to next message
Eclipse UserFriend
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 #207420 is a reply to message #207217] Mon, 20 June 2005 17:11 Go to previous messageGo to next message
Eclipse UserFriend
I am currently working on a project to bring the functionality of Sun's APT
tool into eclipse. See org.eclipse.jdt.apt.

Annotation processing does not allow the modification of AST's. Only the
generation of new types. You can play games like referencing and even
extending the generated types, but you can't modify the bits produced by the
compiler.

Tim

"Rohit Lodha" <rohitlodha@gmail.com> wrote in message
news:d92jqc$lsq$1@news.eclipse.org...
> 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 #207428 is a reply to message #207420] Mon, 20 June 2005 23:26 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: rohitlodha.gmail.com

Tim Hanson wrote:
> I am currently working on a project to bring the functionality of Sun's APT
> tool into eclipse. See org.eclipse.jdt.apt.
>
> Annotation processing does not allow the modification of AST's. Only the
> generation of new types. You can play games like referencing and even
> extending the generated types, but you can't modify the bits produced by the
> compiler.
>
> Tim

Why do you want to restrict like sun modification of ASTs.

-Rohit
Re: Annotation Processing [message #207537 is a reply to message #207428] Tue, 21 June 2005 13:05 Go to previous messageGo to next message
Eclipse UserFriend
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.

Tim


"Rohit Lodha" <rohitlodha@gmail.com> wrote in message
news:d981ch$s5$1@news.eclipse.org...
> Tim Hanson wrote:
>> I am currently working on a project to bring the functionality of Sun's
>> APT tool into eclipse. See org.eclipse.jdt.apt.
>>
>> Annotation processing does not allow the modification of AST's. Only the
>> generation of new types. You can play games like referencing and even
>> extending the generated types, but you can't modify the bits produced by
>> the compiler.
>>
>> Tim
>
> Why do you want to restrict like sun modification of ASTs.
>
> -Rohit
Re: Annotation Processing [message #207655 is a reply to message #207537] Thu, 23 June 2005 01:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: rohitlodha.gmail.com

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 #207750 is a reply to message #207655] Thu, 23 June 2005 19:34 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous message
Eclipse UserFriend
> 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
Previous Topic:Comment versus Javadoc formatting
Next Topic:help with runtime configurations and native libraries
Goto Forum:
  


Current Time: Wed May 07 11:32:00 EDT 2025

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

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

Back to the top