本文整理汇总了Java中net.sf.cglib.proxy.Enhancer.setCallbackFilter方法的典型用法代码示例。如果您正苦于以下问题:Java Enhancer.setCallbackFilter方法的具体用法?Java Enhancer.setCallbackFilter怎么用?Java Enhancer.setCallbackFilter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.cglib.proxy.Enhancer
的用法示例。
在下文中一共展示了Enhancer.setCallbackFilter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
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;
}
}
// Create the proxied class. We're careful to ensure that all enhancer state is not-specific
// 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();
}
}
示例2: postProcessBeforeInstantiation
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public Object postProcessBeforeInstantiation(final Class<?> beanClass, final String beanName) throws BeansException {
if (_interceptors != null && _interceptors.size() > 0) {
if (ComponentMethodInterceptable.class.isAssignableFrom(beanClass)) {
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanClass);
enhancer.setCallbacks(getCallbacks());
enhancer.setCallbackFilter(getCallbackFilter());
enhancer.setNamingPolicy(ComponentNamingPolicy.INSTANCE);
final Object bean = enhancer.create();
return bean;
}
}
return null;
}
示例3: createDynamicProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public <T> T createDynamicProxy(Class<T> type, Supplier<T> targetSupplier, String descriptionPattern, Object... descriptionParams) {
final String description = String.format(descriptionPattern, descriptionParams);
final Enhancer enhancer = new Enhancer();
enhancer.setClassLoader(new DelegatingClassLoader(type.getClassLoader(), Enhancer.class.getClassLoader()));
enhancer.setInterfaces(new Class[]{type});
enhancer.setSuperclass(Object.class);
enhancer.setCallbackFilter(FILTER);
enhancer.setCallbacks(new Callback[]{
(Dispatcher) targetSupplier::get,
(MethodInterceptor) (proxy, method, params, methodProxy) -> proxy == params[0],
(MethodInterceptor) (proxy, method, params, methodProxy) -> System.identityHashCode(proxy),
(MethodInterceptor) (proxy, method, params, methodProxy) -> description
});
return type.cast(enhancer.create());
}
示例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: postProcessBeforeInstantiation
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if (_interceptors != null && _interceptors.size() > 0) {
if (ComponentMethodInterceptable.class.isAssignableFrom(beanClass)) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanClass);
enhancer.setCallbacks(getCallbacks());
enhancer.setCallbackFilter(getCallbackFilter());
enhancer.setNamingPolicy(ComponentNamingPolicy.INSTANCE);
Object bean = enhancer.create();
return bean;
}
}
return null;
}
示例7: benchmarkCglib
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Performs a benchmark of an interface implementation using cglib.
*
* @return The created instance, in order to avoid JIT removal.
*/
@Benchmark
public ExampleInterface benchmarkCglib() {
Enhancer enhancer = new Enhancer();
enhancer.setUseCache(false);
enhancer.setClassLoader(newClassLoader());
enhancer.setSuperclass(baseClass);
CallbackHelper callbackHelper = new CallbackHelper(Object.class, new Class[]{baseClass}) {
@Override
protected Object getCallback(Method method) {
if (method.getDeclaringClass() == baseClass) {
return new FixedValue() {
@Override
public Object loadObject() throws Exception {
return null;
}
};
} else {
return NoOp.INSTANCE;
}
}
};
enhancer.setCallbackFilter(callbackHelper);
enhancer.setCallbacks(callbackHelper.getCallbacks());
return (ExampleInterface) enhancer.create();
}
示例8: createProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public Object createProxy(Interceptor interceptor, Class<?>[] types, MethodPointcut methodPointcut) {
ProxyType proxyType = evalProxyType(types);
Enhancer eh = new Enhancer();
DefaultMethodInterceptor dmi = new DefaultMethodInterceptor(interceptor);
DefaultDispatcher dispatcher = new DefaultDispatcher(interceptor.getTarget());
Callback[] callbacks = new Callback[] {
dmi, dispatcher
};
eh.setCallbacks(callbacks);
CallbackFilter cf = new CallbackFilterAdapter(methodPointcut);
eh.setCallbackFilter(cf);
switch (proxyType) {
case CLASS:
Class<?> clazz = types[0];
eh.setSuperclass(clazz);
return eh.create();
case INTERFACES:
eh.setInterfaces(types);
return eh.create();
}
throw new UnsupportedOperationException("Unsupported proxy types!");
}
示例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: enhance
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public T enhance(Class<T> clz) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clz);
enhancer.setCallbacks(new Callback[]{new PropertyInterceptor<>(this, schema), NoOp.INSTANCE});
enhancer.setCallbackFilter(new SchemaFilter<>(clz));
return (T) enhancer.create();
}
示例12: proxySurround
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* 代理某一个对象指定方法,并在这个方法执行前后加入新方法,可指定过滤掉非代理的方法
* @author nan.li
* @param t
* @param before 执行目标方法前要执行的方法
* @param after 执行目标方法后要执行的方法
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T proxySurround(T t, CustomMethod before, CustomMethod after, CallbackFilter callbackFilter)
{
MethodInterceptor methodInterceptor = new MethodInterceptor()
{
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
throws Throwable
{
before.execute(obj, method, args);
Object result = null;
try
{
result = proxy.invokeSuper(obj, args);
}
finally
{
after.execute(obj, method, args);
}
return result;
}
};
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(t.getClass());
// 回调方法
// enhancer.setCallback(methodInterceptor);
enhancer.setCallbacks(new Callback[] {methodInterceptor, NoOp.INSTANCE});
//NoOp回调把对方法的调用直接委派到这个方法在父类中的实现。
//Methods using this Enhancer callback ( NoOp.INSTANCE ) will delegate directly to the default (super) implementation in the base class.
//setCallbacks中定义了所使用的拦截器,其中NoOp.INSTANCE是CGlib所提供的实际是一个没有任何操作的拦截器, 他们是有序的。一定要和CallbackFilter里面的顺序一致。
enhancer.setCallbackFilter(callbackFilter);
// 创建代理对象
return (T)enhancer.create();
}
示例13: create
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T create(Class<T> targetClass) throws InstantiationException, IllegalAccessException {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(targetClass);
enhancer.setClassLoader(targetClass.getClassLoader());
enhancer.setCallbackFilter(new TransactionalCallbackFilter());
Callback[] callbacks = new Callback[]{new DalTransactionInterceptor(), NoOp.INSTANCE};
enhancer.setCallbacks(callbacks);
enhancer.setInterfaces(new Class[]{TransactionalIntercepted.class});
return (T)enhancer.create();
}
示例14: 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();
}
示例15: getProxyFactory
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
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();
}