org.springframework.aop.ClassFilter Java Examples

The following examples show how to use org.springframework.aop.ClassFilter. 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: AnnotationMatchingPointcut.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new AnnotationMatchingPointcut for the given annotation type.
 * @param classAnnotationType the annotation type to look for at the class level
 * (can be {@code null})
 * @param methodAnnotationType the annotation type to look for at the method level
 * (can be {@code null})
 */
public AnnotationMatchingPointcut(
		Class<? extends Annotation> classAnnotationType, Class<? extends Annotation> methodAnnotationType) {

	Assert.isTrue((classAnnotationType != null || methodAnnotationType != null),
			"Either Class annotation type or Method annotation type needs to be specified (or both)");

	if (classAnnotationType != null) {
		this.classFilter = new AnnotationClassFilter(classAnnotationType);
	}
	else {
		this.classFilter = ClassFilter.TRUE;
	}

	if (methodAnnotationType != null) {
		this.methodMatcher = new AnnotationMethodMatcher(methodAnnotationType);
	}
	else {
		this.methodMatcher = MethodMatcher.TRUE;
	}
}
 
Example #2
Source File: MethodMatchers.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object other) {
	if (this == other) {
		return true;
	}
	if (!super.equals(other)) {
		return false;
	}
	ClassFilter otherCf1 = ClassFilter.TRUE;
	ClassFilter otherCf2 = ClassFilter.TRUE;
	if (other instanceof ClassFilterAwareUnionMethodMatcher) {
		ClassFilterAwareUnionMethodMatcher cfa = (ClassFilterAwareUnionMethodMatcher) other;
		otherCf1 = cfa.cf1;
		otherCf2 = cfa.cf2;
	}
	return (this.cf1.equals(otherCf1) && this.cf2.equals(otherCf2));
}
 
Example #3
Source File: AspectJExpressionPointcutTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatchExplicit() {
	String expression = "execution(int org.springframework.tests.sample.beans.TestBean.getAge())";

	Pointcut pointcut = getPointcut(expression);
	ClassFilter classFilter = pointcut.getClassFilter();
	MethodMatcher methodMatcher = pointcut.getMethodMatcher();

	assertMatchesTestBeanClass(classFilter);

	// not currently testable in a reliable fashion
	//assertDoesNotMatchStringClass(classFilter);

	assertFalse("Should not be a runtime match", methodMatcher.isRuntime());
	assertMatchesGetAge(methodMatcher);
	assertFalse("Expression should match setAge() method", methodMatcher.matches(setAge, TestBean.class));
}
 
Example #4
Source File: AnnotationMatchingPointcut.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create a new AnnotationMatchingPointcut for the given annotation type.
 * @param classAnnotationType the annotation type to look for at the class level
 * (can be {@code null})
 * @param methodAnnotationType the annotation type to look for at the method level
 * (can be {@code null})
 * @param checkInherited whether to also check the superclasses and interfaces
 * as well as meta-annotations for the annotation type
 * @since 5.0
 * @see AnnotationClassFilter#AnnotationClassFilter(Class, boolean)
 * @see AnnotationMethodMatcher#AnnotationMethodMatcher(Class, boolean)
 */
public AnnotationMatchingPointcut(@Nullable Class<? extends Annotation> classAnnotationType,
		@Nullable Class<? extends Annotation> methodAnnotationType, boolean checkInherited) {

	Assert.isTrue((classAnnotationType != null || methodAnnotationType != null),
			"Either Class annotation type or Method annotation type needs to be specified (or both)");

	if (classAnnotationType != null) {
		this.classFilter = new AnnotationClassFilter(classAnnotationType, checkInherited);
	}
	else {
		this.classFilter = ClassFilter.TRUE;
	}

	if (methodAnnotationType != null) {
		this.methodMatcher = new AnnotationMethodMatcher(methodAnnotationType, checkInherited);
	}
	else {
		this.methodMatcher = MethodMatcher.TRUE;
	}
}
 
