Skip to content

Commit 041acb4

Browse files
ctruedenclaude
andcommitted
Improve reflection usage for Java 9+
SciJava Common's core use of reflection -- dependency injection -- still requires reflection, and therefore SciJava Common is far from being JPMS-modularizable. But these fixes do help SJC run better with Java 9+. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 5fe7740 commit 041acb4

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/main/java/org/scijava/event/bushe/AbstractProxySubscriber.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.scijava.event.bushe;
22

33
import java.lang.ref.WeakReference;
4-
import java.lang.reflect.Method;
54
import java.lang.reflect.AccessibleObject;
65
import java.lang.reflect.InvocationTargetException;
6+
import java.lang.reflect.Method;
77

88
/**
99
* Common base class for EventService Proxies.
@@ -104,18 +104,26 @@ protected boolean retryReflectiveCallUsingAccessibleObject(Object[] args, Method
104104
AccessibleObject[] accessibleMethod = {subscriptionMethod};
105105
try {
106106
AccessibleObject.setAccessible(accessibleMethod, true);
107-
Object returnValue = subscriptionMethod.invoke(obj, args);
108-
return Boolean.valueOf(returnValue+"");
109107
} catch (SecurityException ex) {
108+
// SecurityManager (Java 8 and earlier) denied setAccessible
109+
accessibleTriedAndFailed = true;
110+
} catch (RuntimeException ex) {
111+
// InaccessibleObjectException (Java 9+) or similar denied setAccessible
110112
accessibleTriedAndFailed = true;
111-
} catch (InvocationTargetException e1) {
112-
throw new RuntimeException(message, e);
113-
} catch (IllegalAccessException e1) {
114-
throw new RuntimeException(message, e);
113+
}
114+
if (!accessibleTriedAndFailed) {
115+
try {
116+
Object returnValue = subscriptionMethod.invoke(obj, args);
117+
return Boolean.valueOf(returnValue+"");
118+
} catch (InvocationTargetException e1) {
119+
throw new RuntimeException(message, e);
120+
} catch (IllegalAccessException e1) {
121+
throw new RuntimeException(message, e);
122+
}
115123
}
116124
}
117125
if (accessibleTriedAndFailed) {
118-
message = message + ". An attempt was made to make the method accessible, but the SecurityManager denied the attempt.";
126+
message = message + ". An attempt was made to make the method accessible, but access was denied.";
119127
}
120128
throw new RuntimeException(message, e);
121129
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ public final class Colors {
115115
public static final ColorRGB LIGHTBLUE = new ColorRGB(173, 216, 230);
116116
public static final ColorRGB LIGHTCORAL = new ColorRGB(240, 128, 128);
117117
public static final ColorRGB LIGHTCYAN = new ColorRGB(224, 255, 255);
118-
public static final ColorRGB LIGHTGOLDENRODYELLOW = new ColorRGB(250, 250,
119-
210);
118+
public static final ColorRGB LIGHTGOLDENRODYELLOW = new ColorRGB(250, 250, 210);
120119
public static final ColorRGB LIGHTGRAY = new ColorRGB(211, 211, 211);
121120
public static final ColorRGB LIGHTGREEN = new ColorRGB(144, 238, 144);
122121
public static final ColorRGB LIGHTGREY = LIGHTGRAY;
@@ -194,19 +193,15 @@ public final class Colors {
194193
public static final ColorRGB YELLOW = new ColorRGB(255, 255, 0);
195194
public static final ColorRGB YELLOWGREEN = new ColorRGB(154, 205, 50);
196195

197-
private static final Map<String, ColorRGB> COLORS =
198-
new HashMap<>();
196+
private static final Map<String, ColorRGB> COLORS = new HashMap<>();
199197

200198
static {
201-
for (final Field f : Colors.class.getDeclaredFields()) {
199+
for (final Field f : Colors.class.getFields()) {
202200
final Object value;
203201
try {
204202
value = f.get(null);
205203
}
206-
catch (final IllegalArgumentException e) {
207-
continue;
208-
}
209-
catch (final IllegalAccessException e) {
204+
catch (final IllegalAccessException | IllegalArgumentException e) {
210205
continue;
211206
}
212207
if (!(value instanceof ColorRGB)) continue; // not a color

0 commit comments

Comments
 (0)