Search in sources :

Example 11 with Enhancer

use of net.sf.cglib.proxy.Enhancer in project simplejpa by appoxy.

the class BeansCglib method newInstance.

public static Object newInstance(Class clazz) {
    try {
        BeansCglib interceptor = new BeansCglib();
        Enhancer e = new Enhancer();
        e.setSuperclass(clazz);
        e.setCallback(interceptor);
        Object bean = e.create();
        interceptor.propertySupport = new PropertyChangeSupport(bean);
        return bean;
    } catch (Throwable e) {
        e.printStackTrace();
        throw new Error(e.getMessage());
    }
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer) PropertyChangeSupport(java.beans.PropertyChangeSupport)

Example 12 with Enhancer

use of net.sf.cglib.proxy.Enhancer in project guice by google.

the class ProxyFactory method create.

@Override
public ConstructionProxy<T> create() throws ErrorsException {
    if (interceptors.isEmpty()) {
        return new DefaultConstructionProxyFactory<T>(injectionPoint).create();
    }
    @SuppressWarnings("unchecked") Class<? extends Callback>[] callbackTypes = new Class[callbacks.length];
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] == net.sf.cglib.proxy.NoOp.INSTANCE) {
            callbackTypes[i] = net.sf.cglib.proxy.NoOp.class;
        } else {
            callbackTypes[i] = net.sf.cglib.proxy.MethodInterceptor.class;
        }
    }
    // to this injector. Otherwise, the proxies for each injector will waste PermGen memory
    try {
        Enhancer enhancer = BytecodeGen.newEnhancer(declaringClass, visibility);
        enhancer.setCallbackFilter(new IndicesCallbackFilter(methods));
        enhancer.setCallbackTypes(callbackTypes);
        return new ProxyConstructor<T>(enhancer, injectionPoint, callbacks, interceptors);
    } catch (Throwable e) {
        throw new Errors().errorEnhancingClass(declaringClass, e).toException();
    }
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer) InjectionPoint(com.google.inject.spi.InjectionPoint) Callback(net.sf.cglib.proxy.Callback) FastClass(net.sf.cglib.reflect.FastClass)

Example 13 with Enhancer

use of net.sf.cglib.proxy.Enhancer in project CloudStack-archive by CloudStack-extras.

the class ComponentLocator method createInstance.

private static Object createInstance(Class<?> clazz, boolean inject, boolean singleton, Object... args) {
    Factory factory = null;
    Singleton entity = null;
    synchronized (s_factories) {
        if (singleton) {
            entity = s_singletons.get(clazz);
            if (entity != null) {
                s_logger.debug("Found singleton instantiation for " + clazz.toString());
                return entity.singleton;
            }
        }
        InjectInfo info = s_factories.get(clazz);
        if (info == null) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(clazz);
            enhancer.setCallbackFilter(s_callbackFilter);
            enhancer.setCallbacks(s_callbacks);
            factory = (Factory) enhancer.create();
            info = new InjectInfo(enhancer, factory);
            s_factories.put(clazz, info);
        } else {
            factory = info.factory;
        }
    }
    Class<?>[] argTypes = null;
    if (args != null && args.length > 0) {
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            Class<?>[] paramTypes = constructor.getParameterTypes();
            if (paramTypes.length == args.length) {
                boolean found = true;
                for (int i = 0; i < paramTypes.length; i++) {
                    if (!paramTypes[i].isAssignableFrom(args[i].getClass()) && !paramTypes[i].isPrimitive()) {
                        found = false;
                        break;
                    }
                }
                if (found) {
                    argTypes = paramTypes;
                    break;
                }
            }
        }
        if (argTypes == null) {
            throw new CloudRuntimeException("Unable to find constructor to match parameters given: " + clazz.getName());
        }
        entity = new Singleton(factory.newInstance(argTypes, args, s_callbacks));
    } else {
        entity = new Singleton(factory.newInstance(s_callbacks));
    }
    if (inject) {
        inject(clazz, entity.singleton);
        entity.state = Singleton.State.Injected;
    }
    if (singleton) {
        synchronized (s_factories) {
            s_singletons.put(clazz, entity);
        }
    }
    return entity.singleton;
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer) Constructor(java.lang.reflect.Constructor) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Factory(net.sf.cglib.proxy.Factory) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 14 with Enhancer

use of net.sf.cglib.proxy.Enhancer in project tomee by apache.

the class AxisServiceReference method createServiceInterfaceProxy.

