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