当前位置: 首页>>代码示例>>Java>>正文


Java MethodInterceptor类代码示例

本文整理汇总了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();
}
 
开发者ID:zouzhirong,项目名称:configx,代码行数:24,代码来源:StandardBeanLifecycleDecorator.java

示例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();
            }
        }
    }));
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:23,代码来源:SubsystemProxyFactory.java

示例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);
    }
}
 
开发者ID:code4wt,项目名称:toy-spring,代码行数:29,代码来源:JdkDynamicAopProxy.java

示例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);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:22,代码来源:DefaultAdvisorAdapterRegistry.java

示例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()]);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:18,代码来源:DefaultAdvisorAdapterRegistry.java

示例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();
  }
}
 
开发者ID:maetrive,项目名称:businessworks,代码行数:27,代码来源:ProxyFactory.java

示例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);
    }
}
 
开发者ID:directwebremoting,项目名称:dwr,代码行数:39,代码来源:AbstractModule.java

示例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);
      }
    }
  });
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:20,代码来源:SingularityHistoryModule.java

示例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);
          }
        }
      });
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:28,代码来源:GuiceUtils.java

示例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;
          }
        }
      });
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:34,代码来源:GuiceUtils.java

示例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;
    });
}
 
开发者ID:hs-web,项目名称:hsweb-framework,代码行数:21,代码来源:AopAccessLoggerSupport.java

示例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());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:NullPrimitiveTests.java

示例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());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:19,代码来源:NullPrimitiveTests.java

示例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);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:24,代码来源:ProxyFactoryTests.java

示例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();
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:17,代码来源:AsyncExecutionTests.java


注:本文中的org.aopalliance.intercept.MethodInterceptor类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。