добавлены методы поиска 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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user