Skip to content

Commit a00e00b

Browse files
committed
Fixed CGLIB proxy class leaks through further equals/hashCode implementations in Spring AOP pointcuts
Issue: SPR-8008
1 parent b0fce78 commit a00e00b

File tree

7 files changed

+135
-10
lines changed

7 files changed

+135
-10
lines changed

org.springframework.aop/src/main/java/org/springframework/aop/aspectj/AbstractAspectJAdvice.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -681,6 +681,23 @@ public AdviceExcludingMethodMatcher(Method adviceMethod) {
681681
public boolean matches(Method method, Class targetClass) {
682682
return !this.adviceMethod.equals(method);
683683
}
684+
685+
@Override
686+
public boolean equals(Object other) {
687+
if (this == other) {
688+
return true;
689+
}
690+
if (!(other instanceof AdviceExcludingMethodMatcher)) {
691+
return false;
692+
}
693+
AdviceExcludingMethodMatcher otherMm = (AdviceExcludingMethodMatcher) other;
694+
return this.adviceMethod.equals(otherMm.adviceMethod);
695+
}
696+
697+
@Override
698+
public int hashCode() {
699+
return this.adviceMethod.hashCode();
700+
}
684701
}
685702

686703
}

org.springframework.aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMethodMatcher.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,4 +56,21 @@ public boolean matches(Method method, Class targetClass) {
5656
return (specificMethod != method && specificMethod.isAnnotationPresent(this.annotationType));
5757
}
5858

59+
@Override
60+
public boolean equals(Object other) {
61+
if (this == other) {
62+
return true;
63+
}
64+
if (!(other instanceof AnnotationMethodMatcher)) {
65+
return false;
66+
}
67+
AnnotationMethodMatcher otherMm = (AnnotationMethodMatcher) other;
68+
return this.annotationType.equals(otherMm.annotationType);
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return this.annotationType.hashCode();
74+
}
75+
5976
}

org.springframework.context/src/main/java/org/springframework/cache/annotation/AnnotationCacheOperationSource.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
* {@code CacheOperationSource}.
4040
*
4141
* @author Costin Leau
42+
* @author Juergen Hoeller
4243
* @since 3.1
4344
*/
4445
@SuppressWarnings("serial")
@@ -71,6 +72,16 @@ public AnnotationCacheOperationSource(boolean publicMethodsOnly) {
7172
this.annotationParsers.add(new SpringCacheAnnotationParser());
7273
}
7374

75+
/**
76+
* Create a custom AnnotationCacheOperationSource.
77+
* @param annotationParser the CacheAnnotationParser to use
78+
*/
79+
public AnnotationCacheOperationSource(CacheAnnotationParser annotationParser) {
80+
this.publicMethodsOnly = true;
81+
Assert.notNull(annotationParser, "CacheAnnotationParser must not be null");
82+
this.annotationParsers = Collections.singleton(annotationParser);
83+
}
84+
7485
/**
7586
* Create a custom AnnotationCacheOperationSource.
7687
* @param annotationParsers the CacheAnnotationParser to use
@@ -83,6 +94,16 @@ public AnnotationCacheOperationSource(CacheAnnotationParser... annotationParsers
8394
this.annotationParsers = parsers;
8495
}
8596

97+
/**
98+
* Create a custom AnnotationCacheOperationSource.
99+
* @param annotationParsers the CacheAnnotationParser to use
100+
*/
101+
public AnnotationCacheOperationSource(Set<CacheAnnotationParser> annotationParsers) {
102+
this.publicMethodsOnly = true;
103+
Assert.notEmpty(annotationParsers, "At least one CacheAnnotationParser needs to be specified");
104+
this.annotationParsers = annotationParsers;
105+
}
106+
86107

