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


        return newProxy(cls, methodHandler);
    }

    @SuppressWarnings("unchecked")
    private <T> T newProxy(Class<? extends Object> cls, MethodHandler methodHandler) {
        final ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setSuperclass(cls);
        proxyFactory.setInterfaces(ArrayExtensions.combine(cls.getInterfaces(), new Class<?>[] { JavassistEnhanced.class }));

        proxyFactory.setFilter(new MethodFilter() {
            @Override
            public boolean isHandled(final Method m) {
                // ignore finalize()
                return !m.getName().equals("finalize");
            }
        });

        final Class<T> proxySubclass = proxyFactory.createClass();
        try {
            final T newInstance = proxySubclass.newInstance();
            final ProxyObject proxyObject = (ProxyObject) newInstance;
            proxyObject.setHandler(methodHandler);
View Full Code Here


    }

    @SuppressWarnings("unchecked")
    public <T> T newInstance(final Class<T> cls) {

        final ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setSuperclass(cls);
        proxyFactory.setInterfaces(ArrayExtensions.combine(cls.getInterfaces(), new Class<?>[] { JavassistEnhanced.class }));

        proxyFactory.setFilter(new MethodFilter() {
            @Override
            public boolean isHandled(final Method m) {
                // ignore finalize()
                return !m.getName().equals("finalize");
            }
        });

        final Class<T> proxySubclass = proxyFactory.createClass();
        try {
            final T newInstance = proxySubclass.newInstance();
            final ProxyObject proxyObject = (ProxyObject) newInstance;
            proxyObject.setHandler(methodHandler);
View Full Code Here

   
    // //////////////////////////////////////
   
    
    private <T> T instantiateRequestScopedProxy(final Class<T> cls) {
        final ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setSuperclass(cls);
        proxyFactory.setInterfaces(ArrayExtensions.combine(cls.getInterfaces(), new Class<?>[] { RequestScopedService.class, JavassistEnhanced.class }));

        // ignore finalize()
        proxyFactory.setFilter(new MethodFilter() {
            @Override
            public boolean isHandled(final Method m) {
                return !m.getName().equals("finalize");
            }
        });

        @SuppressWarnings("unchecked")
        final Class<T> proxySubclass = proxyFactory.createClass();
        try {
            final T newInstance = proxySubclass.newInstance();
            final ProxyObject proxyObject = (ProxyObject) newInstance;
            proxyObject.setHandler(new MethodHandler() {
                private ThreadLocal<T> serviceByThread = new ThreadLocal<T>();
View Full Code Here

    }
   
    @SuppressWarnings("unchecked")
    private static <T> Class<T> createEnhancedClass(final Class<T> toProxyClass, final InvocationHandler handler, final Class<?>... auxiliaryTypes) {
       
        final ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setSuperclass(toProxyClass);
        proxyFactory.setInterfaces(ArrayExtensions.combine(toProxyClass.getInterfaces(), new Class<?>[]{org.apache.isis.core.metamodel.specloader.classsubstitutor.JavassistEnhanced.class}, auxiliaryTypes));

        proxyFactory.setFilter(new MethodFilter() {
            @Override
            public boolean isHandled(final Method m) {
                // ignore finalize()
                return !m.getName().equals("finalize") || m.isBridge();
            }
        });
       
        return proxyFactory.createClass();
    }
View Full Code Here

        }
    }

    private Class<?> proxyClass(Class<?> mockedType, Class<?>... ancilliaryTypes) {

        final ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setFilter(new MethodFilter() {
            @Override
            public boolean isHandled(final Method m) {
                // ignore finalize() and als bridge methods
                return !m.getName().equals("finalize") || m.isBridge();
            }
        });

        proxyFactory.setSuperclass(mockedType);
        proxyFactory.setInterfaces(ancilliaryTypes);

        return proxyFactory.createClass();

        // original cglib code:

    //        final Enhancer enhancer = new Enhancer() {
    //            @Override
View Full Code Here

              getIdentifierMethod,
              setIdentifierMethod,
              componentIdType,
              session
      );
      ProxyFactory factory = new ProxyFactory();
      factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
      factory.setInterfaces( interfaces );
      factory.setFilter( FINALIZE_FILTER );
      Class cl = factory.createClass();
      final HibernateProxy proxy = ( HibernateProxy ) cl.newInstance();
      ( ( ProxyObject ) proxy ).setHandler( instance );
      instance.constructed = true;
      return proxy;
    }
View Full Code Here

      Class persistentClass,
          Class[] interfaces) throws HibernateException {
    // note: interfaces is assumed to already contain HibernateProxy.class

    try {
      ProxyFactory factory = new ProxyFactory();
      factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
      factory.setInterfaces( interfaces );
      factory.setFilter( FINALIZE_FILTER );
      return factory.createClass();
    }
    catch ( Throwable t ) {
            LOG.error(LOG.javassistEnhancementFailed(persistentClass.getName()), t);
            throw new HibernateException(LOG.javassistEnhancementFailed(persistentClass.getName()), t);
    }
View Full Code Here

         }
      };

      Object enhancedResult = null;

      ProxyFactory f = new ProxyFactory()
      {
         @Override
         protected ClassLoader getClassLoader()
         {
            return loader;
         }
      };

      f.setUseCache(true);

      Class<?>[] hierarchy = null;
      Class<?> unwrappedInstanceType = Proxies.unwrapProxyTypes(instance.getClass(), loader);
      hierarchy = ProxyTypeInspector.getCompatibleClassHierarchy(loader, unwrappedInstanceType);
      if (hierarchy == null || hierarchy.length == 0)
         throw new IllegalArgumentException("Must specify at least one non-final type to enhance for Object: "
                  + instance + " of type " + instance.getClass());

      Class<?> first = hierarchy[0];
      if (!first.isInterface())
      {
         f.setSuperclass(Proxies.unwrapProxyTypes(first, loader));
         hierarchy = Arrays.shiftLeft(hierarchy, new Class<?>[hierarchy.length - 1]);
      }

      int index = Arrays.indexOf(hierarchy, ProxyObject.class);
      if (index >= 0)
      {
         hierarchy = Arrays.removeElementAtIndex(hierarchy, index);
      }

      if (!Proxies.isProxyType(first) && !Arrays.contains(hierarchy, ForgeProxy.class))
         hierarchy = Arrays.append(hierarchy, ForgeProxy.class);

      if (hierarchy.length > 0)
         f.setInterfaces(hierarchy);

      f.setFilter(filter);

      Class<?> c = f.createClass();

      try
      {
         enhancedResult = c.newInstance();
      }
View Full Code Here

         }
      };

      Object enhancedResult = null;

      ProxyFactory f = new ProxyFactory();

      f.setUseCache(true);

      if (type.isInterface() && !ForgeProxy.class.isAssignableFrom(type))
         f.setInterfaces(new Class<?>[] { type, ForgeProxy.class });
      else if (type.isInterface())
         f.setInterfaces(new Class<?>[] { type });
      else
      {
         if (Proxies.isProxyType(type))
            f.setSuperclass(unwrapProxyTypes(type));
         else
         {
            f.setSuperclass(type);
            f.setInterfaces(new Class<?>[] { ForgeProxy.class });
         }
      }

      f.setFilter(filter);
      Class<?> c;
      try
      {
         c = f.createClass();
      }
      catch (RuntimeException e)
      {
         throw e;
      }
