本文整理匯總了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();
}
});
}