добавлены методы поиска Getter и Setter для полей
This commit is contained in:
@@ -28,6 +28,10 @@ public class ReflectionField {
|
||||
return field.getName();
|
||||
}
|
||||
|
||||
public Class<?> type() {
|
||||
return field.getType();
|
||||
}
|
||||
|
||||
public ReflectionObject get() {
|
||||
try {
|
||||
if (!field.isAccessible()) {
|
||||
@@ -44,6 +48,21 @@ public class ReflectionField {
|
||||
return get().getOriginalObject(clazz);
|
||||
}
|
||||
|
||||
public ReflectionMethod getter() {
|
||||
final String methodName = "get" + formatNameForMethod();
|
||||
ReflectionMethod method = new ReflectionObject(object).method(methodName);
|
||||
if (method.returnType().equals(type())) {
|
||||
return method;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public ReflectionMethod setter() {
|
||||
final String methodName = "set" + formatNameForMethod();
|
||||
return new ReflectionObject(object).method(methodName, type());
|
||||
}
|
||||
|
||||
public boolean isStatic() {
|
||||
return Modifier.isStatic(field.getModifiers());
|
||||
}
|
||||
@@ -56,4 +75,8 @@ public class ReflectionField {
|
||||
", isStatic=" + isStatic() +
|
||||
'}';
|
||||
}
|
||||
|
||||
private String formatNameForMethod() {
|
||||
return name().substring(0, 1).toUpperCase() + name().substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,10 @@ public class ReflectionMethod {
|
||||
return method.getName();
|
||||
}
|
||||
|
||||
public Class<?> returnType() {
|
||||
return method.getReturnType();
|
||||
}
|
||||
|
||||
public boolean isStatic() {
|
||||
return Modifier.isStatic(method.getModifiers());
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ public class ReflectionObjectTest {
|
||||
new ReflectionObject(strObj).getOriginalObject(Integer.class);
|
||||
}
|
||||
|
||||
//region Field tests
|
||||
@Test
|
||||
public void fieldsList() {
|
||||
final List<String> expectedList = Arrays.asList("finalizedField", "simpleField");
|
||||
@@ -64,6 +65,30 @@ public class ReflectionObjectTest {
|
||||
refObj.field("field_not_exists");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void field_getter() {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
|
||||
ReflectionField refField = refObj.field("someIntField");
|
||||
assertNotNull(refField);
|
||||
|
||||
ReflectionMethod refGetter = refField.getter();
|
||||
assertNotNull(refGetter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void field_setter() {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
|
||||
ReflectionField refField = refObj.field("someIntField");
|
||||
assertNotNull(refField);
|
||||
|
||||
ReflectionMethod refSetter = refField.setter();
|
||||
assertNotNull(refSetter);
|
||||
}
|
||||
//endregion
|
||||
|
||||
//region Method tests
|
||||
@Test
|
||||
public void methodsList() {
|
||||
final List<String> expectedList = Arrays.asList("getSimpleField", "setSimpleField");
|
||||
@@ -105,4 +130,5 @@ public class ReflectionObjectTest {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
refObj.method("not_exists_method");
|
||||
}
|
||||
//endregion
|
||||
}
|
||||
@@ -5,6 +5,7 @@ class SomeObject {
|
||||
private static final int MAGIC_NUMBER = 33;
|
||||
private final String finalizedField = "value123";
|
||||
private String simpleField = "defaultValue";
|
||||
private int someIntField;
|
||||
|
||||
public String getSimpleField() {
|
||||
return simpleField;
|
||||
@@ -18,6 +19,14 @@ class SomeObject {
|
||||
simpleField = "123value";
|
||||
}
|
||||
|
||||
public int getSomeIntField() {
|
||||
return someIntField;
|
||||
}
|
||||
|
||||
public void setSomeIntField(int someIntField) {
|
||||
this.someIntField = someIntField;
|
||||
}
|
||||
|
||||
public static int getMagicNumber() {
|
||||
return MAGIC_NUMBER;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user