Example #5
Source File: AspectJExpressionPointcutTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatchWithArgs() throws Exception {
	String expression = "execution(void org.springframework.tests.sample.beans.TestBean.setSomeNumber(Number)) && args(Double)";

	Pointcut pointcut = getPointcut(expression);
	ClassFilter classFilter = pointcut.getClassFilter();
	MethodMatcher methodMatcher = pointcut.getMethodMatcher();

	assertMatchesTestBeanClass(classFilter);

	// not currently testable in a reliable fashion
	//assertDoesNotMatchStringClass(classFilter);

	assertTrue("Should match with setSomeNumber with Double input",
			methodMatcher.matches(setSomeNumber, TestBean.class, new Object[]{new Double(12)}));
	assertFalse("Should not match setSomeNumber with Integer input",
			methodMatcher.matches(setSomeNumber, TestBean.class, new Object[]{new Integer(11)}));
	assertFalse("Should not match getAge", methodMatcher.matches(getAge, TestBean.class, null));
	assertTrue("Should be a runtime match", methodMatcher.isRuntime());
}
 
Example #6
Source File: MethodMatchers.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public boolean equals(Object other) {
	if (this == other) {
		return true;
	}
	if (!super.equals(other)) {
		return false;
	}
	ClassFilter otherCf1 = ClassFilter.TRUE;
	ClassFilter otherCf2 = ClassFilter.TRUE;
	if (other instanceof ClassFilterAwareUnionMethodMatcher) {
		ClassFilterAwareUnionMethodMatcher cfa = (ClassFilterAwareUnionMethodMatcher) other;
		otherCf1 = cfa.cf1;
		otherCf2 = cfa.cf2;
	}
	return (this.cf1.equals(otherCf1) && this.cf2.equals(otherCf2));
}
 
Example #7
Source File: DeclareParentsAdvisor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Private constructor to share common code between impl-based delegate and reference-based delegate
 * (cannot use method such as init() to share common code, due the the use of final fields)
 * @param interfaceType static field defining the introduction
 * @param typePattern type pattern the introduction is restricted to
 * @param implementationClass implementation class
 * @param advice delegation advice
 */
private DeclareParentsAdvisor(Class<?> interfaceType, String typePattern, Class<?> implementationClass, Advice advice) {
	this.introducedInterface = interfaceType;
	ClassFilter typePatternFilter = new TypePatternClassFilter(typePattern);

	// Excludes methods implemented.
	ClassFilter exclusion = new ClassFilter() {
		@Override
		public boolean matches(Class<?> clazz) {
			return !(introducedInterface.isAssignableFrom(clazz));
		}
	};

	this.typePatternClassFilter = ClassFilters.intersection(typePatternFilter, exclusion);
	this.advice = advice;
}
 
Example #8
Source File: AspectJExpressionPointcutTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testMatchWithArgs() throws Exception {
	String expression = "execution(void org.springframework.tests.sample.beans.TestBean.setSomeNumber(Number)) && args(Double)";

	Pointcut pointcut = getPointcut(expression);
	ClassFilter classFilter = pointcut.getClassFilter();
	MethodMatcher methodMatcher = pointcut.getMethodMatcher();

	assertMatchesTestBeanClass(classFilter);

	// not currently testable in a reliable fashion
	//assertDoesNotMatchStringClass(classFilter);

	assertTrue("Should match with setSomeNumber with Double input",
			methodMatcher.matches(setSomeNumber, TestBean.class, new Double(12)));
	assertFalse("Should not match setSomeNumber with Integer input",
			methodMatcher.matches(setSomeNumber, TestBean.class, new Integer(11)));
	assertFalse("Should not match getAge", methodMatcher.matches(getAge, TestBean.class));
	assertTrue("Should be a runtime match", methodMatcher.isRuntime());
}
 
