From d434ef9713b456286173363c8fce18198fff8903 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 22 Jan 2014 18:23:59 +0100 Subject: [PATCH] Polishing Issue: SPR-11344 --- .../aop/IntroductionAwareMethodMatcher.java | 10 +++--- .../aop/support/ClassFilters.java | 17 +++++----- .../aop/support/MethodMatchers.java | 31 +++++++++---------- .../aspectj/TargetPointcutSelectionTests.java | 10 ++++-- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/IntroductionAwareMethodMatcher.java b/spring-aop/src/main/java/org/springframework/aop/IntroductionAwareMethodMatcher.java index ffefed7eee..ed228bfa20 100644 --- a/spring-aop/src/main/java/org/springframework/aop/IntroductionAwareMethodMatcher.java +++ b/spring-aop/src/main/java/org/springframework/aop/IntroductionAwareMethodMatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,9 +19,9 @@ package org.springframework.aop; import java.lang.reflect.Method; /** - * A specialized type of MethodMatcher that takes into account introductions when - * matching methods. If there are no introductions on the target class, a method - * matcher may be able to optimize matching more effectively for example. + * A specialized type of {@link MethodMatcher} that takes into account introductions + * when matching methods. If there are no introductions on the target class, + * a method matcher may be able to optimize matching more effectively for example. * * @author Adrian Colyer * @since 2.0 @@ -39,6 +39,6 @@ public interface IntroductionAwareMethodMatcher extends MethodMatcher { * asking is the subject on one or more introductions; {@code false} otherwise * @return whether or not this method matches statically */ - boolean matches(Method method, Class targetClass, boolean hasIntroductions); + boolean matches(Method method, Class targetClass, boolean hasIntroductions); } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/ClassFilters.java b/spring-aop/src/main/java/org/springframework/aop/support/ClassFilters.java index 1dea2cb113..58b19c9029 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/ClassFilters.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/ClassFilters.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,8 +23,7 @@ import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; /** - * Static utility methods for composing - * {@link org.springframework.aop.ClassFilter ClassFilters}. + * Static utility methods for composing {@link ClassFilter ClassFilters}. * * @author Rod Johnson * @author Rob Harrop @@ -96,9 +95,9 @@ public abstract class ClassFilters { this.filters = filters; } - public boolean matches(Class clazz) { - for (int i = 0; i < this.filters.length; i++) { - if (this.filters[i].matches(clazz)) { + public boolean matches(Class clazz) { + for (ClassFilter filter : this.filters) { + if (filter.matches(clazz)) { return true; } } @@ -130,9 +129,9 @@ public abstract class ClassFilters { this.filters = filters; } - public boolean matches(Class clazz) { - for (int i = 0; i < this.filters.length; i++) { - if (!this.filters[i].matches(clazz)) { + public boolean matches(Class clazz) { + for (ClassFilter filter : this.filters) { + if (!filter.matches(clazz)) { return false; } } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/MethodMatchers.java b/spring-aop/src/main/java/org/springframework/aop/support/MethodMatchers.java index ec2aa88949..3489e1b444 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/MethodMatchers.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/MethodMatchers.java @@ -25,12 +25,11 @@ import org.springframework.aop.MethodMatcher; import org.springframework.util.Assert; /** - * Static utility methods for composing - * {@link org.springframework.aop.MethodMatcher MethodMatchers}. + * Static utility methods for composing {@link MethodMatcher MethodMatchers}. * - *

A MethodMatcher may be evaluated statically (based on method - * and target class) or need further evaluation dynamically - * (based on arguments at the time of method invocation). + *

A MethodMatcher may be evaluated statically (based on method and target + * class) or need further evaluation dynamically (based on arguments at the + * time of method invocation). * * @author Rod Johnson * @author Rob Harrop @@ -88,7 +87,7 @@ public abstract class MethodMatchers { * asking is the subject on one or more introductions; {@code false} otherwise * @return whether or not this method matches statically */ - public static boolean matches(MethodMatcher mm, Method method, Class targetClass, boolean hasIntroductions) { + public static boolean matches(MethodMatcher mm, Method method, Class targetClass, boolean hasIntroductions) { Assert.notNull(mm, "MethodMatcher must not be null"); return ((mm instanceof IntroductionAwareMethodMatcher && ((IntroductionAwareMethodMatcher) mm).matches(method, targetClass, hasIntroductions)) || @@ -113,21 +112,21 @@ public abstract class MethodMatchers { this.mm2 = mm2; } - public boolean matches(Method method, Class targetClass, boolean hasIntroductions) { + public boolean matches(Method method, Class targetClass, boolean hasIntroductions) { return (matchesClass1(targetClass) && MethodMatchers.matches(this.mm1, method, targetClass, hasIntroductions)) || (matchesClass2(targetClass) && MethodMatchers.matches(this.mm2, method, targetClass, hasIntroductions)); } - public boolean matches(Method method, Class targetClass) { + public boolean matches(Method method, Class targetClass) { return (matchesClass1(targetClass) && this.mm1.matches(method, targetClass)) || (matchesClass2(targetClass) && this.mm2.matches(method, targetClass)); } - protected boolean matchesClass1(Class targetClass) { + protected boolean matchesClass1(Class targetClass) { return true; } - protected boolean matchesClass2(Class targetClass) { + protected boolean matchesClass2(Class targetClass) { return true; } @@ -135,7 +134,7 @@ public abstract class MethodMatchers { return this.mm1.isRuntime() || this.mm2.isRuntime(); } - public boolean matches(Method method, Class targetClass, Object[] args) { + public boolean matches(Method method, Class targetClass, Object[] args) { return this.mm1.matches(method, targetClass, args) || this.mm2.matches(method, targetClass, args); } @@ -179,12 +178,12 @@ public abstract class MethodMatchers { } @Override - protected boolean matchesClass1(Class targetClass) { + protected boolean matchesClass1(Class targetClass) { return this.cf1.matches(targetClass); } @Override - protected boolean matchesClass2(Class targetClass) { + protected boolean matchesClass2(Class targetClass) { return this.cf2.matches(targetClass); } @@ -225,12 +224,12 @@ public abstract class MethodMatchers { this.mm2 = mm2; } - public boolean matches(Method method, Class targetClass, boolean hasIntroductions) { + public boolean matches(Method method, Class targetClass, boolean hasIntroductions) { return MethodMatchers.matches(this.mm1, method, targetClass, hasIntroductions) && MethodMatchers.matches(this.mm2, method, targetClass, hasIntroductions); } - public boolean matches(Method method, Class targetClass) { + public boolean matches(Method method, Class targetClass) { return this.mm1.matches(method, targetClass) && this.mm2.matches(method, targetClass); } @@ -238,7 +237,7 @@ public abstract class MethodMatchers { return this.mm1.isRuntime() || this.mm2.isRuntime(); } - public boolean matches(Method method, Class targetClass, Object[] args) { + public boolean matches(Method method, Class targetClass, Object[] args) { // Because a dynamic intersection may be composed of a static and dynamic part, // we must avoid calling the 3-arg matches method on a dynamic matcher, as // it will probably be an unsupported operation. diff --git a/spring-context/src/test/java/org/springframework/aop/aspectj/TargetPointcutSelectionTests.java b/spring-context/src/test/java/org/springframework/aop/aspectj/TargetPointcutSelectionTests.java index f01bf14a47..5e71d7d154 100644 --- a/spring-context/src/test/java/org/springframework/aop/aspectj/TargetPointcutSelectionTests.java +++ b/spring-context/src/test/java/org/springframework/aop/aspectj/TargetPointcutSelectionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,16 +34,21 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; public final class TargetPointcutSelectionTests { public TestInterface testImpl1; + public TestInterface testImpl2; + public TestAspect testAspectForTestImpl1; + public TestAspect testAspectForAbstractTestImpl; + public TestInterceptor testInterceptor; @Before public void setUp() { ClassPathXmlApplicationContext ctx = - new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); + new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); + testImpl1 = (TestInterface) ctx.getBean("testImpl1"); testImpl2 = (TestInterface) ctx.getBean("testImpl2"); testAspectForTestImpl1 = (TestAspect) ctx.getBean("testAspectForTestImpl1"); @@ -55,6 +60,7 @@ public final class TargetPointcutSelectionTests { testInterceptor.count = 0; } + @Test public void testTargetSelectionForMatchedType() { testImpl1.interfaceMethod();