View
7.254
Download
4
Category
Tags:
Preview:
DESCRIPTION
How to create Java proxy classes in runtime.
Citation preview
Understanding Dynamic ProxiesHow to create proxy classes in runtime
OsocoRafael Luque
Introduction
Contents
1 Introduction
2 Creating a Proxy ClassProxy Class Properties
3 Creating a Proxy Instance
4 Examples
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 2 / 17
Introduction
Introduction
Dynamic Proxy ClassA class that implements a list of interfaces specified at runtime, suchthat a method invocation through one of the interfaces on an instanceof the class will be encoded and dispatched to another object througha uniform interface.
Usage
Create a proxy object for a list of interfaces without writing the proxyclass at compile-time.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 3 / 17
Introduction
Introduction
Dynamic Proxy ClassA class that implements a list of interfaces specified at runtime, suchthat a method invocation through one of the interfaces on an instanceof the class will be encoded and dispatched to another object througha uniform interface.
Usage
Create a proxy object for a list of interfaces without writing the proxyclass at compile-time.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 3 / 17
Creating a Proxy Class
Contents
1 Introduction
2 Creating a Proxy ClassProxy Class Properties
3 Creating a Proxy Instance
4 Examples
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 4 / 17
Creating a Proxy Class
Creating a Proxy Class
• Proxy classes and instances are created using static methods ofjava.lang.reflect.Proxy.
• Proxy.getProxyClass() returns the java.lang.Classobject for a proxy class given a class loader and an array ofinterfaces.
• The proxy class will be defined in the specified class loader andwill implement all the interfaces.
• Dynamic Proxy Class API implementations keep a cache ofgenerated proxy classes:
• If a proxy class for the same permutation of interfaces has alreadybeen defined by the class loader, then the existing proxy class willbe returned; otherwise, a proxy class for those interfaces will begenerated dynamically.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 5 / 17
Creating a Proxy Class
Creating a Proxy Class
• Proxy classes and instances are created using static methods ofjava.lang.reflect.Proxy.
• Proxy.getProxyClass() returns the java.lang.Classobject for a proxy class given a class loader and an array ofinterfaces.
• The proxy class will be defined in the specified class loader andwill implement all the interfaces.
• Dynamic Proxy Class API implementations keep a cache ofgenerated proxy classes:
• If a proxy class for the same permutation of interfaces has alreadybeen defined by the class loader, then the existing proxy class willbe returned; otherwise, a proxy class for those interfaces will begenerated dynamically.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 5 / 17
Creating a Proxy Class
Creating a Proxy Class
• Proxy classes and instances are created using static methods ofjava.lang.reflect.Proxy.
• Proxy.getProxyClass() returns the java.lang.Classobject for a proxy class given a class loader and an array ofinterfaces.
• The proxy class will be defined in the specified class loader andwill implement all the interfaces.
• Dynamic Proxy Class API implementations keep a cache ofgenerated proxy classes:
• If a proxy class for the same permutation of interfaces has alreadybeen defined by the class loader, then the existing proxy class willbe returned; otherwise, a proxy class for those interfaces will begenerated dynamically.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 5 / 17
Creating a Proxy Class
Creating a Proxy Class
• Proxy classes and instances are created using static methods ofjava.lang.reflect.Proxy.
• Proxy.getProxyClass() returns the java.lang.Classobject for a proxy class given a class loader and an array ofinterfaces.
• The proxy class will be defined in the specified class loader andwill implement all the interfaces.
• Dynamic Proxy Class API implementations keep a cache ofgenerated proxy classes:
• If a proxy class for the same permutation of interfaces has alreadybeen defined by the class loader, then the existing proxy class willbe returned; otherwise, a proxy class for those interfaces will begenerated dynamically.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 5 / 17
Creating a Proxy Class
Creating a Proxy Class
• Proxy classes and instances are created using static methods ofjava.lang.reflect.Proxy.
• Proxy.getProxyClass() returns the java.lang.Classobject for a proxy class given a class loader and an array ofinterfaces.
• The proxy class will be defined in the specified class loader andwill implement all the interfaces.
• Dynamic Proxy Class API implementations keep a cache ofgenerated proxy classes:
• If a proxy class for the same permutation of interfaces has alreadybeen defined by the class loader, then the existing proxy class willbe returned; otherwise, a proxy class for those interfaces will begenerated dynamically.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 5 / 17
Creating a Proxy Class
Proxy.getProxyClass Method
Proxy.getProxyClass() methodpublic static Class getProxyClass(
ClassLoader loader,Class[] interfaces)
throws IllegalArgumentException
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 6 / 17
Creating a Proxy Class Proxy Class Properties
Proxy Class Properties
• Proxy classes are public, final, and not abstract.• A proxy class extends java.lang.reflect.Proxy.• The unqualified name of a proxy class is unspecified.• If a proxy class implements a non-public interface, then it will be
defined in the same package as that interface. Otherwise, thepackage of a proxy class is also unspecified.
• A proxy class implements exactly the interfaces specified at itscreation, in the same order.
• The Proxy.isProxyClass will return true if it is passed a proxyclass.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
Creating a Proxy Class Proxy Class Properties
Proxy Class Properties
• Proxy classes are public, final, and not abstract.• A proxy class extends java.lang.reflect.Proxy.• The unqualified name of a proxy class is unspecified.• If a proxy class implements a non-public interface, then it will be
defined in the same package as that interface. Otherwise, thepackage of a proxy class is also unspecified.
• A proxy class implements exactly the interfaces specified at itscreation, in the same order.
• The Proxy.isProxyClass will return true if it is passed a proxyclass.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
Creating a Proxy Class Proxy Class Properties
Proxy Class Properties
• Proxy classes are public, final, and not abstract.• A proxy class extends java.lang.reflect.Proxy.• The unqualified name of a proxy class is unspecified.• If a proxy class implements a non-public interface, then it will be
defined in the same package as that interface. Otherwise, thepackage of a proxy class is also unspecified.
• A proxy class implements exactly the interfaces specified at itscreation, in the same order.
• The Proxy.isProxyClass will return true if it is passed a proxyclass.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
Creating a Proxy Class Proxy Class Properties
Proxy Class Properties
• Proxy classes are public, final, and not abstract.• A proxy class extends java.lang.reflect.Proxy.• The unqualified name of a proxy class is unspecified.• If a proxy class implements a non-public interface, then it will be
defined in the same package as that interface. Otherwise, thepackage of a proxy class is also unspecified.
• A proxy class implements exactly the interfaces specified at itscreation, in the same order.
• The Proxy.isProxyClass will return true if it is passed a proxyclass.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
Creating a Proxy Class Proxy Class Properties
Proxy Class Properties
• Proxy classes are public, final, and not abstract.• A proxy class extends java.lang.reflect.Proxy.• The unqualified name of a proxy class is unspecified.• If a proxy class implements a non-public interface, then it will be
defined in the same package as that interface. Otherwise, thepackage of a proxy class is also unspecified.
• A proxy class implements exactly the interfaces specified at itscreation, in the same order.
• The Proxy.isProxyClass will return true if it is passed a proxyclass.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
Creating a Proxy Class Proxy Class Properties
Proxy Class Properties
• Proxy classes are public, final, and not abstract.• A proxy class extends java.lang.reflect.Proxy.• The unqualified name of a proxy class is unspecified.• If a proxy class implements a non-public interface, then it will be
defined in the same package as that interface. Otherwise, thepackage of a proxy class is also unspecified.
• A proxy class implements exactly the interfaces specified at itscreation, in the same order.
• The Proxy.isProxyClass will return true if it is passed a proxyclass.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
Creating a Proxy Instance
Contents
1 Introduction
2 Creating a Proxy ClassProxy Class Properties
3 Creating a Proxy Instance
4 Examples
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 8 / 17
Creating a Proxy Instance
Invocation Handler
• Each proxy instance has an associated invocation handler object,which implements the interfacejava.lang.reflect.InvocationHandler.
• A method invocation on a proxy instance will be dispatched to theinvoke method of the instance’s invocation handler.
• Invoke method receives the proxy instance, ajava.lang.reflect.Method object identifying the methodthat was invoked and an array of type Object containing thearguments.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 9 / 17
Creating a Proxy Instance
Invocation Handler
• Each proxy instance has an associated invocation handler object,which implements the interfacejava.lang.reflect.InvocationHandler.
• A method invocation on a proxy instance will be dispatched to theinvoke method of the instance’s invocation handler.
• Invoke method receives the proxy instance, ajava.lang.reflect.Method object identifying the methodthat was invoked and an array of type Object containing thearguments.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 9 / 17
Creating a Proxy Instance
Invocation Handler
• Each proxy instance has an associated invocation handler object,which implements the interfacejava.lang.reflect.InvocationHandler.
• A method invocation on a proxy instance will be dispatched to theinvoke method of the instance’s invocation handler.
• Invoke method receives the proxy instance, ajava.lang.reflect.Method object identifying the methodthat was invoked and an array of type Object containing thearguments.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 9 / 17
Creating a Proxy Instance
Proxy Instance I
• Each proxy class has one public constructor that takes asargument an implementation of the interfaceInvocationHandler.
• You can instantiate the proxy class using the reflection API:
Proxy for the Foo interfaceClass proxyClass = Proxy.getProxyClass(
Foo.class.getClassLoader(), new Class[] { Foo.class });InvocationHandler handler = new MyInvocationHandler(...);Foo f = (Foo) proxyClass.
getConstructor(new Class[] { InvocationHandler.class }).newInstance(new Object[] { handler });
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 10 / 17
Creating a Proxy Instance
Proxy Instance II
• Better, you can use the Proxy.newProxyInstance method:
Proxy using Proxy.newProxyInstanceInvocationHandler handler = new MyInvocationHandler(...);Foo f = (Foo) Proxy.newProxyInstance(
Foo.class.getClassLoader(),new Class[] { Foo.class },handler);
• This method combines the actions of callingProxy.getProxyClass with invoking the constructor with aninvocation handler.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 11 / 17
Creating a Proxy Instance
Proxy Instance Properties I
• Given a proxy instance proxy and one of the interfacesimplemented by its proxy class Foo, the following expression willreturn true:
proxy instanceof Foo
and the following cast operation will succeed:
(Foo) proxy
• The static Proxy.getInvocationHandler method will returnthe invocation handler associated with the proxy instance passedas its argument.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 12 / 17
Creating a Proxy Instance
Proxy Instance Properties II
• An interface method invocation on a proxy instance will beencoded and dispatched to the invocation handler’s invokemethod.
• An invocation of the hashCode, equals, or toString methodsdeclared in java.lang.Object on a proxy instance will be alsoencoded and dispatched.
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 13 / 17
Examples
Contents
1 Introduction
2 Creating a Proxy ClassProxy Class Properties
3 Creating a Proxy Instance
4 Examples
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 14 / 17
Examples
DebugProxy Example I
A proxy that prints out a message before and after each methodinvocation on an object that implements an arbitrary list of interfaces.
1 import java . lang . r e f l e c t . Proxy ;2
3 public class DebugProxy4 implements java . lang . r e f l e c t . Invocat ionHand ler {5
6 private Object ob j ;7
8 public s t a t i c Object newInstance ( Object ob j ) {9 return Proxy . newProxyInstance (
10 ob j . getClass ( ) . getClassLoader ( ) ,11 ob j . getClass ( ) . g e t I n t e r f a c e s ( ) ,12 new DebugProxy ( ob j ) ) ;13 }14
15 private DebugProxy ( Object ob j ) {16 th is . ob j = ob j ;17 }18
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 15 / 17
Examples
DebugProxy Example II
19 public Object invoke ( Object proxy , Method m, Object [ ] args )20 throws Throwable {21
22 Object r e s u l t ;23 t ry {24 System . out . p r i n t l n ( ‘ ‘ before method ’ ’ ) ;25 r e s u l t = m. invoke ( obj , args ) ;26 } catch ( Invoca t ionTarge tExcept ion e ) {27 throw e . getTargetExcept ion ( ) ;28 } catch ( Except ion e ) {29 throw new RuntimeException ( ) ;30 } f i n a l l y {31 System . out . p r i n t l n ( ‘ ‘ a f t e r method ’ ’ ) ;32 }33 return r e s u l t ;34
35 }36
37 }
Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 16 / 17
Understanding Dynamic ProxiesHow to create proxy classes in runtime
OsocoRafael Luque
Recommended