本文整理了Java中net.sf.cglib.proxy.Factory
类的一些代码示例,展示了Factory
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Factory
类的具体详情如下:
包路径:net.sf.cglib.proxy.Factory
类名称:Factory
[英]All enhanced instances returned by the Enhancer class implement this interface. Using this interface for new instances is faster than going through the Enhancer
interface or using reflection. In addition, to intercept methods called during object construction you must use these methods instead of reflection.
[中]增强器类返回的所有增强实例都实现此接口。为新实例使用此接口比通过Enhancer
接口或使用反射更快。此外,要截取在对象构造期间调用的方法,必须使用这些方法,而不是反射。
代码示例来源:origin: org.apache.commons/commons-pool2
@Override
public T resolveProxy(final T proxy) {
@SuppressWarnings("unchecked")
final
CglibProxyHandler<T> cglibProxyHandler =
(CglibProxyHandler<T>) ((Factory) proxy).getCallback(0);
final T pooledObject = cglibProxyHandler.disableProxy();
return pooledObject;
}
代码示例来源:origin: spockframework/spock
static Object createMock(Class<?> type, List<Class<?>> additionalInterfaces, @Nullable List<Object> constructorArgs,
IProxyBasedMockInterceptor interceptor, ClassLoader classLoader, boolean useObjenesis) {
Enhancer enhancer = new ConstructorFriendlyEnhancer();
enhancer.setClassLoader(classLoader);
enhancer.setSuperclass(type);
List<Class<?>> interfaces = new ArrayList<>(additionalInterfaces);
interfaces.add(ISpockMockObject.class);
enhancer.setInterfaces(interfaces.toArray(new Class<?>[interfaces.size()]));
enhancer.setCallbackFilter(BridgeMethodAwareCallbackFilter.INSTANCE);
MethodInterceptor cglibInterceptor = new CglibMockInterceptorAdapter(interceptor);
enhancer.setCallbackTypes(new Class[] {cglibInterceptor.getClass(), NoOp.class});
Class<?> enhancedType = enhancer.createClass();
Object proxy = MockInstantiator.instantiate(type, enhancedType, constructorArgs, useObjenesis);
((Factory) proxy).setCallbacks(new Callback[] {cglibInterceptor, NoOp.INSTANCE});
return proxy;
}
代码示例来源:origin: apache/cloudstack
@Override
@DB()
@SuppressWarnings("unchecked")
public T createForUpdate(final ID id) {
final T entity = (T)_factory.newInstance(new Callback[] {NoOp.INSTANCE, new UpdateBuilder(this)});
if (id != null) {
try {
_idField.set(entity, id);
} catch (final IllegalArgumentException e) {
} catch (final IllegalAccessException e) {
}
}
return entity;
}
代码示例来源:origin: com.thoughtworks.xstream/xstream
private Map createCallbackIndexMap(Factory source) {
Callback[] originalCallbacks = source.getCallbacks();
Callback[] reverseEngineeringCallbacks = new Callback[originalCallbacks.length];
Map callbackIndexMap = new HashMap();
source.setCallbacks(reverseEngineeringCallbacks);
final Set interfaces = new HashSet();
final Set methods = new HashSet();
source.setCallbacks(originalCallbacks);
代码示例来源:origin: org.jdbi/jdbi
@Test
public void testFactory() throws Exception
{
CGLIBDispatchBuilder b = CGLIBDispatchBuilder.create()
.withDefault(NoOp.INSTANCE)
.addCallback(Base.class.getMethod("foo"), new FixedValue()
{
@Override
public Object loadObject() throws Exception
{
return "hello";
}
});
Enhancer e = new Enhancer();
e.setSuperclass(Base.class);
e.setCallbackFilter(b.getFilter());
e.setCallbacks(b.getCallbacks());
Factory factory = (Factory) e.create();
Base base = (Base) factory.newInstance(b.getCallbacks());
assertThat(base.foo(), equalTo("hello"));
assertThat(base.bar(), equalTo("world"));
}
代码示例来源:origin: apache/cloudstack
en = enMap.get(clz);
if (en == null) {
en = new Enhancer();
en.setSuperclass(clz);
en.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
T t = (T)en.create();
Factory factory = (Factory)t;
factory.setCallback(0, new MethodInterceptor() {
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
代码示例来源:origin: org.opensingular/singular-support
public static Object createProxy(final Class<?> type, final IProxyTargetLocator locator, NamingPolicy namingPolicy) {
ObjenesisCGLibInterceptor handler = new ObjenesisCGLibInterceptor(type, locator);
Enhancer e = new Enhancer();
e.setInterfaces(new Class[]{Serializable.class, ILazyInitProxy.class, LazyInitProxyFactory.IWriteReplace.class});
e.setSuperclass(type);
e.setCallbackType(handler.getClass());
e.setNamingPolicy(namingPolicy);
e.setUseCache(false);
Class<?> proxyClass = e.createClass();
Factory proxyInstance = (Factory) OBJENESIS.newInstance(proxyClass);
proxyInstance.setCallbacks(new Callback[]{handler});
return proxyInstance;
}
}
代码示例来源:origin: com.thoughtworks.xstream/xstream
private Object create(final Enhancer enhancer, List callbacks, boolean useFactory) {
Object result = enhancer.create();
if (useFactory) {
((Factory)result).setCallbacks((Callback[])callbacks.toArray(new Callback[callbacks
.size()]));
}
return result;
}
代码示例来源:origin: org.mapdb/thread-weaver
/**
* 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;
}
代码示例来源:origin: com.moz.fiji.testing/fake-hbase
/**
* Creates a proxy to a given concrete class.
*
* @param klass Concrete class to proxy.
* @param handler Handler processing the method calls.
* @return a new proxy for the specified class.
* @throws InstantiationException on error.
*/
@SuppressWarnings("unchecked")
public static <T> T create(Class<?> klass, MethodInterceptor handler)
throws InstantiationException {
// Don't ask me how this work...
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(klass);
enhancer.setInterceptDuringConstruction(true);
enhancer.setCallbackType(handler.getClass());
final Class<?> proxyClass = enhancer.createClass();
final Factory proxy =
(Factory) ClassInstantiatorFactory.getInstantiator().newInstance(proxyClass);
proxy.setCallbacks(new Callback[] { handler });
return (T) proxy;
}
}
代码示例来源:origin: org.jboss.test-jsf/jsf-mock
enhancer.setSuperclass(toMock);
enhancer.setCallbackType(interceptor.getClass());
enhancer.setNamingPolicy(new DefaultNamingPolicy() {
@Override
public String getClassName(String prefix, String source, Object key, Predicate names) {
mock.getCallback(0);
代码示例来源:origin: apache/cloudstack
@SuppressWarnings("unchecked")
public T createSearchEntity(MethodInterceptor interceptor) {
T entity = (T)_searchEnhancer.create();
final Factory factory = (Factory)entity;
factory.setCallback(0, interceptor);
return entity;
}
代码示例来源:origin: org.jmock/jmock-legacy
private Object proxy(Class<?> proxyClass, final Invokable mockObject) {
final Factory proxy = (Factory)objenesis.newInstance(proxyClass);
proxy.setCallbacks(new Callback[] {
new InvocationHandler() {
public Object invoke(Object receiver, Method method, Object[] args) throws Throwable {
return mockObject.invoke(new Invocation(receiver, method, args));
}
},
NoOp.INSTANCE
});
return proxy;
}
代码示例来源:origin: org.glassfish.hk2.external/cglib
protected Object nextInstance(Object instance) {
Class protoclass = (instance instanceof Class) ? (Class)instance : instance.getClass();
if (classOnly) {
return protoclass;
} else if (instance instanceof Factory) {
if (argumentTypes != null) {
return ((Factory)instance).newInstance(argumentTypes, arguments, callbacks);
} else {
return ((Factory)instance).newInstance(callbacks);
}
} else {
return createUsingReflection(protoclass);
}
}
代码示例来源:origin: com.thoughtworks.xstream/xstream
Map callbackIndexMap = null;
Callback[] callbacks = hasFactory
? ((Factory)source).getCallbacks()
: getCallbacks(source);
if (callbacks.length > 1) {
代码示例来源:origin: apache/cloudstack
if (_entity != null) {
final Factory factory = (Factory)_entity;
factory.setCallback(0, null);
_entity = null;
代码示例来源:origin: org.kill-bill.commons/killbill-jdbi
@Test
public void testFactory() throws Exception
{
CGLIBDispatchBuilder b = CGLIBDispatchBuilder.create()
.withDefault(NoOp.INSTANCE)
.addCallback(Base.class.getMethod("foo"), new FixedValue()
{
@Override
public Object loadObject() throws Exception
{
return "hello";
}
});
Enhancer e = new Enhancer();
e.setSuperclass(Base.class);
e.setCallbackFilter(b.getFilter());
e.setCallbacks(b.getCallbacks());
Factory factory = (Factory) e.create();
Base base = (Base) factory.newInstance(b.getCallbacks());
assertThat(base.foo(), equalTo("hello"));
assertThat(base.bar(), equalTo("world"));
}
代码示例来源:origin: MissionCriticalCloud/cosmic
public T getTarget() {
Class<?> clz = _targetObject.getClass();
final String clzName = clz.getName();
if (clzName.contains("EnhancerByCloudStack")) {
clz = clz.getSuperclass();
}
Enhancer en = null;
synchronized (enMap) {
en = enMap.get(clz);
if (en == null) {
en = new Enhancer();
en.setSuperclass(clz);
en.setCallback((MethodInterceptor) (arg0, arg1, arg2, arg3) -> null);
enMap.put(clz, en);
}
}
final T t = (T) en.create();
final Factory factory = (Factory) t;
factory.setCallback(0, (MethodInterceptor) (arg0, arg1, arg2, arg3) -> {
if (arg1.getParameterTypes().length == 0 && arg1.getName().equals("finalize")) {
return null;
} else {
_callbackMethod = arg1;
_callbackMethod.setAccessible(true);
return null;
}
});
return t;
}
代码示例来源:origin: apache/wicket
public static Object createProxy(final Class<?> type, final IProxyTargetLocator locator, NamingPolicy namingPolicy)
{
ObjenesisCGLibInterceptor handler = new ObjenesisCGLibInterceptor(type, locator);
Enhancer e = new Enhancer();
e.setInterfaces(new Class[]{Serializable.class, ILazyInitProxy.class, IWriteReplace.class});
e.setSuperclass(type);
e.setCallbackType(handler.getClass());
e.setNamingPolicy(namingPolicy);
Class<?> proxyClass = e.createClass();
Object instance = OBJENESIS.newInstance(proxyClass);
// set callbacks directly (WICKET-6607)
((Factory) instance).setCallbacks(new Callback[]{handler});
return instance;
}
}
代码示例来源:origin: x-stream/xstream
private Object create(final Enhancer enhancer, final List<Callback> callbacks, final boolean useFactory) {
final Object result = enhancer.create();
if (useFactory) {
((Factory)result).setCallbacks(callbacks.toArray(new Callback[callbacks.size()]));
}
return result;
}
内容来源于网络,如有侵权,请联系作者删除!