commit
fb8224d8d4
|
@ -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>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue