Skip to content

Commit ee73f02

Browse files
committed
Use the ByteCodeAnalyzer to write the annotation index
Instead of loading the classes, we read the annotations directly from the .class files. This has two advantages over the previous method: 1. We avoid polluting the ClassLoader with loaded classes (which can be problematic e.g. when ij1-patcher wants to patch the classes before loading them at a later stage). 2. It is no longer necessary for the dependencies of the classes to be on the class path (though that is only true for the classes, not for the annotations). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent e9b395c commit ee73f02

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/main/java/org/scijava/annotations/DirectoryIndexer.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.net.MalformedURLException;
4242
import java.net.URL;
4343
import java.net.URLClassLoader;
44+
import java.util.Map;
4445

4546
/**
4647
* Indexes a directory of classes.
@@ -88,18 +89,28 @@ else if (file.isFile()) {
8889
final String className =
8990
classNamePrefix + fileName.substring(0, fileName.length() - 6);
9091
try {
91-
final Class<?> clazz = loader.loadClass(className);
92-
for (final Annotation a : clazz.getAnnotations()) {
93-
add(a, className);
92+
for (final Map.Entry<String, Map<String, Object>> entry : ByteCodeAnalyzer
93+
.getAnnotations(file).entrySet())
94+
{
95+
final String annotationName = entry.getKey();
96+
try {
97+
if (!isIndexable(loader.loadClass(annotationName))) {
98+
continue;
99+
}
100+
}
101+
catch (ClassNotFoundException e) {
102+
// fall back to the class loader that laoded the directory indexer
103+
if (!isIndexable(Class.forName(annotationName))) {
104+
continue;
105+
}
106+
}
107+
add(entry.getValue(), annotationName, className);
94108
}
95-
} catch (NoClassDefFoundError e) {
96-
System.err.println("Warning: could not load class '" + className + "' ("
97-
+ e.getMessage() + "); skipping");
98-
} catch (ClassNotFoundException e) {
99-
System.err.println("Warning: could not load class '" + className + "'; skipping");
100-
} catch (Throwable e){
101-
System.err.println("Warning: could not load class '" + className + "' ("
102-
+ e.getMessage() + "); skipping");
109+
}
110+
catch (Throwable e) {
111+
System.err.println("Warning: could not load class '" + className +
112+
"'; skipping");
113+
e.printStackTrace();
103114
}
104115
}
105116
}
@@ -116,7 +127,11 @@ protected synchronized <A extends Annotation> void add(final A annotation,
116127

117128
private static <A extends Annotation> boolean isIndexable(final A annotation)
118129
{
119-
return annotation.annotationType().getAnnotation(Indexable.class) != null;
130+
return isIndexable(annotation.annotationType());
131+
}
132+
133+
private static boolean isIndexable(final Class<?> annotationClass) {
134+
return annotationClass.getAnnotation(Indexable.class) != null;
120135
}
121136

122137
protected synchronized void write(final File directory) throws IOException {

0 commit comments

Comments
 (0)