Understanding Java Dynamic Proxies
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Understanding Java Dynamic Proxies

on

  • 6,404 views

How to create Java proxy classes in runtime.

How to create Java proxy classes in runtime.

Statistics

Views

Total Views
6,404
Views on SlideShare
5,384
Embed Views
1,020

Actions

Likes
1
Downloads
62
Comments
0

5 Embeds 1,020

http://osoco.es 999
http://localhost 11
https://twitter.com 7
http://translate.googleusercontent.com 2
https://si0.twimg.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Understanding Java Dynamic Proxies Presentation Transcript

  • 1. Understanding Dynamic ProxiesHow to create proxy classes in runtimeOsocoRafael Luque
  • 2. IntroductionContents1 Introduction2 Creating a Proxy Class Proxy Class Properties3 Creating a Proxy Instance4 Examples Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 2 / 17
  • 3. IntroductionIntroductionDynamic 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.UsageCreate 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
  • 4. IntroductionIntroductionDynamic 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.UsageCreate 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
  • 5. Creating a Proxy ClassContents1 Introduction2 Creating a Proxy Class Proxy Class Properties3 Creating a Proxy Instance4 Examples Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 4 / 17
  • 6. Creating a Proxy ClassCreating a Proxy Class • Proxy classes and instances are created using static methods of java.lang.reflect.Proxy. • Proxy.getProxyClass() returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces. • The proxy class will be defined in the specified class loader and will implement all the interfaces. • Dynamic Proxy Class API implementations keep a cache of generated proxy classes: • If a proxy class for the same permutation of interfaces has already been defined by the class loader, then the existing proxy class will be returned; otherwise, a proxy class for those interfaces will be generated dynamically. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 5 / 17
  • 7. Creating a Proxy ClassCreating a Proxy Class • Proxy classes and instances are created using static methods of java.lang.reflect.Proxy. • Proxy.getProxyClass() returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces. • The proxy class will be defined in the specified class loader and will implement all the interfaces. • Dynamic Proxy Class API implementations keep a cache of generated proxy classes: • If a proxy class for the same permutation of interfaces has already been defined by the class loader, then the existing proxy class will be returned; otherwise, a proxy class for those interfaces will be generated dynamically. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 5 / 17
  • 8. Creating a Proxy ClassCreating a Proxy Class • Proxy classes and instances are created using static methods of java.lang.reflect.Proxy. • Proxy.getProxyClass() returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces. • The proxy class will be defined in the specified class loader and will implement all the interfaces. • Dynamic Proxy Class API implementations keep a cache of generated proxy classes: • If a proxy class for the same permutation of interfaces has already been defined by the class loader, then the existing proxy class will be returned; otherwise, a proxy class for those interfaces will be generated dynamically. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 5 / 17
  • 9. Creating a Proxy ClassCreating a Proxy Class • Proxy classes and instances are created using static methods of java.lang.reflect.Proxy. • Proxy.getProxyClass() returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces. • The proxy class will be defined in the specified class loader and will implement all the interfaces. • Dynamic Proxy Class API implementations keep a cache of generated proxy classes: • If a proxy class for the same permutation of interfaces has already been defined by the class loader, then the existing proxy class will be returned; otherwise, a proxy class for those interfaces will be generated dynamically. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 5 / 17
  • 10. Creating a Proxy ClassCreating a Proxy Class • Proxy classes and instances are created using static methods of java.lang.reflect.Proxy. • Proxy.getProxyClass() returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces. • The proxy class will be defined in the specified class loader and will implement all the interfaces. • Dynamic Proxy Class API implementations keep a cache of generated proxy classes: • If a proxy class for the same permutation of interfaces has already been defined by the class loader, then the existing proxy class will be returned; otherwise, a proxy class for those interfaces will be generated dynamically. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 5 / 17
  • 11. Creating a Proxy ClassProxy.getProxyClass MethodProxy.getProxyClass() method public static Class getProxyClass( ClassLoader loader, Class[] interfaces) throws IllegalArgumentException Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 6 / 17
  • 12. Creating a Proxy Class Proxy Class PropertiesProxy 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, the package of a proxy class is also unspecified. • A proxy class implements exactly the interfaces specified at its creation, in the same order. • The Proxy.isProxyClass will return true if it is passed a proxy class. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
  • 13. Creating a Proxy Class Proxy Class PropertiesProxy 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, the package of a proxy class is also unspecified. • A proxy class implements exactly the interfaces specified at its creation, in the same order. • The Proxy.isProxyClass will return true if it is passed a proxy class. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
  • 14. Creating a Proxy Class Proxy Class PropertiesProxy 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, the package of a proxy class is also unspecified. • A proxy class implements exactly the interfaces specified at its creation, in the same order. • The Proxy.isProxyClass will return true if it is passed a proxy class. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
  • 15. Creating a Proxy Class Proxy Class PropertiesProxy 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, the package of a proxy class is also unspecified. • A proxy class implements exactly the interfaces specified at its creation, in the same order. • The Proxy.isProxyClass will return true if it is passed a proxy class. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
  • 16. Creating a Proxy Class Proxy Class PropertiesProxy 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, the package of a proxy class is also unspecified. • A proxy class implements exactly the interfaces specified at its creation, in the same order. • The Proxy.isProxyClass will return true if it is passed a proxy class. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
  • 17. Creating a Proxy Class Proxy Class PropertiesProxy 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, the package of a proxy class is also unspecified. • A proxy class implements exactly the interfaces specified at its creation, in the same order. • The Proxy.isProxyClass will return true if it is passed a proxy class. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 7 / 17
  • 18. Creating a Proxy InstanceContents1 Introduction2 Creating a Proxy Class Proxy Class Properties3 Creating a Proxy Instance4 Examples Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 8 / 17
  • 19. Creating a Proxy InstanceInvocation Handler • Each proxy instance has an associated invocation handler object, which implements the interface java.lang.reflect.InvocationHandler. • A method invocation on a proxy instance will be dispatched to the invoke method of the instance’s invocation handler. • Invoke method receives the proxy instance, a java.lang.reflect.Method object identifying the method that was invoked and an array of type Object containing the arguments. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 9 / 17
  • 20. Creating a Proxy InstanceInvocation Handler • Each proxy instance has an associated invocation handler object, which implements the interface java.lang.reflect.InvocationHandler. • A method invocation on a proxy instance will be dispatched to the invoke method of the instance’s invocation handler. • Invoke method receives the proxy instance, a java.lang.reflect.Method object identifying the method that was invoked and an array of type Object containing the arguments. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 9 / 17
  • 21. Creating a Proxy InstanceInvocation Handler • Each proxy instance has an associated invocation handler object, which implements the interface java.lang.reflect.InvocationHandler. • A method invocation on a proxy instance will be dispatched to the invoke method of the instance’s invocation handler. • Invoke method receives the proxy instance, a java.lang.reflect.Method object identifying the method that was invoked and an array of type Object containing the arguments. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 9 / 17
  • 22. Creating a Proxy InstanceProxy Instance I • Each proxy class has one public constructor that takes as argument an implementation of the interface InvocationHandler. • You can instantiate the proxy class using the reflection API:Proxy for the Foo interface Class 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
  • 23. Creating a Proxy InstanceProxy Instance II • Better, you can use the Proxy.newProxyInstance method:Proxy using Proxy.newProxyInstance InvocationHandler handler = new MyInvocationHandler(...); Foo f = (Foo) Proxy.newProxyInstance( Foo.class.getClassLoader(), new Class[] { Foo.class }, handler); • This method combines the actions of calling Proxy.getProxyClass with invoking the constructor with an invocation handler. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 11 / 17
  • 24. Creating a Proxy InstanceProxy Instance Properties I • Given a proxy instance proxy and one of the interfaces implemented by its proxy class Foo, the following expression will return true: proxy instanceof Foo and the following cast operation will succeed: (Foo) proxy • The static Proxy.getInvocationHandler method will return the invocation handler associated with the proxy instance passed as its argument. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 12 / 17
  • 25. Creating a Proxy InstanceProxy Instance Properties II • An interface method invocation on a proxy instance will be encoded and dispatched to the invocation handler’s invoke method. • An invocation of the hashCode, equals, or toString methods declared in java.lang.Object on a proxy instance will be also encoded and dispatched. Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 13 / 17
  • 26. ExamplesContents1 Introduction2 Creating a Proxy Class Proxy Class Properties3 Creating a Proxy Instance4 Examples Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 14 / 17
  • 27. Examples DebugProxy Example I A proxy that prints out a message before and after each method invocation on an object that implements an arbitrary list of interfaces.1 import j a v a . l a n g . r e f l e c t . Proxy ;23 public class DebugProxy4 implements j a v a . l a n g . r e f l e c t . I n v o c a t i o n H a n d l e r {56 private Object obj ;78 public s t a t i c O b j e c t newInstance ( O b j e c t o b j ) {9 r e t u r n Proxy . newProxyInstance (10 o b j . g e t C l a s s ( ) . getClassLoader ( ) ,11 obj . getClass ( ) . g e t I n t e r f a c e s ( ) ,12 new DebugProxy ( o b j ) ) ;13 }1415 p r i v a t e DebugProxy ( O b j e c t o b j ) {16 this . obj = obj ;17 }18 Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 15 / 17
  • 28. Examples DebugProxy Example II19 public O b j e c t i n v o k e ( O b j e c t proxy , Method m, O b j e c t [ ] args )20 throws Throwable {2122 Object r e s u l t ;23 try {24 System . o u t . p r i n t l n ( ‘ ‘ b e f o r e method ’ ’ ) ;25 r e s u l t = m. i n v o k e ( obj , args ) ;26 } catch ( I n v o c a t i o n T a r g e t E x c e p t i o n e ) {27 throw e . g e t T a r g e t E x c e p t i o n ( ) ;28 } catch ( E x c e p t i o n e ) {29 throw new RuntimeException ( ) ;30 } finally {31 System . o u t . p r i n t l n ( ‘ ‘ a f t e r method ’ ’ ) ;32 }33 return r e s u l t ;3435 }3637 } Rafael Luque (Osoco) Java Dynamic Proxies 04/2009 16 / 17
  • 29. Understanding Dynamic ProxiesHow to create proxy classes in runtimeOsocoRafael Luque