Skip to content

Commit 543c87f

Browse files
author
Chris Oberle
committed
initial import of source for BAEL-1786
1 parent 523fbf4 commit 543c87f

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

core-java-8/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@
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>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

0 commit comments

Comments
 (0)