java类org.aopalliance.intercept.MethodInterceptor的实例源码

NullPrimitiveTests.java 文件源码 项目:class-guard 阅读 22 收藏 0 点赞 0 评论 0
@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());
}
NullPrimitiveTests.java 文件源码 项目:class-guard 阅读 18 收藏 0 点赞 0 评论 0
@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());
}
ProxyFactoryTests.java 文件源码 项目:class-guard 阅读 21 收藏 0 点赞 0 评论 0
@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);
}
AsyncExecutionTests.java 文件源码 项目:class-guard 阅读 18 收藏 0 点赞 0 评论 0
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();
}
AsyncExecutionTests.java 文件源码 项目:class-guard 阅读 21 收藏 0 点赞 0 评论 0
public DynamicAsyncMethodsInterfaceBean() {
    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(AsyncMethodsInterface.class);
    pf.addAdvisor(advisor);
    this.proxy = (AsyncMethodsInterface) pf.getProxy();
}
AbstractVaadinSecurityConfiguration.java 文件源码 项目:vaadin4spring 阅读 20 收藏 0 点赞 0 评论 0
@Bean(name = CURRENT_USER_BEAN)
Authentication currentUser() {

    return ProxyFactory.getProxy(Authentication.class, new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            SecurityContext securityContext = SecurityContextHolder.getContext();
            Authentication authentication = securityContext.getAuthentication();
            if (authentication == null) {
                throw new AuthenticationCredentialsNotFoundException("No authentication found in current security context");
            }
            return invocation.getMethod().invoke(authentication, invocation.getArguments());
        }

    });

}
MethodInterceptionTest.java 文件源码 项目:guice 阅读 19 收藏 0 点赞 0 评论 0
public void testGetThis() {
  final AtomicReference<Object> lastTarget = new AtomicReference<>();

  Injector injector =
      Guice.createInjector(
          new AbstractModule() {
            @Override
            protected void configure() {
              bindInterceptor(
                  Matchers.any(),
                  Matchers.any(),
                  new MethodInterceptor() {
                    @Override
                    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
                      lastTarget.set(methodInvocation.getThis());
                      return methodInvocation.proceed();
                    }
                  });
            }
          });

  Interceptable interceptable = injector.getInstance(Interceptable.class);
  interceptable.foo();
  assertSame(interceptable, lastTarget.get());
}
AmazonS3ProxyFactoryTest.java 文件源码 项目:spring-cloud-aws 阅读 18 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
@Test
public void verifyAddingRedirectAdviceToExistingProxy() {

    AmazonS3 amazonS3 = mock(AmazonS3.class);

    ProxyFactory factory = new ProxyFactory(amazonS3);
    factory.addAdvice(new TestAdvice());
    AmazonS3 proxy1 = (AmazonS3) factory.getProxy();

    assertThat(((Advised) proxy1).getAdvisors().length, is(1));

    AmazonS3 proxy2 = AmazonS3ProxyFactory.createProxy(proxy1);
    Advised advised = (Advised) proxy2;

    assertThat(advised.getAdvisors().length, is(2));

    List<Class<? extends MethodInterceptor>> advisorClasses = new ArrayList<>();
    for (Advisor advisor : advised.getAdvisors()) {
        advisorClasses.add(((MethodInterceptor) advisor.getAdvice()).getClass());
    }
    assertThat(advisorClasses, hasItems(TestAdvice.class, AmazonS3ProxyFactory.SimpleStorageRedirectInterceptor.class));

}
ProcessScope.java 文件源码 项目:FiWare-Template-Handler 阅读 18 收藏 0 点赞 0 评论 0
/**
 * creates a proxy that dispatches invocations to the currently bound {@link ProcessInstance}
 *
 * @return shareable {@link ProcessInstance}
 */
private Object createSharedProcessInstance()   {
    ProxyFactory proxyFactoryBean = new ProxyFactory(ProcessInstance.class, new MethodInterceptor() {
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            String methodName = methodInvocation.getMethod().getName() ;

            logger.info("method invocation for " + methodName+ ".");
            if(methodName.equals("toString"))
                return "SharedProcessInstance";


            ProcessInstance processInstance = Context.getExecutionContext().getProcessInstance();
            Method method = methodInvocation.getMethod();
            Object[] args = methodInvocation.getArguments();
            Object result = method.invoke(processInstance, args);
            return result;
        }
    });
    return proxyFactoryBean.getProxy(this.classLoader);
}
ProxyFactory.java 文件源码 项目:guice-old 阅读 30 收藏 0 点赞 0 评论 0
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();
  }
}


问题


面经


文章

微信
公众号

扫码关注公众号