本文整理匯總了Java中net.sf.cglib.proxy.Callback類的典型用法代碼示例。如果您正苦於以下問題:Java Callback類的具體用法?Java Callback怎麽用?Java Callback使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Callback類屬於net.sf.cglib.proxy包,在下文中一共展示了Callback類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createProxy
import net.sf.cglib.proxy.Callback; //導入依賴的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: createReverseEngineeredCallbackOfProperType
import net.sf.cglib.proxy.Callback; //導入依賴的package包/類
private Callback createReverseEngineeredCallbackOfProperType(final Callback callback, final int index,
final Map<? super Object, ? super Object> callbackIndexMap) {
Class<?> iface = null;
Class<?>[] interfaces = callback.getClass().getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
if (Callback.class.isAssignableFrom(interfaces[i])) {
iface = interfaces[i];
if (iface == Callback.class) {
final ConversionException exception = new ConversionException("Cannot handle CGLIB callback");
exception.add("CGLIB callback type", callback.getClass().getName());
throw exception;
}
interfaces = iface.getInterfaces();
if (Arrays.asList(interfaces).contains(Callback.class)) {
break;
}
i = -1;
}
}
return (Callback)Proxy.newProxyInstance(iface.getClassLoader(), new Class[]{iface},
new ReverseEngineeringInvocationHandler(index, callbackIndexMap));
}
示例3: create
import net.sf.cglib.proxy.Callback; //導入依賴的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();
}
}
示例4: shouldGetValuesUsingProxyWrapper
import net.sf.cglib.proxy.Callback; //導入依賴的package包/類
@Test
public void shouldGetValuesUsingProxyWrapper() {
BusinessProposal businessProposal = getHibernateObjectOnRepository();
Negotiation negotiationProxy = BusinessModelProxy.from(businessProposal).proxy(Negotiation.class);
List<Negotiation> list = new ArrayList<>();
if (negotiationProxy instanceof Factory) {
Callback callback = ((Factory) negotiationProxy).getCallback(0);
Object objectModel = ((ProxyInterceptor) callback).getObjectModel();
Object hibernateEntity = ((ProxyInterceptor) callback).getHibernateEntity();
System.out.println("testet");
System.out.println("testet");
System.out.println("testet");
System.out.println("testet");
}
assertThat(negotiationProxy.getId(), Matchers.is(1l));
assertThat(negotiationProxy.getIntroduction(), Matchers.is("test introduction"));
assertThat(negotiationProxy.getCareOf(), Matchers.is("Smith"));
}
示例5: getProxiedTypeIfProxy
import net.sf.cglib.proxy.Callback; //導入依賴的package包/類
/**
* @param object The object to check
* @return The proxied type, null if the object is not a proxy
*/
public static Class<?> getProxiedTypeIfProxy(Object object) {
if (object == null) {
return null;
}
if (object instanceof Factory) {
Callback[] callbacks = ((Factory) object).getCallbacks();
if (callbacks == null || callbacks.length == 0) {
return null;
}
if (callbacks[0] instanceof CglibProxyMethodInterceptor) {
return ((CglibProxyMethodInterceptor) callbacks[0]).getProxiedType();
}
}
return null;
}
示例6: getMockName
import net.sf.cglib.proxy.Callback; //導入依賴的package包/類
/**
* note: don't remove, used through reflection from {@link org.unitils.core.util.ObjectFormatter}
*
* @param object The object to check
* @return The proxied type, null if the object is not a proxy or mock
*/
@SuppressWarnings({"UnusedDeclaration"})
public static String getMockName(Object object) {
if (object == null) {
return null;
}
if (object instanceof MockObject) {
return ((MockObject) object).getName();
}
if (object instanceof Factory) {
Callback callback = ((Factory) object).getCallback(0);
if (callback instanceof CglibProxyMethodInterceptor) {
return ((CglibProxyMethodInterceptor) callback).getMockName();
}
}
return null;
}
示例7: toEntityBean
import net.sf.cglib.proxy.Callback; //導入依賴的package包/類
@DB()
protected T toEntityBean(final ResultSet result, final boolean cache) throws SQLException {
final T entity = (T) _factory.newInstance(new Callback[]{NoOp.INSTANCE, new UpdateBuilder(this)});
toEntityBean(result, entity);
if (cache && _cache != null) {
try {
_cache.put(new Element(_idField.get(entity), entity));
} catch (final Exception e) {
s_logger.debug("Can't put it in the cache", e);
}
}
return entity;
}
示例8: intercept
import net.sf.cglib.proxy.Callback; //導入依賴的package包/類
@Override
public Object intercept(final Object proxy, final Method method, final Object[] args, final MethodProxy methodProxy) throws Throwable {
final Object other = args[0];
if (proxy == other) {
return true;
} else if (other instanceof Factory) {
for (final Callback callback : ((Factory) other).getCallbacks()) {
if (callback.getClass().isAssignableFrom(EqualsInterceptor.class)) {
return target.equals(((EqualsInterceptor) callback).target);
}
}
}
return target.equals(other);
}
示例9: createDynamicProxy
import net.sf.cglib.proxy.Callback; //導入依賴的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());
}
示例10: createReverseEngineeredCallbackOfProperType
import net.sf.cglib.proxy.Callback; //導入依賴的package包/類
private Callback createReverseEngineeredCallbackOfProperType(final Callback callback, final int index,
final Map<? super Object, ? super Object> callbackIndexMap) {
Class<?> iface = null;
Class<?>[] interfaces = callback.getClass().getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
if (Callback.class.isAssignableFrom(interfaces[i])) {
iface = interfaces[i];
if (iface == Callback.class) {
final ConversionException exception = new ConversionException("Cannot handle CGLIB callback");
exception.add("CGLIB-callback-type", callback.getClass().getName());
throw exception;
}
interfaces = iface.getInterfaces();
if (Arrays.asList(interfaces).contains(Callback.class)) {
break;
}
i = -1;
}
}
return (Callback)Proxy.newProxyInstance(iface.getClassLoader(), new Class[]{iface},
new ReverseEngineeringInvocationHandler(index, callbackIndexMap));
}
示例11: getProxy
import net.sf.cglib.proxy.Callback; //導入依賴的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);
}
}
示例12: create
import net.sf.cglib.proxy.Callback; //導入依賴的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;
}
示例13: createReverseEngineeredCallbackOfProperType
import net.sf.cglib.proxy.Callback; //導入依賴的package包/類
private Callback createReverseEngineeredCallbackOfProperType(Callback paramCallback, int paramInt, Map paramMap)
{
Object localObject1 = null;
Object localObject2 = paramCallback.getClass().getInterfaces();
for (int i = 0; i < localObject2.length; i++)
if (Callback.class.isAssignableFrom(localObject2[i]))
{
Object localObject3 = localObject2[i];
localObject1 = localObject3;
if (localObject3 == Callback.class)
{
ConversionException localConversionException = new ConversionException("Cannot handle CGLIB callback");
localConversionException.add("CGLIB callback type", paramCallback.getClass().getName());
throw localConversionException;
}
Class[] arrayOfClass = localObject1.getInterfaces();
localObject2 = arrayOfClass;
if (Arrays.asList(arrayOfClass).contains(Callback.class))
break;
i = -1;
}
return (Callback)Proxy.newProxyInstance(localObject1.getClassLoader(), new Class[] { localObject1 }, new ReverseEngineeringInvocationHandler(paramInt, paramMap));
}
示例14: testMe
import net.sf.cglib.proxy.Callback; //導入依賴的package包/類
public void testMe(){
Article a = (Article) Enhancer.create(Article.class, new Class[]{DynamicUpdatable.class}, null, new Callback[]{
new MethodInterceptor(){
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
proxy.invokeSuper(obj, args) ;
System.out.println(method.getName()) ;
return null;
}
}
}) ;
assertTrue(a instanceof DynamicUpdatable) ;
a.getContent() ;
}
示例15: create
import net.sf.cglib.proxy.Callback; //導入依賴的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;
}