Package javassist.util.proxy

Examples of javassist.util.proxy.ProxyFactory$CacheKey

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

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:

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

You can also specify the default method handler:

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:

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 MethodHandler @since 3.1


    * @throws Exception
    */
   public void testAbstractProxy() throws Exception
   {
      log.info("+++ testAbstractProxy");
      ProxyFactory factory = new ProxyFactory();
      AThing athing = new AThing();
      ThingMethodHandler handler = new ThingMethodHandler(athing);
      factory.setHandler(handler);
      factory.setSuperclass(AbstractThing.class);
      Class[] sig = {};
      Object[] args = {};
      AbstractThing proxy = (AbstractThing) factory.create(sig, args);
      proxy.method1();
      assertEquals("method1Count", 1, athing.getMethod1Count());
      proxy.method2("testInterfaceProxy");
      assertEquals("method2Count", 1, athing.getMethod2Count());
      proxy.method3(athing);
View Full Code Here


    * @throws Exception
    */
   public void testAbstractJDKClassProxy() throws Exception
   {
      log.info("+++ testAbstractJDKClassProxy");
      ProxyFactory factory = new ProxyFactory();
      HashSet aset = new HashSet();
      MyCollectionHandler handler = new MyCollectionHandler(aset);
      factory.setHandler(handler);
      factory.setSuperclass(java.util.AbstractCollection.class);
      Class[] sig = {};
      Object[] args = {};
      AbstractCollection proxy = (AbstractCollection) factory.create(sig, args);
      proxy.add("Add");
      assertEquals("size", 1, aset.size());
      proxy.remove("Add");
      assertEquals("size", 0, aset.size());
      assertEquals("isEmpty", true, proxy.isEmpty());
View Full Code Here

      this.manager = manager;
      for (Class<?> c : classes)
      {
         iarray[count++] = c;
      }
      ProxyFactory f = new ProxyFactory();
      Class<?> retType = method.getJavaMember().getReturnType();
      if (retType.isInterface())
      {
         f.setSuperclass(Object.class);
         Class<?>[] ifaces = { retType };
         f.setInterfaces(ifaces);
      }
      else
      {
         f.setSuperclass(retType);
      }

      f.setFilter(new MethodFilter()
      {
         public boolean isHandled(Method m)
         {
            // ignore finalize()
            return !m.getName().equals("finalize");
         }
      });
      proxyClass = cast(f.createClass());
   }
View Full Code Here

   public ServiceHandlerBeanLifecycle(Class<? extends T> classToImplement, Class<H> handlerClass, BeanManager manager)
   {
      handler = new ServiceHandlerManager<H>(handlerClass, manager);

      // create the proxy
      factory = new ProxyFactory();
      if (classToImplement.isInterface())
      {
         Class<?>[] interfaces = new Class[1];
         interfaces[0] = classToImplement;
         factory.setInterfaces(interfaces);
View Full Code Here

                    if (injectionTarget.getDecoratorStack().size() > 0)
                    {
                        Class<?> proxyClass = JavassistProxyFactory.getInterceptorProxyClasses().get(bean);
                        if (proxyClass == null)
                        {
                            ProxyFactory delegateFactory = JavassistProxyFactory.createProxyFactory(bean);
                            proxyClass = JavassistProxyFactory.getProxyClass(delegateFactory);
                            JavassistProxyFactory.getInterceptorProxyClasses().put(bean, proxyClass);
                        }
                        Object delegate = proxyClass.newInstance();
                        delegateHandler = new DelegateHandler(this.bean);
View Full Code Here

    {
       Object result = null;

        try
        {
            ProxyFactory pf = new ProxyFactory();
            pf.setInterfaces(new Class<?>[] {
                    Closable.class,
                    Serializable.class,
                    intf});
           
            pf.setHandler(new JmsProxyHandler(jmsComponent,intf));

            result = JavassistProxyFactory.getProxyClass(pf).newInstance();

        }
        catch (Exception e)
View Full Code Here

    {
        //Will only get called once while defining the bean, so no need to cache
        Class<?> clazz = null;
        try
        {
            ProxyFactory fact = createProxyFactory(bean);
            AbstractDecoratorMethodHandler handler = new AbstractDecoratorMethodHandler();
            fact.setHandler(handler);
            clazz = SecurityUtil.doPrivilegedCreateClass(fact);
        }
        catch(Exception e)
        {
            WebBeansUtil.throwRuntimeExceptions(e);
View Full Code Here

        try
        {
            Class<?> proxyClass = normalScopedBeanProxyClasses.get(bean);
            if (proxyClass == null)
            {
                ProxyFactory fact = createProxyFactory(bean);

                proxyClass = getProxyClass(fact);
                normalScopedBeanProxyClasses.put(bean, proxyClass);
            }
           
View Full Code Here

        try
        {
            Class<?> proxyClass = dependentScopedBeanProxyClasses.get(bean);
            if (proxyClass == null)
            {
                ProxyFactory fact = createProxyFactory(bean);
                proxyClass = getProxyClass(fact);
                dependentScopedBeanProxyClasses.put(bean, proxyClass);
            }
           
            result = proxyClass.newInstance();
View Full Code Here

        }

        Class<?>[] interfaceArray = new Class<?>[interfaceList.size()];
        interfaceArray = interfaceList.toArray(interfaceArray);

        ProxyFactory fact = new ProxyFactory();       
        fact.setInterfaces(interfaceArray);
        fact.setSuperclass(superClass);       

        // turn off caching since this is utterly broken
        // this is a static field, but we do not know who else
        // might turn it on again ...
        ProxyFactory.useCache = false;
View Full Code Here

TOP

Related Classes of javassist.util.proxy.ProxyFactory$CacheKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.