Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Annotation processing type element look-up problem with Java 9 or later
Annotation processing type element look-up problem with Java 9 or later [message #1835434] Thu, 03 December 2020 10:30 Go to next message
Scott Cameron is currently offline Scott CameronFriend
Messages: 3
Registered: December 2020
Junior Member
Hello,

I have an annotation processor that uses the javax.lang.model classes to do its thing. This works perfectly up to Java 8, but when I try to run my build with Java 9 or later I get AbortCompilation exceptions when I call javax.lang.model.util.Elements.getTypeElement. (See the full call stack below [1]).

Debugging a bit into the JDT, it looks like the element look-up code behaves quite differently in module-based JDK versions (v9 or greater), which makes sense. I think modules is somehow the key to my problem, but I'm a little bit lost about what I need to do to my code to get this to work.

One interesting observation is that my build works fine under Maven using either JDK 1.8 or JDK 9+. This seems to be a problem specific to the JDT annotation processor.

I tried declaring a module-info.java file containing an empty "open module" declaration. This seems to get me a little bit further but it breaks a bunch of other things. Specifically, it seems that doing this brings me into the world of modules where I now need to explicitly declare my module requirements for it to work. This is a lot of overhead and isn't something I want to impose on consumers of my library. I would like to find a solution that "just works" without requiring changes across the various code bases using my utility.

Is there any guidance for getting annotation processors to work with Java 9+ ? Any ideas you might have would be appreciated as I'm a little bit stuck at the moment.

Thanks in advance!

Scott

[1] Full call stack from error:

java.lang.Exception: org.eclipse.jdt.internal.compiler.problem.AbortCompilation: 
	at org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:172)
	at org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.round(RoundDispatcher.java:124)
	at org.eclipse.jdt.internal.compiler.apt.dispatch.BaseAnnotationProcessorManager.processAnnotations(BaseAnnotationProcessorManager.java:171)
	at org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeAnnotationProcessorManager.processAnnotations(IdeAnnotationProcessorManager.java:138)
	at org.eclipse.jdt.internal.compiler.Compiler.processAnnotationsInternal(Compiler.java:958)
	at org.eclipse.jdt.internal.compiler.Compiler.processAnnotations(Compiler.java:932)
	at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:450)
	at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:426)
	at org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.compile(AbstractImageBuilder.java:386)
	at org.eclipse.jdt.internal.core.builder.BatchImageBuilder.compile(BatchImageBuilder.java:214)
	at org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.compile(AbstractImageBuilder.java:318)
	at org.eclipse.jdt.internal.core.builder.BatchImageBuilder.build(BatchImageBuilder.java:79)
	at org.eclipse.jdt.internal.core.builder.JavaBuilder.buildAll(JavaBuilder.java:278)
	at org.eclipse.jdt.internal.core.builder.JavaBuilder.build(JavaBuilder.java:187)
	at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:832)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:220)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:263)
	at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:316)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:319)
	at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:371)
	at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:392)
	at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:154)
	at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:244)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: org.eclipse.jdt.internal.compiler.problem.AbortCompilation: 
	at org.eclipse.jdt.internal.core.builder.NameEnvironment.findClass(NameEnvironment.java:476)
	at org.eclipse.jdt.internal.core.builder.NameEnvironment.findType(NameEnvironment.java:546)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.lambda$0(LookupEnvironment.java:237)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForTypeFromModules(LookupEnvironment.java:372)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(LookupEnvironment.java:236)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getType(LookupEnvironment.java:1703)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getType(LookupEnvironment.java:1672)
	at org.eclipse.jdt.internal.compiler.apt.model.ElementsImpl9.getTypeElement(ElementsImpl9.java:81)
	at org.eclipse.jdt.internal.compiler.apt.model.ElementsImpl9.getTypeElement(ElementsImpl9.java:53)
	at com.scott.toolkit.common.lang.model.element.SkeletonGenerator.findClass(SkeletonGenerator.java:134)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	at com.scott.toolkit.common.lang.model.element.SkeletonGenerator.loadClass(SkeletonGenerator.java:107)
	at com.scott.toolkit.common.bean.codegen.source.BaseValueClassGenerator.generateFiles(BaseValueClassGenerator.java:141)
	at com.scott.toolkit.common.bean.codegen.source.BaseValueClassGenerator.generateFiles(BaseValueClassGenerator.java:38)
	at com.scott.toolkit.common.annotation.processing.SkeletonBasedGenerator$2.eval(SkeletonBasedGenerator.java:68)
	at com.scott.toolkit.common.annotation.processing.SkeletonBasedGenerator$2.eval(SkeletonBasedGenerator.java:65)
	at com.scott.toolkit.common.util.Iterables$11$1.eval(Iterables.java:778)
	at com.scott.toolkit.common.util.Iterables$11$1.eval(Iterables.java:774)
	at com.scott.toolkit.common.util.Iterators$FlatMapIterator.internalNext(Iterators.java:1551)
	at com.scott.toolkit.common.util.Iterators$IteratorWithLookahead.nextElement(Iterators.java:1238)
	at com.scott.toolkit.common.util.Iterators$IteratorWithLookahead.hasNext(Iterators.java:1202)
	at com.scott.toolkit.common.annotation.processing.GeneratorBase.process(GeneratorBase.java:130)
	at org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:142)
	... 25 more
