Open In App

java.lang.reflect.Proxy Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class.

Class declaration:

public class Proxy
    extends Object
        implements Serializable

Fields:

protected InvocationHandler h

It handles the invocation for this proxy instance.

Constructor:

protected Proxy(InvocationHandler h) 

Constructs a Proxy instance from a subclass which is typically a dynamic proxy class. The instance is automatically created with the required value of this invocation handler h.

Creating Invocation Handler:

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();
    }
}


 

Methods

Method Description
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. static InvocationHandler getInvocationHandler(Object proxy): 

Returns the invocation handler for this proxy instance.

Java




// getInvocationHandler() Method implementation
 
//Import required libraries
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));
    }
}


Output

demoInvocationHandler@378fd1ac

2. static Class<?> getProxyClass(ClassLoader loader, Class<?>… interfaces): 

Returns the java.lang.Class object for a proxy class. The proxy class are going to be defined by the required class loader and can implement all the supplied interfaces.

Java




// getProxyClass() Method implementation
 
//Import required libraries
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");
    }
}


Output

Program executed successfully

3. static boolean isProxyClass(Class<?> cl): 

Returns true if this class was dynamically generated to be a proxy class using the getProxyClass method or the newProxyInstance method else returns false.

Java




// isProxyClass() Method implementation
 
// Import required libraries
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));
    }
}


Output

true

4. static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h): 

Returns a proxy object of a proxy class which is defined by the class loader. It implements all the required interfaces.

Java




// newProxyInstance() Method implementation
 
// Import required libraries
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");
    }
}


Output

Proxy object returned successfully

Proxy class inherits all the methods of java.lang.Object class.



Last Updated : 20 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads