Home Java javaTutorial Tutorial on implementing dynamic proxy in Java

Tutorial on implementing dynamic proxy in Java

Jun 30, 2017 am 09:52 AM
invoke question

The following content is partially based on content on the Internet. I would like to express my gratitude to the original author!

The key to realizing dynamic proxy in Java is these two things: Proxy and InvocationHandler. Let’s start with the invoke method in the InvocationHandler interface and briefly explain how Java implements dynamic proxy. N i First, the integrity of the Invoke method is as follows: java code

Public Object Invoke (Object Proxy, Method Method, Object [] ARGS)
Throws Throwable
Tutorial on implementing dynamic proxy in Java
{
  1. method.invoke(obj, args);      

    return 
  2. null;                                      , First of all, guess that Method is the method of calling, that is, the method that needs to be executed; ARGS is the parameter of the method; proxy, what is this parameter? The above implementation of the invoke() method is a relatively standard form. We see that the proxy parameter is not used here. View the description of Proxy in the JDK documentation, as follows:
  3. Java code
  4. A method invocation on a proxy instance through one of its pr oxy interfaces will be dispatched to the invoke method of the instance’s invocation handler, passing the proxy instance, a java.lang.reflect.Method object identifying the method that was invoked, and an array of type Object containing the arguments.

by From this we can know that the above guess is correct, and we also know that the proxy parameter is passed an instance of the proxy class.

For the convenience of explanation, here is a simple example to implement dynamic proxy.
                                                                                                                                
public
interface Subject {

Tutorial on implementing dynamic proxy in Java
    public
  1. void request();

}




Java code Tutorial on implementing dynamic proxy in Java
  1. //Real role: Implemented the Subject’s request() method

  2. public class RealSubject implements Subject{

  3. public void request(){

  4. System.out.println("From real subject.");

  5. }

  6. }


Java code

Tutorial on implementing dynamic proxy in Java
//Implemented InvocationHandler
  1. public
  2. class DynamicSubject
  3. implements In vocationHandler

    {

  4. private Object obj;
  5. //This is the benefit of dynamic proxy. The encapsulated object is of Object type and accepts any type of object.
  6. }

  7. public DynamicSubject(Object obj)
  8. {

  9. this.obj = ob j;

  10. }

  11. // This method is not what we explicitly call
  12. public Object invoke(Object proxy, Method method, Object[] args)

    throws Throwable
  13. {
  14. System.out.println( "before calling " + method);

  15. method.invoke(obj, args); //-----> See the next article for details on this step. Java calls a certain function through the reflection mechanism You will understand the methods of each class after reading them.

  16. System.out.println(
  17. "after calling " + method);
  18. return

    null;

  19. }

  20. )

    1. //Client: generated a proxy instance and called the request() method

    2. public class Client {

    3. public static void main(String [] args) throws Throwable{

    4.                                                          Subject rs=new RealSubject();//Specify the proxy here Class

    5. InvocationHandler ds=
    6. new DynamicSubject(rs);

      Class> cls=rs.getClass();

    7. ​​​​​
    8. //The following is a one-time use Generate proxy

    9.                                                                                                                                                                                   Interfaces(), ds);

    10. /Here it can be proved through the running results that the subject is an instance of Proxy. This instance implements the Subject interface
    11. System.out.println(subject

      instanceof Proxy);

    12.        //Here It can be seen that the subject's Class class is $Proxy0. This $Proxy0 class inherits Proxy and implements the Subject interface.

    13. toString());

    14. System.out.print("The attributes in subject are: "); [] field=subject.getClass() .getDeclaredFields();

    15.                                                                 

    16.             System.out.print(
    17. "n"+
    18. " The methods in subject are: "); ().getDeclaredMethods( );

    19.                                                                
    20. System.out.println(
    21. "n"+

      "subject’s parent class is: "+subject.getClass().getSuperclass());

    22. System. out.print(

      "n"+
    23. "The interface implemented by subject is: ");
    24. Class>[] interfaces=subject.getClass().getInterfaces();

      ​​​
    25. }

    26. System.out.println("nn"+"The operation result is: "); Subject.request();

    27. }
    28. )
    29. The running results are as follows: the package name is omitted here, * **Instead of

