refactor example
This commit is contained in:
parent
543c87fa70
commit
d683b370f6
@ -2,41 +2,17 @@ package com.baeldung.reflect;
|
|||||||
|
|
||||||
public class Person {
|
public class Person {
|
||||||
|
|
||||||
private String firstName;
|
private String fullName;
|
||||||
private String lastName;
|
|
||||||
private Integer age;
|
|
||||||
|
|
||||||
public Person(String firstName, String lastName, Integer age) {
|
public Person(String fullName) {
|
||||||
this.firstName = firstName;
|
this.fullName = fullName;
|
||||||
this.lastName = lastName;
|
|
||||||
this.age = age;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person() {}
|
public void setFullName(String fullName) {
|
||||||
|
this.fullName = fullName;
|
||||||
public String getFirstName() {
|
|
||||||
return firstName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
public String getFullName() {
|
||||||
this.firstName = firstName;
|
return fullName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLastName() {
|
|
||||||
return lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLastName(String lastName) {
|
|
||||||
this.lastName = lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAge(Integer age) {
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,29 +5,30 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
import java.lang.reflect.Parameter;
|
import java.lang.reflect.Parameter;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static java.util.stream.Collectors.toList;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class MethodParamNameTest {
|
public class MethodParamNameTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetConstructorParams_thenOk() throws NoSuchMethodException, SecurityException {
|
public void whenGetConstructorParams_thenOk()
|
||||||
|
throws NoSuchMethodException, SecurityException {
|
||||||
List<Parameter> parameters
|
List<Parameter> parameters
|
||||||
= Arrays.asList(
|
= Arrays.asList(Person.class.getConstructor(String.class).getParameters());
|
||||||
Person.class.getConstructor(String.class, String.class, Integer.class)
|
Optional<Parameter> parameter
|
||||||
.getParameters());
|
= parameters.stream().filter(Parameter::isNamePresent).findFirst();
|
||||||
List<String> parameterNames
|
assertThat(parameter.get().getName()).isEqualTo("fullName");
|
||||||
= parameters.stream().map(Parameter::getName).collect(toList());
|
|
||||||
assertThat(parameterNames)
|
|
||||||
.containsExactlyInAnyOrder("lastName", "firstName", "age");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetMethodParams_thenOk() throws NoSuchMethodException, SecurityException {
|
public void whenGetMethodParams_thenOk()
|
||||||
|
throws NoSuchMethodException, SecurityException {
|
||||||
List<Parameter> parameters
|
List<Parameter> parameters
|
||||||
= Arrays.asList(
|
= Arrays.asList(
|
||||||
Person.class.getMethod("setLastName", String.class).getParameters());
|
Person.class.getMethod("setFullName", String.class).getParameters());
|
||||||
assertThat(parameters.get(0).getName()).isEqualTo("lastName");
|
Optional<Parameter> parameter
|
||||||
|
= parameters.stream().filter(Parameter::isNamePresent).findFirst();
|
||||||
|
assertThat(parameter.get().getName()).isEqualTo("fullName");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user