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 fullName ;
6+
7+ public Person (String fullName ) {
8+ this .fullName = fullName ;
9+ }
10+
11+ public void setFullName (String fullName ) {
12+ this .fullName = fullName ;
13+ }
14+
15+ public String getFullName () {
16+ return fullName ;
17+ }
18+ }
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+ import java .util .Optional ;
9+
10+ import org .junit .Test ;
11+
12+ public class MethodParamNameTest {
13+
14+ @ Test
15+ public void whenGetConstructorParams_thenOk ()
16+ throws NoSuchMethodException , SecurityException {
17+ List <Parameter > parameters
18+ = Arrays .asList (Person .class .getConstructor (String .class ).getParameters ());
19+ Optional <Parameter > parameter
20+ = parameters .stream ().filter (Parameter ::isNamePresent ).findFirst ();
21+ assertThat (parameter .get ().getName ()).isEqualTo ("fullName" );
22+ }
23+
24+ @ Test
25+ public void whenGetMethodParams_thenOk ()
26+ throws NoSuchMethodException , SecurityException {
27+ List <Parameter > parameters
28+ = Arrays .asList (
29+ Person .class .getMethod ("setFullName" , String .class ).getParameters ());
30+ Optional <Parameter > parameter
31+ = parameters .stream ().filter (Parameter ::isNamePresent ).findFirst ();
32+ assertThat (parameter .get ().getName ()).isEqualTo ("fullName" );
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments