Class EclipseHelper
- java.lang.Object
-
- org.scijava.annotations.AbstractIndexWriter
-
- org.scijava.annotations.DirectoryIndexer
-
- org.scijava.annotations.EclipseHelper
-
public class EclipseHelper extends DirectoryIndexer
Helps Eclipse's lack of support for annotation processing in incremental build mode.Eclipse has a very, let's say, "creative" way to interpret the Java specifications when it comes to annotation processing: while Java mandates that annotation processors need to be run after compiling Java classes, Eclipse cops out of that because it poses a challenge to its incremental compilation (and especially to Eclipse's attempt at compiling .class files even from .java sources that contain syntax errors).
So we need to do something about this. Our strategy is to detect when the annotation index was not updated properly and just do it ourselves, whenever
Index.load(Class)is called.Since our aim here is to compensate for Eclipse's shortcoming, we need only care about the scenario where the developer launches either a Java main class or a unit test from within Eclipse, and even then only when the annotation index is to be accessed.
The way Eclipse launches Java main classes or unit tests, it makes a single
URLClassLoaderwith all the necessary class path elements. Crucially, the class path elements corresponding to Eclipse projects will never point to.jarfiles but to directories. This allows us to assume that the annotation classes as well as the annotated classes can be loaded using that exact class loader, too.It is quite possible that a developer may launch a main class in a different project than the one which needs annotation indexing, therefore we need to inspect all class path elements.
To provide at least a semblance of a performant component, before going all out and indexing the annotations, we verify that the
META-INF/json/directory has an outdated timestamp relative to the.classfiles. If that is not the case, we may safely assume that the annotation indexes are up-to-date.To avoid indexing class path elements over and over again which simply do not contain indexable annotations, we make the
META-INF/json/directory nevertheless, updating the timestamp to reflect that we indexed the annotations.- Author:
- Johannes Schindelin
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.scijava.annotations.AbstractIndexWriter
AbstractIndexWriter.StreamFactory
-
-
Constructor Summary
Constructors Constructor Description EclipseHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidmain(String... args)Updates the annotation index in the current Eclipse project.static voidupdateAnnotationIndex(ClassLoader loader)Updates the annotation index in the current Eclipse project.-
Methods inherited from class org.scijava.annotations.DirectoryIndexer
add, discoverAnnotations, index, write
-
Methods inherited from class org.scijava.annotations.AbstractIndexWriter
adapt, adapt, add, foundAnnotations, merge, write, writeMap
-
-
-
-
Method Detail
-
updateAnnotationIndex
public static void updateAnnotationIndex(ClassLoader loader)
Updates the annotation index in the current Eclipse project.The assumption is that Eclipse -- after failing to run the annotation processors correctly -- will launch any tests or main classes with a class path that contains the project's output directory with the
.classfiles (as opposed to a.jarfile). We only need to update that first class path element (or for tests, the first two), and only if it is a local directory.- Parameters:
loader- the class loader whose class path to inspect
-
main
public static void main(String... args)
Updates the annotation index in the current Eclipse project.The assumption is that Eclipse -- after failing to run the annotation processors correctly -- will launch any tests or main classes with a class path that contains the project's output directory with the
.classfiles (as opposed to a.jarfile). We only need to update that first class path element (or for tests, the first two), and only if it is a local directory.
-
-