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());
}
}
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();
}
}
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;
}
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;
}
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();
}
Aggregations