Example #9
Source File: ComposablePointcutTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testFilterByClass() throws NoSuchMethodException {
	ComposablePointcut pc = new ComposablePointcut();

	assertTrue(pc.getClassFilter().matches(Object.class));

	ClassFilter cf = new RootClassFilter(Exception.class);
	pc.intersection(cf);
	assertFalse(pc.getClassFilter().matches(Object.class));
	assertTrue(pc.getClassFilter().matches(Exception.class));
	pc.intersection(new RootClassFilter(NestedRuntimeException.class));
	assertFalse(pc.getClassFilter().matches(Exception.class));
	assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class));
	assertFalse(pc.getClassFilter().matches(String.class));
	pc.union(new RootClassFilter(String.class));
	assertFalse(pc.getClassFilter().matches(Exception.class));
	assertTrue(pc.getClassFilter().matches(String.class));
	assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class));
}
 
Example #10
Source File: AspectJExpressionPointcutTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatchWithTypePattern() throws Exception {
	String expression = "execution(* *..TestBean.*Age(..))";

	Pointcut pointcut = getPointcut(expression);
	ClassFilter classFilter = pointcut.getClassFilter();
	MethodMatcher methodMatcher = pointcut.getMethodMatcher();

	assertMatchesTestBeanClass(classFilter);

	// not currently testable in a reliable fashion
	//assertDoesNotMatchStringClass(classFilter);

	assertFalse("Should not be a runtime match", methodMatcher.isRuntime());
	assertMatchesGetAge(methodMatcher);
	assertTrue("Expression should match setAge(int) method", methodMatcher.matches(setAge, TestBean.class));
}
 
Example #11
Source File: ComposablePointcutTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testUnionMethodMatcher() {
	// Matches the getAge() method in any class
	ComposablePointcut pc = new ComposablePointcut(ClassFilter.TRUE, GET_AGE_METHOD_MATCHER);
	assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class));
	assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class));
	assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class));

	pc.union(GETTER_METHOD_MATCHER);
	// Should now match all getter methods
	assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class));
	assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class));
	assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class));

	pc.union(ABSQUATULATE_METHOD_MATCHER);
	// Should now match absquatulate() as well
	assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class));
	assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class));
	assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class));
	// But it doesn't match everything
	assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_SET_AGE, TestBean.class));
}
 
Example #12
Source File: MethodMatchers.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object other) {
	if (this == other) {
		return true;
	}
	if (!super.equals(other)) {
		return false;
	}
	ClassFilter otherCf1 = ClassFilter.TRUE;
	ClassFilter otherCf2 = ClassFilter.TRUE;
	if (other instanceof ClassFilterAwareUnionMethodMatcher) {
		ClassFilterAwareUnionMethodMatcher cfa = (ClassFilterAwareUnionMethodMatcher) other;
		otherCf1 = cfa.cf1;
		otherCf2 = cfa.cf2;
	}
	return (this.cf1.equals(otherCf1) && this.cf2.equals(otherCf2));
}
 
Example #13
Source File: ComposablePointcutTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilterByClass() throws NoSuchMethodException {
	ComposablePointcut pc = new ComposablePointcut();

	assertTrue(pc.getClassFilter().matches(Object.class));

	ClassFilter cf = new RootClassFilter(Exception.class);
	pc.intersection(cf);
	assertFalse(pc.getClassFilter().matches(Object.class));
	assertTrue(pc.getClassFilter().matches(Exception.class));
	pc.intersection(new RootClassFilter(NestedRuntimeException.class));
	assertFalse(pc.getClassFilter().matches(Exception.class));
	assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class));
	assertFalse(pc.getClassFilter().matches(String.class));
	pc.union(new RootClassFilter(String.class));
	assertFalse(pc.getClassFilter().matches(Exception.class));
	assertTrue(pc.getClassFilter().matches(String.class));
	assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class));
}
 
Example #14
Source File: MetaAnnotationMatchingPointcut.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new MetaAnnotationMatchingPointcut for the given annotation type.
 *
 * @param classAnnotationType	the annotation type to look for at the class level
 *                             (can be <code>null</code>)
 * @param methodAnnotationType the annotation type to look for at the method level
 *                             (can be <code>null</code>)
 */
public MetaAnnotationMatchingPointcut(
		Class<? extends Annotation> classAnnotationType, Class<? extends Annotation> methodAnnotationType) {

	Assert.isTrue((classAnnotationType != null || methodAnnotationType != null),
			"Either Class annotation type or Method annotation type needs to be specified (or both)");

	if (classAnnotationType != null) {
		this.classFilter = new AnnotationClassFilter(classAnnotationType);
	} else {
		this.classFilter = ClassFilter.TRUE;
	}

	if (methodAnnotationType != null) {
		this.methodMatcher = new MetaAnnotationMethodMatcher(methodAnnotationType);
	} else {
		this.methodMatcher = MethodMatcher.TRUE;
	}
}
 
Example #15
Source File: ComposablePointcutTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testFilterByClass() throws NoSuchMethodException {
	ComposablePointcut pc = new ComposablePointcut();

	assertTrue(pc.getClassFilter().matches(Object.class));

	ClassFilter cf = new RootClassFilter(Exception.class);
	pc.intersection(cf);
	assertFalse(pc.getClassFilter().matches(Object.class));
	assertTrue(pc.getClassFilter().matches(Exception.class));
	pc.intersection(new RootClassFilter(NestedRuntimeException.class));
	assertFalse(pc.getClassFilter().matches(Exception.class));
	assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class));
	assertFalse(pc.getClassFilter().matches(String.class));
	pc.union(new RootClassFilter(String.class));
	assertFalse(pc.getClassFilter().matches(Exception.class));
	assertTrue(pc.getClassFilter().matches(String.class));
	assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class));
}
 
Example #16
Source File: CglibProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ITestBean getAdvisedProxy(TestBean target) {
	ProxyFactory pf = new ProxyFactory(new Class<?>[]{ITestBean.class});
	pf.setProxyTargetClass(true);

	MethodInterceptor advice = new NopInterceptor();
	Pointcut pointcut = new Pointcut() {
		@Override
		public ClassFilter getClassFilter() {
			return ClassFilter.TRUE;
		}
		@Override
		public MethodMatcher getMethodMatcher() {
			return MethodMatcher.TRUE;
		}
		@Override
		public boolean equals(Object obj) {
			return true;
		}
		@Override
		public int hashCode() {
			return 0;
		}
	};
	pf.addAdvisor(new DefaultPointcutAdvisor(pointcut, advice));

	pf.setTarget(target);
	pf.setFrozen(true);
	pf.setExposeProxy(false);

	return (ITestBean) pf.getProxy();
}
 
Example #17
Source File: ClassFiltersTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testIntersection() {
	assertTrue(exceptionFilter.matches(RuntimeException.class));
	assertTrue(hasRootCauseFilter.matches(NestedRuntimeException.class));
	ClassFilter intersection = ClassFilters.intersection(exceptionFilter, hasRootCauseFilter);
	assertFalse(intersection.matches(RuntimeException.class));
	assertFalse(intersection.matches(TestBean.class));
	assertTrue(intersection.matches(NestedRuntimeException.class));
}
 
Example #18
Source File: DeclareParentsAdvisor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Private constructor to share common code between impl-based delegate and reference-based delegate
 * (cannot use method such as init() to share common code, due the use of final fields).
 * @param interfaceType static field defining the introduction
 * @param typePattern type pattern the introduction is restricted to
 * @param interceptor the delegation advice as {@link IntroductionInterceptor}
 */
private DeclareParentsAdvisor(Class<?> interfaceType, String typePattern, IntroductionInterceptor interceptor) {
	this.advice = interceptor;
	this.introducedInterface = interfaceType;

	// Excludes methods implemented.
	ClassFilter typePatternFilter = new TypePatternClassFilter(typePattern);
	ClassFilter exclusion = (clazz -> !this.introducedInterface.isAssignableFrom(clazz));
	this.typePatternClassFilter = ClassFilters.intersection(typePatternFilter, exclusion);
}
 
Example #19
Source File: ClassFilters.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean matches(Class<?> clazz) {
	for (ClassFilter filter : this.filters) {
		if (filter.matches(clazz)) {
			return true;
		}
	}
	return false;
}
 
