本文整理汇总了Java中org.aopalliance.intercept.MethodInterceptor类的典型用法代码示例。如果您正苦于以下问题:Java MethodInterceptor类的具体用法?Java MethodInterceptor怎么用?Java MethodInterceptor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MethodInterceptor类属于org.aopalliance.intercept包,在下文中一共展示了MethodInterceptor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDisposalLockProxy
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
/**
* Apply a lock (preferably a read lock allowing multiple concurrent access) to the bean. Callers should replace the
* bean input with the output.
*
* @param bean the bean to lock
* @param lock the lock to apply
* @return a proxy that locks while its methods are executed
*/
private Object getDisposalLockProxy(Object bean, final Lock lock) {
ProxyFactory factory = new ProxyFactory(bean);
factory.setProxyTargetClass(proxyTargetClass);
factory.addAdvice(new MethodInterceptor() {
public Object invoke(MethodInvocation invocation) throws Throwable {
lock.lock();
try {
return invocation.proceed();
} finally {
lock.unlock();
}
}
});
return factory.getProxy();
}
示例2: SubsystemProxyFactory
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
/**
* Instantiates a new managed subsystem proxy factory.
*/
public SubsystemProxyFactory()
{
addAdvisor(new DefaultPointcutAdvisor(new MethodInterceptor()
{
public Object invoke(MethodInvocation mi) throws Throwable
{
Method method = mi.getMethod();
try
{
return method.invoke(locateBean(mi), mi.getArguments());
}
catch (InvocationTargetException e)
{
// Unwrap invocation target exceptions
throw e.getTargetException();
}
}
}));
}
示例3: invoke
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
/**
* InvocationHandler 接口中的 invoke 方法具体实现,封装了具体的代理逻辑
*
* @param proxy
* @param method
* @param args
* @return 代理方法或原方法的返回值
* @throws Throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
MethodMatcher methodMatcher = advised.getMethodMatcher();
// 使用方法匹配器 methodMatcher 测试 bean 中原始方法 method 是否符合匹配规则
if (methodMatcher != null && methodMatcher.matchers(method, advised.getTargetSource().getTargetClass())) {
// 获取 Advice。MethodInterceptor 的父接口继承了 Advice
MethodInterceptor methodInterceptor = advised.getMethodInterceptor();
// 将 bean 的原始 method 封装成 MethodInvocation 实现类对象,
// 将生成的对象传给 Adivce 实现类对象,执行通知逻辑
return methodInterceptor.invoke(
new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(), method, args));
} else {
// 当前 method 不符合匹配规则,直接调用 bean 中的原始 method
return method.invoke(advised.getTargetSource().getTarget(), args);
}
}
示例4: wrap
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
@Override
public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {
if (adviceObject instanceof Advisor) {
return (Advisor) adviceObject;
}
if (!(adviceObject instanceof Advice)) {
throw new UnknownAdviceTypeException(adviceObject);
}
Advice advice = (Advice) adviceObject;
if (advice instanceof MethodInterceptor) {
// So well-known it doesn't even need an adapter.
return new DefaultPointcutAdvisor(advice);
}
for (AdvisorAdapter adapter : this.adapters) {
// Check that it is supported.
if (adapter.supportsAdvice(advice)) {
return new DefaultPointcutAdvisor(advice);
}
}
throw new UnknownAdviceTypeException(advice);
}
示例5: getInterceptors
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
@Override
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>(3);
Advice advice = advisor.getAdvice();
if (advice instanceof MethodInterceptor) {
interceptors.add((MethodInterceptor) advice);
}
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) {
interceptors.add(adapter.getInterceptor(advisor));
}
}
if (interceptors.isEmpty()) {
throw new UnknownAdviceTypeException(advisor.getAdvice());
}
return interceptors.toArray(new MethodInterceptor[interceptors.size()]);
}
示例6: create
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的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();
}
}
示例7: bindInterceptor
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
/**
* Variant of {@link #bindInterceptor(Class, Class...) bindInterceptor} that
* allows constructor-injection of interceptors described by class, each
* wrapped by a method interceptor wrapper.
* @param classMatcher matches classes the interception should apply to.
* For example: {@code only(Runnable.class)}.
* @param methodMatcher matches methods the interception should apply to.
* For example: {@code annotatedWith(Transactional.class)}.
* @param methodInterceptorWrapper a wrapper applied to each of the specified interceptors.
* @param methodInterceptorClasses chain of
* {@link org.aopalliance.intercept.MethodInterceptor MethodInterceptor}s
* used to intercept calls, specified by class.
*/
public void bindInterceptor(Matcher<? super Class<?>> classMatcher,
Matcher<? super Method> methodMatcher,
MethodInterceptorWrapper methodInterceptorWrapper,
Class<?>... methodInterceptorClasses)
{
if (methodInterceptorClasses != null)
{
MethodInterceptor[] interceptors = new MethodInterceptor[methodInterceptorClasses.length];
int i = 0;
for (Class<?> cls : methodInterceptorClasses)
{
if (!MethodInterceptor.class.isAssignableFrom(cls))
{
addError("bindInterceptor: %s does not implement MethodInterceptor", cls.getName());
}
else
{
@SuppressWarnings("unchecked")
Class<? extends MethodInterceptor> c = (Class<? extends MethodInterceptor>) cls;
interceptors[i++] = wrap(methodInterceptorWrapper, c);
}
}
bindInterceptor(classMatcher, methodMatcher, interceptors);
}
}
示例8: bindMethodInterceptorForStringTemplateClassLoaderWorkaround
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
private void bindMethodInterceptorForStringTemplateClassLoaderWorkaround() {
bindInterceptor(Matchers.subclassesOf(JDBIHistoryManager.class), Matchers.any(), new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
}
try {
return invocation.proceed();
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}
});
}
示例9: bindJNIContextClassLoader
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
/**
* Binds an interceptor that ensures the main ClassLoader is bound as the thread context
* {@link ClassLoader} during JNI callbacks from mesos. Some libraries require a thread
* context ClassLoader be set and this ensures those libraries work properly.
*
* @param binder The binder to use to register an interceptor with.
* @param wrapInterface Interface whose methods should wrapped.
*/
public static void bindJNIContextClassLoader(Binder binder, Class<?> wrapInterface) {
final ClassLoader mainClassLoader = GuiceUtils.class.getClassLoader();
binder.bindInterceptor(
Matchers.subclassesOf(wrapInterface),
interfaceMatcher(wrapInterface, false),
new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Thread currentThread = Thread.currentThread();
ClassLoader prior = currentThread.getContextClassLoader();
try {
currentThread.setContextClassLoader(mainClassLoader);
return invocation.proceed();
} finally {
currentThread.setContextClassLoader(prior);
}
}
});
}
示例10: bindExceptionTrap
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
/**
* Binds an exception trap on all interface methods of all classes bound against an interface.
* Individual methods may opt out of trapping by annotating with {@link AllowUnchecked}.
* Only void methods are allowed, any non-void interface methods must explicitly opt out.
*
* @param binder The binder to register an interceptor with.
* @param wrapInterface Interface whose methods should be wrapped.
* @throws IllegalArgumentException If any of the non-whitelisted interface methods are non-void.
*/
public static void bindExceptionTrap(Binder binder, Class<?> wrapInterface)
throws IllegalArgumentException {
Set<Method> disallowed = ImmutableSet.copyOf(Iterables.filter(
ImmutableList.copyOf(wrapInterface.getMethods()),
Predicates.and(Predicates.not(IS_WHITELISTED), Predicates.not(VOID_METHOD))));
Preconditions.checkArgument(disallowed.isEmpty(),
"Non-void methods must be explicitly whitelisted with @AllowUnchecked: " + disallowed);
Matcher<Method> matcher =
Matchers.not(WHITELIST_MATCHER).and(interfaceMatcher(wrapInterface, false));
binder.bindInterceptor(Matchers.subclassesOf(wrapInterface), matcher,
new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
return invocation.proceed();
} catch (RuntimeException e) {
LOG.warn("Trapped uncaught exception: " + e, e);
return null;
}
}
});
}
示例11: AopAccessLoggerSupport
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
public AopAccessLoggerSupport() {
setAdvice((MethodInterceptor) methodInvocation -> {
MethodInterceptorHolder methodInterceptorHolder = MethodInterceptorHolder.create(methodInvocation);
AccessLoggerInfo info = createLogger(methodInterceptorHolder);
Object response;
try {
listeners.forEach(listener -> listener.onLogBefore(info));
response = methodInvocation.proceed();
info.setResponse(response);
info.setResponseTime(System.currentTimeMillis());
} catch (Throwable e) {
info.setException(e);
throw e;
} finally {
//触发监听
listeners.forEach(listener -> listener.onLogger(info));
}
return response;
});
}
示例12: testNullPrimitiveWithJdkProxy
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
@Test
public void testNullPrimitiveWithJdkProxy() {
class SimpleFoo implements Foo {
@Override
public int getValue() {
return 100;
}
}
SimpleFoo target = new SimpleFoo();
ProxyFactory factory = new ProxyFactory(target);
factory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return null;
}
});
Foo foo = (Foo) factory.getProxy();
thrown.expect(AopInvocationException.class);
thrown.expectMessage("Foo.getValue()");
assertEquals(0, foo.getValue());
}
示例13: testNullPrimitiveWithCglibProxy
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
@Test
public void testNullPrimitiveWithCglibProxy() {
Bar target = new Bar();
ProxyFactory factory = new ProxyFactory(target);
factory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return null;
}
});
Bar bar = (Bar) factory.getProxy();
thrown.expect(AopInvocationException.class);
thrown.expectMessage("Bar.getValue()");
assertEquals(0, bar.getValue());
}
示例14: testInterceptorInclusionMethods
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
@Test
public void testInterceptorInclusionMethods() {
class MyInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
throw new UnsupportedOperationException();
}
}
NopInterceptor di = new NopInterceptor();
NopInterceptor diUnused = new NopInterceptor();
ProxyFactory factory = new ProxyFactory(new TestBean());
factory.addAdvice(0, di);
assertThat(factory.getProxy(), instanceOf(ITestBean.class));
assertTrue(factory.adviceIncluded(di));
assertTrue(!factory.adviceIncluded(diUnused));
assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 1);
assertTrue(factory.countAdvicesOfType(MyInterceptor.class) == 0);
factory.addAdvice(0, diUnused);
assertTrue(factory.adviceIncluded(diUnused));
assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 2);
}
示例15: DynamicAsyncInterfaceBean
import org.aopalliance.intercept.MethodInterceptor; //导入依赖的package包/类
public DynamicAsyncInterfaceBean() {
ProxyFactory pf = new ProxyFactory(new HashMap<>());
DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
if (Future.class.equals(invocation.getMethod().getReturnType())) {
return new AsyncResult<String>(invocation.getArguments()[0].toString());
}
return null;
}
});
advisor.addInterface(AsyncInterface.class);
pf.addAdvisor(advisor);
this.proxy = (AsyncInterface) pf.getProxy();
}