Skip to main content



      Home
Home » Newcomers » Newcomers » Annotation processing Java 6
Annotation processing Java 6 [message #244334] Mon, 31 December 2007 08:15 Go to next message
Eclipse UserFriend
Originally posted by: Per.Westling.cybercom.se

Hi, I am a bit new to Eclipse so excuse me if I ask an obvious question...
Have spent quite some time to find the appropriate information but not
had enough success so now I try to ask instead.

I use Eclipse Europe (3.3.0) and is trying to have Eclipse generate some
java source code from annotations during build using Java 6. Here is a
small extract of the source code of the annotation processor I have
written:

@SupportedAnnotationTypes(value = {ManagedProcessor.NAMESPACE })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public final class ManagedProcessor extends AbstractProcessor

I can now use this via javac by compiling my annotation processing class,
and doing
javac <some Java-files...> -processor <compiled ManagedProcessor class>
This do generate the requested code in the current directory.

So what I would like to do is to let Eclipse generate this during build.
This should be accomplished by changing project properties, Java Compiler
/ Annotation Processing, check "Enable project specific setting" and
"Enable Annotation processing" (checking "Enable processing in editor" is
not yet supported in Eclipse 3.3), keep ".apt_generated" in entry field
"Generated source directory" and then add a JAR-file with my annotation
processing code (mentioned above) in "Factory Path". This does not
generate any code, though, regardless of clean/rebuilds. So I am lacking
something.

I tried to add the APTdemo plugin
(http://www.eclipse.org/jdt/apt/introToAPT.html) as "Factory Path". Now,
under "Advanced" I can see the two factories
(demo.DemoAnnotationProcessorFactory and
demo.TypeGeneratingAnnotationProcessorFactory). But the Java 6 APT does
not use any factories, does it? The APTdemo jara does put generated code
in the .apt_generated directory in the Navigator view so that works fine.

I assume there should appear a processor in advanced options (for my
annotation processing JAR) but it does not do so , so the JAR-file is
probably not a correct plugin. Or rather, I know it is not a real plugin
as it is just a collection of the compiled java-files used by my
annotation processor. Extracting APTdemo.jar I can see what is requested
for a Java5-APT-plugin, but what about a Java6-APT-plugin? (eg.
plugin.xml contains extension tag for point
org.eclipse.jdt.apt.core.annotationProcessorFactory, and another factories
tag, as well as com.sun.mirror.apt.AnnotationProcessorFactory in the
services in META-INF.) Where to find information more about this for Java
6 / Eclipse 3.3? Is there any plans to update the APTdemo to Java6 /
Eclipse3.3?

The method of putting annotation processors in a JAR file is a bit
cumbersome, especially if you are developing some project and the
annotation it uses in parallell. It would be nice if one could build the
JAR file automatically but as I understand it you cannot do this in
Eclipse. Right? Instead you manually have to Export each time you make any
update. I suppose you cant have your annotation processor in the same
project as you are developing, can you? That would be nice...

Anyway, thanks in advance for any help and a happy new 2008!
Re: Annotation processing Java 6 [message #244375 is a reply to message #244334] Mon, 31 December 2007 16:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Per Westling" <Per.Westling@cybercom.se> wrote in message
news:40cc5da573afd26bd1a46b35dd5f9a35$1@www.eclipse.org...
> Hi, I am a bit new to Eclipse so excuse me if I ask an obvious question...
> Have spent quite some time to find the appropriate information but not had
> enough success so now I try to ask instead.
>
> I use Eclipse Europe (3.3.0) and is trying to have Eclipse generate some
> java source code from annotations during build using Java 6. Here is a
> small extract of the source code of the annotation processor I have
> written:
>
> @SupportedAnnotationTypes(value = {ManagedProcessor.NAMESPACE })
> @SupportedSourceVersion(SourceVersion.RELEASE_6)
> public final class ManagedProcessor extends AbstractProcessor
>
> I can now use this via javac by compiling my annotation processing class,
> and doing
> javac <some Java-files...> -processor <compiled ManagedProcessor class>
> This do generate the requested code in the current directory.
>
> So what I would like to do is to let Eclipse generate this during build.


Hi, Per. I think maybe you are missing the necessary entry in the
META-INF/services directory. This is how the processor gets declared to the
compiler. (If you had that entry, then you would not need to specify the
processor class in your javac command line.)

That directory needs a file named "javax.annotation.processing.Processor",
that is, the qualified name of the interface being implemented. Within the
file there should be a line of text giving the qualified name of the
implementation class, e.g., "com.xyz.MyProcessor".

If you have that file, then the implementation class should show up in the
Factory Path -> Advanced dialog.

If adding that entry to your jar doesn't work, let me know. My apologies
for the demo being so out of date; we haven't had time to update it. There
was an EclipseCon 2007 tutorial on writing annotation processors, that
included sample processor code for both Java 5 and Java 6 processors; that
presentation is still up on the eclipsecon web site, at
http://www.eclipsecon.org/2007/index.php?page=sub/&id=36 18 , and you may
want to download the presentation and take a look at it.

Also, eclipse.newcomer is always a fine place to start, but feel free to
post questions about APT in the eclipse.tools.jdt newsgroup, in the future.

Thanks,
-Walter Harley
JDT APT team
Re: Annotation processing Java 6 [message #244447 is a reply to message #244375] Wed, 02 January 2008 07:21 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Per.Westling.cybercom.se

Thanks for the reply. I still have problem though. As soon as that is
solved I will pose any new questions to the other newsgroup...

Walter Harley wrote:

> Hi, Per. I think maybe you are missing the necessary entry in the
> META-INF/services directory. This is how the processor gets declared to the
> compiler. (If you had that entry, then you would not need to specify the
> processor class in your javac command line.)

> That directory needs a file named "javax.annotation.processing.Processor",
> that is, the qualified name of the interface being implemented. Within the
> file there should be a line of text giving the qualified name of the
> implementation class, e.g., "com.xyz.MyProcessor".

> If you have that file, then the implementation class should show up in the
> Factory Path -> Advanced dialog.

Correct - now the processor is found both by javac (by having the jar in
the class path instead of using the -processor parameter) and Eclipse (in
the Advanced setting), but the .apt_generated directory is still empty. So
something is still missing.

When Eclipse is performing compilation and processing the annotation
processor, is there any way to follow this? (E.g. in the Console view?)
The processor have some diagnostic messages a la:

processingEnv.getMessager().printMessage(
Diagnostic.Kind.NOTE,
"Entered AnnotationEntityProcessor.process ...");

that appear when doing javac, but not in Eclipse. Should these appear in
the Problems view in Eclipse when things is working OK?

> If adding that entry to your jar doesn't work, let me know. My apologies
> for the demo being so out of date; we haven't had time to update it. There
> was an EclipseCon 2007 tutorial on writing annotation processors, that
> included sample processor code for both Java 5 and Java 6 processors; that
> presentation is still up on the eclipsecon web site, at
> http://www.eclipsecon.org/2007/index.php?page=sub/&id=36 18 , and you may
> want to download the presentation and take a look at it.

Thanks for the link. I will look into that more deeply. It could of course
be a simple way to see if Java 6 and Eclipse 3.3 works as it should by
updating the APTdemo ;-) but there is just a limited amount of time
available... Anyway, thanks for spending time of improving the situation.
Re: Annotation processing Java 6 [message #244499 is a reply to message #244447] Wed, 02 January 2008 11:24 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Per Westling" <Per.Westling@cybercom.se> wrote in message
news:1ccf199df9896f80722fbe67f573cf64$1@www.eclipse.org...
> Correct - now the processor is found both by javac (by having the jar in
> the class path instead of using the -processor parameter) and Eclipse (in
> the Advanced setting), but the .apt_generated directory is still empty. So
> something is still missing.

It sounds like the processor is not running. That could be because
something is throwing an exception early in the processing. If you put a
breakpoint in your init() and process() methods, does it get hit? It's
possible that your processor is calling an APT API that we've not yet
implemented, getting an UnsupportedOperationException, and terminating
early. That *should* get reported in either the error log or problems view,
but maybe there's a bug.

When I debug processors, I usually have my processor code in a host
workspace; generally I choose to implement the processor as a plug-in,
declaring the processor with the
org.eclipse.jdt.apt.core.annotationProcessorFactory extension point (which
applies to both Java 6 and Java 5 processors). Then I launch a debug
workspace that contains the annotated code.

It is easy and fine to declare the same processor both with the extension
point and the META-INF/services entry; in fact I recommend that as a best
practice. This is discussed in the Eclipsecon 2007 APT tutorial that I
pointed you to. It's just a bit easier to debug plug-ins than ordinary jar
files.


> When Eclipse is performing compilation and processing the annotation
> processor, is there any way to follow this? (E.g. in the Console view?)

There are some tracing methods in APT that may help you figure out what's
not happening. You can turn them on the same way that other Eclipse debug
options get turned on, e.g., with a .options file or with the Tracing tab in
the launch configuration. If you use the Tracing tab, you need to first
turn tracing on globally (the "enable tracing" checkbox), then select
org.eclipse.jdt.apt.pluggable.core and org.eclipse.jdt.apt.core and enable
tracing for both of those plug-ins, and then for each of the plug-ins enable
the specific options you want. For pluggable.core there's just the one
"debug" tracing option so far; for apt.core you might start with "debug" and
"debug/generatedFiles".


> The processor have some diagnostic messages a la:
>
> processingEnv.getMessager().printMessage(
> Diagnostic.Kind.NOTE,
> "Entered AnnotationEntityProcessor.process ...");
>
> that appear when doing javac, but not in Eclipse. Should these appear in
> the Problems view in Eclipse when things is working OK?

That's correct. They should show up just like other compile errors.

By the way, you can also run the Eclipse compiler at the command line like
javac, using the "JDT Core Batch Compiler" download from the download site.
This won't do Java 5 annotation processing (just as javac 1.5 doesn't) but
it will do Java 6 processing. I'm not sure it's directly helpful here, just
wanted you to know that it exists. The command-line APT implementation
shares about 70% of its code with the IDE implementation, so many of the
same bugs are in both :-)


> Thanks for the link. I will look into that more deeply. It could of course
> be a simple way to see if Java 6 and Eclipse 3.3 works as it should by
> updating the APTdemo ;-) but there is just a limited amount of time
> available...

Yes. Part of the issue is that the web page design is out of date, compared
to the rest of the eclipse.org web design, and I'm not a very good web
programmer so I shudder whenever I contemplate trying to fix it because not
only the content but also the HTML needs a complete rewrite.
Re: Annotation processing Java 6 [message #244663 is a reply to message #244499] Fri, 04 January 2008 03:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Per.Westling.cybercom.se

By doing debug (via -debug startup option, and using .options file) I got
enough information (via "sysout" :-) to locate the problem. More on that
below...

> It sounds like the processor is not running. That could be because
> something is throwing an exception early in the processing. If you put a
> breakpoint in your init() and process() methods, does it get hit? It's
> possible that your processor is calling an APT API that we've not yet
> implemented, getting an UnsupportedOperationException, and terminating
> early. That *should* get reported in either the error log or problems view,
> but maybe there's a bug.

It seems to be a bug (in 3.3.0 although debugging indicated 3.3.1) as it
is not reported, and the annotation processor dies and will not process
any more annotations.

The core reason for not working was the
java.lang.UnsupportedOperationException . This was thrown by
openOutputStream in JavaFileObject, via createSourceFile in Filer, that
was created using processingEnv.getFiler. That did not occur when using
javac though, so it seems that it as you guessed.

I am trying to find a way around this by creating files explicitely, that
both works from Eclipse as well as from command line (Ant). Or are there
better versions of JavaFileObject around? It could be nice to let the API
handle the hassle of putting the generated java source file there they
should be...

> When I debug processors, I usually have my processor code in a host
> workspace; generally I choose to implement the processor as a plug-in,
> declaring the processor with the
> org.eclipse.jdt.apt.core.annotationProcessorFactory extension point (which
> applies to both Java 6 and Java 5 processors). Then I launch a debug
> workspace that contains the annotated code.

Yes, the org.clipse.jdt.apt.core.annotationProcessorFactory extension
point, entries java6processors and java6processor did work. As well as
the org.eclipse.jdt.core.annotation.ProcessorManager, entry
annotationProcessorManager.
Got a confusing debug message: "Leaving project P/xxx early because there
are no AnnotationProcessorFactory instances registered." But then it uses
the java 6 annotation processor anyway...

> It is easy and fine to declare the same processor both with the extension
> point and the META-INF/services entry; in fact I recommend that as a best
> practice. This is discussed in the Eclipsecon 2007 APT tutorial that I
> pointed you to. It's just a bit easier to debug plug-ins than ordinary jar
> files.

A good recommendation. We will follow your advice.

>> The processor have some diagnostic messages a la:
>>
>> processingEnv.getMessager().printMessage(
>> Diagnostic.Kind.NOTE,
>> "Entered AnnotationEntityProcessor.process ...");
>>
>> that appear when doing javac, but not in Eclipse. Should these appear in
>> the Problems view in Eclipse when things is working OK?

> That's correct. They should show up just like other compile errors.

As I wrote above they do not seem to do this in Eclipse 3.3.0. Not even
if the exception causing statement is removed and the annotation
processing completes. It worked using APTDemo and Java 5 style though.
Re: Annotation processing Java 6 [message #244721 is a reply to message #244663] Fri, 04 January 2008 10:48 Go to previous message
Eclipse UserFriend
Originally posted by: Per.Westling.cybercom.se

Per Westling wrote:

> It seems to be a bug (in 3.3.0 although debugging indicated 3.3.1) as it
> is not reported, and the annotation processor dies and will not process
> any more annotations.

> The core reason for not working was the
> java.lang.UnsupportedOperationException . This was thrown by
> openOutputStream in JavaFileObject, via createSourceFile in Filer, that
> was created using processingEnv.getFiler. That did not occur when using
> javac though, so it seems that it as you guessed.

To test this more I upgraded to Classic 3.3.1.1 - same problem remains.

I then installed 3.4M4 (win32 version) aka Ganymede. Now the annotation
processing do work and files appear under .apt_generated. Great. (Although
still no messages in the Problem view in Eclipse... so that bug probably
remains.)

> I am trying to find a way around this by creating files explicitely, that
> both works from Eclipse as well as from command line (Ant). Or are there
> better versions of JavaFileObject around? It could be nice to let the API
> handle the hassle of putting the generated java source file there they
> should be...

Unfourtunately, upgrading to 3.4 is not an option at the moment, so the
problem remains. I don't suppose it is possible to download just the
needed parts from 3.4 to make it work on 3.3? Haven't located any later
version of 3.3 either.

BTW, this thread should (as you wrote in your first reply) have been
cross-posted to eclipse.tools.jdt, but I go through the "News-Portal" so I
do not know how to do that...
Previous Topic:undefined reference c++
Next Topic:need good tutorials to use Eclipse 3.3
Goto Forum:
  


Current Time: Wed May 07 11:59:15 EDT 2025

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

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

Back to the top