reflection

Create Proxy object

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 created MyProxy class,that implements the InvocationHandler. It has an Object o and a constructor where it initializes its object. It also has a method, Object invoke(Object proxy, Method m, Object[] args), that uses invoke(Object obj, Object... args) API method of Method 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, using newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) API method of Proxy.
  • Then we invoke the method of the object.

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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 handler
        MyInterface myintf = (MyInterface)Proxy.newProxyInstance(
                MyInterface.class.getClassLoader(),
                new Class[]{MyInterface.class},
                new MyProxy(new MyInterfaceImpl())
        );
 
        // Invoke the method
        myintf.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 invocation
Plain method is invoked
Proxy 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 a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
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 the Terms and Privacy Policy

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button