0% found this document useful (0 votes)
3 views9 pages

Proxy Class Java Notes

The Proxy class in Java, found in the java.lang.reflect package, is used to create dynamic proxy objects that act as substitutes for other objects, allowing for method interception and additional logic. It utilizes the InvocationHandler interface to handle method calls dynamically, with key methods including newProxyInstance(), getInvocationHandler(), and isProxyClass(). Proxy classes are commonly used in frameworks like Spring and Hibernate, as well as in logging, security, and remote method invocation applications.

Uploaded by

mmeena2203
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Proxy Class Java Notes

The Proxy class in Java, found in the java.lang.reflect package, is used to create dynamic proxy objects that act as substitutes for other objects, allowing for method interception and additional logic. It utilizes the InvocationHandler interface to handle method calls dynamically, with key methods including newProxyInstance(), getInvocationHandler(), and isProxyClass(). Proxy classes are commonly used in frameworks like Spring and Hibernate, as well as in logging, security, and remote method invocation applications.

Uploaded by

mmeena2203
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA PROGRAMMING

Proxy Class in Java


[Link] Class

1. Introduction
A Proxy class is present in the [Link] 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.

In simple words, a Proxy object acts as a substitute for another object. Instead of directly calling
methods on the original object, the call goes through the proxy, which can intercept it, add extra logic,
and then forward it to the real object.

Proxy Classes are mainly used in:


• Dynamic method invocation
• Framework development (Spring, Hibernate)
• Aspect-Oriented Programming (AOP)
• Remote Method Invocation (RMI)
• Logging and security systems

2. Class Declaration
public class Proxy
extends Object
implements Serializable

The Proxy class extends Object and implements Serializable. All proxy classes created using Proxy
methods are subclasses of this Proxy class.

3. Fields
protected InvocationHandler h
This field stores the invocation handler for the proxy instance. It handles all method invocations made
on this proxy object.

4. Constructor
protected Proxy(InvocationHandler h)
Constructs a Proxy instance from a subclass (typically a dynamic proxy class). The instance is
automatically created with the required value of the invocation handler h.

5. What is InvocationHandler?
InvocationHandler is an interface used to handle method calls made on proxy objects dynamically.
Whenever any method is called on a proxy object, the call is automatically forwarded to the invoke()
method of the InvocationHandler.

Syntax:
public interface InvocationHandler

Important Method — invoke():


Object invoke(Object proxy, Method method, Object[] args)
throws Throwable

Parameter Description
proxy The proxy object on which the method was called
method The method that was called
args Arguments passed to the method

6. Creating Invocation Handler


Below is how to create a custom InvocationHandler by implementing the interface:

// Invocation handler implementation


import [Link];
import [Link];

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

7. Methods of Proxy Class


Method Description
getInvocationHandler(Object proxy) Returns the invocation handler for the specified proxy instance
getProxyClass(ClassLoader loader, Returns the [Link] object for a proxy class given a
Class<?>... interfaces) class loader and an array of interfaces
isProxyClass(Class<?> cl) Returns true if the specified class was dynamically generated
to be a proxy class
newProxyInstance(ClassLoader Returns an instance of a proxy class that dispatches method
loader, Class<?>[] interfaces, invocations to the specified invocation handler
InvocationHandler h)

8. Method 1: getInvocationHandler()
Definition:
Returns the invocation handler for the specified proxy instance.

Syntax:
static InvocationHandler getInvocationHandler(Object proxy)

Example Program:
// getInvocationHandler() Method implementation
import [Link];
import [Link];
import [Link];
import [Link];

class demoInvocationHandler implements InvocationHandler {


@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
return null;
}
}

interface demoInterface { }

public class GFG {


public static void main(String[] args) {
// Object created
InvocationHandler h = new demoInvocationHandler();

// ProxyClass objects stored in list


List proxyClass = (List)[Link](
[Link](),
new Class[] { [Link] },
new demoInvocationHandler());

[Link](
[Link](proxyClass));
}
}
Output:
demoInvocationHandler@378fd1ac

9. Method 2: getProxyClass()
Definition:
Returns the [Link] object for a proxy class. The proxy class will be defined by the required
class loader and can implement all the supplied interfaces.

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

Example Program:
// getProxyClass() Method implementation
import [Link];
import [Link];
import [Link];

class demoInvocationHandler implements InvocationHandler {


@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
return null;
}
}

interface demoInterface { }

class demo { }

public class GFG {


public static void main(String[] args) {
// Object created
InvocationHandler h = new demoInvocationHandler();

@SuppressWarnings("deprecation")
Class<?> proxyClass = (Class<?>)[Link](
[Link](),
new Class[] { [Link] });

[Link]("Program executed successfully");


}
}
Output:
Program executed successfully

10. Method 3: isProxyClass()


Definition:
Returns true if this class was dynamically generated to be a proxy class using the getProxyClass
method or the newProxyInstance method. Returns false otherwise.
Syntax:
static boolean isProxyClass(Class<?> cl)

