Skip to content

Commit 5f6b042

Browse files
committed
Workaround for inner class constructor parameter annotation bug in javac
Issue: SPR-16652 (cherry picked from commit 53d0139)
1 parent 618cb61 commit 5f6b042

File tree

2 files changed

+81
-12
lines changed

2 files changed

+81
-12
lines changed

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

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -21,6 +21,7 @@
2121
import java.lang.reflect.Constructor;
2222
import java.lang.reflect.Member;
2323
import java.lang.reflect.Method;
24+
import java.lang.reflect.Modifier;
2425
import java.lang.reflect.ParameterizedType;
2526
import java.lang.reflect.Type;
2627
import java.util.HashMap;
@@ -47,6 +48,8 @@
4748
*/
4849
public class MethodParameter {
4950

51+
private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0];
52+
5053
private static final Class<?> javaUtilOptionalClass;
5154

5255
static {
@@ -482,17 +485,27 @@ public <A extends Annotation> boolean hasMethodAnnotation(Class<A> annotationTyp
482485
* Return the annotations associated with the specific method/constructor parameter.
483486
*/
484487
public Annotation[] getParameterAnnotations() {
485-
if (this.parameterAnnotations == null) {
488+
Annotation[] paramAnns = this.parameterAnnotations;
489+
if (paramAnns == null) {
486490
Annotation[][] annotationArray = (this.method != null ?
487491
this.method.getParameterAnnotations() : this.constructor.getParameterAnnotations());
488-
if (this.parameterIndex >= 0 && this.parameterIndex < annotationArray.length) {
489-
this.parameterAnnotations = adaptAnnotationArray(annotationArray[this.parameterIndex]);
492+
int index = this.parameterIndex;
493+
if (this.constructor != null && this.constructor.getDeclaringClass().isMemberClass() &&
494+
!Modifier.isStatic(this.constructor.getDeclaringClass().getModifiers()) &&
495+
annotationArray.length == this.constructor.getParameterTypes().length - 1) {
496+
// Bug in javac in JDK <9: annotation array excludes enclosing instance parameter
497+
// for inner classes, so access it with the actual parameter index lowered by 1
498+
index = this.parameterIndex - 1;
499+
}
500+
if (index >= 0 && index < annotationArray.length) {
501+
paramAnns = adaptAnnotationArray(annotationArray[index]);
490502
}
491503
else {
492-
this.parameterAnnotations = new Annotation[0];
504+
paramAnns = EMPTY_ANNOTATION_ARRAY;
493505
}
506+
this.parameterAnnotations = paramAnns;
494507
}
495-
return this.parameterAnnotations;
508+
return paramAnns;
496509
}
497510

498511
/**

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

+62-6
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-2018 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,6 +16,11 @@
1616

1717
package org.springframework.core;
1818

19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
import java.lang.reflect.Constructor;
1924
import java.lang.reflect.Method;
2025

2126
import org.junit.Before;
@@ -25,9 +30,13 @@
2530

2631
/**
2732
* @author Arjen Poutsma
33+
* @author Juergen Hoeller
34+
* @author Sam Brannen
2835
*/
2936
public class MethodParameterTests {
3037

38+
private Method method;
39+
3140
private MethodParameter stringParameter;
3241

3342
private MethodParameter longParameter;
@@ -36,13 +45,14 @@ public class MethodParameterTests {
3645

3746

3847
@Before
39-
public void setUp() throws NoSuchMethodException {
40-
Method method = getClass().getMethod("method", String.class, Long.TYPE);
48+
public void setup() throws NoSuchMethodException {
49+
method = getClass().getMethod("method", String.class, Long.TYPE);
4150
stringParameter = new MethodParameter(method, 0);
4251
longParameter = new MethodParameter(method, 1);
4352
intReturnType = new MethodParameter(method, -1);
4453
}
4554

55+
4656
@Test
4757
public void testEquals() throws NoSuchMethodException {
4858
assertEquals(stringParameter, stringParameter);
@@ -60,8 +70,8 @@ public void testEquals() throws NoSuchMethodException {
6070
MethodParameter methodParameter = new MethodParameter(method, 0);
6171
assertEquals(stringParameter, methodParameter);
6272
assertEquals(methodParameter, stringParameter);
63-
assertFalse(longParameter.equals(methodParameter));
64-
assertFalse(methodParameter.equals(longParameter));
73+
assertNotEquals(longParameter, methodParameter);
74+
assertNotEquals(methodParameter, longParameter);
6575
}
6676

6777
@Test
@@ -73,12 +83,58 @@ public void testHashCode() throws NoSuchMethodException {
7383
Method method = getClass().getMethod("method", String.class, Long.TYPE);
7484
MethodParameter methodParameter = new MethodParameter(method, 0);
7585
assertEquals(stringParameter.hashCode(), methodParameter.hashCode());
76-
assertTrue(longParameter.hashCode() != methodParameter.hashCode());
86+
assertNotEquals(longParameter.hashCode(), methodParameter.hashCode());
87+
}
88+
89+
@Test
90+
public void annotatedConstructorParameterInStaticNestedClass() throws Exception {
91+
Constructor<?> constructor = NestedClass.class.getDeclaredConstructor(String.class);
92+
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(constructor, 0);
93+
assertEquals(String.class, methodParameter.getParameterType());
94+
assertNotNull("Failed to find @Param annotation", methodParameter.getParameterAnnotation(Param.class));
95+
assertNotNull(methodParameter.getParameterAnnotation(Param.class));
96+
}
97+
98+
@Test // SPR-16652
99+
public void annotatedConstructorParameterInInnerClass() throws Exception {
100+
Constructor<?> constructor = InnerClass.class.getConstructor(getClass(), String.class, Integer.class);
101+
102+
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(constructor, 0);
103+
assertEquals(getClass(), methodParameter.getParameterType());
104+
assertNull(methodParameter.getParameterAnnotation(Param.class));
105+
106+
methodParameter = MethodParameter.forMethodOrConstructor(constructor, 1);
107+
assertEquals(String.class, methodParameter.getParameterType());
108+
// The following assertion currently fails if this test class is compiled using JDK 8.
109+
assertNotNull("Failed to find @Param annotation", methodParameter.getParameterAnnotation(Param.class));
110+
111+
methodParameter = MethodParameter.forMethodOrConstructor(constructor, 2);
112+
assertEquals(Integer.class, methodParameter.getParameterType());
113+
assertNull(methodParameter.getParameterAnnotation(Param.class));
77114
}
78115

79116

80117
public int method(String p1, long p2) {
81118
return 42;
82119
}
83120

121+
@SuppressWarnings("unused")
122+
private static class NestedClass {
123+
124+
NestedClass(@Param String s) {
125+
}
126+
}
127+
128+
@SuppressWarnings("unused")
129+
private class InnerClass {
130+
131+
public InnerClass(@Param String s, Integer i) {
132+
}
133+
}
134+
135+
@Retention(RetentionPolicy.RUNTIME)
136+
@Target(ElementType.PARAMETER)
137+
private @interface Param {
138+
}
139+
84140
}

0 commit comments

Comments
 (0)