本文整理汇总了Java中net.sf.cglib.proxy.MethodInterceptor类的典型用法代码示例。如果您正苦于以下问题:Java MethodInterceptor类的具体用法?Java MethodInterceptor怎么用?Java MethodInterceptor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MethodInterceptor类属于net.sf.cglib.proxy包,在下文中一共展示了MethodInterceptor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createProxy
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的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: ReflectiveParserImpl
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的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);
}
示例3: addTransactionSupport
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的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);
}
});
}
示例4: main
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的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();
}
}
示例5: main
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的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());
}
示例6: wrapWithCrashStackLogging
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的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;
}
}
示例7: decode
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的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();
}
示例8: createEnhancedClass
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的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();
}
示例9: createDynamicProxy
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的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: bind
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public T bind() {
if (__bound == null) {
__bound = (T) ClassUtils.wrapper(__source).duplicate(Enhancer.create(__targetClass, new MethodInterceptor() {
public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable {
Object _result = methodProxy.invokeSuper(targetObject, methodParams);
PropertyStateMeta _meta = __propertyStates.get(targetMethod.getName());
if (_meta != null && ArrayUtils.isNotEmpty(methodParams) && !ObjectUtils.equals(_meta.getOriginalValue(), methodParams[0])) {
_meta.setNewValue(methodParams[0]);
}
return _result;
}
}));
}
return __bound;
}
示例11: testMe
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的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() ;
}
示例12: decorateInstance
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的package包/类
@Override
protected void decorateInstance(T result, final MethodIntercept methodIntercept) {
MethodInterceptor methodInterceptor = new MethodInterceptor() {
public Object intercept(final Object proxy, Method method, Object[] args, final MethodProxy superProxy) throws Throwable {
return methodIntercept.intercept(proxy, new MethodAdapter(method), args, new MethodIntercept.SuperInvoker() {
public Object invokeSuper(Object[] superArgs) throws Throwable {
return superProxy.invokeSuper(proxy, superArgs);
}
});
}
};
Factory factory = (Factory) result;
int callbackCount = factory.getCallbacks().length;
for(int i = 0; i < callbackCount; i++) {
factory.setCallback(i, methodInterceptor);
}
}
示例13: createProxy
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的package包/类
/**
* Creates a dynamic proxy
*
* @param interceptor The interceptor that manages the invocations to the created proxy
* @param clazz The class to be proxied
* @param failSafe If true return null if it is not possible to proxy the request class, otherwise throws an UnproxableClassException
* @param implementedInterface The interfaces that has to be implemented by the new proxy
* @return The newly created proxy
*/
public static <I extends MethodInterceptor & InvocationHandler, T> T createProxy(I interceptor, Class<T> clazz, boolean failSafe, Class<?>... implementedInterface) {
if (clazz.isInterface())
return (T) createNativeJavaProxy(clazz.getClassLoader(), interceptor, concatClasses(new Class<?>[]{clazz}, implementedInterface));
RuntimeException e;
try {
return (T) createEnhancer(interceptor, clazz, implementedInterface).create();
} catch (RuntimeException ex) {
e = ex;
}
if (Proxy.isProxyClass(clazz))
return (T) createNativeJavaProxy(clazz.getClassLoader(), interceptor, concatClasses(implementedInterface, clazz.getInterfaces()));
if (isProxable(clazz)) return ClassImposterizer.INSTANCE.imposterise(interceptor, clazz, implementedInterface);
if (failSafe) return null;
throw e;
}
示例14: newProxyByCglib
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的package包/类
private static Object newProxyByCglib(Typing typing, Handler handler) {
Enhancer enhancer = new Enhancer() {
/** includes all constructors */
protected void filterConstructors(Class sc, List constructors) {}
};
enhancer.setClassLoader(Thread.currentThread().getContextClassLoader());
enhancer.setUseFactory(true);
enhancer.setSuperclass(typing.superclass);
enhancer.setInterfaces(typing.interfaces.toArray(new Class[0]));
enhancer.setCallbackTypes(new Class[] { MethodInterceptor.class, NoOp.class });
enhancer.setCallbackFilter(new CallbackFilter() {
/** ignores bridge methods */
public int accept(Method method) {
return method.isBridge() ? 1 : 0;
}
});
Class<?> proxyClass = enhancer.createClass();
Factory proxy = (Factory) new ObjenesisStd().newInstance(proxyClass);
proxy.setCallbacks(new Callback[] { asMethodInterceptor(handler), new SerializableNoOp() });
return proxy;
}
示例15: createProxy
import net.sf.cglib.proxy.MethodInterceptor; //导入依赖的package包/类
/**
* 根据目标类targetClass和代理链proxyList,生成代理对象
* @param targetClass
* @param proxyList
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T createProxy(final Class<?> targetClass, final List<Proxy> proxyList) {
return (T) Enhancer.create(targetClass, new MethodInterceptor() {
@Override
public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable {
return new ProxyChain(targetClass, targetObject, targetMethod, methodProxy, methodParams, proxyList).doProxyChain();
}
});
}