本文整理汇总了Java中net.sf.cglib.proxy.Enhancer类的典型用法代码示例。如果您正苦于以下问题:Java Enhancer类的具体用法?Java Enhancer怎么用?Java Enhancer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Enhancer类属于net.sf.cglib.proxy包,在下文中一共展示了Enhancer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
});
}
示例2: 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;
}
});
}
示例3: 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);
}
示例4: 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;
}
示例5: createProxy
import net.sf.cglib.proxy.Enhancer; //导入依赖的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;
}
示例6: 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 );
}
}
示例7: getJedisProxy
import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
/**
* 使用 cglib 库创建 JedisProxy 的代理对象
*
* @return JedisProxy 代理
*/
public JedisProxy getJedisProxy() {
if (jedisProxy != null) {
return jedisProxy;
}
synchronized (JedisMethodInterceptor.class) {
if (jedisProxy != null) {
return jedisProxy;
}
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(JedisProxy.class);
enhancer.setCallback(this);
jedisProxy = (JedisProxy) enhancer.create();
}
return jedisProxy;
}
示例8: create
import net.sf.cglib.proxy.Enhancer; //导入依赖的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();
}
}
示例9: addTransactionSupport
import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
/**
* Adds transaction support to the page. The transaction support captures execution time of methods annotated with
* {@link io.devcon5.pageobjects.tx.Transaction}
*
* @param <T>
*
* @param transactionSupport
* the transactionSupport element to be enhanced.
* @return
*/
@SuppressWarnings("unchecked")
public static <T extends TransactionSupport> T addTransactionSupport(TransactionSupport transactionSupport) {
return (T) Enhancer.create(transactionSupport.getClass(), (MethodInterceptor) (obj, method, args, proxy) -> {
final Optional<String> txName = getTxName(transactionSupport, method);
try {
txName.ifPresent(transactionSupport::txBegin);
Object result = method.invoke(transactionSupport, args);
//dynamically enhance return values, if they are transactionSupport and not yet enhanced
//this is required, i.e. if method return 'this' or create new objects which will
//not be enhanced
if (!isCGLibProxy(result) && result instanceof TransactionSupport) {
result = addTransactionSupport(transactionSupport);
}
return result;
} finally {
txName.ifPresent(transactionSupport::txEnd);
}
});
}
示例10: main
import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
public static void main(String[] args) {
while(true) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(ClassPermGenOOM.class);
enhancer.setUseCache(Boolean.FALSE);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2,
MethodProxy arg3) throws Throwable {
return arg3.invokeSuper(arg0, arg2);
}
});
enhancer.create();
}
}
示例11: 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;
}
示例12: main
import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
public static void main(String[] args) {
Tmp tmp = new Tmp();
while (!Thread.interrupted()) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Tmp.class);
enhancer.setUseCache(false);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
return arg3.invokeSuper(arg0, arg2);
}
});
enhancer.create();
}
System.out.println(tmp.hashCode());
}
示例13: wrapWithCrashStackLogging
import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
/**
* Wraps the specified API object to dump caller stacktraces right before invoking
* native methods
*
* @param api API
* @return wrapped API
*/
static <T> T wrapWithCrashStackLogging(final Class<T> apiClazz, final T api) {
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {
@Override
public T run() throws Exception {
MethodInterceptor handler = new MethodInterceptorWithStacktraceLogging<T>(api);
T wrapperWithStacktraceLogging = (T) Enhancer.create(apiClazz, handler);
return wrapperWithStacktraceLogging;
}
});
} catch (PrivilegedActionException e) {
e.printStackTrace();
return api;
}
}
示例14: decode
import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public T decode(final BitBuffer buffer, final Resolver resolver,
final Builder builder) throws DecodingException {
final int size = wrapped.getSize().eval(resolver);
final long pos = buffer.getBitPos();
ClassLoader loader = this.getClass().getClassLoader();
Enhancer enhancer = new Enhancer();
enhancer.setClassLoader(loader);
enhancer.setSuperclass(type);
enhancer.setCallback(new MethodInterceptor() {
private Object actual;
public Object intercept(Object target, Method method,
Object[] args, MethodProxy proxy) throws Throwable {
if (actual == null) {
buffer.setBitPos(pos);
actual = wrapped.decode(buffer, resolver, builder);
}
return proxy.invoke(actual, args);
}
});
buffer.setBitPos(pos + size);
return (T) enhancer.create();
}
示例15: proxy
import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
public Object proxy() {
Optional<Class> genericClassCollection = ReflectionUtils.getGenericClassCollection(fieldOrigin);
if (!genericClassCollection.isPresent()) {
throw new IllegalArgumentException("Invalid collection on field [" + fieldOrigin.getName() + "]");
}
Boolean hasPrimitive = PrimitiveTypeFields.getInstance().contains(genericClassCollection.get());
if (hasPrimitive) {
return hibernateCollection;
} else {
return this.hibernateCollection.stream()
.map(item ->
Enhancer.create(genericClassCollection.get(), ProxyInterceptor.create(item))
).collect(Collectors.toList());
}
}