Java Doc for ProxyFactory.java in  » Byte-Code » Javassist » javassist » util » proxy » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Byte Code » Javassist » javassist.util.proxy 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   javassist.util.proxy.ProxyFactory

ProxyFactory
public class ProxyFactory (Code)
Factory of dynamic proxy classes.

This factory generates a class that extends the given super class and implements the given interfaces. The calls of the methods inherited from the super class are forwarded and then invoke() is called on the method handler associated with the generated class. The calls of the methods from the interfaces are also forwarded to the method handler.

For example, if the following code is executed,

     ProxyFactory f = new ProxyFactory();
     f.setSuperclass(Foo.class);
     MethodHandler mi = new MethodHandler() {
     public Object invoke(Object self, Method m, Method proceed,
     Object[] args) throws Throwable {
     System.out.println("Name: " + m.getName());
     return proceed.invoke(self, args);  // execute the original method.
     }
     };
     f.setFilter(new MethodFilter() {
     public boolean isHandled(Method m) {
     // ignore finalize()
     return !m.getName().equals("finalize");
     }
     });
     Class c = f.createClass();
     Foo foo = (Foo)c.newInstance();
     ((ProxyObject)foo).setHandler(mi);
     

Then, the following method call will be forwarded to MethodHandler mi and prints a message before executing the originally called method bar() in Foo.

     foo.bar();
     

The last three lines of the code shown above can be replaced with a call to the helper method create, which generates a proxy class, instantiates it, and sets the method handler of the instance:

     :
     Foo foo = (Foo)f.create(new Class[0], new Object[0], mi);
     

To change the method handler during runtime, execute the following code:

     MethodHandler mi2 = ... ;    // another handler
     ((ProxyObject)foo).setHandler(mi2);
     

You can also specify the default method handler:

     ProxyFactory f2 = new ProxyFactory();
     f2.setSuperclass(Foo.class);
     f2.setHandler(mi);            // set the default handler
     Class c2 = f2.createClass();
     

The default handler is implicitly attached to an instance of the generated class c2. Calling setHandler on the instance is not necessary unless another method handler must be attached to the instance.

The following code is an example of method handler. It does not execute anything except invoking the original method:

     class SimpleHandler implements MethodHandler {
     public Object invoke(Object self, Method m,
     Method proceed, Object[] args) throws Exception {
     return proceed.invoke(self, args);
     }
     }
     

A proxy object generated by ProxyFactory is serializable if its super class or interfaces implement a java.io.Serializable. However, a serialized proxy object will not be compatible with future releases. The serialization support should be used for short-term storage or RMI.
See Also:   MethodHandler
since:
   3.1


Inner Class :static class CacheKey
Inner Class :public static interface ClassLoaderProvider

Field Summary
public static  ClassLoaderProviderclassLoaderProvider
     A provider used by createClass() for obtaining a class loader.
public static  booleanuseCache
     If true, a generated proxy class is cached and it will be reused when generating the proxy class with the same properties is requested.
public  StringwriteDirectory
     If the value of this variable is not null, the class file of the generated proxy class is written under the directory specified by this variable.

Constructor Summary
public  ProxyFactory()
     Constructs a factory of proxy class.

Method Summary
public  Objectcreate(Class[] paramTypes, Object[] args, MethodHandler mh)
     Creates a proxy class and returns an instance of that class.
public  Objectcreate(Class[] paramTypes, Object[] args)
     Creates a proxy class and returns an instance of that class.
public  ClasscreateClass()
     Generates a proxy class.
protected  ClassLoadergetClassLoader()
    
protected  ClassLoadergetClassLoader0()
    
protected  ProtectionDomaingetDomain()
    
static  MethodFiltergetFilter(Class clazz)
    
static  MethodHandlergetHandler(Class clazz)
    
public  Class[]getInterfaces()
     Obtains the interfaces set by setInterfaces.
public  ClassgetSuperclass()
     Obtains the super class set by setSuperclass().
public  voidsetFilter(MethodFilter mf)
     Sets a filter that selects the methods that will be controlled by a handler.
public  voidsetHandler(MethodHandler mi)
     Sets the default invocation handler.
public  voidsetInterfaces(Class[] ifs)
     Sets the interfaces of a proxy class.
public  voidsetSuperclass(Class clazz)
     Sets the super class of a proxy class.

Field Detail
classLoaderProvider
public static ClassLoaderProvider classLoaderProvider(Code)
A provider used by createClass() for obtaining a class loader. get() on this ClassLoaderProvider object is called to obtain a class loader.

The value of this field can be updated for changing the default implementation.

Example:

     ProxyFactory.classLoaderProvider = new ProxyFactory.ClassLoaderProvider() {
     public ClassLoader get(ProxyFactory pf) {
     return Thread.currentThread().getContextClassLoader();
     }
     };
     

since:
   3.4



useCache
public static boolean useCache(Code)
If true, a generated proxy class is cached and it will be reused when generating the proxy class with the same properties is requested. The default value is true.
since:
   3.4



writeDirectory
public String writeDirectory(Code)
If the value of this variable is not null, the class file of the generated proxy class is written under the directory specified by this variable. For example, if the value is ".", then the class file is written under the current directory. This method is for debugging.

The default value is null.





Constructor Detail
ProxyFactory
public ProxyFactory()(Code)
Constructs a factory of proxy class.




Method Detail
create
public Object create(Class[] paramTypes, Object[] args, MethodHandler mh) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException(Code)
Creates a proxy class and returns an instance of that class.
Parameters:
  paramTypes - parameter types for a constructor.
Parameters:
  args - arguments passed to a constructor.
Parameters:
  mh - the method handler for the proxy class.
since:
   3.4



create
public Object create(Class[] paramTypes, Object[] args) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException(Code)
Creates a proxy class and returns an instance of that class.
Parameters:
  paramTypes - parameter types for a constructor.
Parameters:
  args - arguments passed to a constructor.



createClass
public Class createClass()(Code)
Generates a proxy class.



getClassLoader
protected ClassLoader getClassLoader()(Code)



getClassLoader0
protected ClassLoader getClassLoader0()(Code)



getDomain
protected ProtectionDomain getDomain()(Code)



getFilter
static MethodFilter getFilter(Class clazz)(Code)



getHandler
static MethodHandler getHandler(Class clazz)(Code)



getInterfaces
public Class[] getInterfaces()(Code)
Obtains the interfaces set by setInterfaces.
since:
   3.4



getSuperclass
public Class getSuperclass()(Code)
Obtains the super class set by setSuperclass().
since:
   3.4



setFilter
public void setFilter(MethodFilter mf)(Code)
Sets a filter that selects the methods that will be controlled by a handler.



setHandler
public void setHandler(MethodHandler mi)(Code)
Sets the default invocation handler. This invocation handler is shared among all the instances of a proxy class unless another is explicitly specified.



setInterfaces
public void setInterfaces(Class[] ifs)(Code)
Sets the interfaces of a proxy class.



setSuperclass
public void setSuperclass(Class clazz)(Code)
Sets the super class of a proxy class.



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.