View Full Code Here

                     }
                  };

                  Object enhancedResult = null;

                  ProxyFactory f = new ProxyFactory()
                  {
                     @Override
                     protected ClassLoader getClassLoader0()
                     {
                        ClassLoader result = callingLoader;
                        if (!ClassLoaders.containsClass(result, ProxyObject.class))
                           result = super.getClassLoader0();
                        return result;
                     };
                  };

                  f.setUseCache(true);

                  Class<?> first = hierarchy[0];
                  if (!first.isInterface())
                  {
                     f.setSuperclass(Proxies.unwrapProxyTypes(first, callingLoader, delegateLoader));
                     hierarchy = Arrays.shiftLeft(hierarchy, new Class<?>[hierarchy.length - 1]);
                  }

                  int index = Arrays.indexOf(hierarchy, ProxyObject.class);
                  if (index >= 0)
                  {
                     hierarchy = Arrays.removeElementAtIndex(hierarchy, index);
                  }

                  if (!Proxies.isProxyType(first) && !Arrays.contains(hierarchy, ForgeProxy.class))
                     hierarchy = Arrays.append(hierarchy, ForgeProxy.class);

                  if (hierarchy.length > 0)
                     f.setInterfaces(hierarchy);

                  f.setFilter(filter);
                  Class<?> c = f.createClass();
                  enhancedResult = c.newInstance();

                  try
                  {
                     ((ProxyObject) enhancedResult)
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.