ExecutableElement#getReturnType() - always returns null [message #257806] |
Mon, 15 December 2008 19:57  |
Eclipse User |
|
|
|
I've been trying to rewrite an annotation processor so that it uses the
Java 6 annotation-processing facilities in place of the Java 5 facilities,
which I understand are obsolete. (Incidentally, I'm not convinced that
I'm calling these by their correct names; is there a better way to refer
to the two different processing APIs/tools?) My problem is that I have
not been successful in retrieving the return types of my annotated methods
when running the processor in Eclipse, even though the same processor is
able to get at the types when run under Sun's compiler.
To access the return type I call getReturnType() on the ExecutableElement
representing the method of interest, and under javac this returns the
correct type, but with the JDT's compiler it seems always to return null.
Alternatively, calling getReturnType() on the ExecutableType returned by
Element#asType() for the method of interest results in a
NullPointerException within the compiler. The stack trace is reported in
the log:
!ENTRY org.eclipse.jdt.apt.pluggable.core 4 1 2008-12-15 15:28:30.503
!MESSAGE Exception thrown by Java annotation processor
test.MethodQueryProcessor@1ca8d36
!STACK 0
java.lang.NullPointerException
at
org.eclipse.jdt.internal.compiler.apt.model.Factory.newTypeM irror(Factory.java:384)
at
org.eclipse.jdt.internal.compiler.apt.model.ExecutableTypeIm pl.getReturnType(ExecutableTypeImpl.java:77)
at test.MethodQueryProcessor.process(MethodQueryProcessor.java: 25)
at
org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatch er.handleProcessor(RoundDispatcher.java:139)
at
org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatch er.round(RoundDispatcher.java:121)
at
org.eclipse.jdt.internal.compiler.apt.dispatch.BaseAnnotatio nProcessorManager.processAnnotations(BaseAnnotationProcessor Manager.java:159)
at
org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeAnno tationProcessorManager.processAnnotations(IdeAnnotationProce ssorManager.java:134)
at
org.eclipse.jdt.internal.compiler.Compiler.processAnnotation s(Compiler.java:794)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler. java:423)
at
org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.c ompile(AbstractImageBuilder.java:363)
at
org.eclipse.jdt.internal.core.builder.BatchImageBuilder.comp ile(BatchImageBuilder.java:178)
at
org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.c ompile(AbstractImageBuilder.java:300)
at
org.eclipse.jdt.internal.core.builder.BatchImageBuilder.buil d(BatchImageBuilder.java:60)
at
org.eclipse.jdt.internal.core.builder.JavaBuilder.buildAll(J avaBuilder.java:254)
at
org.eclipse.jdt.internal.core.builder.JavaBuilder.build(Java Builder.java:173)
at
org.eclipse.core.internal.events.BuildManager$2.run(BuildMan ager.java:633)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at
org.eclipse.core.internal.events.BuildManager.basicBuild(Bui ldManager.java:170)
at
org.eclipse.core.internal.events.BuildManager.basicBuild(Bui ldManager.java:201)
at
org.eclipse.core.internal.events.BuildManager$1.run(BuildMan ager.java:253)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at
org.eclipse.core.internal.events.BuildManager.basicBuild(Bui ldManager.java:256)
at
org.eclipse.core.internal.events.BuildManager.basicBuildLoop (BuildManager.java:309)
at
org.eclipse.core.internal.events.BuildManager.build(BuildMan ager.java:341)
at
org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBu ildJob.java:140)
at
org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJ ob.java:238)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
For testing purposes, I have created a simple annotation type
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
public @interface MethodQuery {}
and a simple annotation processor to process it.
@SupportedAnnotationTypes("test.MethodQuery")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public final class MethodQueryProcessor extends AbstractProcessor
{
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv)
{
for(Element annotatedMethod :
roundEnv.getElementsAnnotatedWith(MethodQuery.class))
processingEnv.getMessager().printMessage(
Diagnostic.Kind.OTHER,
"Method return type: " +
((ExecutableElement)annotatedMethod).getReturnType(),
annotatedMethod);
return true;
}
}
I assume I have wired everything up correctly, because the processor does
run in Eclipse and print its messages; it just fails to give what I see as
the correct answers. On the other hand, if I replace the call to
getReturnType() with a call to getSimpleName(), the processor works as I
intend with both javac and the JDT.
I am running version 3.4.1 of Eclipse under Windows. Any idea what the
matter could be?
Please let me know if you need any more information about my situation,
and thank you very much for your time.
Erin
|
|
|
Re: ExecutableElement#getReturnType() - always returns null [message #257845 is a reply to message #257806] |
Thu, 18 December 2008 03:26   |
Eclipse User |
|
|
|
"Erin Crosland" <erin.crosland@dnr.wa.gov> wrote in message
news:32f459143c0f98f04245eef3e7e3f81c$1@www.eclipse.org...
> I've been trying to rewrite an annotation processor so that it uses the
> Java 6 annotation-processing facilities in place of the Java 5 facilities,
> which I understand are obsolete. (Incidentally, I'm not convinced that
> I'm calling these by their correct names; is there a better way to refer
> to the two different processing APIs/tools?)
I usually refer to the Java 5 API as the Mirror API or as the com.sun.mirror
API, and to the Java 6 API as the JSR269 API or as the
javax.annotation.processing API. But really, calling them the Java 5 and
Java 6 API is not bad.
The Java 5 API is not really "obsolete", but it is proprietary and it's true
that no further work is going into it. Still, I expect people will continue
to write Java 5 processors for some time. It can be argued that the Java 6
API is just different, not necessarily better. However, I think you're
probably doing the right thing by trying to switch to the Java 6 API.
> My problem is that I have not been successful in retrieving the return
> types of my annotated methods when running the processor in Eclipse, even
> though the same processor is able to get at the types when run under Sun's
> compiler.
Can you post a bug report in Bugzilla, containing the same information you
have below? It sounds like a bug, but it will be easier for me to dig into
it if it's in Bugzilla. Put it in the JDT APT category.
Please also include the code that you are processing, in addition to the
items you already mentioned.
Thanks!
-Walter Harley
JDT APT lead
|
|
|
|
Powered by
FUDForum. Page generated in 0.04478 seconds