org.springframework.aop.Pointcut Java Examples
The following examples show how to use
org.springframework.aop.Pointcut.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AspectJPointcutAdvisorTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testPerTarget() 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 PerTargetAspect(), "someBean"), 1, "someBean"); assertNotSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut()); assertTrue(ajpa.getAspectMetadata().getPerClausePointcut() instanceof AspectJExpressionPointcut); assertTrue(ajpa.isPerInstance()); assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getClassFilter().matches(TestBean.class)); assertFalse(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches( TestBean.class.getMethod("getAge"), TestBean.class)); assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches( TestBean.class.getMethod("getSpouse"), TestBean.class)); }
Example #2
Source File: Pointcuts.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Perform the least expensive check for a pointcut match. * @param pointcut the pointcut to match * @param method the candidate method * @param targetClass the target class * @param args arguments to the method * @return whether there's a runtime match */ public static boolean matches(Pointcut pointcut, Method method, Class<?> targetClass, Object[] args) { Assert.notNull(pointcut, "Pointcut must not be null"); if (pointcut == Pointcut.TRUE) { return true; } if (pointcut.getClassFilter().matches(targetClass)) { // Only check if it gets past first hurdle. MethodMatcher mm = pointcut.getMethodMatcher(); if (mm.matches(method, targetClass)) { // We may need additional runtime (argument) check. return (!mm.isRuntime() || mm.matches(method, targetClass, args)); } } return false; }
Example #3
Source File: AspectJPointcutAdvisorTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testPerTarget() 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 PerTargetAspect(), "someBean"), 1, "someBean"); assertNotSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut()); assertTrue(ajpa.getAspectMetadata().getPerClausePointcut() instanceof AspectJExpressionPointcut); assertTrue(ajpa.isPerInstance()); assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getClassFilter().matches(TestBean.class)); assertFalse(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches( TestBean.class.getMethod("getAge"), TestBean.class)); assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches( TestBean.class.getMethod("getSpouse"), TestBean.class)); }
Example #4
Source File: PointcutsTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testUnionOfSpecificGetters() { Pointcut union = Pointcuts.union(allClassGetAgePointcut, allClassGetNamePointcut); assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)); assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class)); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class)); assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)); // Union with all setters union = Pointcuts.union(union, allClassSetterPointcut); assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)); assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class)); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class)); assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)); assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); }
Example #5
Source File: PointcutsTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testUnionOfSpecificGetters() { Pointcut union = Pointcuts.union(allClassGetAgePointcut, allClassGetNamePointcut); assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object[] { new Integer(6)})); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null)); assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class, null)); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class, null)); assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); // Union with all setters union = Pointcuts.union(union, allClassSetterPointcut); assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object[] { new Integer(6)})); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null)); assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class, null)); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class, null)); assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object[] { new Integer(6)})); }
Example #6
Source File: Pointcuts.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Perform the least expensive check for a pointcut match. * @param pointcut the pointcut to match * @param method the candidate method * @param targetClass the target class * @param args arguments to the method * @return whether there's a runtime match */ public static boolean matches(Pointcut pointcut, Method method, Class<?> targetClass, Object... args) { Assert.notNull(pointcut, "Pointcut must not be null"); if (pointcut == Pointcut.TRUE) { return true; } if (pointcut.getClassFilter().matches(targetClass)) { // Only check if it gets past first hurdle. MethodMatcher mm = pointcut.getMethodMatcher(); if (mm.matches(method, targetClass)) { // We may need additional runtime (argument) check. return (!mm.isRuntime() || mm.matches(method, targetClass, args)); } } return false; }
Example #7
Source File: JavaMelodyAutoConfiguration.java From javamelody with Apache License 2.0 | 6 votes |
private MonitoringSpringAdvisor createMonitoringSpringAdvisorWithExclusions(Pointcut pointcut, Pointcut... excludedPointcuts) { final Pointcut myPointcut; if (excludedPointcuts.length == 0) { myPointcut = pointcut; } else { Pointcut excludedPointcut = excludedPointcuts[0]; if (excludedPointcuts.length > 1) { for (int i = 1; i < excludedPointcuts.length; i++) { excludedPointcut = Pointcuts.union(excludedPointcut, excludedPointcuts[i]); } } myPointcut = new ExcludingPointcut(pointcut).exclude(excludedPointcut); } return new MonitoringSpringAdvisor(myPointcut); }
Example #8
Source File: CglibProxyControllerTests.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("serial") private void initServlet(final Class<?> controllerClass) throws ServletException { servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setProxyTargetClass(true); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); Pointcut pointcut = new AnnotationMatchingPointcut(Controller.class); DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleTraceInterceptor(true)); wac.getBeanFactory().registerSingleton("advisor", advisor); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); }
Example #9
Source File: AbstractAspectJAdvice.java From spring-analysis-note with MIT License | 5 votes |
/** * Build a 'safe' pointcut that excludes the AspectJ advice method itself. * @return a composable pointcut that builds on the original AspectJ expression pointcut * @see #getPointcut() */ public final Pointcut buildSafePointcut() { Pointcut pc = getPointcut(); MethodMatcher safeMethodMatcher = MethodMatchers.intersection( new AdviceExcludingMethodMatcher(this.aspectJAdviceMethod), pc.getMethodMatcher()); return new ComposablePointcut(pc.getClassFilter(), safeMethodMatcher); }
Example #10
Source File: PointcutsTests.java From spring-analysis-note with MIT License | 5 votes |
/** * Should match all setters and getters on any class */ @Test public void testUnionOfSettersAndGetters() { Pointcut union = Pointcuts.union(allClassGetterPointcut, allClassSetterPointcut); assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)); assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)); }
Example #11
Source File: InstantiationModelAwarePointcutAdvisorImpl.java From lams with GNU General Public License v2.0 | 5 votes |
private PerTargetInstantiationModelPointcut(AspectJExpressionPointcut declaredPointcut, Pointcut preInstantiationPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory) { this.declaredPointcut = declaredPointcut; this.preInstantiationPointcut = preInstantiationPointcut; if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator) { this.aspectInstanceFactory = (LazySingletonAspectInstanceFactoryDecorator) aspectInstanceFactory; } }
Example #12
Source File: PointcutsTests.java From spring-analysis-note with MIT License | 5 votes |
/** * Tests vertical composition. First pointcut matches all setters. * Second one matches all getters in the MyTestBean class. TestBean getters shouldn't pass. */ @Test public void testUnionOfAllSettersAndSubclassSetters() { assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); assertTrue(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, MyTestBean.class, new Integer(6))); assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class)); Pointcut union = Pointcuts.union(myTestBeanSetterPointcut, allClassGetterPointcut); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class)); // Still doesn't match superclass setter assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, MyTestBean.class, new Integer(6))); assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); }
Example #13
Source File: InstantiationModelAwarePointcutAdvisorImpl.java From spring-analysis-note with MIT License | 5 votes |
public PerTargetInstantiationModelPointcut(AspectJExpressionPointcut declaredPointcut, Pointcut preInstantiationPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory) { this.declaredPointcut = declaredPointcut; this.preInstantiationPointcut = preInstantiationPointcut; if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator) { this.aspectInstanceFactory = (LazySingletonAspectInstanceFactoryDecorator) aspectInstanceFactory; } }
Example #14
Source File: AspectMetadataTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testPerTargetAspect() { AspectMetadata am = new AspectMetadata(PerTargetAspect.class,"someBean"); assertTrue(am.isPerThisOrPerTarget()); assertNotSame(Pointcut.TRUE, am.getPerClausePointcut()); assertEquals(PerClauseKind.PERTARGET, am.getAjType().getPerClause().getKind()); }
Example #15
Source File: AspectMetadataTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testSingletonAspect() { AspectMetadata am = new AspectMetadata(ExceptionAspect.class,"someBean"); assertFalse(am.isPerThisOrPerTarget()); assertSame(Pointcut.TRUE, am.getPerClausePointcut()); assertEquals(PerClauseKind.SINGLETON, am.getAjType().getPerClause().getKind()); }
Example #16
Source File: InstantiationModelAwarePointcutAdvisorImpl.java From lams with GNU General Public License v2.0 | 5 votes |
public InstantiationModelAwarePointcutAdvisorImpl(AspectJExpressionPointcut declaredPointcut, Method aspectJAdviceMethod, AspectJAdvisorFactory aspectJAdvisorFactory, MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) { this.declaredPointcut = declaredPointcut; this.declaringClass = aspectJAdviceMethod.getDeclaringClass(); this.methodName = aspectJAdviceMethod.getName(); this.parameterTypes = aspectJAdviceMethod.getParameterTypes(); this.aspectJAdviceMethod = aspectJAdviceMethod; this.aspectJAdvisorFactory = aspectJAdvisorFactory; this.aspectInstanceFactory = aspectInstanceFactory; this.declarationOrder = declarationOrder; this.aspectName = aspectName; if (aspectInstanceFactory.getAspectMetadata().isLazilyInstantiated()) { // Static part of the pointcut is a lazy type. Pointcut preInstantiationPointcut = Pointcuts.union( aspectInstanceFactory.getAspectMetadata().getPerClausePointcut(), this.declaredPointcut); // Make it dynamic: must mutate from pre-instantiation to post-instantiation state. // If it's not a dynamic pointcut, it may be optimized out // by the Spring AOP infrastructure after the first evaluation. this.pointcut = new PerTargetInstantiationModelPointcut( this.declaredPointcut, preInstantiationPointcut, aspectInstanceFactory); this.lazy = true; } else { // A singleton aspect. this.pointcut = this.declaredPointcut; this.lazy = false; this.instantiatedAdvice = instantiateAdvice(this.declaredPointcut); } }
Example #17
Source File: AspectJExpressionPointcutTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
private TestBean getAdvisedProxy(String pointcutExpression, CallCountingInterceptor interceptor) { TestBean target = new TestBean(); Pointcut pointcut = getPointcut(pointcutExpression); DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(); advisor.setAdvice(interceptor); advisor.setPointcut(pointcut); ProxyFactory pf = new ProxyFactory(); pf.setTarget(target); pf.addAdvisor(advisor); return (TestBean) pf.getProxy(); }
Example #18
Source File: ControlFlowPointcutTests.java From spring-analysis-note with MIT License | 5 votes |
/** * Check that we can use a cflow pointcut only in conjunction with * a static pointcut: e.g. all setter methods that are invoked under * a particular class. This greatly reduces the number of calls * to the cflow pointcut, meaning that it's not so prohibitively * expensive. */ @Test public void testSelectiveApplication() { TestBean target = new TestBean(); target.setAge(27); NopInterceptor nop = new NopInterceptor(); ControlFlowPointcut cflow = new ControlFlowPointcut(One.class); Pointcut settersUnderOne = Pointcuts.intersection(Pointcuts.SETTERS, cflow); ProxyFactory pf = new ProxyFactory(target); ITestBean proxied = (ITestBean) pf.getProxy(); pf.addAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop)); // Not advised, not under One target.setAge(16); assertEquals(0, nop.getCount()); // Not advised; under One but not a setter assertEquals(16, new One().getAge(proxied)); assertEquals(0, nop.getCount()); // Won't be advised new One().set(proxied); assertEquals(1, nop.getCount()); // We saved most evaluations assertEquals(1, cflow.getEvaluations()); }
Example #19
Source File: PointcutsTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testTrue() { assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class)); assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class)); assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class)); assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class)); }
Example #20
Source File: InstantiationModelAwarePointcutAdvisorImpl.java From java-technology-stack with MIT License | 5 votes |
public PerTargetInstantiationModelPointcut(AspectJExpressionPointcut declaredPointcut, Pointcut preInstantiationPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory) { this.declaredPointcut = declaredPointcut; this.preInstantiationPointcut = preInstantiationPointcut; if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator) { this.aspectInstanceFactory = (LazySingletonAspectInstanceFactoryDecorator) aspectInstanceFactory; } }
Example #21
Source File: PointcutsTests.java From java-technology-stack with MIT License | 5 votes |
/** * Should match all setters and getters on any class */ @Test public void testUnionOfSettersAndGetters() { Pointcut union = Pointcuts.union(allClassGetterPointcut, allClassSetterPointcut); assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)); assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)); }
Example #22
Source File: PointcutsTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * The intersection of these two pointcuts leaves nothing. */ @Test public void testSimpleIntersection() { Pointcut intersection = Pointcuts.intersection(allClassGetterPointcut, allClassSetterPointcut); assertFalse(Pointcuts.matches(intersection, TEST_BEAN_SET_AGE, TestBean.class, new Object[] { new Integer(6)})); assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class, null)); assertFalse(Pointcuts.matches(intersection, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); }
Example #23
Source File: AopUtilsTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testPointcutAppliesToOneMethodOnObject() { class TestPointcut extends StaticMethodMatcherPointcut { @Override public boolean matches(Method method, Class<?> clazz) { return method.getName().equals("hashCode"); } } Pointcut pc = new TestPointcut(); // will return true if we're not proxying interfaces assertTrue(AopUtils.canApply(pc, Object.class)); }
Example #24
Source File: AspectMetadataTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testSingletonAspect() { AspectMetadata am = new AspectMetadata(ExceptionAspect.class,"someBean"); assertFalse(am.isPerThisOrPerTarget()); assertSame(Pointcut.TRUE, am.getPerClausePointcut()); assertEquals(PerClauseKind.SINGLETON, am.getAjType().getPerClause().getKind()); }
Example #25
Source File: RegexpMethodPointcutAdvisor.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Initialize the singleton Pointcut held within this Advisor. */ @Override public Pointcut getPointcut() { synchronized (this.pointcutMonitor) { if (this.pointcut == null) { this.pointcut = createPointcut(); this.pointcut.setPatterns(this.patterns); } return pointcut; } }
Example #26
Source File: BeanFactoryLimitedResourceSourceAdvisor.java From Limiter with Apache License 2.0 | 4 votes |
@Override public Pointcut getPointcut() { return this.pointcut; }
Example #27
Source File: ComposablePointcutTests.java From java-technology-stack with MIT License | 4 votes |
@Test public void testMatchAll() throws NoSuchMethodException { Pointcut pc = new ComposablePointcut(); assertTrue(pc.getClassFilter().matches(Object.class)); assertTrue(pc.getMethodMatcher().matches(Object.class.getMethod("hashCode"), Exception.class)); }
Example #28
Source File: GraphTransactionAdvisor.java From atlas with Apache License 2.0 | 4 votes |
@Override public Pointcut getPointcut() { return pointcut; }
Example #29
Source File: NameMatchMethodPointcutAdvisor.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public Pointcut getPointcut() { return this.pointcut; }
Example #30
Source File: ApiBootMessagePushClientSwitchAdvisor.java From api-boot with Apache License 2.0 | 4 votes |
/** * build pointcut instance */ private Pointcut buildPointcut() { // method Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(MessagePushSwitch.class); return mpc; }