Skip to content

Commit a35b9c9

Browse files
committed
Revert "Cache MethodParameter annotation lookup results"
This reverts commit c10d63d.
1 parent 33471a1 commit a35b9c9

File tree

2 files changed

+6
-78
lines changed

2 files changed

+6
-78
lines changed

spring-core/src/main/java/org/springframework/core/MethodParameter.java

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -26,8 +26,6 @@
2626
import java.lang.reflect.TypeVariable;
2727
import java.util.HashMap;
2828
import java.util.Map;
29-
import java.util.concurrent.ConcurrentHashMap;
30-
import java.util.concurrent.ConcurrentMap;
3129

3230
import org.springframework.util.Assert;
3331

@@ -39,21 +37,11 @@
3937
* @author Juergen Hoeller
4038
* @author Rob Harrop
4139
* @author Andy Clement
42-
* @author Nikita Tovstoles
43-
* @author Chris Beams
4440
* @since 2.0
4541
* @see GenericCollectionTypeResolver
4642
*/
4743
public class MethodParameter {
4844

49-
50-
private static final Annotation[][] EMPTY_ANNOTATION_MATRIX = new Annotation[0][0];
51-
52-
private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0];
53-
54-
static final ConcurrentMap<Method, Annotation[][]> methodParamAnnotationsCache =
55-
new ConcurrentHashMap<Method, Annotation[][]>();
56-
5745
private final Method method;
5846

5947
private final Constructor constructor;
@@ -291,7 +279,7 @@ public <T extends Annotation> T getMethodAnnotation(Class<T> annotationType) {
291279
public Annotation[] getParameterAnnotations() {
292280
if (this.parameterAnnotations == null) {
293281
Annotation[][] annotationArray = (this.method != null ?
294-
getMethodParameterAnnotations(this.method) : this.constructor.getParameterAnnotations());
282+
this.method.getParameterAnnotations() : this.constructor.getParameterAnnotations());
295283
if (this.parameterIndex >= 0 && this.parameterIndex < annotationArray.length) {
296284
this.parameterAnnotations = annotationArray[this.parameterIndex];
297285
}
@@ -450,41 +438,6 @@ else if (methodOrConstructor instanceof Constructor) {
450438
}
451439
}
452440

453-
/**
454-
* Return the parameter annotations for the given method, retrieving cached values
455-
* if a lookup has already been performed for this method, otherwise perform a fresh
456-
* lookup and populate the cache with the result before returning. <strong>For
457-
* internal use only.</strong>
458-
* @param method the method to introspect for parameter annotations
459-
*/
460-
static Annotation[][] getMethodParameterAnnotations(Method method) {
461-
Assert.notNull(method);
462-
463-
Annotation[][] result = methodParamAnnotationsCache.get(method);
464-
if (result == null) {
465-
result = method.getParameterAnnotations();
466-
467-
if(result.length == 0) {
468-
result = EMPTY_ANNOTATION_MATRIX;
469-
}
470-
else {
471-
for (int i = 0; i < result.length; i++) {
472-
if (result[i].length == 0) {
473-
result[i] = EMPTY_ANNOTATION_ARRAY;
474-
}
475-
}
476-
}
477-
methodParamAnnotationsCache.put(method, result);
478-
}
479-
480-
//always return deep copy to prevent caller from modifying cache state
481-
Annotation[][] resultCopy = new Annotation[result.length][0];
482-
for(int i = 0; i < result.length; i++) {
483-
resultCopy[i] = result[i].clone();
484-
}
485-
return resultCopy;
486-
}
487-
488441
@Override
489442
public boolean equals(Object obj) {
490443
if (this == obj) {
@@ -506,6 +459,7 @@ else if (this.getMember().equals(other.getMember())) {
506459
return false;
507460
}
508461

462+
509463
@Override
510464
public int hashCode() {
511465
int result = this.hash;

spring-core/src/test/java/org/springframework/core/MethodParameterTests.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -16,11 +16,6 @@
1616

1717
package org.springframework.core;
1818

19-
import java.lang.annotation.Annotation;
20-
import java.lang.annotation.ElementType;
21-
import java.lang.annotation.Retention;
22-
import java.lang.annotation.RetentionPolicy;
23-
import java.lang.annotation.Target;
2419
import java.lang.reflect.Method;
2520

2621
import org.junit.Before;
@@ -30,7 +25,6 @@
3025

3126
/**
3227
* @author Arjen Poutsma
33-
* @author Nikita Tovstoles
3428
*/
3529
public class MethodParameterTests {
3630

@@ -49,6 +43,7 @@ public void setUp() throws NoSuchMethodException {
4943
}
5044

5145

46+
5247
@Test
5348
public void testEquals() throws NoSuchMethodException {
5449
assertEquals(stringParameter, stringParameter);
@@ -82,30 +77,9 @@ public void testHashCode() throws NoSuchMethodException {
8277
assertTrue(longParameter.hashCode() != methodParameter.hashCode());
8378
}
8479

85-
@Test
86-
public void testGetMethodParamaterAnnotations() {
87-
Method method = stringParameter.getMethod();
88-
Annotation[][] expectedAnnotations = method.getParameterAnnotations();
89-
assertEquals(2, expectedAnnotations.length);
90-
assertEquals(DummyAnnotation.class, expectedAnnotations[0][0].annotationType());
91-
92-
//start with empty cache
93-
MethodParameter.methodParamAnnotationsCache.clear();
94-
95-
//check correctness
96-
assertArrayEquals(expectedAnnotations, MethodParameter.getMethodParameterAnnotations(method));
97-
//check that return value's been cached
98-
assertArrayEquals(expectedAnnotations, MethodParameter.methodParamAnnotationsCache.get(method));
99-
}
100-
10180

102-
public int method(@DummyAnnotation String p1, long p2) {
81+
public int method(String p1, long p2) {
10382
return 42;
10483
}
10584

106-
@Target(ElementType.PARAMETER)
107-
@Retention(RetentionPolicy.RUNTIME)
108-
public @interface DummyAnnotation {
109-
110-
}
11185
}

0 commit comments

Comments
 (0)