public void setExpression(@Nullable String expression) { this.pointcut.setExpression(expression); }
private Pointcut getPointcut(String expression) { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(expression); return pointcut; }
private static boolean matches(final String beanName, String pcExpression) { @SuppressWarnings("serial") AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut() { @Override protected String getCurrentProxiedBeanName() { return beanName; } }; pointcut.setExpression(pcExpression); return pointcut.matches(TestBean.class); }
@Nullable private AspectJExpressionPointcut getPointcut(Method candidateAdviceMethod, Class<?> candidateAspectClass) { AspectJAnnotation<?> aspectJAnnotation = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod); if (aspectJAnnotation == null) { return null; } AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(candidateAspectClass, new String[0], new Class<?>[0]); ajexp.setExpression(aspectJAnnotation.getPointcutExpression()); if (this.beanFactory != null) { ajexp.setBeanFactory(this.beanFactory); } return ajexp; }
@Before public void setUp() throws Exception { this.comparator = new AspectJPrecedenceComparator(); this.anyOldMethod = getClass().getMethods()[0]; this.anyOldPointcut = new AspectJExpressionPointcut(); this.anyOldPointcut.setExpression("execution(* *(..))"); }
@Test public void manualGroovyBeanWithStaticPointcut() throws Exception { TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource( new ClassPathResource("GroovyServiceImpl.grv", getClass()))); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(String.format("execution(* %s.TestService+.*(..))", ClassUtils.getPackageName(getClass()))); testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", true); }
@Test public void manualGroovyBeanWithDynamicPointcut() throws Exception { TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource( new ClassPathResource("GroovyServiceImpl.grv", getClass()))); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(String.format("@within(%s.Log)", ClassUtils.getPackageName(getClass()))); testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", false); }
@Test public void manualGroovyBeanWithDynamicPointcutProxyTargetClass() throws Exception { TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource( new ClassPathResource("GroovyServiceImpl.grv", getClass()))); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(String.format("@within(%s.Log)", ClassUtils.getPackageName(getClass()))); testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", true); }
@Test public void testManualProxyJavaWithStaticPointcut() throws Exception { TestService target = new TestServiceImpl(); LogUserAdvice logAdvice = new LogUserAdvice(); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(String.format("execution(* %s.TestService.*(..))", getClass().getName())); testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "TestServiceImpl"); }
@Test public void testManualProxyJavaWithDynamicPointcutAndProxyTargetClass() throws Exception { TestService target = new TestServiceImpl(); LogUserAdvice logAdvice = new LogUserAdvice(); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(String.format("@within(%s.Log)", getClass().getName())); testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "TestServiceImpl", true); }
@Test public void testManualProxyJavaWithDynamicPointcut() throws Exception { TestService target = new TestServiceImpl(); LogUserAdvice logAdvice = new LogUserAdvice(); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(String.format("@within(%s.Log)", getClass().getName())); testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "TestServiceImpl"); }
@Test public void testAnnotationOnCglibProxyMethod() throws Exception { String expression = "@annotation(test.annotation.transaction.Tx)"; AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(expression); ProxyFactory factory = new ProxyFactory(new BeanA()); factory.setProxyTargetClass(true); BeanA proxy = (BeanA) factory.getProxy(); assertTrue(ajexp.matches(BeanA.class.getMethod("getAge"), proxy.getClass())); }
@Test public void testAnnotationOnDynamicProxyMethod() throws Exception { String expression = "@annotation(test.annotation.transaction.Tx)"; AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(expression); ProxyFactory factory = new ProxyFactory(new BeanA()); factory.setProxyTargetClass(false); IBeanA proxy = (IBeanA) factory.getProxy(); assertTrue(ajexp.matches(IBeanA.class.getMethod("getAge"), proxy.getClass())); }
@Test public void testManualProxyJavaWithStaticPointcutAndTwoClassLoaders() throws Exception { LogUserAdvice logAdvice = new LogUserAdvice(); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(String.format("execution(* %s.TestService.*(..))", getClass().getName())); // Test with default class loader first... testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, new TestServiceImpl(), "TestServiceImpl"); // Then try again with a different class loader on the target... SimpleThrowawayClassLoader loader = new SimpleThrowawayClassLoader(new TestServiceImpl().getClass().getClassLoader()); // Make sure the interface is loaded from the parent class loader loader.excludeClass(TestService.class.getName()); loader.excludeClass(TestException.class.getName()); TestService other = (TestService) loader.loadClass(TestServiceImpl.class.getName()).newInstance(); testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, other, "TestServiceImpl"); }
private AspectJExpressionPointcut testMatchAnnotationOnClass(String expression) throws Exception { AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(expression); assertFalse(ajexp.matches(getAge, TestBean.class)); assertTrue(ajexp.matches(HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)); assertTrue(ajexp.matches(HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)); assertTrue(ajexp.matches(BeanB.class.getMethod("setName", String.class), BeanB.class)); assertFalse(ajexp.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); return ajexp; }
/** * This and target are equivalent. Really instanceof pointcuts. * @param which this or target */ private void testThisOrTarget(String which) throws SecurityException, NoSuchMethodException { String matchesTestBean = which + "(org.springframework.tests.sample.beans.TestBean)"; String matchesIOther = which + "(org.springframework.tests.sample.beans.IOther)"; AspectJExpressionPointcut testBeanPc = new AspectJExpressionPointcut(); testBeanPc.setExpression(matchesTestBean); AspectJExpressionPointcut iOtherPc = new AspectJExpressionPointcut(); iOtherPc.setExpression(matchesIOther); assertTrue(testBeanPc.matches(TestBean.class)); assertTrue(testBeanPc.matches(getAge, TestBean.class)); assertTrue(iOtherPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)); assertFalse(testBeanPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)); }
@Test public void testAnnotationOnMethodWithFQN() throws Exception { String expression = "@annotation(test.annotation.transaction.Tx)"; AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(expression); assertFalse(ajexp.matches(getAge, TestBean.class)); assertFalse(ajexp.matches(HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)); assertFalse(ajexp.matches(HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)); assertFalse(ajexp.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); assertTrue(ajexp.matches(BeanA.class.getMethod("getAge"), BeanA.class)); assertFalse(ajexp.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); }
@Test public void testMatchGenericArgument() { String expression = "execution(* set*(java.util.List<org.springframework.tests.sample.beans.TestBean>) )"; AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(expression); // TODO this will currently map, would be nice for optimization //assertTrue(ajexp.matches(HasGeneric.class)); //assertFalse(ajexp.matches(TestBean.class)); Method takesGenericList = methodsOnHasGeneric.get("setFriends"); assertTrue(ajexp.matches(takesGenericList, HasGeneric.class)); assertTrue(ajexp.matches(methodsOnHasGeneric.get("setEnemies"), HasGeneric.class)); assertFalse(ajexp.matches(methodsOnHasGeneric.get("setPartners"), HasGeneric.class)); assertFalse(ajexp.matches(methodsOnHasGeneric.get("setPhoneNumbers"), HasGeneric.class)); assertFalse(ajexp.matches(getAge, TestBean.class)); }
@Test public void testAnnotationOnMethodWithWildcard() throws Exception { String expression = "execution(@(test.annotation..*) * *(..))"; AspectJExpressionPointcut anySpringMethodAnnotation = new AspectJExpressionPointcut(); anySpringMethodAnnotation.setExpression(expression); assertFalse(anySpringMethodAnnotation.matches(getAge, TestBean.class)); assertFalse(anySpringMethodAnnotation.matches( HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)); assertFalse(anySpringMethodAnnotation.matches( HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)); assertFalse(anySpringMethodAnnotation.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); assertTrue(anySpringMethodAnnotation.matches(BeanA.class.getMethod("getAge"), BeanA.class)); assertFalse(anySpringMethodAnnotation.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); }
@Test public void testSingleton() throws SecurityException, NoSuchMethodException { AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(AspectJExpressionPointcutTests.MATCH_ALL_METHODS); InstantiationModelAwarePointcutAdvisorImpl ajpa = new InstantiationModelAwarePointcutAdvisorImpl( ajexp, TestBean.class.getMethod("getAge"), af, new SingletonMetadataAwareAspectInstanceFactory(new AbstractAspectJAdvisorFactoryTests.ExceptionAspect(null), "someBean"), 1, "someBean"); assertSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut()); assertFalse(ajpa.isPerInstance()); }