true subject’s Class class is: class $Proxy0 The attributes in
subject are: m1, m3, m0, m2,
methods in subject are: request , hashCode, equals, toString,

subject’s parent class is: class java.lang.reflect.Proxy
subject implements the interface: cn.edu.ustc.dynamicproxy.Subject, Tutorial on implementing dynamic proxy in Java
  1. The running result is:
  2. before calling public abstract void ***.Subject.request()
  3. From real subject.
  4. after calling public abstract void ***.Subject.re quest( )
  5. PS: The information of this result is very important, at least to me. Because the root cause of my confusion about dynamic proxy is that I misunderstood the subject.request() above. At least I was confused by the surface. I did not find the connection between the subject and Proxy. I was entangled in the last call of request( ) is connected with invoke(), and how does invoke know that request exists. In fact, the above true and class $Proxy0 can solve many questions. Together with the source code of $Proxy0 mentioned below, it can completely solve the doubts about dynamic proxy.

  6. As you can see from the above code and results, we did not explicitly call the invoke() method, but this method was indeed executed. Let’s analyze the whole process:
  7. From the code in the Client, we can use the newProxyInstance method as a breakthrough. Let’s first take a look at the source code of the newProxyInstance method in the Proxy class:

Java code

  1. public static Object newProxyInstance(ClassLoader loader,  

  2.         Class>[] interfaces,  

  3.         InvocationHandler h)  

  4. throws IllegalArgumentException  

  5. {  

  6.     if (h == null) {  

  7.         throw new NullPointerException();  

  8.     }  

  9.     /* 

  10.      * Look up or generate the designated proxy class. 

  11.      */  

  12.     Class cl = getProxyClass(loader, interfaces);  

  13.     /* 

  14.      * Invoke its constructor with the designated invocation handler. 

  15.      */  

  16.     try {  

  17.            /* 

  18.             * Proxy源码开始有这样的定义: 

  19.             * private final static Class[] constructorParams = { InvocationHandler.class }; 

  20.             * cons即是形参为InvocationHandler类型的构造方法 

  21.            */  

  22.         Constructor cons = cl.getConstructor(constructorParams);  

  23.         return (Object) cons.newInstance(new Object[] { h });  

  24.     } catch (NoSuchMethodException e) {  

  25.         throw new InternalError(e.toString());  

  26.     } catch (IllegalAccessException e) {  

  27.         throw new InternalError(e.toString());  

  28.     } catch (InstantiationException e) {  

  29.         throw new InternalError(e.toString());  

  30.     } catch (InvocationTargetException e) {  

  31.         throw new InternalError(e.toString());  

  32.     }  

  33. }  



Proxy.newProxyInstance(ClassLoader loader, Class>[] interfaces, InvocationHandler h) does the following things.
(1) According to the parameters loader and interfaces calling method getProxyClass(loader, interfaces) creates the proxy class $Proxy0. The $Proxy0 class implements the interfaces interface and inherits the Proxy class.
                                (2) Instantiate $Proxy0 and pass the DynamicSubject in the constructor, and then call $Proxy0 The constructor of the parent class Proxy assigns a value to h as follows:

Java code Tutorial on implementing dynamic proxy in Java
  1. class Proxy{

  2. InvocationHandler h=null;

  3. protected Proxy(InvocationHandler h) {

  4. }

  5. ...

  6. }

Let’s take a look at the source code of $Proxy0 which inherits Proxy:


Java code

  1. public final class $Proxy0 extends Proxy implements Subject {  

  2.     private static Method m1;  

  3.     private static Method m0;  

  4.     private static Method m3;  

  5.     private static Method m2;  

  6.     static {  

  7.         try {  

  8.             m1 = Class.forName("java.lang.Object").getMethod("equals",  

  9.                     new Class[] { Class.forName("java.lang.Object") });  

  10.             m0 = Class.forName("java.lang.Object").getMethod("hashCode",  

  11.                     new Class[0]);  

  12.             m3 = Class.forName("***.RealSubject").getMethod("request",  

  13.                     new Class[0]);  

  14.             m2 = Class.forName("java.lang.Object").getMethod("toString",  

  15.                     new Class[0]);  

  16.         } catch (NoSuchMethodException nosuchmethodexception) {  

  17.             throw new NoSuchMethodError(nosuchmethodexception.getMessage());  

  18.         } catch (ClassNotFoundException classnotfoundexception) {  

  19.             throw new NoClassDefFoundError(classnotfoundexception.getMessage());  

  20.         }  

  21.     } //static  

  22.     public $Proxy0(InvocationHandler invocationhandler) {  

  23.         super(invocationhandler);  

  24.     }  

  25.     @Override  

  26.     public final boolean equals(Object obj) {  

  27.         try {  

  28.             return ((Boolean) super.h.invoke(this, m1, new Object[] { obj })) .booleanValue();  

  29.         } catch (Throwable throwable) {  

  30.             throw new UndeclaredThrowableException(throwable);  

  31.         }  

  32.     }  

  33.     @Override  

  34.     public final int hashCode() {  

  35.         try {  

  36.             return ((Integer) super.h.invoke(this, m0, null)).intValue();  

  37.         } catch (Throwable throwable) {  

  38.             throw new UndeclaredThrowableException(throwable);  

  39.         }  

  40.     }  

  41.     public final void request() {  

  42.         try {  

  43.             super.h.invoke(this, m3, null);  

  44.             return;  

  45.         } catch (Error e) {  

  46.         } catch (Throwable throwable) {  

  47.             throw new UndeclaredThrowableException(throwable);  

  48.         }  

  49.     }  

  50.     @Override  

  51.     public final String toString() {  

  52.         try {  

  53.             return (String) super.h.invoke(this, m2, null);  

  54.         } catch (Throwable throwable) {  

  55.             throw new UndeclaredThrowableException(throwable);  

  56.         }  

  57.     }  

  58. }  



Then cast the obtained $Proxy0 instance into Subject and assign the reference to subject. When the subject.request() method is executed, the request() method in the $Proxy0 class is called, and then the invoke() method of h in the parent class Proxy is called. That is, InvocationHandler.invoke().

PS: 1. One thing that needs to be explained is that the getProxyClass method in the Proxy class returns the Proxy Class class. The reason why I explain this is because I made a low-level mistake at the beginning, thinking that what was returned was the "Class of the proxied class" - -! It is recommended to take a look at the source code of getProxyClass, it is very long =. =
2. It can be seen from the source code of $Proxy0 that the dynamic proxy class not only proxies the methods in the explicitly defined interface, but also proxies the inherited equals() and hashcode() in the java root class Object. , toString(), and only these three methods.

Q: Until now, there is still a question. The first parameter in the invoke method is an instance of Proxy (to be precise, the instance of $Proxy0 is ultimately used), but what is its use? In other words, how does the program show its effect?
A: From my current level, this proxy parameter has no effect. In the entire dynamic proxy mechanism, the proxy parameter of the invoke method in InvocationHandler is not used. The parameter passed in is actually an instance of the proxy class. I think it may be to allow programmers to use reflection in the invoke method to obtain some information about the proxy class.

The above is the detailed content of Tutorial on implementing dynamic proxy in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solve the 'error: redefinition of class 'ClassName'' problem that appears in C++ code Solve the 'error: redefinition of class 'ClassName'' problem that appears in C++ code Aug 25, 2023 pm 06:01 PM

Solve the "error:redefinitionofclass'ClassName'" problem in C++ code. In C++ programming, we often encounter various compilation errors. One of the common errors is "error:redefinitionofclass 'ClassName'" (redefinition error of class 'ClassName'). This error usually occurs when the same class is defined multiple times. This article will

