Create a No-op InvocationHandler

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.


Depending on the context, this might have a high cost because this might not be inlinable by the JVM. If you're doing this on a Java server, inlining will matter, and permgen size won't, but if you're doing this on Android, inlining might not matter much, but permgen size (or rather all the redundant methods that would result from all of these useless empty implementations) will matter.

If this is on a Java server, you could probably write an annotation processor that could automatically generate all the empty implementations you might need.

However, I'd just use @Nonnull annotations everywhere, and force the null check before use.

Need Your Help

Xamarin forms signed apk

xamarin cross-platform

I am using Xamarin trail version.I build one sample application with two entry fields and one button field in xamarin cross platform. When i build and run the app with the USB cable it works fine w...

error: ‘REPLACE_INVALID_UTF8’ is not a member of ‘v8::String’

node.js npm raspbian raspberry-pi2 node-gyp

So I wanted to build this nodejs tool in Raspbian. So as the instructions say I need to npm install I go ahead and install node with apt-get: