import code
This commit is contained in:
106
src/main/java/ru/dmitriymx/reflection/ReflectionClass.java
Normal file
106
src/main/java/ru/dmitriymx/reflection/ReflectionClass.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class ReflectionClass {
|
||||
|
||||
private final Class clazz;
|
||||
|
||||
public ReflectionClass(Class clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public ReflectionClass(String className) {
|
||||
this.clazz = ReflectionClass.forName(className);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ReflectionConstructor constructor(Class<?>... parameterTypes) {
|
||||
try {
|
||||
return new ReflectionConstructor(this.clazz.getDeclaredConstructor(parameterTypes));
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ReflectionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<ReflectionField> fieldsList() {
|
||||
Field[] declaredFields = clazz.getDeclaredFields();
|
||||
|
||||
if (declaredFields.length > 0) {
|
||||
List<ReflectionField> result = new ArrayList<>(10);
|
||||
|
||||
for (Field field : declaredFields) {
|
||||
if (Modifier.isStatic(field.getModifiers())) {
|
||||
result.add(new ReflectionField(clazz, field));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public ReflectionField field(String name) {
|
||||
try {
|
||||
Field field = clazz.getDeclaredField(name);
|
||||
if (Modifier.isStatic(field.getModifiers())) {
|
||||
return new ReflectionField(clazz, field);
|
||||
} else {
|
||||
throw new ReflectionException(
|
||||
"No such static field '" + name + "' in Class " + clazz.getCanonicalName());
|
||||
}
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new ReflectionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<ReflectionMethod> methodsList() {
|
||||
Method[] declaredMethods = clazz.getDeclaredMethods();
|
||||
|
||||
if (declaredMethods.length > 0) {
|
||||
List<ReflectionMethod> result = new ArrayList<>(10);
|
||||
|
||||
for (Method method : declaredMethods) {
|
||||
if (Modifier.isStatic(method.getModifiers())) {
|
||||
result.add(new ReflectionMethod(clazz, method));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ReflectionMethod method(String name, Class<?>... parameterTypes) {
|
||||
try {
|
||||
Method method = clazz.getDeclaredMethod(name, parameterTypes);
|
||||
if (Modifier.isStatic(method.getModifiers())) {
|
||||
return new ReflectionMethod(clazz, method);
|
||||
} else {
|
||||
throw new ReflectionException(
|
||||
"No such static method '" + name + "(" + Arrays.toString(parameterTypes) + ")' " +
|
||||
"in Class " + clazz.getCanonicalName()
|
||||
);
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ReflectionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Class forName(String className) {
|
||||
try {
|
||||
return Class.forName(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new ReflectionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class ReflectionConstructor {
|
||||
|
||||
private final Constructor constructor;
|
||||
|
||||
public ReflectionConstructor(Constructor constructor) {
|
||||
this.constructor = constructor;
|
||||
}
|
||||
|
||||
public ReflectionObject newInstance(Object... params) {
|
||||
try {
|
||||
if (!constructor.isAccessible()) {
|
||||
constructor.setAccessible(true);
|
||||
}
|
||||
|
||||
return new ReflectionObject(constructor.newInstance(params));
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new ReflectionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
public class ReflectionException extends RuntimeException {
|
||||
|
||||
public ReflectionException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public ReflectionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
59
src/main/java/ru/dmitriymx/reflection/ReflectionField.java
Normal file
59
src/main/java/ru/dmitriymx/reflection/ReflectionField.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
@EqualsAndHashCode
|
||||
public class ReflectionField {
|
||||
|
||||
private final Object object;
|
||||
private final Class clazz;
|
||||
private final Field field;
|
||||
|
||||
public ReflectionField(Object object, Field field) {
|
||||
this.object = object;
|
||||
this.clazz = object.getClass();
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public ReflectionField(Class clazz, Field field) {
|
||||
this.object = null;
|
||||
this.clazz = clazz;
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return field.getName();
|
||||
}
|
||||
|
||||
public ReflectionObject get() {
|
||||
try {
|
||||
if (!field.isAccessible()) {
|
||||
field.setAccessible(true);
|
||||
}
|
||||
|
||||
return new ReflectionObject(field.get(object));
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new ReflectionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T get(Class<T> clazz) {
|
||||
return get().getOriginalObject(clazz);
|
||||
}
|
||||
|
||||
public boolean isStatic() {
|
||||
return Modifier.isStatic(field.getModifiers());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ReflectionField{" +
|
||||
"objectClass=" + clazz +
|
||||
", fieldName=" + field.getName() +
|
||||
", isStatic=" + isStatic() +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
64
src/main/java/ru/dmitriymx/reflection/ReflectionMethod.java
Normal file
64
src/main/java/ru/dmitriymx/reflection/ReflectionMethod.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
@EqualsAndHashCode
|
||||
public class ReflectionMethod {
|
||||
|
||||
private final Object object;
|
||||
private final Class clazz;
|
||||
private final Method method;
|
||||
|
||||
public ReflectionMethod(Object object, Method method) {
|
||||
this.object = object;
|
||||
this.clazz = object.getClass();
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public ReflectionMethod(Class clazz, Method method) {
|
||||
this.object = null;
|
||||
this.clazz = clazz;
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return method.getName();
|
||||
}
|
||||
|
||||
public boolean isStatic() {
|
||||
return Modifier.isStatic(method.getModifiers());
|
||||
}
|
||||
|
||||
public ReflectionObject invoke(Object... parameters) {
|
||||
try {
|
||||
if (!method.isAccessible()) {
|
||||
method.setAccessible(true);
|
||||
}
|
||||
Object resultInvoke = method.invoke(object, parameters);
|
||||
if (resultInvoke != null) {
|
||||
return new ReflectionObject(resultInvoke);
|
||||
} else {
|
||||
return ReflectionObject.NULL;
|
||||
}
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new ReflectionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T invoke(Class<T> clazz, Object... parameters) {
|
||||
return invoke(parameters).getOriginalObject(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ReflectionMethod{" +
|
||||
"objectClass=" + clazz +
|
||||
", methodName=" + method.getName() +
|
||||
", isStatic=" + isStatic() +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
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