Clustering effect evaluation problem in clustering algorithm Clustering effect evaluation problem in clustering algorithm Oct 10, 2023 pm 01:12 PM

The clustering effect evaluation problem in the clustering algorithm requires specific code examples. Clustering is an unsupervised learning method that groups similar samples into one category by clustering data. In clustering algorithms, how to evaluate the effect of clustering is an important issue. This article will introduce several commonly used clustering effect evaluation indicators and give corresponding code examples. 1. Clustering effect evaluation index Silhouette Coefficient Silhouette coefficient evaluates the clustering effect by calculating the closeness of the sample and the degree of separation from other clusters.

What to do if win10 cannot download steam What to do if win10 cannot download steam Jul 07, 2023 pm 01:37 PM

Steam is a very popular game platform with many high-quality games, but some win10 users report that they cannot download steam. What is going on? It is very likely that the user's IPv4 server address is not set properly. To solve this problem, you can try to install Steam in compatibility mode, and then manually modify the DNS server to 114.114.114.114, and you should be able to download it later. What to do if Win10 cannot download Steam: Under Win10, you can try to install it in compatibility mode. After updating, you must turn off compatibility mode, otherwise the web page will not load. Click the properties of the program installation to run the program in compatibility mode. Restart to increase memory, power

Teach you how to diagnose common iPhone problems Teach you how to diagnose common iPhone problems Dec 03, 2023 am 08:15 AM

Known for its powerful performance and versatile features, the iPhone is not immune to the occasional hiccup or technical difficulty, a common trait among complex electronic devices. Experiencing iPhone problems can be frustrating, but usually no alarm is needed. In this comprehensive guide, we aim to demystify some of the most commonly encountered challenges associated with iPhone usage. Our step-by-step approach is designed to help you resolve these common issues, providing practical solutions and troubleshooting tips to get your equipment back in peak working order. Whether you're facing a glitch or a more complex problem, this article can help you resolve them effectively. General Troubleshooting Tips Before delving into specific troubleshooting steps, here are some helpful

Solve PHP error: problems encountered when inheriting parent class Solve PHP error: problems encountered when inheriting parent class Aug 17, 2023 pm 01:33 PM

Solving PHP errors: Problems encountered when inheriting parent classes In PHP, inheritance is an important feature of object-oriented programming. Through inheritance, we can reuse existing code and extend and improve it without modifying the original code. Although inheritance is widely used in development, sometimes you may encounter some error problems when inheriting from a parent class. This article will focus on solving common problems encountered when inheriting from a parent class and provide corresponding code examples. Question 1: The parent class is not found. During the process of inheriting the parent class, if the system does not

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

Label acquisition problem in weakly supervised learning Label acquisition problem in weakly supervised learning Oct 08, 2023 am 09:18 AM

The label acquisition problem in weakly supervised learning requires specific code examples. Introduction: Weakly supervised learning is a machine learning method that uses weak labels for training. Different from traditional supervised learning, weakly supervised learning only needs to use fewer labels to train the model, rather than each sample needs to have an accurate label. However, in weakly supervised learning, how to accurately obtain useful information from weak labels is a key issue. This article will introduce the label acquisition problem in weakly supervised learning and give specific code examples. Introduction to the label acquisition problem in weakly supervised learning:

The problem of generalization ability of machine learning models The problem of generalization ability of machine learning models Oct 08, 2023 am 10:46 AM

The generalization ability of machine learning models requires specific code examples. With the development and application of machine learning becoming more and more widespread, people are paying more and more attention to the generalization ability of machine learning models. Generalization ability refers to the prediction ability of a machine learning model on unlabeled data, and can also be understood as the adaptability of the model in the real world. A good machine learning model should have high generalization ability and be able to make accurate predictions on new data. However, in practical applications, we often encounter models that perform well on the training set, but fail on the test set or real

See all articles