本文整理匯總了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();
}