Skip to content

Commit b831e16

Browse files
committed
Encapsulate interface, superclass mappings
Need to encapsulate the annotated methods and fields for each interface and superclass. This should speed things up a bit by reducing the scope for lookups of lower-level (i.e. higher up in the class hierarchy) omponents.
1 parent 6bffebb commit b831e16

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

src/main/java/org/scijava/util/ClassUtils.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,17 @@ public static <A extends Annotation> List<Method> getAnnotatedMethods(
344344
// are the main sorts of methods we are interested in.
345345
if (c == null || c == Object.class) return;
346346

347-
// check supertypes for annotated methods first
348-
getAnnotatedMethods(c.getSuperclass(), annotationClass, methods);
347+
final Class<?> sc = c.getSuperclass();
348+
if (sc != null) {
349+
List<Method> superMethods = lookupMethods(sc, annotationClass);
350+
if (superMethods == null) {
351+
superMethods = new ArrayList<Method>();
352+
// check supertypes for annotated methods first
353+
getAnnotatedMethods(sc, annotationClass, superMethods);
354+
}
355+
methods.addAll(superMethods);
356+
}
357+
349358
// NB: In some cases, we may not need to recursively scan interfaces.
350359
// In particular, for the @EventHandler annotation, we only care about
351360
// concrete methods, not interface method declarations. So we could have
@@ -354,7 +363,13 @@ public static <A extends Annotation> List<Method> getAnnotatedMethods(
354363
// suggest that the performance difference, even when creating a
355364
// full-blown Context with a large classpath, is negligible.
356365
for (final Class<?> iface : c.getInterfaces()) {
357-
getAnnotatedMethods(iface, annotationClass, methods);
366+
List<Method> ifaceMethods = lookupMethods(iface, annotationClass);
367+
368+
if (ifaceMethods == null) {
369+
ifaceMethods = new ArrayList<Method>();
370+
getAnnotatedMethods(iface, annotationClass, ifaceMethods);
371+
}
372+
methods.addAll(ifaceMethods);
358373
}
359374

360375
for (final Method m : c.getDeclaredMethods()) {
@@ -410,10 +425,24 @@ public static <A extends Annotation> void getAnnotatedFields(
410425
// are the main sorts of fields we are interested in.
411426
if (c == null || c == Object.class) return;
412427

413-
// check supertypes for annotated fields first
414-
getAnnotatedFields(c.getSuperclass(), annotationClass, fields);
428+
final Class<?> sc = c.getSuperclass();
429+
if (sc != null) {
430+
List<Field> superFields = lookupFields(sc, annotationClass);
431+
if (superFields == null) {
432+
superFields = new ArrayList<Field>();
433+
// check supertypes for annotated fields first
434+
getAnnotatedFields(sc, annotationClass, superFields);
435+
}
436+
fields.addAll(superFields);
437+
}
438+
415439
for (final Class<?> iface : c.getInterfaces()) {
416-
getAnnotatedFields(iface, annotationClass, fields);
440+
List<Field> ifaceFields = lookupFields(iface, annotationClass);
441+
if (ifaceFields == null) {
442+
ifaceFields = new ArrayList<Field>();
443+
getAnnotatedFields(iface, annotationClass, ifaceFields);
444+
}
445+
fields.addAll(ifaceFields);
417446
}
418447

419448
for (final Field f : c.getDeclaredFields()) {

0 commit comments

Comments
 (0)