Example Program:
// isProxyClass() Method implementation
import [Link];
import [Link];
import [Link];

class demoInvocationHandler implements InvocationHandler {


@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
return null;
}
}

interface demoInterface { }

class demo { }

public class GFG {


public static void main(String[] args) {
// Object created
InvocationHandler h = new demoInvocationHandler();

@SuppressWarnings("deprecation")
Class<?> proxyClass = (Class<?>)[Link](
[Link](),
new Class[] { [Link] });

[Link]([Link](proxyClass));
}
}
Output:
true

11. Method 4: newProxyInstance()


Definition:
Returns a proxy object of a proxy class which is defined by the class loader. It implements all the
required interfaces and dispatches method invocations to the specified invocation handler.

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

Parameters:
Parameter Description
loader The class loader to define the proxy class
interfaces The list of interfaces for the proxy class to implement
h The invocation handler to dispatch method calls to

Example Program:
// newProxyInstance() Method implementation
import [Link];
import [Link];
import [Link];
import [Link];

class demoInvocationHandler implements InvocationHandler {


@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
return null;
}
}

interface demoInterface { }

public class GFG {


public static void main(String[] args) {
// Object created
InvocationHandler h = new demoInvocationHandler();

List proxyClass = (List)[Link](


[Link](),
new Class[] { [Link] },
new demoInvocationHandler());

[Link]("Proxy object returned successfully");


}
}
Output:
Proxy object returned successfully

12. How Proxy Class Works


The working of Proxy class follows these steps:

1. Create an interface that the proxy will implement


2. Create an InvocationHandler by implementing the InvocationHandler interface
3. Use newProxyInstance() to create the proxy object dynamically
4. When any method is called on the proxy, the call goes to invoke() in InvocationHandler
5. InvocationHandler processes the call (can add logic, log, or delegate)

13. Architecture of Proxy Class


Client / User Code

Proxy Object (Dynamic)


[Link]()


Real Method Execution

14. Important Point


Proxy class inherits all the methods of [Link] class.

15. Advantages of Proxy Class


• Dynamic method handling at runtime
• Supports loose coupling between classes
• Provides runtime flexibility
• Reduces code duplication
• Useful in framework development
• Easy to add logging and security checks

16. Disadvantages of Proxy Class


• Works mainly with interfaces, not concrete classes
• Uses reflection — execution may be slightly slower
• Slightly complex for beginners to understand
• Difficult to debug in large systems

17. Applications of Proxy Class


Application Use Case
Spring Framework Proxy objects used for dependency injection and AOP
Hibernate Lazy loading of database objects via proxy
RMI Remote method invocation across network
AOP Adding cross-cutting concerns like logging
Security Frameworks Access control and authentication checks
Logging Systems Intercepting method calls for logging purposes
18. Important Exam Points
Point Detail
Package [Link]
Uses InvocationHandler interface
Inherits from [Link]
Proxy objects Created dynamically at runtime
Key method to create proxy newProxyInstance()
Key method to check proxy isProxyClass()

19. Viva Questions & Answers


Q1. What is Proxy class in Java?
A class in [Link] package used to create dynamic proxy objects at runtime.

Q2. Which package contains Proxy class?


[Link] package.

Q3. What is InvocationHandler?


An interface used to handle method calls made on proxy objects dynamically through the invoke()
method.

Q4. Which method creates a proxy object?


newProxyInstance() method creates a new proxy object dynamically.

Q5. What does isProxyClass() return?


It returns true if the specified class is a dynamically generated proxy class, otherwise false.

Q6. Does Proxy class support runtime method handling?


Yes, through InvocationHandler, Proxy class handles all method calls dynamically at runtime.

Q7. What does getInvocationHandler() do?


It returns the InvocationHandler associated with a proxy instance.

Q8. Does Proxy class work with concrete classes?


No, Proxy class mainly works with interfaces. It cannot create proxies for concrete classes directly.
20. 2-Mark Answer
Proxy Class:
A Proxy class in Java is present in [Link] package and is used to create dynamic proxy
objects at runtime. It uses InvocationHandler to handle method calls dynamically.

21. 5-Mark Answer


Proxy Class — Detailed:
The Proxy class in Java is available in the [Link] package and is used to create dynamic
proxy objects during runtime. A proxy object acts as a substitute for another object, intercepting
method calls through InvocationHandler. The InvocationHandler interface contains the invoke()
method which is called whenever a method is invoked on the proxy object. Important methods of
Proxy class include getInvocationHandler() which returns the handler, getProxyClass() which
returns the proxy class object, isProxyClass() which checks whether a class is a proxy, and
newProxyInstance() which creates a new proxy object. Proxy classes are widely used in
frameworks like Spring and Hibernate, logging systems, security systems, Remote Method
Invocation (RMI), and Aspect-Oriented Programming (AOP). Proxy class inherits all methods from
[Link] class.

You might also like