1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .core ;
18
18
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 ;
19
24
import java .lang .reflect .Method ;
20
25
21
26
import org .junit .Before ;
25
30
26
31
/**
27
32
* @author Arjen Poutsma
33
+ * @author Juergen Hoeller
34
+ * @author Sam Brannen
28
35
*/
29
36
public class MethodParameterTests {
30
37
38
+ private Method method ;
39
+
31
40
private MethodParameter stringParameter ;
32
41
33
42
private MethodParameter longParameter ;
@@ -36,13 +45,14 @@ public class MethodParameterTests {
36
45
37
46
38
47
@ 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 );
41
50
stringParameter = new MethodParameter (method , 0 );
42
51
longParameter = new MethodParameter (method , 1 );
43
52
intReturnType = new MethodParameter (method , -1 );
44
53
}
45
54
55
+
46
56
@ Test
47
57
public void testEquals () throws NoSuchMethodException {
48
58
assertEquals (stringParameter , stringParameter );
@@ -60,8 +70,8 @@ public void testEquals() throws NoSuchMethodException {
60
70
MethodParameter methodParameter = new MethodParameter (method , 0 );
61
71
assertEquals (stringParameter , methodParameter );
62
72
assertEquals (methodParameter , stringParameter );
63
- assertFalse (longParameter . equals ( methodParameter ) );
64
- assertFalse (methodParameter . equals ( longParameter ) );
73
+ assertNotEquals (longParameter , methodParameter );
74
+ assertNotEquals (methodParameter , longParameter );
65
75
}
66
76
67
77
@ Test
@@ -73,12 +83,58 @@ public void testHashCode() throws NoSuchMethodException {
73
83
Method method = getClass ().getMethod ("method" , String .class , Long .TYPE );
74
84
MethodParameter methodParameter = new MethodParameter (method , 0 );
75
85
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 ));
77
114
}
78
115
79
116
80
117
public int method (String p1 , long p2 ) {
81
118
return 42 ;
82
119
}
83
120
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
+
84
140
}
0 commit comments