Package net.sf.cglib.proxy

Examples of net.sf.cglib.proxy.Enhancer


    public BasicProxyFactoryImpl(Class superClass, Class[] interfaces) {
      if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) {
        throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" );
      }

      Enhancer en = new Enhancer();
      en.setUseCache( false );
      en.setInterceptDuringConstruction( false );
      en.setUseFactory( true );
      en.setCallbackTypes( CALLBACK_TYPES );
      en.setCallbackFilter( FINALIZE_FILTER );
      if ( superClass != null ) {
        en.setSuperclass( superClass );
      }
      if ( interfaces != null && interfaces.length > 0 ) {
        en.setInterfaces( interfaces );
      }
      proxyClass = en.createClass();
      try {
        factory = ( Factory ) proxyClass.newInstance();
      }
      catch ( Throwable t ) {
        throw new HibernateException( "Unable to build CGLIB Factory instance" );
View Full Code Here


            } else {
                theInterfaces.add(c);
            }
        }
        if (superClass != null) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(superClass);
            enhancer.setInterfaces(theInterfaces.toArray(new Class[]{}));
            enhancer.setCallback(new MethodInterceptor() {

                public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
                    throws Throwable {
                    return h.invoke(obj, method, args);
                }
               
            });
            return enhancer.create();
        } else {
            return super.getProxyInternal(loader, interfaces, h);
        }
    }
View Full Code Here

        callbackTypes[i] = net.sf.cglib.proxy.MethodInterceptor.class;
      }
    }

    // Create the proxied class.
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(declaringClass);
    enhancer.setUseCache(false); // We do enough caching.
    enhancer.setCallbackFilter(new CallbackFilter() {
      public int accept(Method method) {
        return indices.get(method);
      }
    });
    enhancer.setCallbackTypes(callbackTypes);
    enhancer.setUseFactory(false);
    enhancer.setNamingPolicy(new GuiceNamingPolicy());

    Class<?> proxied = enhancer.createClass();

    // Store callbacks.
    Enhancer.registerStaticCallbacks(proxied, callbacks);

    return createConstructionProxy(proxied, constructor.getParameterTypes());
View Full Code Here

    public BasicProxyFactoryImpl(Class superClass, Class[] interfaces) {
      if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) {
        throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" );
      }

      Enhancer en = new Enhancer();
      en.setUseCache( false );
      en.setInterceptDuringConstruction( false );
      en.setUseFactory( true );
      en.setCallbackTypes( CALLBACK_TYPES );
      en.setCallbackFilter( FINALIZE_FILTER );
      if ( superClass != null ) {
        en.setSuperclass( superClass );
      }
      if ( interfaces != null && interfaces.length > 0 ) {
        en.setInterfaces( interfaces );
      }
      proxyClass = en.createClass();
      try {
        factory = ( Factory ) proxyClass.newInstance();
      }
      catch ( Throwable t ) {
        throw new HibernateException( "Unable to build CGLIB Factory instance" );
View Full Code Here

    return proxy;
  }

    public static Class getProxyFactory(Class persistentClass, Class[] interfaces)
      throws HibernateException {
    Enhancer e = new Enhancer();
    e.setSuperclass( interfaces.length == 1 ? persistentClass : null );
    e.setInterfaces(interfaces);
    e.setCallbackTypes(new Class[]{
      InvocationHandler.class,
      NoOp.class,
        });
      e.setCallbackFilter(FINALIZE_FILTER);
      e.setUseFactory(false);
    e.setInterceptDuringConstruction( false );
    return e.createClass();
  }
View Full Code Here

        };
    }

    @SuppressWarnings("unchecked")
    public <T> T newInstance(final Class<T> cls) {
        final Enhancer enhancer = lookupOrCreateEnhancerFor(cls);
        return (T) enhancer.create();
    }
View Full Code Here

        final Enhancer enhancer = lookupOrCreateEnhancerFor(cls);
        return (T) enhancer.create();
    }

    private Enhancer lookupOrCreateEnhancerFor(final Class<?> cls) {
        Enhancer enhancer = enhancerByClass.get(cls);
        if (enhancer == null) {
            enhancer = new Enhancer();
            enhancer.setSuperclass(cls);
            enhancer.setInterfaces(ArrayExtensions.combine(cls.getInterfaces(), new Class<?>[] { CglibEnhanced.class }));
            enhancer.setCallback(callback);
            enhancerByClass.put(cls, enhancer);
        }
        return enhancer;
    }
View Full Code Here

    @SuppressWarnings("unchecked")
    private static <T> Class<T> createEnhancedClass(final Class<T> toProxyClass, final InvocationHandler handler, final Class<?>... auxiliaryTypes) {

        // Create the proxy
        final Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(toProxyClass);
        enhancer.setInterfaces(auxiliaryTypes);
        enhancer.setCallbackType(newMethodInterceptor(handler).getClass());

        final Class<?> enhancedClass = enhancer.createClass();

        Enhancer.registerCallbacks(enhancedClass, new Callback[] { newMethodInterceptor(handler) });
        return (Class<T>) enhancedClass;
    }
View Full Code Here

    }

    public <T> T createProxy(final Class<T> interfaze, Invocable invocable) throws ProxyCreationException {
        if (invocable instanceof RuntimeEndpoint) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(interfaze);
            enhancer.setCallback(new CglibMethodInterceptor<T>(interfaze, invocable));
            Object proxy = enhancer.create();
            return interfaze.cast(proxy);
        }       
        ServiceReference<T> serviceReference = new ServiceReferenceImpl(interfaze, invocable, null);
        return createProxy(serviceReference);
    }
View Full Code Here

    /**
     * create the proxy with cglib. use the same JDKInvocationHandler as
     * JDKProxyService.
     */
    public <T> T createProxy(ServiceReference<T> callableReference) throws ProxyCreationException {
        Enhancer enhancer = new Enhancer();
        Class<T> interfaze = callableReference.getBusinessInterface();
        enhancer.setSuperclass(interfaze);
        enhancer.setCallback(new CglibMethodInterceptor<T>(callableReference));
        Object proxy = enhancer.create();
    ((ServiceReferenceImpl)callableReference).setProxy(proxy);
        return interfaze.cast(proxy);
    }
View Full Code Here

TOP

Related Classes of net.sf.cglib.proxy.Enhancer

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.