本文整理汇总了Java中net.sf.cglib.proxy.Enhancer.setCallbacks方法的典型用法代码示例。如果您正苦于以下问题:Java Enhancer.setCallbacks方法的具体用法?Java Enhancer.setCallbacks怎么用?Java Enhancer.setCallbacks使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.cglib.proxy.Enhancer
的用法示例。
在下文中一共展示了Enhancer.setCallbacks方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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());
}
示例3: 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;
}
示例4: 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();
}
示例5: 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!");
}
示例6: 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();
}
示例7: 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();
}
示例8: 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();
}
示例9: testSupportProxiesUsingFactoryWithMultipleCallbacks
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testSupportProxiesUsingFactoryWithMultipleCallbacks()
throws NullPointerException {
final Enhancer enhancer = new Enhancer();
enhancer.setCallbacks(new Callback[]{
new DelegatingInterceptor(null), new DelegatingHandler(null),
new DelegatingDispatcher(null), NoOp.INSTANCE});
enhancer.setCallbackFilter(new CallbackFilter() {
int i = 1;
public int accept(Method method) {
if (method.getDeclaringClass() == Runnable.class) {
return 0;
}
return i < 3 ? i++ : i;
}
});
enhancer.setInterfaces(new Class[]{Runnable.class});
enhancer.setUseFactory(true);
final Runnable orig = (Runnable)enhancer.create();
final String xml = xstream.toXML(orig);
final Factory deserialized = (Factory)xstream.fromXML(xml);
assertTrue("Not a Runnable anymore", deserialized instanceof Runnable);
Callback[] callbacks = deserialized.getCallbacks();
assertEquals(4, callbacks.length);
assertTrue(callbacks[0] instanceof DelegatingInterceptor);
assertTrue(callbacks[1] instanceof DelegatingHandler);
assertTrue(callbacks[2] instanceof DelegatingDispatcher);
assertTrue(callbacks[3] instanceof NoOp);
}
示例10: testThrowsExceptionForProxiesNotUsingFactoryWithMultipleCallbacks
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testThrowsExceptionForProxiesNotUsingFactoryWithMultipleCallbacks()
throws NullPointerException {
final Enhancer enhancer = new Enhancer();
enhancer.setCallbacks(new Callback[]{
new DelegatingInterceptor(null), new DelegatingHandler(null),
new DelegatingDispatcher(null), NoOp.INSTANCE});
enhancer.setCallbackFilter(new CallbackFilter() {
int i = 1;
public int accept(Method method) {
if (method.getDeclaringClass() == Runnable.class) {
return 0;
}
return i < 3 ? i++ : i;
}
});
enhancer.setInterfaces(new Class[]{Runnable.class});
enhancer.setUseFactory(false);
final Runnable orig = (Runnable)enhancer.create();
try {
xstream.toXML(orig);
fail("Thrown " + ConversionException.class.getName() + " expected");
} catch (final ConversionException e) {
}
}
示例11: getAuthInstance
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static TableDao getAuthInstance(AuthProxy authProxy) {
Enhancer en=new Enhancer(); // Enhancer用来生成一个原有类的子类
// 进行代理
en.setSuperclass(TableDao.class);
// 设置织入逻辑
// en.setCallback(authProxy);
// en.setCallbacks()方法里的数组参数顺序就是上面方法的返回值所代表的方法拦截器,如果return 0则使用authProxy拦截器,
// return 1则使用NoOp.INSTANCE拦截器,NoOp.INSTANCE是默认的方法拦截器,不做什么处理。
en.setCallbacks(new Callback[]{authProxy, NoOp.INSTANCE}); // 设置两个方法拦截器
en.setCallbackFilter(new AuthProxyFilter());
// 生成代理实例
return (TableDao)en.create();
}
示例12: getProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T getProxy(TypeToken<T> type) {
Class<? super T> cls = type.getRawType();
if (isTerminal(type)) {
return (T) Defaults.defaultValue(cls);
}
Enhancer e = new Enhancer();
if (Enhancer.isEnhanced(cls)) {
e.setSuperclass(cls.getSuperclass());
e.setInterfaces(cls.getInterfaces());
} else {
e.setSuperclass(cls);
}
e.setCallbackFilter(FINALIZE_FILTER);
e.setCallbacks(new Callback[] { NoOp.INSTANCE, new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
invocations.add(new MethodInvocation<Object>(type, method, Arrays.asList(args)));
return getProxy(type.resolveType(method.getGenericReturnType()));
}
} });
try {
return (T) e.create();
} catch (Exception ex) {
throw new RuntimeException("Error while creating proxy of " + type, ex);
}
}
示例13: createProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public <T> T createProxy(Class<T> localizable, LocalizationProvider localizationProvider) {
Method[] methods = localizable.getDeclaredMethods();
List<Callback> callbacks = new ArrayList<Callback>(methods.length + 1);
final Map<Method, Integer> method2CallbackIndex = new HashMap<Method, Integer>(methods.length);
callbacks.add(NoOp.INSTANCE);
for (Method localizableMethod : methods) {
callbacks.add(createCallback(localizationProvider, localizableMethod));
method2CallbackIndex.put(localizableMethod, callbacks.size() - 1);
}
CallbackFilter callbackFilter = new CallbackFilter() {
@Override
public int accept(Method method) {
Integer index = method2CallbackIndex.get(method);
return index == null ? 0 : index;
}
};
Enhancer enhancer = new Enhancer();
enhancer.setInterfaces(new Class[]{localizable});
enhancer.setCallbackFilter(callbackFilter);
enhancer.setCallbacks(callbacks.toArray(new Callback[callbacks.size()]));
enhancer.setUseFactory(false);
@SuppressWarnings("unchecked")
T instance = (T) enhancer.create();
return instance;
}
示例14: unmarshal
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
final Enhancer enhancer = new Enhancer();
reader.moveDown();
enhancer.setSuperclass((Class<?>)context.convertAnother(null, Class.class));
reader.moveUp();
reader.moveDown();
final List<Class<?>> interfaces = new ArrayList<Class<?>>();
while (reader.hasMoreChildren()) {
reader.moveDown();
interfaces.add((Class<?>)context.convertAnother(null, mapper.realClass(reader.getNodeName())));
reader.moveUp();
}
enhancer.setInterfaces(interfaces.toArray(new Class[interfaces.size()]));
reader.moveUp();
reader.moveDown();
final boolean useFactory = Boolean.valueOf(reader.getValue()).booleanValue();
enhancer.setUseFactory(useFactory);
reader.moveUp();
final List<Callback> callbacksToEnhance = new ArrayList<Callback>();
final List<Callback> callbacks = new ArrayList<Callback>();
Map<Method, Integer> callbackIndexMap = null;
reader.moveDown();
if ("callbacks".equals(reader.getNodeName())) {
reader.moveDown();
@SuppressWarnings("unchecked")
final Map<Method, Integer> typedMap = (Map<Method, Integer>)context.convertAnother(null, HashMap.class);
callbackIndexMap = typedMap;
reader.moveUp();
while (reader.hasMoreChildren()) {
reader.moveDown();
readCallback(reader, context, callbacksToEnhance, callbacks);
reader.moveUp();
}
} else {
readCallback(reader, context, callbacksToEnhance, callbacks);
}
enhancer.setCallbacks(callbacksToEnhance.toArray(new Callback[callbacksToEnhance.size()]));
if (callbackIndexMap != null) {
enhancer.setCallbackFilter(new ReverseEngineeredCallbackFilter(callbackIndexMap));
}
reader.moveUp();
Object result = null;
while (reader.hasMoreChildren()) {
reader.moveDown();
if (reader.getNodeName().equals("serialVersionUID")) {
enhancer.setSerialVersionUID(Long.valueOf(reader.getValue()));
} else if (reader.getNodeName().equals("instance")) {
result = create(enhancer, callbacks, useFactory);
super.doUnmarshalConditionally(result, reader, context);
}
reader.moveUp();
}
if (result == null) {
result = create(enhancer, callbacks, useFactory);
}
return serializationMethodInvoker.callReadResolve(result);
}
示例15: proxySurroundIncludes
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* 代理一个对象<br>
* 仅代理其列出的几个方法
* @author Administrator
* @param t
* @param before
* @param after
* @param methodNames
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T proxySurroundIncludes(T t, CustomMethod before, CustomMethod after, String... methodNames)
{
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.setCallbacks(new Callback[] {methodInterceptor, NoOp.INSTANCE});
enhancer.setCallbackFilter(new CallbackFilter()
{
@Override
public int accept(Method paramMethod)
{
//仅判断代理的情况
String name = paramMethod.getName();
if (ArrayUtils.contains(methodNames, name))
{
return ProxyFlag.DO_PROXY.get();
}
//默认不代理
return ProxyFlag.DO_NOT_PROXY.get();
}
});
// 创建代理对象
return (T)enhancer.create();
}