87108
@Override
88109
protected Collection<CacheOperation> findCacheOperations(Class<?> clazz) {
@@ -106,7 +127,6 @@ protected Collection<CacheOperation> findCacheOperations(Method method) {
106127
*/
107128
protected Collection<CacheOperation> determineCacheOperations(AnnotatedElement ae) {
108129
Collection<CacheOperation> ops = null;
109-
110130
for (CacheAnnotationParser annotationParser : this.annotationParsers) {
111131
Collection<CacheOperation> annOps = annotationParser.parseCacheAnnotations(ae);
112132
if (annOps != null) {
@@ -126,4 +146,24 @@ protected Collection<CacheOperation> determineCacheOperations(AnnotatedElement a
126146
protected boolean allowPublicMethodsOnly() {
127147
return this.publicMethodsOnly;
128148
}
149+
150+
151+
@Override
152+
public boolean equals(Object other) {
153+
if (this == other) {
154+
return true;
155+
}
156+
if (!(other instanceof AnnotationCacheOperationSource)) {
157+
return false;
158+
}
159+
AnnotationCacheOperationSource otherCos = (AnnotationCacheOperationSource) other;
160+
return (this.annotationParsers.equals(otherCos.annotationParsers) &&
161+
this.publicMethodsOnly == otherCos.publicMethodsOnly);
162+
}
163+
164+
@Override
165+
public int hashCode() {
166+
return this.annotationParsers.hashCode();
167+
}
168+
129169
}

org.springframework.context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -135,7 +135,7 @@ Collection<CacheOperation> parseCachingAnnotation(AnnotatedElement ae, Caching c
135135
return ops;
136136
}
137137

138-
private static <T extends Annotation> Collection<T> getAnnotations(AnnotatedElement ae, Class<T> annotationType) {
138+
private <T extends Annotation> Collection<T> getAnnotations(AnnotatedElement ae, Class<T> annotationType) {
139139
Collection<T> anns = new ArrayList<T>(2);
140140

141141
// look at raw annotation
@@ -154,4 +154,15 @@ private static <T extends Annotation> Collection<T> getAnnotations(AnnotatedElem
154154

155155
return (anns.isEmpty() ? null : anns);
156156
}
157-
}
157+
158+
@Override
159+
public boolean equals(Object other) {
160+
return (this == other || other instanceof SpringCacheAnnotationParser);
161+
}
162+
163+
@Override
164+
public int hashCode() {
165+
return SpringCacheAnnotationParser.class.hashCode();
166+
}
167+
168+
}

org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -147,4 +147,23 @@ protected boolean allowPublicMethodsOnly() {
147147
return this.publicMethodsOnly;
148148
}
149149

150+
151+
@Override
152+
public boolean equals(Object other) {
153+
if (this == other) {
154+
return true;
155+
}
156+
if (!(other instanceof AnnotationTransactionAttributeSource)) {
157+
return false;
158+
}
159+
AnnotationTransactionAttributeSource otherTas = (AnnotationTransactionAttributeSource) other;
160+
return (this.annotationParsers.equals(otherTas.annotationParsers) &&
161+
this.publicMethodsOnly == otherTas.publicMethodsOnly);
162+
}
163+
164+
@Override
165+
public int hashCode() {
166+
return this.annotationParsers.hashCode();
167+
}
168+
150169
}

org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/Ejb3TransactionAnnotationParser.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,6 +47,16 @@ public TransactionAttribute parseTransactionAnnotation(javax.ejb.TransactionAttr
4747
return new Ejb3TransactionAttribute(ann.value());
4848
}
4949

50+
@Override
51+
public boolean equals(Object other) {
52+
return (this == other || other instanceof Ejb3TransactionAnnotationParser);
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return Ejb3TransactionAnnotationParser.class.hashCode();
58+
}
59+
5060

5161
/**
5262
* EJB3-specific TransactionAttribute, implementing EJB3's rollback rules
@@ -58,6 +68,7 @@ public Ejb3TransactionAttribute(TransactionAttributeType type) {
5868
setPropagationBehaviorName(PREFIX_PROPAGATION + type.name());
5969
}
6070

71+
@Override
6172
public boolean rollbackOn(Throwable ex) {
6273
ApplicationException ann = ex.getClass().getAnnotation(ApplicationException.class);
6374
return (ann != null ? ann.rollback() : super.rollbackOn(ex));

org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/SpringTransactionAnnotationParser.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,4 +76,14 @@ public TransactionAttribute parseTransactionAnnotation(Transactional ann) {
7676
return rbta;
7777
}
7878

79+
@Override
80+
public boolean equals(Object other) {
81+
return (this == other || other instanceof SpringTransactionAnnotationParser);
82+
}
83+
84+
@Override
85+
public int hashCode() {
86+
return SpringTransactionAnnotationParser.class.hashCode();
87+
}
88+
7989
}

0 commit comments

Comments
 (0)