import code
This commit is contained in:
100
src/main/java/ru/dmitriymx/reflection/ReflectionObject.java
Normal file
100
src/main/java/ru/dmitriymx/reflection/ReflectionObject.java
Normal file
@@ -0,0 +1,100 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
import lombok.ToString;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@ToString
|
||||
public class ReflectionObject {
|
||||
|
||||
public static final ReflectionObject NULL = new ReflectionObject(null);
|
||||
|
||||
private final Object object;
|
||||
|
||||
public ReflectionObject(Object object) {
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
public Object getOriginalObject() {
|
||||
return object;
|
||||
}
|
||||
|
||||
public <T> T getOriginalObject(Class<T> clazz) {
|
||||
return object == null ? null : clazz.cast(object);
|
||||
}
|
||||
|
||||
public List<ReflectionField> fieldList() {
|
||||
final Class clazz = object.getClass();
|
||||
|
||||
Field[] declaredFields = clazz.getDeclaredFields();
|
||||
|
||||
if (declaredFields.length > 0) {
|
||||
List<ReflectionField> result = new ArrayList<>(declaredFields.length);
|
||||
|
||||
for (Field field : declaredFields) {
|
||||
result.add(new ReflectionField(object, field));
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public ReflectionField field(String name) {
|
||||
final Class clazz = object.getClass();
|
||||
|
||||
try {
|
||||
return new ReflectionField(object, clazz.getDeclaredField(name));
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new ReflectionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<ReflectionMethod> methodsList() {
|
||||
final Class clazz = object.getClass();
|
||||
|
||||
Method[] declaredMethods = clazz.getDeclaredMethods();
|
||||
if (declaredMethods.length > 0) {
|
||||
List<ReflectionMethod> result = new ArrayList<>(declaredMethods.length);
|
||||
|
||||
for (Method method : declaredMethods) {
|
||||
result.add(new ReflectionMethod(object, method));
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ReflectionMethod method(String methodName, Class<?>... parameterTypes) {
|
||||
final Class clazz = object.getClass();
|
||||
|
||||
try {
|
||||
return new ReflectionMethod(object, clazz.getDeclaredMethod(methodName, parameterTypes));
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ReflectionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof ReflectionObject)) return false;
|
||||
|
||||
final ReflectionObject other = (ReflectionObject) o;
|
||||
return this.object.equals(other.object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int PRIME = 59;
|
||||
return PRIME + (this.object == null ? 43 : this.object.hashCode());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user