From be2d915cc2f9129db030a12cf193c5caa19e65c3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 5 Jan 2014 00:02:47 +0100 Subject: [PATCH] Consistent equals/hashCode/toString implementations in AnnotationMatchingPointcut/ClassFilter/MethodMatcher Issue: SPR-11275 Issue: SPR-11276 (cherry picked from commit 0de307b) --- .../annotation/AnnotationClassFilter.java | 26 +++++++++++++-- .../AnnotationMatchingPointcut.java | 33 ++++++++++++++++++- .../annotation/AnnotationMethodMatcher.java | 7 +++- .../annotation/AsyncAnnotationAdvisor.java | 2 +- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationClassFilter.java b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationClassFilter.java index 9136c8974e..ea8a3adf15 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationClassFilter.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationClassFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 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. @@ -59,10 +59,32 @@ public class AnnotationClassFilter implements ClassFilter { } - public boolean matches(Class clazz) { + public boolean matches(Class clazz) { return (this.checkInherited ? (AnnotationUtils.findAnnotation(clazz, this.annotationType) != null) : clazz.isAnnotationPresent(this.annotationType)); } + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (!(other instanceof AnnotationClassFilter)) { + return false; + } + AnnotationClassFilter otherCf = (AnnotationClassFilter) other; + return (this.annotationType.equals(otherCf.annotationType) && this.checkInherited == otherCf.checkInherited); + } + + @Override + public int hashCode() { + return this.annotationType.hashCode(); + } + + @Override + public String toString() { + return getClass().getName() + ": " + this.annotationType; + } + } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcut.java index acdbbeff88..5bc54af587 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcut.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. @@ -22,6 +22,7 @@ import org.springframework.aop.ClassFilter; import org.springframework.aop.MethodMatcher; import org.springframework.aop.Pointcut; import org.springframework.util.Assert; +import org.springframework.util.ObjectUtils; /** * Simple Pointcut that looks for a specific Java 5 annotation @@ -98,6 +99,36 @@ public class AnnotationMatchingPointcut implements Pointcut { return this.methodMatcher; } + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (!(other instanceof AnnotationMatchingPointcut)) { + return false; + } + AnnotationMatchingPointcut that = (AnnotationMatchingPointcut) other; + return ObjectUtils.nullSafeEquals(that.classFilter, this.classFilter) && + ObjectUtils.nullSafeEquals(that.methodMatcher, this.methodMatcher); + } + + @Override + public int hashCode() { + int code = 17; + if (this.classFilter != null) { + code = 37 * code + this.classFilter.hashCode(); + } + if (this.methodMatcher != null) { + code = 37 * code + this.methodMatcher.hashCode(); + } + return code; + } + + @Override + public String toString() { + return "AnnotationMatchingPointcut: " + this.classFilter + ", " +this.methodMatcher; + } + /** * Factory method for an AnnotationMatchingPointcut that matches diff --git a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMethodMatcher.java b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMethodMatcher.java index 4f4dd6c391..15f83f7b11 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMethodMatcher.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMethodMatcher.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. @@ -73,4 +73,9 @@ public class AnnotationMethodMatcher extends StaticMethodMatcher { return this.annotationType.hashCode(); } + @Override + public String toString() { + return getClass().getName() + ": " + this.annotationType; + } + } diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java index 35e57c59d2..78114af869 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java @@ -140,7 +140,7 @@ public class AsyncAnnotationAdvisor extends AbstractPointcutAdvisor implements B ComposablePointcut result = null; for (Class asyncAnnotationType : asyncAnnotationTypes) { Pointcut cpc = new AnnotationMatchingPointcut(asyncAnnotationType, true); - Pointcut mpc = new AnnotationMatchingPointcut(null, asyncAnnotationType); + Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(asyncAnnotationType); if (result == null) { result = new ComposablePointcut(cpc).union(mpc); }