0

добавлены методы поиска Getter и Setter для полей

This commit is contained in:
2020-04-17 18:16:32 +03:00
parent 094318904c
commit a16fed9785
4 changed files with 62 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -29,6 +29,10 @@ public class ReflectionMethod {
return method.getName();
}
public Class<?> returnType() {
return method.getReturnType();
}
public boolean isStatic() {
return Modifier.isStatic(method.getModifiers());
}