Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Annotations in Eclipse (Europa)
Annotations in Eclipse (Europa) [message #247556] Thu, 06 September 2007 21:59 Go to next message
David Goodenough is currently offline David GoodenoughFriend
Messages: 157
Registered: July 2009
Senior Member
I am trying to use annotations in Eclipse, and I am stuck.

I have a workplace with a project within which I have defined a couple
of annotation processors, the annotation classes, and some classes which
use the annotations. I have made sure that I have started Eclipse with
a Version 6 JVM (a Sun one) and I have enabled annotation processing for
this project. But nothing happens, and the processor is never invoked
(as far as I can tell - at least there are no errors and nothing gets
generated).

I did come across a reference to having to enable something specific to
allow new classes to be generated while I am editing so that new classes
can be generated by annotations and then referenced in the class I am
editing, but I do not understand exactly what I am supposed to do to enable
this. The option is apparently enableTypeGenerationInEditor, and so I set
this to true in the processor options on the annotations processing property
page for the project.

Does anyone have a worked example of writing an annotation processor in
Eclipse, and then using it - I guess I am missing some simple stage in
the process.

David
Re: Annotations in Eclipse (Europa) [message #247560 is a reply to message #247556] Thu, 06 September 2007 22:32 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"David Goodenough" <david.goodenough@btconnect.com> wrote in message
news:fbpt81$29d$1@build.eclipse.org...
>I am trying to use annotations in Eclipse, and I am stuck.
>
> I have a workplace with a project within which I have defined a couple
> of annotation processors, the annotation classes, and some classes which
> use the annotations. I have made sure that I have started Eclipse with
> a Version 6 JVM (a Sun one) and I have enabled annotation processing for
> this project. But nothing happens, and the processor is never invoked
> (as far as I can tell - at least there are no errors and nothing gets
> generated).

Heh. I was just in the process of writing up an enhancement request for
features to support this situation :-)

You're right, it can be quite hard to debug what's going on when processing
isn't happening.

But I think you may have gotten off on the wrong foot here, by putting your
processors, annotations, and annotated code all in the same project. If I
correctly understand what you mean, that will not, in general, work.

Think of the processors as being part of the compiler, not part of what is
being compiled. So you can't put the processor code in the same project as
the code to be processed. Typically if you are just using a readymade
processor, it is in a plug-in or a jar file of its own; you would then refer
to that in the Factory Path of the project containing the annotated code to
be processed.

If you are developing annotation processors, you would typically have one
workspace (or at least a separate project) for your processors, and a
separate workspace containing the projects with annotated code.



> I did come across a reference to having to enable something specific to
> allow new classes to be generated while I am editing so that new classes
> can be generated by annotations and then referenced in the class I am
> editing, but I do not understand exactly what I am supposed to do to
> enable
> this.

I would avoid this for now. Stick with generating types during *build*,
which is the default. The distinction is simply that if type generation
during reconcile is enabled, then your annotation processors are able to
generate types as you work in the editor, rather than only when you build.
This is rarely necessary.


> Does anyone have a worked example of writing an annotation processor in
> Eclipse, and then using it - I guess I am missing some simple stage in
> the process.

You can take a look at our EclipseCon 2007 presentation on writing
processors at http://www.eclipsecon.org/2007/index.php?page=sub/&id=36 18 .
The presentation file there contains a PowerPoint presentation and some
worked example code.

If you still have questions after looking at that I'd be delighted to help.
Feel free to post more questions to this newsgroup.

Thanks,
-Walter Harley
JDT APT team
Re: Annotations in Eclipse (Europa) [message #247564 is a reply to message #247560] Thu, 06 September 2007 22:39 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"David Goodenough" <david.goodenough@btconnect.com> wrote in message
news:fbpt81$29d$1@build.eclipse.org...
>I am trying to use annotations in Eclipse, and I am stuck.

I should also mention that support in the Eclipse 3.3 IDE for Java 6
annotation processors (that is, processors that implement
javax.annotation.processing.Processor, as opposed to the
com.sun.mirror.apt.AnnotationProcessor API in Java 5) is "introductory".
That is, there are a lot of missing features, and probably some bugs as
well. It's possible that your processor is actually running, but aborting
silently due to an exception of some sort.

Eclipse 3.3.1 improves the situation somewhat, so if you are writing Java 6
processors you might want to upgrade. 3.3.1 hasn't officially been released
yet but we are in final testing stages and it is in very good shape. That
said, we probably are not going to be solidly 100% on the Java 6 annotation
processing front until 3.4.

-Walter Harley
JDT APT team
Re: Annotations in Eclipse (Europa) [message #247582 is a reply to message #247556] Thu, 06 September 2007 23:39 Go to previous messageGo to next message
Charlie Kelly is currently offline Charlie KellyFriend
Messages: 276
Registered: July 2009
Senior Member
Hi David,

Did you try
Windows->Preferences->Java->Compiler CompilerComplianceLevel

Charlie

David Goodenough wrote:

> I am trying to use annotations in Eclipse, and I am stuck.
>
> I have a workplace with a project within which I have defined a couple
> of annotation processors, the annotation classes, and some classes which
> use the annotations. I have made sure that I have started Eclipse with
> a Version 6 JVM (a Sun one) and I have enabled annotation processing for
> this project. But nothing happens, and the processor is never invoked
> (as far as I can tell - at least there are no errors and nothing gets
> generated).
>
> I did come across a reference to having to enable something specific to
> allow new classes to be generated while I am editing so that new classes
> can be generated by annotations and then referenced in the class I am
> editing, but I do not understand exactly what I am supposed to do to enable
> this. The option is apparently enableTypeGenerationInEditor, and so I set
> this to true in the processor options on the annotations processing property
> page for the project.
>
> Does anyone have a worked example of writing an annotation processor in
> Eclipse, and then using it - I guess I am missing some simple stage in
> the process.
>
> David
Re: Annotations in Eclipse (Europa) [message #247638 is a reply to message #247582] Fri, 07 September 2007 08:32 Go to previous messageGo to next message
David Goodenough is currently offline David GoodenoughFriend
Messages: 157
Registered: July 2009
Senior Member
Charlie Kelly wrote:

> Hi David,
>
> Did you try
> Windows->Preferences->Java->Compiler CompilerComplianceLevel
Yes, it is set to 6.0.

David
>
> Charlie
>
> David Goodenough wrote:
>
>> I am trying to use annotations in Eclipse, and I am stuck.
>>
>> I have a workplace with a project within which I have defined a couple
>> of annotation processors, the annotation classes, and some classes which
>> use the annotations. I have made sure that I have started Eclipse with
>> a Version 6 JVM (a Sun one) and I have enabled annotation processing for
>> this project. But nothing happens, and the processor is never invoked
>> (as far as I can tell - at least there are no errors and nothing gets
>> generated).
>>
>> I did come across a reference to having to enable something specific to
>> allow new classes to be generated while I am editing so that new classes
>> can be generated by annotations and then referenced in the class I am
>> editing, but I do not understand exactly what I am supposed to do to
>> enable
>> this. The option is apparently enableTypeGenerationInEditor, and so I
>> set this to true in the processor options on the annotations processing
>> property page for the project.
>>
>> Does anyone have a worked example of writing an annotation processor in
>> Eclipse, and then using it - I guess I am missing some simple stage in
>> the process.
>>
>> David
Re: Annotations in Eclipse (Europa) [message #247643 is a reply to message #247560] Fri, 07 September 2007 08:52 Go to previous messageGo to next message
David Goodenough is currently offline David GoodenoughFriend
Messages: 157
Registered: July 2009
Senior Member
Walter Harley wrote:

> "David Goodenough" <david.goodenough@btconnect.com> wrote in message
> news:fbpt81$29d$1@build.eclipse.org...
>>I am trying to use annotations in Eclipse, and I am stuck.
>>
>> I have a workplace with a project within which I have defined a couple
>> of annotation processors, the annotation classes, and some classes which
>> use the annotations. I have made sure that I have started Eclipse with
>> a Version 6 JVM (a Sun one) and I have enabled annotation processing for
>> this project. But nothing happens, and the processor is never invoked
>> (as far as I can tell - at least there are no errors and nothing gets
>> generated).
>
> Heh. I was just in the process of writing up an enhancement request for
> features to support this situation :-)
>
> You're right, it can be quite hard to debug what's going on when
> processing isn't happening.
>
> But I think you may have gotten off on the wrong foot here, by putting
> your
> processors, annotations, and annotated code all in the same project. If I
> correctly understand what you mean, that will not, in general, work.

Well if I read the Sun Javac documentation right this is allowed. I am
not writing general purpose annotations, but ones specific to this project
so putting them in the project makes a whole heap of sense.

In fact the default for javac is to look in the source tree and the
classpath for the processors if you do not specify -processpath.

>
> Think of the processors as being part of the compiler, not part of what is
> being compiled. So you can't put the processor code in the same project
> as
> the code to be processed. Typically if you are just using a readymade
> processor, it is in a plug-in or a jar file of its own; you would then
> refer to that in the Factory Path of the project containing the annotated
> code to be processed.

I am not using a readymade processor, I am writing my own.

>
> If you are developing annotation processors, you would typically have one
> workspace (or at least a separate project) for your processors, and a
> separate workspace containing the projects with annotated code.
>
>
>
>> I did come across a reference to having to enable something specific to
>> allow new classes to be generated while I am editing so that new classes
>> can be generated by annotations and then referenced in the class I am
>> editing, but I do not understand exactly what I am supposed to do to
>> enable
>> this.
>
> I would avoid this for now. Stick with generating types during *build*,
> which is the default. The distinction is simply that if type generation
> during reconcile is enabled, then your annotation processors are able to
> generate types as you work in the editor, rather than only when you build.
> This is rarely necessary.
>

My problem is that the annotation creates the class to which it is applied,
and so the editor complains about errors until the class is created. What
I am trying to do is create a data binding annotation for Swing objects. So
for instance I have one for Combo boxes which create a ComboBoxModel
class which pulled a particular bean property as the dropdown content:-

MyBean[]myBeanArray;
@ComboBinding(dataClass="MyBean",column="property")
MyComboModel model = new MyComboModel(myBeanArray);

where MyComboModel is a class that is build by the annotation processor.
So the Eclipse editor flags the last line as having errors (the type does
not exist) until the build has been done.

>
>> Does anyone have a worked example of writing an annotation processor in
>> Eclipse, and then using it - I guess I am missing some simple stage in
>> the process.
>
> You can take a look at our EclipseCon 2007 presentation on writing
> processors at http://www.eclipsecon.org/2007/index.php?page=sub/&id=36 18 .
> The presentation file there contains a PowerPoint presentation and some
> worked example code.

I will have a look and see if there are then any more questions that I have.

David
>
> If you still have questions after looking at that I'd be delighted to
> help. Feel free to post more questions to this newsgroup.
>
> Thanks,
> -Walter Harley
> JDT APT team
Re: Annotations in Eclipse (Europa) [message #247648 is a reply to message #247564] Fri, 07 September 2007 08:54 Go to previous messageGo to next message
David Goodenough is currently offline David GoodenoughFriend
Messages: 157
Registered: July 2009
Senior Member
Walter Harley wrote:

> "David Goodenough" <david.goodenough@btconnect.com> wrote in message
> news:fbpt81$29d$1@build.eclipse.org...
>>I am trying to use annotations in Eclipse, and I am stuck.
>
> I should also mention that support in the Eclipse 3.3 IDE for Java 6
> annotation processors (that is, processors that implement
> javax.annotation.processing.Processor, as opposed to the
> com.sun.mirror.apt.AnnotationProcessor API in Java 5) is "introductory".
> That is, there are a lot of missing features, and probably some bugs as
> well. It's possible that your processor is actually running, but aborting
> silently due to an exception of some sort.
>
> Eclipse 3.3.1 improves the situation somewhat, so if you are writing Java
> 6
> processors you might want to upgrade. 3.3.1 hasn't officially been
> released
> yet but we are in final testing stages and it is in very good shape. That
> said, we probably are not going to be solidly 100% on the Java 6
> annotation processing front until 3.4.
Would I be better off writing to the Java5 version, or do you think
(assuming 3.3.1) that the Java6 stuff is sufficiently complete to use?

David
>
> -Walter Harley
> JDT APT team
Re: Annotations in Eclipse (Europa) [message #247670 is a reply to message #247643] Fri, 07 September 2007 19:56 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"David Goodenough" <david.goodenough@btconnect.com> wrote in message
news:fbr3fu$ai3$1@build.eclipse.org...
>> But I think you may have gotten off on the wrong foot here, by putting
>> your
>> processors, annotations, and annotated code all in the same project. If
>> I
>> correctly understand what you mean, that will not, in general, work.
>
> Well if I read the Sun Javac documentation right this is allowed. I am
> not writing general purpose annotations, but ones specific to this project
> so putting them in the project makes a whole heap of sense.
>
> In fact the default for javac is to look in the source tree and the
> classpath for the processors if you do not specify -processpath.

javac will look for processor classes on the classpath, yes. If it will
look for processor source code, and compile it in time to use the processor
on the rest of the source code that it's compiling, then that is news to me.

That is, if you had a project containing FooAnnotation.java,
FooProcessor.java, and AnnotatedWithFoo.java, I don't believe that you could
say "javac *.java" and have your annotations get processed. Is that what
you're saying? On the other hand, you could certainly rig up an ant script
to do it in the right sequence, I guess.

In general I would seek to strongly discourage you from combining these
things into the same project. The processor is part of the compiler, in
many important senses (for instance, it runs in the VM of the compiler).


> My problem is that the annotation creates the class to which it is
> applied,
> and so the editor complains about errors until the class is created. What
> I am trying to do is create a data binding annotation for Swing objects.
> So
> for instance I have one for Combo boxes which create a ComboBoxModel
> class which pulled a particular bean property as the dropdown content:-
>
> MyBean[]myBeanArray;
> @ComboBinding(dataClass="MyBean",column="property")
> MyComboModel model = new MyComboModel(myBeanArray);
>
> where MyComboModel is a class that is build by the annotation processor.
> So the Eclipse editor flags the last line as having errors (the type does
> not exist) until the build has been done.

Yeah, we talk about that design pattern a bit in our tutorial. I'll let you
read the tutorial and then ask questions, but for now suffice it to say if
you can do it some other way (e.g. with an interface and factory method) you
might have more satisfactory results.

You can make it work the way you want it to, if you turn on reconcile-time
type generation. Reconcile-time type generation is only supported for the
Java 5 interface, not for the Java 6 interface. Frankly I'm pushing to keep
it that way. Reconcile-time type generation was a bad experiment, in my
personal opinion. The compiler's type model is a fairly heavyweight object;
by generating types at reconcile time, we're asking that model to revise
itself on every keystroke (imagine, for instance, that you're editing the
annotation parameters, causing the name or content of a generated type to
change). There is just no way to make that perform speedily enough to
satisfy the user of the IDE.
Re: Annotations in Eclipse (Europa) [message #247675 is a reply to message #247648] Fri, 07 September 2007 20:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"David Goodenough" <david.goodenough@btconnect.com> wrote in message
news:fbr3kt$ai3$2@build.eclipse.org...
> Would I be better off writing to the Java5 version, or do you think
> (assuming 3.3.1) that the Java6 stuff is sufficiently complete to use?

There are two Java 6 implementations in Eclipse, which share some code. One
implementation is used to support the Eclipse batch-mode compiler, that is,
the one that you get if you use "java -jar ecj.jar ..." instead of "javac
....". This one is in pretty solid shape, definitely comparable to javac.

The other is used to support compilation within the IDE, so it's the one
most people see. Unfortunately this one is also the one with the most
holes. It's good enough for people to start exploring writing processors to
the Java 6 interface, which was the main goal; but even in 3.3.1 it's very
easy to hit UnsupportedOperationException("Not Yet Implemented").

With regard to the holes in the IDE implementation: the typesystem code
(javax.lang.model.* packages) is shared between both implementations, so it
is very solid. The Messager implementation is reasonable. Dispatch
(calling the right processor on the right files at the right time) is solid,
but it's missing a lot of niceties, such as the ability to locate processors
on the Java Build Path. The Filer implementation is the weakest link; it
has a lot of missing methods.

-Walter Harley
JDT APT team
Re: Annotations in Eclipse (Europa) [message #247680 is a reply to message #247670] Fri, 07 September 2007 20:50 Go to previous messageGo to next message
David Goodenough is currently offline David GoodenoughFriend
Messages: 157
Registered: July 2009
Senior Member
Walter Harley wrote:

> "David Goodenough" <david.goodenough@btconnect.com> wrote in message
> news:fbr3fu$ai3$1@build.eclipse.org...
>>> But I think you may have gotten off on the wrong foot here, by putting
>>> your
>>> processors, annotations, and annotated code all in the same project. If
>>> I
>>> correctly understand what you mean, that will not, in general, work.
>>
>> Well if I read the Sun Javac documentation right this is allowed. I am
>> not writing general purpose annotations, but ones specific to this
>> project so putting them in the project makes a whole heap of sense.
>>
>> In fact the default for javac is to look in the source tree and the
>> classpath for the processors if you do not specify -processpath.
>
> javac will look for processor classes on the classpath, yes. If it will
> look for processor source code, and compile it in time to use the
> processor on the rest of the source code that it's compiling, then that is
> news to me.
>
> That is, if you had a project containing FooAnnotation.java,
> FooProcessor.java, and AnnotatedWithFoo.java, I don't believe that you
> could
> say "javac *.java" and have your annotations get processed. Is that what
> you're saying? On the other hand, you could certainly rig up an ant
> script to do it in the right sequence, I guess.
I will give it a try. But certainly javac will read processors from -cp
which logically includes what has been compiled. Given the rounds and that
it is quite possible to have annotations in generated code I do not see
why it should not be capable of going from source. The only thing that
might be a problem would be if you started with an empty bin (i.e. output)
directory which would not have a META-INF/services directory in it. With
Ant it would be easy to set up to copy that first - I will try it.
>
> In general I would seek to strongly discourage you from combining these
> things into the same project. The processor is part of the compiler, in
> many important senses (for instance, it runs in the VM of the compiler).
>
What I have done in the interim is to create a JAR from the annotations
and tell Eclipse to use that - it seems to work.
>
>> My problem is that the annotation creates the class to which it is
>> applied,
>> and so the editor complains about errors until the class is created.
>> What I am trying to do is create a data binding annotation for Swing
>> objects. So
>> for instance I have one for Combo boxes which create a ComboBoxModel
>> class which pulled a particular bean property as the dropdown content:-
>>
>> MyBean[]myBeanArray;
>> @ComboBinding(dataClass="MyBean",column="property")
>> MyComboModel model = new MyComboModel(myBeanArray);
>>
>> where MyComboModel is a class that is build by the annotation processor.
>> So the Eclipse editor flags the last line as having errors (the type does
>> not exist) until the build has been done.
>
> Yeah, we talk about that design pattern a bit in our tutorial. I'll let
> you read the tutorial and then ask questions, but for now suffice it to
> say if you can do it some other way (e.g. with an interface and factory
> method) you might have more satisfactory results.
Now that I have everything working it seems to work after I have got through
the basic bootstrap of creating the first instance of the target class.
>
> You can make it work the way you want it to, if you turn on reconcile-time
> type generation. Reconcile-time type generation is only supported for the
> Java 5 interface, not for the Java 6 interface. Frankly I'm pushing to
> keep
> it that way. Reconcile-time type generation was a bad experiment, in my
> personal opinion. The compiler's type model is a fairly heavyweight
> object; by generating types at reconcile time, we're asking that model to
> revise itself on every keystroke (imagine, for instance, that you're
> editing the annotation parameters, causing the name or content of a
> generated type to
> change). There is just no way to make that perform speedily enough to
> satisfy the user of the IDE.
I suppose that all that is needed is to run it the first time it is met,
almost on the off chance that it might create the classes. Alternatively
it could be a Quick Fix option - that way you are running it manually
and only when it is really needed.

David
Re: Annotations in Eclipse (Europa) [message #247685 is a reply to message #247680] Fri, 07 September 2007 23:01 Go to previous message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"David Goodenough" <david.goodenough@btconnect.com> wrote in message
news:fbsdjb$jtc$1@build.eclipse.org...
> I will give it a try. But certainly javac will read processors from -cp
> which logically includes what has been compiled. Given the rounds and
> that
> it is quite possible to have annotations in generated code I do not see
> why it should not be capable of going from source. The only thing that
> might be a problem would be if you started with an empty bin (i.e. output)
> directory which would not have a META-INF/services directory in it. With
> Ant it would be easy to set up to copy that first - I will try it.

Rhetorical question: Why not put the code for javac and ant into the project
as well? Then you could, for instance, modify the compiler to automatically
generate the particular opcodes you want, without having to write all that
pesky java in your source code. You'd need some sort of shell script to
bootstrap the build, but after it ran the first time there shouldn't be any
problem.

Putting your annotation processor code into the same project as your
annotated code probably feels different to you, because you're the author of
both of them. But there is no conceptual difference, particularly if you
think that anyone else will ever use your framework or your annotations.
Essentially the same issues apply.

To name just one of those issues: at runtime, the debugger does not see your
annotation processor; so when you are developing your processor at the same
time that you are developing the code it processes, debugging becomes quite
a challenge. That challenge is much easier if you have your annotations and
your processor source code in one work space, and can develop them against
simple, stable test cases and then deploy them to the workspace where you
are developing your annotated code.
Previous Topic:org.eclipse.jdt.core.JDTCompilerAdapter seems to find invalid Access restrictions
Next Topic:F3 key no more working
Goto Forum:
  


Current Time: Sat Nov 09 23:13:11 GMT 2024

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

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

Back to the top