private Object createServiceInterfaceProxy(String serviceInterfaceClassName, Map seiPortNameToFactoryMap, Map seiClassNameToFactoryMap, ClassLoader classLoader) throws NamingException {
    boolean initialize = (this.serviceConstructor == null);
    if (initialize) {
        Class serviceInterface;
        try {
            serviceInterface = classLoader.loadClass(serviceInterfaceClassName);
        } catch (ClassNotFoundException e) {
            throw (NamingException) new NamingException("Could not load service interface class " + serviceInterfaceClassName).initCause(e);
        }
        // create method interceptors
        Callback callback = new ServiceMethodInterceptor(seiPortNameToFactoryMap);
        this.methodInterceptors = new Callback[] { NoOp.INSTANCE, callback };
        // create service class
        Enhancer enhancer = new Enhancer();
        enhancer.setClassLoader(classLoader);
        enhancer.setSuperclass(ServiceImpl.class);
        enhancer.setInterfaces(new Class[] { serviceInterface });
        enhancer.setCallbackFilter(new NoOverrideCallbackFilter(Service.class));
        enhancer.setCallbackTypes(new Class[] { NoOp.class, MethodInterceptor.class });
        enhancer.setUseFactory(false);
        enhancer.setUseCache(false);
        this.enhancedServiceClass = enhancer.createClass();
        // get constructor
        this.serviceConstructor = FastClass.create(this.enhancedServiceClass).getConstructor(SERVICE_CONSTRUCTOR_TYPES);
    }
    // associate the method interceptors with the generated service class on the current thread
    Enhancer.registerCallbacks(this.enhancedServiceClass, this.methodInterceptors);
    Object[] arguments = new Object[] { seiPortNameToFactoryMap, seiClassNameToFactoryMap };
    Object serviceInstance = null;
    try {
        serviceInstance = this.serviceConstructor.newInstance(arguments);
    } catch (InvocationTargetException e) {
        throw (NamingException) new NamingException("Could not construct service instance").initCause(e.getTargetException());
    }
    if (initialize) {
        for (Iterator iterator = seiPortNameToFactoryMap.values().iterator(); iterator.hasNext(); ) {
            SeiFactoryImpl seiFactory = (SeiFactoryImpl) iterator.next();
            try {
                seiFactory.initialize(serviceInstance, classLoader);
            } catch (ClassNotFoundException e) {
                throw (NamingException) new NamingException("Could not load service interface class; " + e.getMessage()).initCause(e);
            }
        }
    }
    return serviceInstance;
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer) Service(org.apache.axis.client.Service) InvocationTargetException(java.lang.reflect.InvocationTargetException) Callback(net.sf.cglib.proxy.Callback) Iterator(java.util.Iterator) FastClass(net.sf.cglib.reflect.FastClass) NamingException(javax.naming.NamingException)

Example 15 with Enhancer

use of net.sf.cglib.proxy.Enhancer in project tomee by apache.

the class SeiFactoryImpl method enhanceServiceEndpointInterface.

private Class enhanceServiceEndpointInterface(Class serviceEndpointInterface, ClassLoader classLoader) {
    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(classLoader);
    enhancer.setSuperclass(GenericServiceEndpointWrapper.class);
    enhancer.setInterfaces(new Class[] { serviceEndpointInterface });
    enhancer.setCallbackFilter(new NoOverrideCallbackFilter(GenericServiceEndpointWrapper.class));
    enhancer.setCallbackTypes(new Class[] { NoOp.class, MethodInterceptor.class });
    enhancer.setUseFactory(false);
    enhancer.setUseCache(false);
    return enhancer.createClass();
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer)

Aggregations

Enhancer (net.sf.cglib.proxy.Enhancer)27 MethodInterceptor (net.sf.cglib.proxy.MethodInterceptor)8 Callback (net.sf.cglib.proxy.Callback)6 Method (java.lang.reflect.Method)5 MethodProxy (net.sf.cglib.proxy.MethodProxy)5 FastClass (net.sf.cglib.reflect.FastClass)3 InjectionPoint (com.google.inject.spi.InjectionPoint)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 Factory (net.sf.cglib.proxy.Factory)2 UndeclaredThrowableStrategy (net.sf.cglib.transform.impl.UndeclaredThrowableStrategy)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ShardingJdbcException (com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException)1 AndroidDirectoryResolver (com.facebook.buck.android.AndroidDirectoryResolver)1 FakeAndroidDirectoryResolver (com.facebook.buck.android.FakeAndroidDirectoryResolver)1 BuckConfig (com.facebook.buck.cli.BuckConfig)1 FakeBuckConfig (com.facebook.buck.cli.FakeBuckConfig)1 CellConfig (com.facebook.buck.config.CellConfig)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 Watchman (com.facebook.buck.io.Watchman)1 NULL_WATCHMAN (com.facebook.buck.io.Watchman.NULL_WATCHMAN)1