📌  相关文章
📜  Java的.lang.reflect.Proxy类在Java中

📅  最后修改于: 2022-05-13 01:54:29.545000             🧑  作者: Mango

Java的.lang.reflect.Proxy类在Java中

代理类存在于Java.lang 包中。代理类具有某些用于创建动态代理类和实例的方法,这些方法创建的所有类都充当该代理类的子类。

类声明:

public class Proxy
    extends Object
        implements Serializable

领域:

protected InvocationHandler h

它处理此代理实例的调用。

构造函数:



protected Proxy(InvocationHandler h) 

从通常是动态代理类的子类构造 Proxy 实例。使用此调用处理程序 h 的所需值自动创建实例。

创建调用处理程序:

Java
// Invocation handler implementation
import java.lang.reflect.InvocationHandler;
  
class demoInvocationHandler implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable
    {
        return null;
    }
}
  
public class GFG {
  
    public static void main(String[] args)
    {
        InvocationHandler h = new demoInvocationHandler();
    }
}


Java
// getInvocationHandler() Method implementation
  
//Import required librabries
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
  
// demoInvocationHandler class
class demoInvocationHandler implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable
    {
        return null;
    }
}
  
// demoInterface
interface demoInterface {
}
  
// Driver code
public class GFG {
  
    public static void main(String[] args)
    {
        // Object created
        InvocationHandler h = new demoInvocationHandler();
  
        // ProxyClass objects stored in list
        List proxyClass = (List)Proxy.newProxyInstance(
            GFG.class.getClassLoader(),
            new Class[] { List.class },
            new demoInvocationHandler());
        System.out.println(
            Proxy.getInvocationHandler(proxyClass));
    }
}


Java
// getProxyClass() Method implementation
  
//Import required librabries
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
  
// demoInvocationHandler class
class demoInvocationHandler implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable
    {
        return null;
    }
}
  
// demoInterface
interface demoInterface {
}
  
// demo class
class demo {
}
  
// driver code
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Object created
        InvocationHandler h = new demoInvocationHandler();
  
        @SuppressWarnings("deprecation")
        Class proxyClass = (Class)Proxy.getProxyClass(
            demo.class.getClassLoader(),
            new Class[] { demoInterface.class });
        System.out.println("Program executed successfully");
    }
}


Java
// isProxyClass() Method implementation
  
// Import required librabries
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
  
// demoInvocationHandler class
class demoInvocationHandler implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable
    {
        return null;
    }
}
  
// demoInterface
interface demoInterface {
}
  
// demo class
class demo {
}
  
// Driver code
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Object created
        InvocationHandler h = new demoInvocationHandler();
  
        @SuppressWarnings("deprecation")
        Class proxyClass = (Class)Proxy.getProxyClass(
            demo.class.getClassLoader(),
            new Class[] { demoInterface.class });
        System.out.println(Proxy.isProxyClass(proxyClass));
    }
}


Java
// newProxyInstance() Method implementation
  
// Import required librabries
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
  
// demoInvocationHandler class
class demoInvocationHandler implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable
    {
        return null;
    }
}
  
// demoInterface
interface demoInterface {
}
  
// driver code
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Object created
        InvocationHandler h = new demoInvocationHandler();
        List proxyClass = (List)Proxy.newProxyInstance(
            GFG.class.getClassLoader(),
            new Class[] { List.class },
            new demoInvocationHandler());
        System.out.println(
            "Proxy object returned successfully");
    }
}


方法

MethodDescription
getInvocationHandler(Object proxy)This method returns the invocation handler for the specified proxy instance.
getProxyClass(ClassLoader loader, Class… interfaces)This method returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces.
isProxyClass(Class cl)This method returns true if and only if the specified class was dynamically generated to be a proxy class using the getProxyClass method or the newProxyInstance method.
newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h)This method returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.

1.静态InvocationHandler getInvocationHandler(Object proxy):

返回此代理实例的调用处理程序。

Java

// getInvocationHandler() Method implementation
  
//Import required librabries
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
  
// demoInvocationHandler class
class demoInvocationHandler implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable
    {
        return null;
    }
}
  
// demoInterface
interface demoInterface {
}
  
// Driver code
public class GFG {
  
    public static void main(String[] args)
    {
        // Object created
        InvocationHandler h = new demoInvocationHandler();
  
        // ProxyClass objects stored in list
        List proxyClass = (List)Proxy.newProxyInstance(
            GFG.class.getClassLoader(),
            new Class[] { List.class },
            new demoInvocationHandler());
        System.out.println(
            Proxy.getInvocationHandler(proxyClass));
    }
}
输出
demoInvocationHandler@378fd1ac

2.静态Class getProxyClass(ClassLoader loader, Class... interfaces):



返回代理类的Java.lang.Class 对象。代理类将由所需的类加载器定义,并且可以实现所有提供的接口。

Java

// getProxyClass() Method implementation
  
//Import required librabries
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
  
// demoInvocationHandler class
class demoInvocationHandler implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable
    {
        return null;
    }
}
  
// demoInterface
interface demoInterface {
}
  
// demo class
class demo {
}
  
// driver code
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Object created
        InvocationHandler h = new demoInvocationHandler();
  
        @SuppressWarnings("deprecation")
        Class proxyClass = (Class)Proxy.getProxyClass(
            demo.class.getClassLoader(),
            new Class[] { demoInterface.class });
        System.out.println("Program executed successfully");
    }
}
输出
Program executed successfully

3. static boolean isProxyClass(Class cl):

如果使用 getProxyClass 方法或 newProxyInstance 方法将此类动态生成为代理类,则返回 true,否则返回 false。

Java

// isProxyClass() Method implementation
  
// Import required librabries
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
  
// demoInvocationHandler class
class demoInvocationHandler implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable
    {
        return null;
    }
}
  
// demoInterface
interface demoInterface {
}
  
// demo class
class demo {
}
  
// Driver code
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Object created
        InvocationHandler h = new demoInvocationHandler();
  
        @SuppressWarnings("deprecation")
        Class proxyClass = (Class)Proxy.getProxyClass(
            demo.class.getClassLoader(),
            new Class[] { demoInterface.class });
        System.out.println(Proxy.isProxyClass(proxyClass));
    }
}
输出
true

4.静态对象newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h):

返回由类加载器定义的代理类的代理对象。它实现了所有必需的接口。

Java

// newProxyInstance() Method implementation
  
// Import required librabries
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
  
// demoInvocationHandler class
class demoInvocationHandler implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable
    {
        return null;
    }
}
  
// demoInterface
interface demoInterface {
}
  
// driver code
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Object created
        InvocationHandler h = new demoInvocationHandler();
        List proxyClass = (List)Proxy.newProxyInstance(
            GFG.class.getClassLoader(),
            new Class[] { List.class },
            new demoInvocationHandler());
        System.out.println(
            "Proxy object returned successfully");
    }
}
输出
Proxy object returned successfully

Proxy 类继承了Java.lang.Object 类的所有方法。