Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Seam Core JSF2 2.3.0.Final
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.jboss.seam.util
Class ProxyFactory

    java.lang.Object
      org.jboss.seam.util.ProxyFactory

    public class ProxyFactory

extends Object

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.

Since:
  3.1
See Also:
  `MethodHandler`

Nested Class Summary
`static interface` | `ProxyFactory.ClassLoaderProvider` A provider of class loaders.

Field Summary
`static ProxyFactory.ClassLoaderProvider` | `classLoaderProvider` A provider used by `createClass()` for obtaining a class loader.
`static boolean` | `useCache` If true, a generated proxy class is cached and it will be reused when generating the proxy class with the same properties is requested.
`String` | `writeDirectory` 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
`ProxyFactory()` Constructs a factory of proxy class.

Method Summary
`Object` | `create(Class[] paramTypes, Object[] args)` Creates a proxy class and returns an instance of that class.
`Object` | `create(Class[] paramTypes, Object[] args, javassist.util.proxy.MethodHandler mh)` Creates a proxy class and returns an instance of that class.
`Class` | `createClass()` Generates a proxy class.
`protected ClassLoader` | `getClassLoader()`
`protected ClassLoader` | `getClassLoader0()`
`protected ProtectionDomain` | `getDomain()`
`Class[]` | `getInterfaces()` Obtains the interfaces set by `setInterfaces`.
`Class` | `getSuperclass()` Obtains the super class set by `setSuperclass()`.
`void` | `setFilter(javassist.util.proxy.MethodFilter mf)` Sets a filter that selects the methods that will be controlled by a handler.
`void` | `setHandler(javassist.util.proxy.MethodHandler mi)` Sets the default invocation handler.
`void` | `setInterfaces(Class[] ifs)` Sets the interfaces of a proxy class.
`void` | `setSuperclass(Class clazz)` Sets the super class of a proxy class.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### writeDirectory

    public String writeDirectory

  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.

### useCache

    public static boolean useCache

  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

### classLoaderProvider

    public static ProxyFactory.ClassLoaderProvider classLoaderProvider

  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

Constructor Detail

### ProxyFactory

    public ProxyFactory()

  Constructs a factory of proxy class.

Method Detail

### setSuperclass

    public void setSuperclass(Class clazz)

  Sets the super class of a proxy class.

### getSuperclass

    public Class getSuperclass()

  Obtains the super class set by `setSuperclass()`.

  Since:
    3.4

### setInterfaces

    public void setInterfaces(Class[] ifs)

  Sets the interfaces of a proxy class.

### getInterfaces

    public Class[] getInterfaces()

  Obtains the interfaces set by `setInterfaces`.

  Since:
    3.4

### setFilter

    public void setFilter(javassist.util.proxy.MethodFilter mf)

  Sets a filter that selects the methods that will be controlled by a handler.

### createClass

    public Class createClass()

  Generates a proxy class.

### getClassLoader

    protected ClassLoader getClassLoader()

### getClassLoader0

    protected ClassLoader getClassLoader0()

### getDomain

    protected ProtectionDomain getDomain()

### create

    public Object create(Class[] paramTypes,
                         Object[] args,
                         javassist.util.proxy.MethodHandler mh)
                  throws NoSuchMethodException,
                         IllegalArgumentException,
                         InstantiationException,
                         IllegalAccessException,
                         InvocationTargetException

  Creates a proxy class and returns an instance of that class.

  Parameters:
    `paramTypes` - parameter types for a constructor.
    `args` - arguments passed to a constructor.
    `mh` - the method handler for the proxy class.
  Throws:
    `NoSuchMethodException`
    `IllegalArgumentException`
    `InstantiationException`
    `IllegalAccessException`
    `InvocationTargetException`
  Since:
    3.4

### create

    public Object create(Class[] paramTypes,
                         Object[] args)
                  throws NoSuchMethodException,
                         IllegalArgumentException,
                         InstantiationException,
                         IllegalAccessException,
                         InvocationTargetException

  Creates a proxy class and returns an instance of that class.

  Parameters:
    `paramTypes` - parameter types for a constructor.
    `args` - arguments passed to a constructor.
  Throws:
    `NoSuchMethodException`
    `IllegalArgumentException`
    `InstantiationException`
    `IllegalAccessException`
    `InvocationTargetException`

### setHandler

    public void setHandler(javassist.util.proxy.MethodHandler mi)

  Sets the default invocation handler. This invocation handler is shared among all the instances of a proxy class unless another is explicitly specified.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Seam Core JSF2 2.3.0.Final
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2012 Seam Framework. All Rights Reserved.