Java Code Examples for org.springframework.aop.Pointcut

The following examples show how to use org.springframework.aop.Pointcut. These examples are extracted from open source projects. 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 Project: javamelody   Source File: JavaMelodyAutoConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
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 2
Source Project: lams   Source File: Pointcuts.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 Project: spring-analysis-note   Source File: AspectJPointcutAdvisorTests.java    License: MIT License 6 votes vote down vote up
@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 Project: java-technology-stack   Source File: PointcutsTests.java    License: MIT License 6 votes vote down vote up
@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 Project: spring-analysis-note   Source File: CglibProxyControllerTests.java    License: MIT License 6 votes vote down vote up
@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 6
Source Project: spring4-understanding   Source File: PointcutsTests.java    License: Apache License 2.0 6 votes vote down vote up
@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 7
Source Project: spring4-understanding   Source File: Pointcuts.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 8
@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 9
private PerTargetInstantiationModelPointcut(AspectJExpressionPointcut declaredPointcut,
		Pointcut preInstantiationPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory) {
	this.declaredPointcut = declaredPointcut;
	this.preInstantiationPointcut = preInstantiationPointcut;
	if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator) {
		this.aspectInstanceFactory = (LazySingletonAspectInstanceFactoryDecorator) aspectInstanceFactory;
	}
}
 
Example 10
public PerTargetInstantiationModelPointcut(AspectJExpressionPointcut declaredPointcut,
		Pointcut preInstantiationPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory) {

	this.declaredPointcut = declaredPointcut;
	this.preInstantiationPointcut = preInstantiationPointcut;
	if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator) {
		this.aspectInstanceFactory = (LazySingletonAspectInstanceFactoryDecorator) aspectInstanceFactory;
	}
}
 
Example 11
Source Project: spring-analysis-note   Source File: AbstractAspectJAdvice.java    License: MIT License 5 votes vote down vote up
/**
 * 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 12
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 13
Source Project: java-technology-stack   Source File: AspectMetadataTests.java    License: MIT License 5 votes vote down vote up
@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 14
Source Project: spring4-understanding   Source File: PointcutsTests.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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 15
Source Project: spring-analysis-note   Source File: AspectMetadataTests.java    License: MIT License 5 votes vote down vote up
@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 Project: java-technology-stack   Source File: AspectMetadataTests.java    License: MIT License 5 votes vote down vote up
@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 17
Source Project: spring-analysis-note   Source File: ControlFlowPointcutTests.java    License: MIT License 5 votes vote down vote up
/**
 * 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 18
Source Project: spring-analysis-note   Source File: PointcutsTests.java    License: MIT License 5 votes vote down vote up
/**
 * 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 19
Source Project: spring-analysis-note   Source File: PointcutsTests.java    License: MIT License 5 votes vote down vote up
/**
 * 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 20
/**
 * 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 21
Source Project: spring4-understanding   Source File: AopUtilsTests.java    License: Apache License 2.0 5 votes vote down vote up
@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 22
Source Project: java-technology-stack   Source File: PointcutsTests.java    License: MIT License 5 votes vote down vote up
/**
 * 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 23
public PerTargetInstantiationModelPointcut(AspectJExpressionPointcut declaredPointcut,
		Pointcut preInstantiationPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory) {

	this.declaredPointcut = declaredPointcut;
	this.preInstantiationPointcut = preInstantiationPointcut;
	if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator) {
		this.aspectInstanceFactory = (LazySingletonAspectInstanceFactoryDecorator) aspectInstanceFactory;
	}
}
 
Example 24
Source Project: java-technology-stack   Source File: PointcutsTests.java    License: MIT License 5 votes vote down vote up
@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 25
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 26
Source Project: incubator-atlas   Source File: GraphTransactionAdvisor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Pointcut getPointcut() {
    return pointcut;
}
 
Example 27
Source Project: beihu-boot   Source File: ApiBootDataSourceSwitchAdvisor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Pointcut getPointcut() {
    Assert.notNull(this.pointcut, "pointcut is required.");
    return this.pointcut;
}
 
Example 28
@Override
public Pointcut getPointcut() {
	return this.pointcut;
}
 
Example 29
/**
 * The pointcut for Spring AOP to use.
 * Actual behaviour of the pointcut will change depending on the state of the advice.
 */
@Override
public Pointcut getPointcut() {
	return this.pointcut;
}
 
Example 30
@Override
public Pointcut getPointcut() {
	return this.pointcut;
}