本文整理汇总了Java中net.sf.cglib.proxy.Enhancer.createClass方法的典型用法代码示例。如果您正苦于以下问题:Java Enhancer.createClass方法的具体用法?Java Enhancer.createClass怎么用?Java Enhancer.createClass使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.cglib.proxy.Enhancer
的用法示例。
在下文中一共展示了Enhancer.createClass方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static Object createProxy(Object realObject) {
try {
MethodInterceptor interceptor = new HammerKiller();
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(realObject.getClass());
enhancer.setCallbackType(interceptor.getClass());
Class classForProxy = enhancer.createClass();
Enhancer.registerCallbacks(classForProxy, new Callback[]{interceptor});
Object createdProxy = classForProxy.newInstance();
for (Field realField : FieldUtils.getAllFieldsList(realObject.getClass())) {
if (Modifier.isStatic(realField.getModifiers()))
continue;
realField.setAccessible(true);
realField.set(createdProxy, realField.get(realObject));
}
CreeperKiller.LOG.info("Removed HammerCore main menu hook, ads will no longer be displayed.");
return createdProxy;
} catch (Exception e) {
CreeperKiller.LOG.error("Failed to create a proxy for HammerCore ads, they will not be removed.", e);
}
return realObject;
}
示例2: ReflectiveParserImpl
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
ReflectiveParserImpl(Class<?> base, List<Property<?, ?>> properties) {
InjectionChecks.checkInjectableCGLibProxyBase(base);
this.properties = properties;
this.propertyNames = properties.stream()
.flatMap(property -> property.parser.names().stream())
.collect(Collectors.toImmutableSet());
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(base);
enhancer.setInterfaces(new Class[]{ type.getRawType() });
enhancer.setCallbackType(MethodInterceptor.class);
enhancer.setUseFactory(true);
this.impl = enhancer.createClass();
this.injector = getMembersInjector((Class<T>) impl);
}
示例3: createEnhancedClass
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
protected static <T> Class<T> createEnhancedClass(Class<T> proxiedClass, Class<?>... implementedInterfaces) {
Enhancer enhancer = new Enhancer();
Set<Class<?>> interfaces = new HashSet<Class<?>>();
if (proxiedClass.isInterface()) {
enhancer.setSuperclass(Object.class);
interfaces.add(proxiedClass);
} else {
enhancer.setSuperclass(proxiedClass);
}
if (implementedInterfaces != null && implementedInterfaces.length > 0) {
interfaces.addAll(asList(implementedInterfaces));
}
if (!interfaces.isEmpty()) {
enhancer.setInterfaces(interfaces.toArray(new Class<?>[interfaces.size()]));
}
enhancer.setCallbackType(MethodInterceptor.class);
enhancer.setUseFactory(true);
return enhancer.createClass();
}
示例4: BasicProxyFactoryImpl
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
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" );
}
}
示例5: getProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public T getProxy() {
Class proxyClass = getProxy(delegateClass.getName());
if (proxyClass == null) {
Enhancer enhancer = new Enhancer();
if (delegateClass.isInterface()) { // 判断是否为接口,优先进行接口代理可以解决service为final
enhancer.setInterfaces(new Class[] { delegateClass });
} else {
enhancer.setSuperclass(delegateClass);
}
enhancer.setCallbackTypes(new Class[] { ProxyDirect.class, ProxyInterceptor.class });
enhancer.setCallbackFilter(new ProxyRoute());
proxyClass = enhancer.createClass();
// 注册proxyClass
registerProxy(delegateClass.getName(), proxyClass);
}
Enhancer.registerCallbacks(proxyClass, new Callback[] { new ProxyDirect(), new ProxyInterceptor() });
try {
Object[] _constructorArgs = new Object[0];
Constructor _constructor = proxyClass.getConstructor(new Class[] {});// 先尝试默认的空构造函数
return (T) _constructor.newInstance(_constructorArgs);
} catch (Throwable e) {
throw new OptimizerException(e);
} finally {
// clear thread callbacks to allow them to be gc'd
Enhancer.registerStaticCallbacks(proxyClass, null);
}
}
示例6: create
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T create(Class<T> clazz, String widgetId) {
// creating proxy class
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clazz);
enhancer.setUseFactory(true);
enhancer.setCallbackType(RemoteObjectMethodInterceptor.class);
if (clazz.getSigners() != null) {
enhancer.setNamingPolicy(NAMING_POLICY_FOR_CLASSES_IN_SIGNED_PACKAGES);
}
Class<?> proxyClass = enhancer.createClass();
// instantiating class without constructor call
ObjenesisStd objenesis = new ObjenesisStd();
Factory proxy = (Factory) objenesis.newInstance(proxyClass);
proxy.setCallbacks(new Callback[]{new RemoteObjectMethodInterceptor(this, invoker, widgetId)});
T widget = (T) proxy;
widgetIds.put(widget, widgetId);
return widget;
}
示例7: create
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Creates a new instance of the given class, using the supplied interceptor.
* Uses the EasyMock ClassInstantiatorFactory in order to avoid the cglib
* limitation that prevents us from creating instances of classes that do not
* have public default constructors.
*/
private Object create(Class<?> clss, Interceptor interceptor) {
Enhancer e = new Enhancer();
e.setSuperclass(clss);
e.setCallbackType(interceptor.getClass());
Class<?> controlClass = e.createClass();
Enhancer.registerCallbacks(controlClass, new Callback[] { interceptor });
Factory result = (Factory) objenesis.newInstance(controlClass);
// This call is required to work around a cglib feature. See the comment in
// org.easymock.classextension.internal.ClassProxyFactory, which uses the
// same approach.
result.getCallback(0);
// And this call is required to work around a memory leak in cglib, which
// sticks references to the class in a ThreadLocal that is never cleared.
// See http://opensource.atlassian.com/projects/hibernate/browse/HHH-2481
Enhancer.registerCallbacks(controlClass, null);
return result;
}
示例8: of
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
static <T> T of(final Class<?> originalBeanClass, final TypeSafeWicketId parent) {
final Enhancer enhancer = new Enhancer();
final List<Class<?>> interfaces = new ArrayList<>();
if (Modifier.isInterface(originalBeanClass.getModifiers())) {
interfaces.add(originalBeanClass);
} else {
enhancer.setSuperclass(originalBeanClass);
}
interfaces.add(TYPE_SAFE_WICKET_ID_INTERFACE);
final Class<?>[] interfacesAsArray = interfaces.toArray(EMPTY);
enhancer.setInterfaces(interfacesAsArray);
enhancer.setCallbackType(InterfaceMethodInterceptor.class);
final Class<?> proxyClass = enhancer.createClass();
Enhancer.registerCallbacks(proxyClass, new Callback[] {new InterfaceMethodInterceptor(parent)});
final T proxy = (T) ObjenesisHelper.newInstance(proxyClass);
saveProxy((TypeSafeWicketId) proxy);
return proxy;
}
示例9: getProxyClass
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static Class<?> getProxyClass(Class<?> clazz) {
Enhancer e = new Enhancer();
if (clazz.isInterface()) {
e.setSuperclass(clazz);
} else {
e.setSuperclass(clazz);
e.setInterfaces(clazz.getInterfaces());
}
e.setCallbackTypes(new Class[]{
InvocationHandler.class,
NoOp.class,
});
e.setCallbackFilter(BAD_OBJECT_METHOD_FILTER);
e.setUseFactory(true);
e.setNamingPolicy(new LithiumTestProxyNamingPolicy());
return e.createClass();
}
示例10: newProxyByCglib
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
private static Object newProxyByCglib(Typing typing, Handler handler) {
Enhancer enhancer = new Enhancer() {
/** includes all constructors */
protected void filterConstructors(Class sc, List constructors) {}
};
enhancer.setClassLoader(Thread.currentThread().getContextClassLoader());
enhancer.setUseFactory(true);
enhancer.setSuperclass(typing.superclass);
enhancer.setInterfaces(typing.interfaces.toArray(new Class[0]));
enhancer.setCallbackTypes(new Class[] { MethodInterceptor.class, NoOp.class });
enhancer.setCallbackFilter(new CallbackFilter() {
/** ignores bridge methods */
public int accept(Method method) {
return method.isBridge() ? 1 : 0;
}
});
Class<?> proxyClass = enhancer.createClass();
Factory proxy = (Factory) new ObjenesisStd().newInstance(proxyClass);
proxy.setCallbacks(new Callback[] { asMethodInterceptor(handler), new SerializableNoOp() });
return proxy;
}
示例11: createProxyClass
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
protected Class<?> createProxyClass() {
Enhancer en = new Enhancer();
en.setInterceptDuringConstruction(false);
en.setUseFactory(true);
en.setSuperclass(unproxiedClass);
en.setInterfaces(new Class[] { Persistent.class });
en.setCallbackType(NoOp.class);
en.setStrategy(new DefaultGeneratorStrategy() {
protected ClassGenerator transform(ClassGenerator cg) throws Exception {
return new TransformingClassGenerator(cg, new AddPropertyTransformer(
new String[] { ORIGINAL_ONE, ORIGINAL_THE_OTHER },
new Type[] { Type.getType(String.class), Type.getType(List.class) }
));
}
});
return en.createClass();
}
示例12: implement
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public <T, D extends Decorator<T>> DecoratorGenerator.Meta<T, D> implement(Class<T> type, Class<D> decorator) {
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(decorator);
enhancer.setCallbackType(MethodInterceptor.class);
final Class<? extends D> impl = enhancer.createClass();
enhancer.setCallback(new Callback<>(type, decorator));
return new CGMeta(type, decorator, impl, enhancer);
}
示例13: ProxyConstructor
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked") // the constructor promises to construct 'T's
ProxyConstructor(Enhancer enhancer, InjectionPoint injectionPoint, Callback[] callbacks,
ImmutableMap<Method, List<MethodInterceptor>> methodInterceptors) {
this.enhanced = enhancer.createClass(); // this returns a cached class if possible
this.injectionPoint = injectionPoint;
this.constructor = (Constructor<T>) injectionPoint.getMember();
this.callbacks = callbacks;
this.methodInterceptors = methodInterceptors;
this.fastClass = newFastClassForMember(enhanced, constructor);
this.constructorIndex = fastClass.getIndex(constructor.getParameterTypes());
}
示例14: forClass
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static <T> MethodProxy<T> forClass(Class<T> clazz) {
MethodSignatureInterceptor methodSignatureInterceptor = new MethodSignatureInterceptor();
final Enhancer enhancer = new Enhancer();
enhancer.setUseCache(false);
enhancer.setSuperclass(clazz);
enhancer.setCallbackType(methodSignatureInterceptor.getClass());
final Class<?> proxyClass = enhancer.createClass();
Enhancer.registerCallbacks(proxyClass, new Callback[]{methodSignatureInterceptor});
T proxied = (T) ObjenesisHelper.newInstance(proxyClass);
return new MethodProxy<>(methodSignatureInterceptor, proxied);
}
示例15: createFactory
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
private Factory createFactory(Class<?> classToProxify) throws IllegalAccessException, InstantiationException {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(classToProxify);
enhancer.setCallbackFilter(new ImmutabilityCallbackFilter(this, classToProxify));
enhancer.setCallbackTypes(new Class[]{
EqualsMethodInterceptor.class,
ExceptionMethodInterceptor.class,
DelegatingMethodInterceptor.class,
FreezingMethodInterceptor.class
});
Class proxyClass = enhancer.createClass();
return (Factory) proxyClass.newInstance();
}