public class EclipseHelper extends DirectoryIndexer
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
URLClassLoader
with all the necessary class path elements. Crucially,
the class path elements corresponding to Eclipse projects will never point to
.jar
files 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 .class
files. 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.
AbstractIndexWriter.StreamFactory
Constructor and Description |
---|
EclipseHelper() |
Modifier and Type | Method and Description |
---|---|
static void |
main(String... args)
Updates the annotation index in the current Eclipse project.
|
static void |
updateAnnotationIndex(ClassLoader loader)
Updates the annotation index in the current Eclipse project.
|
add, discoverAnnotations, index, write
adapt, adapt, add, foundAnnotations, merge, write, writeMap
public static void updateAnnotationIndex(ClassLoader loader)
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 .class
files (as opposed to a .jar
file). We only need to update that
first class path element (or for tests, the first two), and only if it is a
local directory.
loader
- the class loader whose class path to inspectpublic static void main(String... args)
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 .class
files (as opposed to a .jar
file). We only need to update that
first class path element (or for tests, the first two), and only if it is a
local directory.
Copyright © 2015–2022 SciJava. All rights reserved.