Skip to content

Commit b782281

Browse files
committed
Nil: do not implement InvocationHandler
Rather than leaking this type, let's encapsulate it.
1 parent 61f69f6 commit b782281

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

  • scijava-types/src/main/java/org/scijava/types

‎scijava-types/src/main/java/org/scijava/types/Nil.java‎

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@
5353
*
5454
* @author Curtis Rueden
5555
*/
56-
public abstract class Nil<T> implements GenericTyped, Proxyable<T>,
57-
InvocationHandler
58-
{
56+
public abstract class Nil<T> implements GenericTyped, Proxyable<T> {
5957

6058
/** Generic type of the object. */
6159
private final TypeToken<?> typeToken;
@@ -192,26 +190,27 @@ public T proxy() {
192190
// NB: Technically, this cast is not safe, because T might not be
193191
// an interface, and thus might not be one of the proxy's types.
194192
@SuppressWarnings("unchecked")
195-
final T proxy = (T) Proxy.newProxyInstance(loader, interfaces, this);
196-
return proxy;
197-
}
198-
199-
// -- InvocationHandler methods --
193+
final T proxy = (T) Proxy.newProxyInstance(loader, interfaces,
194+
new InvocationHandler() {
195+
@Override
196+
public Object invoke(final Object proxy, final Method method,
197+
final Object[] args) throws Throwable
198+
{
199+
try {
200+
// Look for a Nil subclass method of the same signature.
201+
final Method m = callbacks.getClass().getMethod(method.getName(), //
202+
method.getParameterTypes());
203+
return m.invoke(callbacks, args);
204+
}
205+
catch (final NoSuchMethodException exc) {
206+
// NB: Default behavior is to do nothing and return null.
207+
return Classes.nullValue(method.getReturnType());
208+
}
209+
}
210+
}
211+
);
200212

201-
@Override
202-
public Object invoke(final Object proxy, final Method method,
203-
final Object[] args) throws Throwable
204-
{
205-
try {
206-
// Look for a Nil subclass method of the same signature.
207-
final Method m = callbacks.getClass().getMethod(method.getName(), //
208-
method.getParameterTypes());
209-
return m.invoke(callbacks, args);
210-
}
211-
catch (final NoSuchMethodException exc) {
212-
// NB: Default behavior is to do nothing and return null.
213-
return Classes.nullValue(method.getReturnType());
214-
}
213+
return proxy;
215214
}
216215

217216
// -- Helper methods --

0 commit comments

Comments
 (0)