I'm trying to create a no-op "listener" using Java's InvocationHandler
and Proxy
classes. I'm not really sure the implementation below will handle every scenario, nor whether it is the best solution. What I'd really like is for this class to have no effects. Is there a better way to express this concept than the below?
public class EmptyListener {
public static <T> T createEmptyListenerFor(Class<T> aInterface) throws Exception {
return (T) Proxy.newProxyInstance(aInterface.getClassLoader(), new Class[] { aInterface }, new EmptyInvocationHandler());
}
private static class EmptyInvocationHandler implements InvocationHandler {
public EmptyInvocationHandler() {}
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Class<?> returnType = method.getReturnType();
if (returnType.isPrimitive()) {
if (returnType.equals(boolean.class)) {
return false;
} else if (returnType.equals(void.class)) {
return null;
} else if ( returnType.equals(byte.class)) {
return (byte)0;
} else if ( returnType.equals(short.class)) {
return (short)0;
} else if ( returnType.equals(int.class)) {
return 0;
} else if ( returnType.equals(long.class)) {
return 0l;
} else if ( returnType.equals(float.class)) {
return 0f;
} else if ( returnType.equals(double.class)) {
return 0d;
} else if ( returnType.equals(char.class)) {
return '\u0000';
}
}
return null;
}
}
Perhaps I'm barking up the wrong tree here. Again, this is intended to be an easy way to create surrogate, placeholder, or Null objects, at initialization time for listener interfaces. The idea here is that this listener could be injected and prevent accidental calls to a null
reference. Is there some better way to accomplish what I'm trying to do here? Or has someone written something similar in an open source project that I don't know about? I'm aware of Guava's Optional
but that's not really what I want.