Skip to content

Avoid monitor contention inside MethodParameter.getParameterAnnotations() #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.lang.reflect.TypeVariable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.springframework.util.Assert;

Expand Down Expand Up @@ -66,7 +68,10 @@ public class MethodParameter {
Map<TypeVariable, Type> typeVariableMap;

private int hash = 0;


private static ConcurrentMap<Method, Annotation[][]> methodParamAnnotationsCache = new ConcurrentHashMap<Method, Annotation[][]>();
final private static Annotation[][] EMPTY_ANNOTATION_MATRIX = new Annotation[0][0];
final private static Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0];

/**
* Create a new MethodParameter for the given method, with nesting level 1.
Expand Down Expand Up @@ -280,7 +285,7 @@ public <T extends Annotation> T getMethodAnnotation(Class<T> annotationType) {
public Annotation[] getParameterAnnotations() {
if (this.parameterAnnotations == null) {
Annotation[][] annotationArray = (this.method != null ?
this.method.getParameterAnnotations() : this.constructor.getParameterAnnotations());
getMethodParameterAnnotations(this.method) : this.constructor.getParameterAnnotations());
if (this.parameterIndex >= 0 && this.parameterIndex < annotationArray.length) {
this.parameterAnnotations = annotationArray[this.parameterIndex];
}
Expand All @@ -290,6 +295,42 @@ public Annotation[] getParameterAnnotations() {
}
return this.parameterAnnotations;
}

/**
*
* @param m
* @return parameter annotations for given m. returns
*/
static Annotation[][] getMethodParameterAnnotations(Method m) {
assert m != null;

Annotation[][] result = getMethodParamAnnotationsCache().get(m);
if (result == null) {
//no point in synchronizing call to below since it's an idempotent read
result = m.getParameterAnnotations(); //always returns a new copy

//minimize cache size: do not store copies of empty arrays
if(result.length == 0)
{
result = EMPTY_ANNOTATION_MATRIX;
} else {
for (int i = 0; i < result.length; i++) {
if (result[i].length == 0) {
result[i] = EMPTY_ANNOTATION_ARRAY;
}
}
}
getMethodParamAnnotationsCache().put(m, result);
}

//always return deep copy to prevent caller from modifying cache state
Annotation[][] resultCopy = new Annotation[result.length][0];
for(int i = 0; i < result.length; i++)
{
resultCopy[i] = result[i].clone();
}
return resultCopy;
}

/**
* Return the parameter annotation of the given type, if available.
Expand Down Expand Up @@ -472,4 +513,8 @@ public int hashCode() {
return result;
}

static ConcurrentMap<Method, Annotation[][]> getMethodParamAnnotationsCache() {
return methodParamAnnotationsCache;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@

package org.springframework.core;

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

import org.junit.Before;
import org.junit.Test;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -76,10 +85,33 @@ public void testHashCode() throws NoSuchMethodException {
assertEquals(stringParameter.hashCode(), methodParameter.hashCode());
assertTrue(longParameter.hashCode() != methodParameter.hashCode());
}

@Test
public void testGetMethodParamaterAnnotations() {
Method method = stringParameter.getMethod();
Annotation[][] expectedAnnotations = method.getParameterAnnotations();
assertEquals(2, expectedAnnotations.length);
assertEquals(DummyAnnotation.class, expectedAnnotations[0][0].annotationType());

//start with empty cache
MethodParameter.getMethodParamAnnotationsCache().clear();

//check correctness
assertArrayEquals(expectedAnnotations, MethodParameter.getMethodParameterAnnotations(method));
//check that return value's been cached
assertArrayEquals(expectedAnnotations, MethodParameter.getMethodParamAnnotationsCache().get(method));
}


public int method(String p1, long p2) {
public int method(@DummyAnnotation String p1, long p2) {
return 42;
}

@Target({ CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface DummyAnnotation {

}


}