How do they create proxy of a class
in java. Do they create the proxy on as needed basis or they create it and have it around forever.
Asked
Active
Viewed 1.5k times
6

javaguy
- 4,404
- 10
- 32
- 35
-
yeah, what context are you talking about? – hvgotcodes Jan 23 '11 at 22:55
-
Do you mean the proxy object or the proxy class? – maaartinus Jan 23 '11 at 22:56
-
1"They" means people/programmers :D – Maksim Jan 23 '11 at 23:02
-
"They" => java developers actually @javaguy want to ask how to create runtime class in java. For example. Arrays in java also creates class at runtime. That is called proxy class. – Akhilesh Dhar Dubey Nov 29 '13 at 06:57
-
http://stackoverflow.com/questions/933993/what-are-dynamic-proxy-classes-and-why-would-i-use-one – Akhilesh Dhar Dubey Nov 29 '13 at 07:04
2 Answers
15
You can create proxies in two basic ways:
- using the JDK mechanism, by interface. Take a look at
java.lang.reflect.Proxy
. - using some byte-code generation/manipulation library, on a concrete class. Take a look at cglib and javassist
Apache has a nice utility: commons-proxy. It wraps many mechanisms and frameworks for creating proxies.
This is all about dynamic proxies - i.e. those created at runtime. For static proxies - see wikipedia about the proxy pattern
Note that you are not making a proxy of a class - you are making a proxy of an object.

Bozho
- 588,226
- 146
- 1,060
- 1,140
3
I agree with the comments that the question is a little vague. However, I'd suggest you look at some of the mocking frameworks such as easymock and mockito. Their source code is available and their core functionality is creating proxies of class and interfaces. So they are good concrete examples of how to go about creating proxies.

drekka
- 20,957
- 14
- 79
- 135