atteo / classindex

Index classes, do not scan them!
Apache License 2.0
263 stars 42 forks source link

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

Open MrKuip opened 6 years ago

MrKuip commented 6 years ago

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 commented 6 years ago

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;

} }`