java.lang.reflect.Proxy Class in Java
Last Updated :
20 Jul, 2022
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));
}
}
OutputdemoInvocationHandler@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");
}
}
OutputProgram 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));
}
}
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");
}
}
OutputProxy object returned successfully
Proxy class inherits all the methods of java.lang.Object class.
Similar Reads
java.lang.reflect.Method Class in Java
java.lang.reflect.Method class provides necessary details about one method on a certain category or interface and provides access for the same. The reflected method could also be a category method or an instance method (including an abstract method). This class allows broadening of conversions to oc
3 min read
java.lang.reflect.Parameter Class in Java
java.lang.reflect.Parameter class provides Information about method parameters, including their name and modifiers. It also provides an alternate means of obtaining attributes for the parameter. Some useful methods of Parameter class are: public int getModifiers(): It returns the modifier flags for
4 min read
java.lang.reflect.Modifier Class in Java
The java.lang.reflect.Modifier class contains methods used to get information about class, member and method access modifiers. The modifiers are represented as int value with set bits at distinct positions. The int value represents different modifiers. These values are taken from the tables in secti
7 min read
java.lang.reflect.Field Class in Java
The ability of the software to analyze itself is known as Reflection. This is provided by the java.lang.reflect package and elements in Class .Field serves the same purpose as the whole reflection mechanism, analyze a software component and describe its capabilities dynamically, at run time rather t
5 min read
java.lang.ref.Reference Class in Java
java.lang.ref.Reference Class is an abstract base class for reference object. This class contains methods used to get information about the reference objects. This class is not a direct subclass because the operations on the reference objects are in close co-operation with the garbage collector. Cla
3 min read
java.net.Proxy Class in Java
A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are
3 min read
java.lang.reflect.Constructor Class in Java
java.lang.reflect.Constructor class is used to manage the constructor metadata like the name of the constructors, parameter types of constructors, and access modifiers of the constructors. We can inspect the constructors of classes and instantiate objects at runtime. The Constructor[] array will hav
4 min read
java.lang.reflect.ReflectPermission Class in Java
ReflectPermission class extends BasicPermission class. It is a ânamedâ permission i.e it contains a name but no action. It may implement actions on top of BasicPermission, if desired. It is used to get information about the behaviour of Constructors. ConstructorsDescriptionReflectPermission(String n
2 min read
Java.lang.Package Class in Java
In Java, the package class was introduced in JDK 1.2 to encapsulate version data associated with a package. As the number of packages increases, knowing the version of the package has become important. This versioning information is retrieved and made available by the ClassLoader instance that loade
9 min read
java.lang.MethodType Class in Java
MethodType is a Class that belongs to java.lang package. This class consists of various types of methods that help in most of cases to find the method type based on the input object, or parameters of the method. All the instances of methodType are immutable. This means the objects of the methodType
4 min read