Home » Language IDEs » Java Development Tools (JDT) » State of JDT-APT
|
Re: State of JDT-APT [message #258877 is a reply to message #258872] |
Tue, 24 February 2009 18:56 |
Walter Harley Messages: 847 Registered: July 2009 |
Senior Member |
|
|
"Michael Furtak" <mfurtak@cra.com> wrote in message
news:917ecc244b7d468a3580d61097a69c53$1@www.eclipse.org...
> Hello,
>
> I have created an annotation processor with the Java 6 APIs that runs on
> the command line with javac (it is also an OSGi bundle). I would also now
> like to have it work within Eclipse. I have perused the JDT-APT pages on
> eclipse.org, but they seem to be somewhat out of date.
> I am left with a couple of questions:
>
> 1) What is the status of Java 6 APT support within JDT-APT and Eclipse
> 3.4?
>
> 2) Is there a more up-to-date resource than
> http://www.eclipse.org/jdt/apt/index.html that details how to make my
> annotation processor available to Eclipse?
Hi, Michael.
Both the Java 5 and Java 6 APT APIs are supported in Eclipse 3.3 and later,
with 3.4 and 3.5 containing respectively more complete implementations and
fewer bugs.
There are some differences in the way that the Java 5 and Java 6 API support
was done; for instance, Java 5 processors are able to run during 'reconcile'
(so that red squiggles can be shown as a user is typing) whereas Java 6
processors only run during a build, but Java 6 processors are supported in
the command-line Eclipse compiler (ecj) whereas Java 5 processors aren't.
The APT project is active in the sense that bugs are fixed and missing
functionality continues to be filled in, but unlike the rest of JDT, APT is
staffed only by volunteers (primarily me, plus whoever contributes a bug or
patch) on a part-time basis, so bug reports sometimes take a while to follow
up on. Nonetheless, we remain committed to having APT be a fully usable and
functional part of the product. Support is available here on the JDT
newsgroup, and also in Bugzilla via the JDT/APT component.
The web page is in terrible shape, I agree. The best resources for learning
how to work with APT and develop annotation processors are the EclipseCon
tutorials that my colleagues and I presented in 2006 and 2007; those are
linked from the second paragraph of the web page you mentioned above.
If you have your plug-in already packaged as an OSGi bundle and working in
javac, you are 95% of the way done. You should be able to use it as is, by
putting the jar file on the annotation processing factory path; or, you can
load it as a plug-in, by declaring an extension to the
org.eclipse.jdt.apt.core.annotationProcessingFactories extension point.
This sort of "dual-mode" processor (available both as a jar file and a
plug-in) is in my opinion the best practice for plug-in development, so
you're on a good track.
|
|
|
Re: State of JDT-APT [message #259024 is a reply to message #258877] |
Tue, 10 March 2009 20:24 |
Michael Furtak Messages: 5 Registered: July 2009 |
Junior Member |
|
|
Hi Walter,
Thank you for your reply. I believe I have gotten a little closer to
success, but am still not seeing results. Here's what I do have:
I prepared a simple processor as follows:
@SupportedAnnotationTypes(value = { "com.cra.annotations.*" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class AnnotationProcessor extends AbstractProcessor
{
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv)
{
processingEnv.getMessager().printMessage(Kind.ERROR, "Processing
attempted!");
return true;
}
}
My expectation is that when it is run, that I should see build errors on
all of my annotated files with my "Processing attempted!" message. After a
Project | Clean... | Clean all projects, I still see no change.
Here is what I can say about my configuration:
I packaged that AnnotationProcessor up as an Eclipse plugin. The plugin
XML is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension point="org.eclipse.jdt.apt.core.annotationProcessorFactory" >
<java6processors enableDefault="true">
<java6processor class="com.cra.osgi.annotations.AnnotationProcessor"/>
</java6processors>
</extension>
</plugin>
I have the following as Required Plug-ins: (perhaps overkill)
org.eclipse.jdt
org.eclipse.jdt.apt.core
org.eclipse.jdt.core
That plug-in is present in my eclipse/plugins directory, and is visible in
the eclipse Plug-ins view.
In my workspace I have a project that defines some annotations in the
com.cra.annotations package, and applies them to another source file.
Within Project Properties | Java Compiler, JDK compliance is set for 1.6
Under Annotation Processing, the following things are checked:
Enable project specific settings
Enable annotation processing
Enable processing in editor
Under Factory Path, the following things are checked:
Enable project specific settings
AnnotationProcessor (the name of my plug-in bundle, which is visible in
the processor list).
All of these things make me believe that I should be seeing results, but I
seem to be one step short. Any idea what the hiccup might be?
Thanks,
-Mike
|
|
|
Re: State of JDT-APT [message #259040 is a reply to message #259024] |
Thu, 12 March 2009 00:37 |
Walter Harley Messages: 847 Registered: July 2009 |
Senior Member |
|
|
"Michael Furtak" <mfurtak@cra.com> wrote in message
news:77ff5e65b09d386842e997fabf219d5c$1@www.eclipse.org...
> That plug-in is present in my eclipse/plugins directory, and is visible in
> the eclipse Plug-ins view.
A friend of mine says "if you light a man a fire, he stays warm for one
night; if you light a man *on* fire, he stays warm for the rest of his
life." ;-) In that spirit, rather than starting by debugging your app for
you remotely, let me suggest some tools:
First, I would NOT recommend testing your plug-in by deploying it into an
Eclipse install. In fact I wouldn't actually deploy it until you're quite
certain it works. Instead, I'd make use of the Eclipse Plug-in Development
Environment (PDE). You've already got a host workspace that contains a
plug-in project that is your processor, right? So, instead of exporting
that project and installing it into Eclipse, just run that project, as an
Eclipse Application. In the launch configuration you can specify the
location of a target workspace, which will contain a Java project with your
annotated code. You'll end up with two running instances of Eclipse: the
host instance, with your plug-in as source code, and the target instance,
with your plug-in as part of Eclipse.
The advantage to this is that you can debug like a normal Java application,
putting breakpoints in your processor, viewing variables, etc; you can even
step into the APT and JDT code if something seems wrong (the PDE
perspective's Plug-Ins view will let you import JDT and APT source code as
projects in your workspace). You can also make changes to code on the fly,
within certain limits.
If you do this, then you can put a breakpoint on your processor's process()
method to see if it's ever hit. You might also want to override init() to
see if that's getting hit.
Similarly, you can write JUnit test cases that exercise your processor, and
run them as JUnit Plug-in Tests, and they'll get executed in a target
Eclipse instance that contains your processor.
The second tool is tracing, and again it'll help if you're running with PDE.
In the launch configuration go to the "Tracing" tab; check "enable tracing",
then enable tracing for org.eclipse.jdt.apt.pluggable.core, and check
'debug' on the right pane. There are also some trace options for the other
APT plug-ins that you might want to explore. The output of the trace will
go into the host workspace's console, and should help you figure out what is
and is not happening.
All that said: in this particular case, the problem is that you've not
specified an Element in your call to printMessage(), so the output is going
to the error log. (Which is viewable in the Error Log view of the PDE
Runtime Perspective.) Try using the "printMessage(Diagnostic.Kind kind,
CharSequence msg, Element e);" form of that call, to get the error
associated with a particular annotation or source file.
But I didn't realize that until I tried debugging your app by putting a
breakpoint in the process() call :-)
Hope that helps,
-walter
|
|
|
Re: State of JDT-APT [message #665047 is a reply to message #259040] |
Wed, 13 April 2011 05:08 |
Karl Messages: 3 Registered: April 2011 |
Junior Member |
|
|
I have tried the suggested solution to getting out into the Problem window of the workspace in which the annotations are being used without any luck.
The error seems stuck in the console windows of the Plugin Eclipse workspace from which the 2nd eclipse instance is launched.
Here is my processor code
@Override()
public boolean
process(Set<? extends TypeElement> annotations, RoundEnvironment round)
{
for ( TypeElement typeElement: annotations ) {
if ("Watcher".equals(typeElement.getQualifiedName().toString())) {
processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "Error : " + typeElement.getQualifiedName(), typeElement);
// Claim the annotation
return (true);
}
}
return (false);
}
Do you have any ideas as to the right way to get this message into the window of the launched eclipse.
P.s. the debugger does stop in my plugin correctly.
Also while it seems the processor is called and I see my annotation the spawned eclipse instance still marks the annotation as being not found ? I have read a ton of tutorials and videos etc etc etc without being able to locate the vital clue as to how Java 6 annotations work with Eclipse without these dangling issues. Are there any up-todate resources using the newer API's that people may be aware of ?
Many Thanks
Karl
Watcher.java
package com.karlmutch.ucbadvancedjava.annotator;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* A marker annotation that will be used to mark classes that
* wish to watch classes. The classes to watch will be determined
* by examining the marked classes for methods that take a single
* parameter who's type derives from the base Message class.
*
**/
@Retention( RetentionPolicy.RUNTIME )
@Target( ElementType.TYPE )
public @interface Watcher {
}
WatcherActivator.java
package com.karlmutch.ucbadvancedjava.annotator;
import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
/**
* This class is the interface between the Eclipse based factory and the
* packaging strategy we use for our plugin.
**/
public class WatcherActivator extends Plugin implements BundleActivator {
// The plug-in ID, must match the plugin.xml ID
public static final String PLUGIN_ID = "com.karlmutch.ucbadvancedjava.annotator";
// The shared instance
private static WatcherActivator plugin;
// A default constructor just to get things started
public WatcherActivator() {
}
/*
* (non-Javadoc)
* @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.Bu ndleContext)
*/
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
}
/*
* (non-Javadoc)
* @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.Bund leContext)
*/
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
/**
* Accessor method that is used to get a shared instance of our activator
* for the processor
*
* @return the shared instance
*/
public static WatcherActivator getDefault() {
return plugin;
}
}
WatcherAnnotationProcessor.java
import java.util.Set;
import javax.tools.Diagnostic;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
/**
* This class implements the actual code processing engine that
* the java compiler will invoke to handle annotations
*/
@SupportedAnnotationTypes( "Watcher" )
@SupportedSourceVersion( SourceVersion.RELEASE_6 )
public class WatcherAnnotationProcessor extends AbstractProcessor
{
@Override()
public boolean
process(Set<? extends TypeElement> annotations, RoundEnvironment round)
{
for ( TypeElement typeElement: annotations ) {
if ("Watcher".equals(typeElement.getQualifiedName().toString())) {
processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "Error : " + typeElement.getQualifiedName(), typeElement);
// Claim the annotation
return (true);
}
}
return (false);
}
}
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: com.karlmutch.ucbadvancedjava.annotator
Bundle-SymbolicName: com.karlmutch.ucbadvancedjava.annotator;singleton:=true
Bundle-Version: 1.0.0
Export-Package: com.karlmutch.ucbadvancedjava.annotator;uses:=" org.eclipse.core.runtime,org.osgi.framework,javax.annotation .processing "
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.core.runtime,
org.osgi.framework
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: org.eclipse.jdt;bundle-version="3.6.1",
org.eclipse.jdt.apt.core;bundle-version="3.3.401",
org.eclipse.jdt.core;bundle-version="3.6.1"
Bundle-Activator: com.karlmutch.ucbadvancedjava.annotator.WatcherActivator
META-INF/services/javax.annotation.processing.Processor
com.karlmutch.ucbadvancedjava.annotator.WatcherAnnotationPro cessor
build.properties
source.. = src/
bin.includes = META-INF/,\
., \
plugin.xml
plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.jdt.apt.core.annotationProcessorFactory" >
<java6processors enableDefault="true">
<java6processor class=" com.karlmutch.ucbadvancedjava.annotator.WatcherAnnotationPro cessor "/>
</java6processors>
</extension>
</plugin>
|
|
| | | | | |
Goto Forum:
Current Time: Fri Sep 20 09:46:38 GMT 2024
Powered by FUDForum. Page generated in 0.04174 seconds
|