Source Code : Creating a Proxy Object

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Creating a Proxy Object

   

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

interface MyInterface {
  void method();
}

class MyInterfaceImpl implements MyInterface {
  public void method() {
    System.out.println("method");
  }
}

class ProxyClass implements InvocationHandler {
  Object obj;

  public ProxyClass(Object o) {
    obj = o;
  }

  public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
    Object result = null;
    try {
      System.out.println("before the method is called ");
      result = m.invoke(obj, args);
    } catch (Exception eBj) {
    } finally {
      System.out.println("after the method is called"); 
    }
    return result;
  }
}

public class Main {
  public static void main(String[] argv) throws Exception {
    MyInterface myintf = (MyInterface) Proxy.newProxyInstance(MyInterface.class.getClassLoader(),
        new Class[] { MyInterface.class }, new ProxyClass(new MyInterfaceImpl()));
    myintf.method();
  }
}

   
    
    
  

Thank with us