Example #20
Source File: ClassFiltersTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testUnion() {
	assertTrue(exceptionFilter.matches(RuntimeException.class));
	assertFalse(exceptionFilter.matches(TestBean.class));
	assertFalse(itbFilter.matches(Exception.class));
	assertTrue(itbFilter.matches(TestBean.class));
	ClassFilter union = ClassFilters.union(exceptionFilter, itbFilter);
	assertTrue(union.matches(RuntimeException.class));
	assertTrue(union.matches(TestBean.class));
}
 
Example #21
Source File: SleuthAdvisorConfig.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Override
public ClassFilter getClassFilter() {
	return new ClassFilter() {
		@Override
		public boolean matches(Class<?> clazz) {
			return new AnnotationClassOrMethodFilter(NewSpan.class).matches(clazz)
					|| new AnnotationClassOrMethodFilter(ContinueSpan.class)
							.matches(clazz);
		}
	};
}
 
Example #22
Source File: CglibProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ITestBean getAdvisedProxy(TestBean target) {
	ProxyFactory pf = new ProxyFactory(new Class<?>[]{ITestBean.class});
	pf.setProxyTargetClass(true);

	MethodInterceptor advice = new NopInterceptor();
	Pointcut pointcut = new Pointcut() {
		@Override
		public ClassFilter getClassFilter() {
			return ClassFilter.TRUE;
		}
		@Override
		public MethodMatcher getMethodMatcher() {
			return MethodMatcher.TRUE;
		}
		@Override
		public boolean equals(Object obj) {
			return true;
		}
		@Override
		public int hashCode() {
			return 0;
		}
	};
	pf.addAdvisor(new DefaultPointcutAdvisor(pointcut, advice));

	pf.setTarget(target);
	pf.setFrozen(true);
	pf.setExposeProxy(false);

	return (ITestBean) pf.getProxy();
}
 
Example #23
Source File: PointcutsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ClassFilter getClassFilter() {
	return new ClassFilter() {
		@Override
		public boolean matches(Class<?> clazz) {
			return clazz.equals(TestBean.class);
		}
	};
}
 
Example #24
Source File: ClassFilters.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean matches(Class<?> clazz) {
	for (ClassFilter filter : this.filters) {
		if (!filter.matches(clazz)) {
			return false;
		}
	}
	return true;
}
 
Example #25
Source File: ClassFiltersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testUnion() {
	assertTrue(exceptionFilter.matches(RuntimeException.class));
	assertFalse(exceptionFilter.matches(TestBean.class));
	assertFalse(itbFilter.matches(Exception.class));
	assertTrue(itbFilter.matches(TestBean.class));
	ClassFilter union = ClassFilters.union(exceptionFilter, itbFilter);
	assertTrue(union.matches(RuntimeException.class));
	assertTrue(union.matches(TestBean.class));
}
 
Example #26
Source File: ClassFilters.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean matches(Class<?> clazz) {
	for (ClassFilter filter : this.filters) {
		if (filter.matches(clazz)) {
			return true;
		}
	}
	return false;
}
 
Example #27
Source File: ClassFiltersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testIntersection() {
	assertTrue(exceptionFilter.matches(RuntimeException.class));
	assertTrue(hasRootCauseFilter.matches(NestedRuntimeException.class));
	ClassFilter intersection = ClassFilters.intersection(exceptionFilter, hasRootCauseFilter);
	assertFalse(intersection.matches(RuntimeException.class));
	assertFalse(intersection.matches(TestBean.class));
	assertTrue(intersection.matches(NestedRuntimeException.class));
}
 
Example #28
Source File: PointcutsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public ClassFilter getClassFilter() {
	return new ClassFilter() {
		@Override
		public boolean matches(Class<?> clazz) {
			return clazz.equals(TestBean.class);
		}
	};
}
 
Example #29
Source File: AspectJExpressionPointcut.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public ClassFilter getClassFilter() {
	checkReadyToMatch();
	return this;
}
 
Example #30
Source File: DefaultIntroductionAdvisor.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public ClassFilter getClassFilter() {
	return this;
}