113 lines
3.2 KiB
Java
113 lines
3.2 KiB
Java
package ru.dmitriymx.reflection;
|
|
|
|
import lombok.ToString;
|
|
|
|
import java.lang.annotation.Annotation;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Method;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
|
|
@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 List<ReflectionField> fieldsWithAnnotation(final Class<? extends Annotation> annotationType) {
|
|
final Class clazz = object.getClass();
|
|
|
|
return Stream.of(clazz.getDeclaredFields())
|
|
.filter(field -> field.isAnnotationPresent(annotationType))
|
|
.map(field -> new ReflectionField(object, field))
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|