本文整理汇总了Java中net.sf.cglib.proxy.Enhancer.setInterfaces方法的典型用法代码示例。如果您正苦于以下问题:Java Enhancer.setInterfaces方法的具体用法?Java Enhancer.setInterfaces怎么用?Java Enhancer.setInterfaces使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.cglib.proxy.Enhancer
的用法示例。
在下文中一共展示了Enhancer.setInterfaces方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ReflectiveParserImpl
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
ReflectiveParserImpl(Class<?> base, List<Property<?, ?>> properties) {
InjectionChecks.checkInjectableCGLibProxyBase(base);
this.properties = properties;
this.propertyNames = properties.stream()
.flatMap(property -> property.parser.names().stream())
.collect(Collectors.toImmutableSet());
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(base);
enhancer.setInterfaces(new Class[]{ type.getRawType() });
enhancer.setCallbackType(MethodInterceptor.class);
enhancer.setUseFactory(true);
this.impl = enhancer.createClass();
this.injector = getMembersInjector((Class<T>) impl);
}
示例2: cglibcreate
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static <T> T cglibcreate(T o, OrientElement oe, Transaction transaction ) {
// this is the main cglib api entry-point
// this object will 'enhance' (in terms of CGLIB) with new capabilities
// one can treat this class as a 'Builder' for the dynamic proxy
Enhancer e = new Enhancer();
// the class will extend from the real class
e.setSuperclass(o.getClass());
// we have to declare the interceptor - the class whose 'intercept'
// will be called when any method of the proxified object is called.
ObjectProxy po = new ObjectProxy(o.getClass(),oe, transaction);
e.setCallback(po);
e.setInterfaces(new Class[]{IObjectProxy.class});
// now the enhancer is configured and we'll create the proxified object
T proxifiedObj = (T) e.create();
po.___setProxyObject(proxifiedObj);
// the object is ready to be used - return it
return proxifiedObj;
}
示例3: createEnhancedClass
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
protected static <T> Class<T> createEnhancedClass(Class<T> proxiedClass, Class<?>... implementedInterfaces) {
Enhancer enhancer = new Enhancer();
Set<Class<?>> interfaces = new HashSet<Class<?>>();
if (proxiedClass.isInterface()) {
enhancer.setSuperclass(Object.class);
interfaces.add(proxiedClass);
} else {
enhancer.setSuperclass(proxiedClass);
}
if (implementedInterfaces != null && implementedInterfaces.length > 0) {
interfaces.addAll(asList(implementedInterfaces));
}
if (!interfaces.isEmpty()) {
enhancer.setInterfaces(interfaces.toArray(new Class<?>[interfaces.size()]));
}
enhancer.setCallbackType(MethodInterceptor.class);
enhancer.setUseFactory(true);
return enhancer.createClass();
}
示例4: doBuild
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Build CGLIB <i>implementee</i> bean.
*
* @param implementation
* @param implementorBeanFactory
* @return
*/
protected Object doBuild(
Implementation<?> implementation,
ImplementorBeanFactory implementorBeanFactory)
{
InvocationHandler invocationHandler = new CglibImplementeeInvocationHandler(
implementation, implementorBeanFactory,
implementeeMethodInvocationFactory);
Enhancer enhancer = new Enhancer();
enhancer.setInterfaces(new Class[] { CglibImplementee.class });
enhancer.setSuperclass(implementation.getImplementee());
enhancer.setCallback(invocationHandler);
return enhancer.create();
}
示例5: 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());
}
示例6: getSuperPoweredRequest
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
private ServletWebRequest getSuperPoweredRequest(ServletWebRequest request, HttpServletRequest superPoweredMockRequest) {
if (isSuperPowered(request)) {
return request;
}
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(SingularServletWebRequest.class);
enhancer.setInterfaces(new Class[]{SuperPowered.class});
enhancer.setCallback(new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects)
throws InvocationTargetException, IllegalAccessException {
if ("getContainerRequest".equals(method.getName())) {
return superPoweredMockRequest;
}
return method.invoke(request, objects);
}
});
return (ServletWebRequest) enhancer.create();
}
示例7: 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" );
}
}
示例8: testSupportForClassBasedProxyWithAdditionalInterface
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testSupportForClassBasedProxyWithAdditionalInterface()
throws NullPointerException {
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(HashMap.class);
enhancer.setCallback(NoOp.INSTANCE);
enhancer.setInterfaces(new Class[]{Runnable.class});
final Map orig = (Map)enhancer.create();
final String xml = ""
+ "<CGLIB-enhanced-proxy>\n"
+ " <type>java.util.HashMap</type>\n"
+ " <interfaces>\n"
+ " <java-class>java.lang.Runnable</java-class>\n"
+ " </interfaces>\n"
+ " <hasFactory>true</hasFactory>\n"
+ " <net.sf.cglib.proxy.NoOp_-1/>\n"
+ "</CGLIB-enhanced-proxy>";
final Object serialized = assertBothWays(orig, xml);
assertTrue(serialized instanceof HashMap);
assertTrue(serialized instanceof Map);
assertTrue(serialized instanceof Runnable);
}
示例9: testSupportsProxiesWithMultipleInterfaces
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testSupportsProxiesWithMultipleInterfaces() throws NullPointerException {
final Enhancer enhancer = new Enhancer();
enhancer.setCallback(NoOp.INSTANCE);
enhancer.setInterfaces(new Class[]{Map.class, Runnable.class});
final Map orig = (Map)enhancer.create();
final String xml = ""
+ "<CGLIB-enhanced-proxy>\n"
+ " <type>java.lang.Object</type>\n"
+ " <interfaces>\n"
+ " <java-class>java.util.Map</java-class>\n"
+ " <java-class>java.lang.Runnable</java-class>\n"
+ " </interfaces>\n"
+ " <hasFactory>true</hasFactory>\n"
+ " <net.sf.cglib.proxy.NoOp_-1/>\n"
+ "</CGLIB-enhanced-proxy>";
final Object serialized = assertBothWays(orig, xml);
assertTrue(serialized instanceof Map);
assertTrue(serialized instanceof Runnable);
}
示例10: testSupportsSerialVersionUID
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testSupportsSerialVersionUID()
throws NullPointerException, NoSuchFieldException, IllegalAccessException {
final Enhancer enhancer = new Enhancer();
enhancer.setCallback(NoOp.INSTANCE);
enhancer.setInterfaces(new Class[]{Runnable.class});
enhancer.setSerialVersionUID(new Long(20060804L));
final Runnable orig = (Runnable)enhancer.create();
final String xml = ""
+ "<CGLIB-enhanced-proxy>\n"
+ " <type>java.lang.Object</type>\n"
+ " <interfaces>\n"
+ " <java-class>java.lang.Runnable</java-class>\n"
+ " </interfaces>\n"
+ " <hasFactory>true</hasFactory>\n"
+ " <net.sf.cglib.proxy.NoOp_-1/>\n"
+ " <serialVersionUID>20060804</serialVersionUID>\n"
+ "</CGLIB-enhanced-proxy>";
final Object serialized = assertBothWays(orig, xml);
final Field field = serialized.getClass().getDeclaredField("serialVersionUID");
field.setAccessible(true);
assertEquals(20060804L, field.getLong(null));
}
示例11: testSupportsProxiesAsFieldMember
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testSupportsProxiesAsFieldMember() throws NullPointerException {
ClassWithProxyMember expected = new ClassWithProxyMember();
xstream.alias("with-proxy", ClassWithProxyMember.class);
final Enhancer enhancer = new Enhancer();
enhancer.setCallback(NoOp.INSTANCE);
enhancer.setInterfaces(new Class[]{Map.class, Runnable.class});
final Map orig = (Map)enhancer.create();
expected.runnable = (Runnable)orig;
expected.map = orig;
final String xml = ""
+ "<with-proxy>\n"
+ " <runnable class=\"CGLIB-enhanced-proxy\">\n"
+ " <type>java.lang.Object</type>\n"
+ " <interfaces>\n"
+ " <java-class>java.util.Map</java-class>\n"
+ " <java-class>java.lang.Runnable</java-class>\n"
+ " </interfaces>\n"
+ " <hasFactory>true</hasFactory>\n"
+ " <net.sf.cglib.proxy.NoOp_-1/>\n"
+ " </runnable>\n"
+ " <map class=\"CGLIB-enhanced-proxy\" reference=\"../runnable\"/>\n"
+ "</with-proxy>";
final Object serialized = assertBothWays(expected, xml);
assertTrue(serialized instanceof ClassWithProxyMember);
}
示例12: 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);
}
}
示例13: createAdapter
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Create an adapter for the given {@code object} in a specific {@code type}.
*
* @param adaptableObject the object to adapt
* @param adapterType the class in which the object must be adapted
*
* @return an adapted object in the given {@code type}
*/
private static Object createAdapter(Object adaptableObject, Class<?> adapterType) {
/*
* Compute the interfaces that the proxy has to implement
* These are the current interfaces + PersistentEObject
*/
List<Class<?>> interfaces = ClassUtils.getAllInterfaces(adaptableObject.getClass());
interfaces.add(PersistentEObject.class);
// Create the proxy
Enhancer proxy = new Enhancer();
/*
* Use the ClassLoader of the type, otherwise it will cause OSGi troubles (like project trying to
* create an PersistentEObject while it does not have a dependency to NeoEMF core)
*/
proxy.setClassLoader(adapterType.getClassLoader());
proxy.setSuperclass(adaptableObject.getClass());
proxy.setInterfaces(interfaces.toArray(new Class[interfaces.size()]));
proxy.setCallback(new PersistentEObjectProxyHandler());
return proxy.create();
}
示例14: createProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Creates a proxy object that will write-replace with a wrapper around the {@link EntityManager}.
* @param factory a factory to generate EntityManagers.
* @return the proxied instance.
*/
static EntityManager createProxy(final HibernateEntityManagerFactory factory) {
final EntityManagerInterceptor handler = new EntityManagerInterceptor(factory);
final Enhancer e = new Enhancer();
// make sure we're Serializable and have a write replace method
e.setInterfaces(new Class[] { EntityManager.class, Serializable.class, IWriteReplace.class });
e.setSuperclass(Object.class);
e.setCallback(handler);
e.setNamingPolicy(new DefaultNamingPolicy() {
@Override
public String getClassName(final String prefix,
final String source,
final Object key,
final Predicate names) {
return super.getClassName("CROQUET_ENTITY_MANAGER_PROXY_" + prefix, source, key, names);
}
});
LOG.trace("Created proxy for an EntityManager");
return (EntityManager)e.create();
}
示例15: createProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Creates a proxy object that will write-replace with a wrapper around a {@link QueryRunner}.
* @param dataSourceFactory a provider to generate DataSources.
* @return the proxied instance.
*/
public static QueryRunner createProxy(final DataSourceFactory dataSourceFactory) {
final QueryRunnerInterceptor handler = new QueryRunnerInterceptor(dataSourceFactory);
final Enhancer e = new Enhancer();
// make sure we're Serializable and have a write replace method
e.setInterfaces(new Class[] { Serializable.class, IWriteReplace.class });
e.setSuperclass(QueryRunner.class);
e.setCallback(handler);
e.setNamingPolicy(new DefaultNamingPolicy() {
@Override
public String getClassName(final String prefix,
final String source,
final Object key,
final Predicate names) {
return super.getClassName("PROXY_" + prefix, source, key, names);
}
});
LOG.trace("Created proxy for an EntityManager");
return (QueryRunner)e.create();
}