Merge pull request #4367 from chrisoberle/master

BAEL-1786
This commit is contained in:
Tom Hombergs 2018-06-03 21:32:03 +02:00 committed by GitHub
commit fb8224d8d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 0 deletions

View File

@ -195,6 +195,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

View File

@ -0,0 +1,18 @@
package com.baeldung.reflect;
public class Person {
private String fullName;
public Person(String fullName) {
this.fullName = fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getFullName() {
return fullName;
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.reflect;
import static org.assertj.core.api.Assertions.assertThat;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.junit.Test;
public class MethodParamNameTest {
@Test
public void whenGetConstructorParams_thenOk()
throws NoSuchMethodException, SecurityException {
List<Parameter> parameters
= Arrays.asList(Person.class.getConstructor(String.class).getParameters());
Optional<Parameter> parameter
= parameters.stream().filter(Parameter::isNamePresent).findFirst();
assertThat(parameter.get().getName()).isEqualTo("fullName");
}
@Test
public void whenGetMethodParams_thenOk()
throws NoSuchMethodException, SecurityException {
List<Parameter> parameters
= Arrays.asList(
Person.class.getMethod("setFullName", String.class).getParameters());
Optional<Parameter> parameter
= parameters.stream().filter(Parameter::isNamePresent).findFirst();
assertThat(parameter.get().getName()).isEqualTo("fullName");
}
}