本文整理汇总了Java中net.sf.cglib.proxy.Enhancer.create方法的典型用法代码示例。如果您正苦于以下问题:Java Enhancer.create方法的具体用法?Java Enhancer.create怎么用?Java Enhancer.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.cglib.proxy.Enhancer
的用法示例。
在下文中一共展示了Enhancer.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: newProxyInstance
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* 生成代理对象
* @param targetClass 被代理对象的类型(类或接口)
* @param target 被代理对象实例
* @return 代理对象
*/
@SuppressWarnings("unchecked")
public <T> T newProxyInstance(final Class<T> targetClass, final Object target) {
return (T) Enhancer.create(targetClass, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
before(targetClass, method, args);
Object ret = null;
try {
ret = method.invoke(target, args);
} catch (Exception e) {
exception(targetClass, method, args, e);
}
after(targetClass, method, args);
return ret;
}
});
}
示例3: newProxyInstance
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public <T> T newProxyInstance(final Class<T> targetClass, final Object target) {
return (T) Enhancer.create(targetClass, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
/**
* 如果没有@Transaction,则直接调用原方法
*/
if (!method.isAnnotationPresent(Transaction.class)) {
return method.invoke(target, args);
}
before(targetClass, method, args);
Object ret = null;
try {
ret = method.invoke(target, args);
} catch (Exception e) {
exception(targetClass, method, args, e);
}
after(targetClass, method, args);
return ret;
}
});
}
示例4: getProxyFactory
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static Factory getProxyFactory(Class persistentClass, Class[] interfaces) throws HibernateException {
//note: interfaces is assumed to already contain HibernateProxy.class
try {
return (Factory) Enhancer.create(
(interfaces.length==1) ?
persistentClass :
null,
interfaces,
NULL_METHOD_INTERCEPTOR
);
}
catch (Throwable t) {
LogFactory.getLog(LazyInitializer.class).error("CGLIB Enhancement failed", t);
throw new HibernateException( "CGLIB Enhancement failed", t );
}
}
示例5: 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;
}
示例6: 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;
}
示例7: scope
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public Supplier<Object> scope(Supplier<Object> supplier, Binding binding, CoreDependencyKey<?> requestedKey) {
// create the proxy right away, such that it can be reused
// afterwards
Object proxy = Enhancer.create(requestedKey.getRawType(), new Dispatcher() {
@Override
public Object loadObject() throws Exception {
Map<Binding, Object> scopedObjects = tryGetValueMap().orElseThrow(
() -> new RuntimeException("Cannot access " + requestedKey + " outside of scope " + scopeName));
return scopedObjects.computeIfAbsent(binding, b -> supplier.get());
}
});
return () -> proxy;
}
示例8: testSupportProxiesWithMultipleCallbackSetToNull
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testSupportProxiesWithMultipleCallbackSetToNull() throws NullPointerException {
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(HashMap.class);
enhancer.setCallback(NoOp.INSTANCE);
final HashMap orig = (HashMap)enhancer.create();
((Factory)orig).setCallback(0, null);
final String xml = ""
+ "<CGLIB-enhanced-proxy>\n"
+ " <type>java.util.HashMap</type>\n"
+ " <interfaces/>\n"
+ " <hasFactory>true</hasFactory>\n"
+ " <null/>\n"
+ "</CGLIB-enhanced-proxy>";
assertBothWays(orig, xml);
}
示例9: 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();
}
示例10: 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();
}
示例11: 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();
}
示例12: handle
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public boolean handle(Field f, Object target, Supplier<Fixture<S>> current) {
if (f.isAnnotationPresent(org.magenta.annotations.InjectDataSpecification.class)) {
InjectDataSpecification annotation = f.getAnnotation(org.magenta.annotations.InjectDataSpecification.class);
if (DataSpecification.class.isAssignableFrom(f.getType())) {
Enhancer e = new Enhancer();
e.setSuperclass(current.get().getSpecification().getClass());
e.setCallback(interceptor(supplierOfDataSpecification(current)));
e.setInterceptDuringConstruction(false);
DataSpecification proxy = (DataSpecification)e.create();
FieldInjectorUtils.injectInto(target, f, proxy);
return true;
}else{
throw new IllegalStateException("Invalid field : Annotation ["+InjectDataSpecification.class.getName()+"] is present on field named ["+f.getName()+"] on the class ["+f.getDeclaringClass().getCanonicalName()+"],\n but this field type should implement ["+DataSpecification.class.getName()+"] instead of ["+f.getType()+"].");
}
} else {
return false;
}
}
示例13: anyInstanceOf
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static Object anyInstanceOf(Class type) {
try {
if (type == null || type == void.class) return null;
if (type.isArray()) return anyArrayOf(type.getComponentType());
Object triangulatedInstance = tryToTriangulateFromThisClass(type);
if (triangulatedInstance != null) return triangulatedInstance;
if (type.isInterface())
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] {type},
new TriangulatingInvocationHandler());
if (Modifier.isAbstract(type.getModifiers()))
return Enhancer.create(type, new TriangulatingInvocationHandler());
return ObjenesisHelper.newInstance(type);
} catch (Exception e) {
throw new NestableRuntimeException(e);
}
}
示例14: replaceCOWithProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static Object replaceCOWithProxy(Class<?> className, Object[] parameters, Class<?>[] parameterTypes) throws Throwable {
Enhancer enhancer = new Enhancer();
enhancer.setCallback(new JCloudScaleCloudObjectHandler());
enhancer.setSuperclass(className);
try {
if(parameters != null && parameterTypes.length > 0)
return enhancer.create(parameterTypes, parameters);
else
return enhancer.create();
} catch(CodeGenerationException e) {
// we are not particularly interested in the CodeGenerationException, more in what caused it
if(e.getCause() != null)
throw e.getCause();
else
throw e;
}
}
示例15: main
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static void main(String[] args) {
LoggingInterceptor interceptor = new LoggingInterceptor();
Enhancer e = new Enhancer();
e.setSuperclass(Calculator.class);
e.setCallback(interceptor);
Calculator real = new Calculator();
Calculator proxy = (Calculator) e.create();
/* Will not be logged */
System.out.println("2 + 2 = " + real.add(2, 2));
/* Will be logged */
System.out.println("2 + 2 = " + proxy.add(2, 2));
}