reflection

Create Proxy object

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: August 20th, 2013
0 186 2 minutes read

With this example we are going to demonstrate how to create a Proxy object. A proxy is a class functioning as an interface to a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, to create a Proxy object we have followed the steps below:

  • We have created an interface,MyInterface with a method and the implementation of the interface,MyInterfaceImpl.
  • We have createdMyProxy class,that implements theInvocationHandler. It has anObject o and a constructor where it initializes its object. It also has a method,Object invoke(Object proxy, Method m, Object[] args), that usesinvoke(Object obj, Object... args) API method ofMethod to get the underlying method represented by this Method object, on the specified object with the specified parameters and returns this result.
  • We create an instance of a proxy class for the specified interface that dispatches method invocations to the specified invocation handler, usingnewProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) API method ofProxy.
  • Then we invoke the method of the object.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;import java.lang.reflect.InvocationHandler;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class CreateProxyObject {public static void main(String[] args) {// return an instance of a proxy class for the specified interfaces    // that dispatches method invocations to the specified invocation handlerMyInterface myintf = (MyInterface)Proxy.newProxyInstance(    MyInterface.class.getClassLoader(),    new Class[]{MyInterface.class},    new MyProxy(new MyInterfaceImpl()));// Invoke the methodmyintf.method();}private static interface MyInterface {    void method();}private static class MyInterfaceImpl implements MyInterface {    public void method() {    System.out.println("Plain method is invoked");    }}private static class MyProxy implements InvocationHandler {    Object obj;    public MyProxy(Object o) {  obj = o;    }    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {      Object result = null;   try {System.out.println("Proxy Class is called before method invocation");result = m.invoke(obj, args);  }  catch (InvocationTargetException e) {  System.out.println("Invocation Target Exception: " + e);  }  catch (Exception e) {  System.out.println("Invocation Target Exception: " + e);  }  finally {  System.out.println("Proxy Class is called after method invocation");  }  return result;      }    }}

Output:

Proxy Class is called before method invocationPlain method is invokedProxy Class is called after method invocation

 
This was an example of how to create a Proxy object in Java.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: August 20th, 2013
0 186 2 minutes read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Bipartite Graph

Get package name

November 11th, 2012
Bipartite Graph

Get super class of an object

November 11th, 2012
Bipartite Graph

Get Methods from an Object

November 11th, 2012
Bipartite Graph

Java Reflection Example

January 7th, 2014
Bipartite Graph

Get Fields from an Object

November 11th, 2012
Bipartite Graph

Get methods return type

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.