File tree Expand file tree Collapse file tree
main/java/com/baeldung/reflect
test/java/com/baeldung/reflect Expand file tree Collapse file tree Original file line number Diff line number Diff line change 195195 </execution >
196196 </executions >
197197 </plugin >
198+ <plugin >
199+ <groupId >org.apache.maven.plugins</groupId >
200+ <artifactId >maven-compiler-plugin</artifactId >
201+ <version >3.1</version >
202+ <configuration >
203+ <source >1.8</source >
204+ <target >1.8</target >
205+ <compilerArgument >-parameters</compilerArgument >
206+ </configuration >
207+ </plugin >
198208 <plugin >
199209 <groupId >org.springframework.boot</groupId >
200210 <artifactId >spring-boot-maven-plugin</artifactId >
Original file line number Diff line number Diff line change 1+ package com .baeldung .reflect ;
2+
3+ public class Person {
4+
5+ private String firstName ;
6+ private String lastName ;
7+ private Integer age ;
8+
9+ public Person (String firstName , String lastName , Integer age ) {
10+ this .firstName = firstName ;
11+ this .lastName = lastName ;
12+ this .age = age ;
13+ }
14+
15+ public Person () {}
16+
17+ public String getFirstName () {
18+ return firstName ;
19+ }
20+
21+ public void setFirstName (String firstName ) {
22+ this .firstName = firstName ;
23+ }
24+
25+ public String getLastName () {
26+ return lastName ;
27+ }
28+
29+ public void setLastName (String lastName ) {
30+ this .lastName = lastName ;
31+ }
32+
33+ public Integer getAge () {
34+ return age ;
35+ }
36+
37+ public void setAge (Integer age ) {
38+ this .age = age ;
39+ }
40+
41+
42+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .reflect ;
2+
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
5+ import java .lang .reflect .Parameter ;
6+ import java .util .Arrays ;
7+ import java .util .List ;
8+
9+ import static java .util .stream .Collectors .toList ;
10+ import org .junit .Test ;
11+
12+ public class MethodParamNameTest {
13+
14+ @ Test
15+ public void whenGetConstructorParams_thenOk () throws NoSuchMethodException , SecurityException {
16+ List <Parameter > parameters
17+ = Arrays .asList (
18+ Person .class .getConstructor (String .class , String .class , Integer .class )
19+ .getParameters ());
20+ List <String > parameterNames
21+ = parameters .stream ().map (Parameter ::getName ).collect (toList ());
22+ assertThat (parameterNames )
23+ .containsExactlyInAnyOrder ("lastName" , "firstName" , "age" );
24+ }
25+
26+ @ Test
27+ public void whenGetMethodParams_thenOk () throws NoSuchMethodException , SecurityException {
28+ List <Parameter > parameters
29+ = Arrays .asList (
30+ Person .class .getMethod ("setLastName" , String .class ).getParameters ());
31+ assertThat (parameters .get (0 ).getName ()).isEqualTo ("lastName" );
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments