Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with eclipse and "Indexing when annotations cannot be used" #47

Open
MrKuip opened this issue Nov 1, 2018 · 1 comment
Open

Comments

@MrKuip
Copy link

MrKuip commented Nov 1, 2018

I want to find classes with the annotation "javax.persistence.Entity".
This annotation is in the jar "hibernate-jpa.jar"

I've created a Processor as explained in "Indexing when annotations cannot be used".
This works with javac.
But it doesn't work with eclipse. The stacktraces is included below.
The problem is that the Entity.class is not found on the path. I do not know why javac has the hibernate-jpa.jar on its claspath and the eclipsecompiler doesn't.

But this problem can be circumvented if the method indexAnnotations(Class... classes) is changed
to indexAnnotations(String... classNames). The dependency on the external libs dissappear now.
Maybe this method should be added?

Unable to create instance of annotation processor gti.classindex.processor.MesClassIndexProcessor
java.lang.NoClassDefFoundError: javax/persistence/Entity
at gti.classindex.processor.MesClassIndexProcessor.(MesClassIndexProcessor.java:11)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at org.eclipse.jdt.apt.core.internal.ClassServiceFactory.newInstance(ClassServiceFactory.java:26)
at org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeAnnotationProcessorManager.discoverNextProcessor(IdeAnnotationProcessorManager.java:93)
at org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.round(RoundDispatcher.java:116)
at org.eclipse.jdt.internal.compiler.apt.dispatch.BaseAnnotationProcessorManager.processAnnotations(BaseAnnotationProcessorManager.java:159)
at org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeAnnotationProcessorManager.processAnnotations(IdeAnnotationProcessorManager.java:135)
at org.eclipse.jdt.internal.compiler.Compiler.processAnnotations(Compiler.java:933)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:443)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:419)
at org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.compile(AbstractImageBuilder.java:372)
at org.eclipse.jdt.internal.core.builder.BatchImageBuilder.compile(BatchImageBuilder.java:187)
at org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.compile(AbstractImageBuilder.java:332)
at org.eclipse.jdt.internal.core.builder.BatchImageBuilder.build(BatchImageBuilder.java:61)
at org.eclipse.jdt.internal.core.builder.JavaBuilder.buildAll(JavaBuilder.java:256)
at org.eclipse.jdt.internal.core.builder.JavaBuilder.build(JavaBuilder.java:175)
at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:735)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:206)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:246)
at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:301)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:304)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:263)
at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:403)
at org.eclipse.core.internal.resources.Project$1.run(Project.java:566)
at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2240)
at org.eclipse.core.internal.resources.Project.internalBuild(Project.java:544)
at org.eclipse.core.internal.resources.Project.build(Project.java:112)
at org.eclipse.jdt.internal.ui.util.CoreUtility$BuildJob.run(CoreUtility.java:159)

@MrKuip
Copy link
Author

MrKuip commented Nov 1, 2018

This is my workaround:
`package gti.classindex.processor;

import org.atteo.classindex.processor.ClassIndexProcessor;
import java.lang.reflect.Field;
import java.util.Set;

public class MesClassIndexProcessor extends ClassIndexProcessor
{
public MesClassIndexProcessor()
throws Exception
{
indexAnnotations("javax.xml.bind.annotation.XmlAccessorType",
"javax.persistence.Entity");
}

protected final void indexAnnotations(String... classNames)
throws Exception
{
Set indexedAnnotations;
Field annotationDriven;

 indexedAnnotations = (Set<String>) getField("indexedAnnotations").get(this);

 for (String className : classNames)
 {
   indexedAnnotations.add(className);
 }

 getField("annotationDriven").setBoolean(this, false);

}

private Field getField(String fieldName)
throws Exception
{
Field field;

 field = getClass().getSuperclass().getDeclaredField(fieldName);
 field.setAccessible(true);

 return field;

}
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant