JDK Dynamic Proxies - Intercept method calls using dynamic Proxy

Ref : https://www.logicbig.com/tutorials/core-java-tutorial/java-dynamic-proxies/method-interceptors.html

Step 1:

public class MyInvocationHandler implements InvocationHandler {

    private String theString;

    public MyInvocationHandler (String theString) {
    this.theString = theString;
    }

    @Override
    public Object invoke (Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("before method call : " + method.getName());
    Object result = method.invoke(theString, args);
    System.out.println("after method call : " + method.getName());
    return result;
    }
Step 2: 
public class MyInterceptor<T> implements InvocationHandler {

  private T t;

  public MyInterceptor(T t) {
      this.t = t;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      System.out.println("before method call : " + method.getName());
      Object result = method.invoke(t, args);
      System.out.println("after method call : " + method.getName());
      return result;
  }

  @SuppressWarnings("unchecked")
  public static <T> T getProxy(T t, Class<? super T> interfaceType) {
      MyInterceptor handler = new MyInterceptor(t);
      return (T) Proxy.newProxyInstance(interfaceType.getClassLoader(),
              new Class<?>[]{interfaceType}, handler
      );
  }
}

Step 3: 
public class MyInterceptorExample {
  public static void main(String[] args) {
      List<String> list = MyInterceptor.getProxy(new ArrayList<>(), List.class);
      list.add("one");
      list.add("two");
      System.out.println(list);
      list.remove("one");
  }
}


Output
before method call : add
after method call : add
before method call : add
after method call : add
before method call : toString
after method call : toString
[one, two]
before method call : remove
after method call : remove

2. Another example

public class JdkProxyDemo {
 
    interface If {
        void originalMethod(String s);
    }
 
    static class Original implements If {
        public void originalMethod(String s) {
            System.out.println(s);
        }
    }
 
    static class Handler implements InvocationHandler {
        private final If original;
 
        public Handler(If original) {
            this.original = original;
        }
 
        public Object invoke(Object proxy, Method method, Object[] args)
                throws IllegalAccessException, IllegalArgumentException,
                InvocationTargetException {
            System.out.println("BEFORE");
            method.invoke(original, args);
            System.out.println("AFTER");
            return null;
        }
    }
 
    public static void main(String[] args){
        Original original = new Original();
        Handler handler = new Handler(original);
        If f = (If) Proxy.newProxyInstance(If.class.getClassLoader(),
                new Class[] { If.class },
                handler);
        f.originalMethod("Hallo");
    }
 
}

Comments

Popular posts from this blog

Fixing the DeepSpeed Import Error While Fine-Tuning the Qwen Model

Amazon Linux 2023 - User data configuration for launch templates to connect to the EKS cluster

How to create ISM policy and rotate logs in opensearch