Skip to content

Instantly share code, notes, and snippets.

@ncornette
Last active November 16, 2015 17:35
Show Gist options
  • Save ncornette/1e44d0d24e7eda0a38b3 to your computer and use it in GitHub Desktop.
Save ncornette/1e44d0d24e7eda0a38b3 to your computer and use it in GitHub Desktop.

Revisions

  1. ncornette revised this gist Nov 16, 2015. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions DynamicProxy.java
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,17 @@

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;

    public class DynamicProxy {

    public static <T> T create(Class<T> cls) {
    return (T) Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, new InvocationHandler() {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    return null;
    }
    });
    private static final InvocationHandler INVOKE_DO_NOTHING = new InvocationHandler() {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    return null;
    }
    };

    public static <T> T create(Class<T> ifc) {
    return (T) Proxy.newProxyInstance(ifc.getClassLoader(), new Class[]{ifc}, INVOKE_DO_NOTHING);
    }
    }
  2. ncornette created this gist Nov 16, 2015.
    16 changes: 16 additions & 0 deletions DynamicProxy.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;

    public class DynamicProxy {

    public static <T> T create(Class<T> cls) {
    return (T) Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, new InvocationHandler() {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    return null;
    }
    });
    }
    }