Re: Annotation processing type element look-up problem with Java 9 or later [message #1835470 is a reply to message #1835434] Thu, 03 December 2020 23:04 Go to previous messageGo to next message
Manoj Palat is currently offline Manoj PalatFriend
Messages: 19
Registered: October 2014
Junior Member
Hi Scott,
This looks like a bug in JDT - can you please file a bug report at https://bugs.eclipse.org/bugs/enter_bug.cgi?product=JDT - please select Core as the component and please attach a reproducible testcase/project.

Thanks,
Manoj


Manoj N Palat
Eclipse Java Development Tools (JDT).
Re: Annotation processing type element look-up problem with Java 9 or later [message #1835475 is a reply to message #1835470] Fri, 04 December 2020 06:08 Go to previous messageGo to next message
Scott Cameron is currently offline Scott CameronFriend
Messages: 3
Registered: December 2020
Junior Member
Hi Manoj,

OK, I'll create a bug report and see if I can narrow down a simple repro project.
In the meantime, can you think of anything we can try to work around the issue? We're trying to update all of our code from Java 8 to Java 11 and this particular utility is used all over the place, so we're stuck until we find a way to get this to work.

Best regards,
scott
Re: Annotation processing type element look-up problem with Java 9 or later [message #1835494 is a reply to message #1835475] Fri, 04 December 2020 17:22 Go to previous messageGo to next message
Scott Cameron is currently offline Scott CameronFriend
Messages: 3
Registered: December 2020
Junior Member
Hello again,

As I was trying to simplify my code enough to share a repro case, I noticed something that is a bit more specific. The exception happens when I pass a nested class name into Elements.getTypeElement. Interestingly, this triggers the exception:

elements.getTypeElement("com.scott.MyClass.MyNestedClass")


But this works fine:

elements.getTypeElement("com.scott.MyClass$MyNestedClass")


With the compiler Maven uses, only the first one works. The second one returns null because the type isn't found.

I'm guessing this must be a bug because the Javadoc for getTypeElement says that name should be the canonical name but Class.getCanonicalName returns the dot-separated version (e.g. com.scott.MyClass.MyNestedClass) so this fails:

elements.getTypeElement(MyNestedClass.class.getCanonicalName())


Do you know if this is a known issue already, or should I great a bug for that?

Cheers,
scott

[Updated on: Fri, 04 December 2020 17:26]

Report message to a moderator

Re: Annotation processing type element look-up problem with Java 9 or later [message #1836974 is a reply to message #1835494] Wed, 20 January 2021 12:31 Go to previous message
Mark Smith is currently offline Mark SmithFriend
Messages: 84
Registered: September 2020
Member
Please check this one
https://www.oracle.com/technical-resources/articles/java/ma14-architect-annotations.html
Previous Topic:Error when "mvn package" the JDT Core component
Next Topic:How to select specific OSGI bundles to run in Eclipse
Goto Forum:
  


Current Time: Mon Jul 26 14:40:34 GMT 2021

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

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

Back to the top