Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package reghzy.carrottools.utils.reflect;
- import reghzy.carrottools.utils.text.FormatMFLF;
- import javax.annotation.Nonnull;
- import javax.annotation.Nullable;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- /**
- * A class to cleanly use reflection without having to use try catch blocks everywhere
- */
- public class Reflect {
- /**
- * Casts the object value to another value (generic)
- *
- * <p>
- * This should not return null, unless you give a null value,
- * or are daft enough to try to cast an object that wasn't remapped :-)
- * </p>
- */
- public static <T> T cast(Object value) {
- if (value == null) {
- return null;
- }
- try {
- return (T) value;
- }
- catch (ClassCastException e) {
- throw new RuntimeException("Given value was probably not remapped", e);
- }
- }
- public static Class<?> findClass(String className) {
- try {
- return Class.forName(className);
- }
- catch (ClassNotFoundException e) {
- throw new RuntimeException(FormatMFLF.format("The class '{0}' could not be found", className), e);
- }
- }
- public static Method findPublicMethod(Class<?> clazz, @Nonnull String name, Class<?>... parameterTypes) {
- if (clazz == null) {
- throw new NullPointerException("Class cannot be null!");
- }
- Class<?> nextClass = clazz;
- while (nextClass != null) {
- try {
- return nextClass.getMethod(name, parameterTypes);
- }
- catch (NoSuchMethodException e) {
- nextClass = nextClass.getSuperclass();
- }
- }
- throw new RuntimeException(FormatMFLF.format("Could not find the method '{0}' in the hierarchy for the class '{1}'", name, clazz.getSimpleName()));
- }
- public static Method findPrivateMethod(Class<?> clazz, @Nonnull String name, Class<?>... parameterTypes) {
- if (clazz == null) {
- throw new NullPointerException("Class cannot be null!");
- }
- Class<?> nextClass = clazz;
- while (nextClass != null) {
- try {
- Method method = nextClass.getDeclaredMethod(name, parameterTypes);
- if (!method.isAccessible()) {
- method.setAccessible(true);
- }
- return method;
- }
- catch (NoSuchMethodException e) {
- nextClass = nextClass.getSuperclass();
- }
- }
- throw new RuntimeException(FormatMFLF.format("Could not find the declared method '{0}' in the hierarchy for the class '{1}'", name, clazz.getSimpleName()));
- }
- public static Field findPublicField(Class<?> clazz, @Nonnull String name) {
- if (clazz == null) {
- throw new NullPointerException("Class cannot be null!");
- }
- Class<?> nextClass = clazz;
- while (nextClass != null) {
- try {
- return nextClass.getField(name);
- }
- catch (NoSuchFieldException e) {
- nextClass = nextClass.getSuperclass();
- }
- }
- throw new RuntimeException(FormatMFLF.format("Could not find the field '{0}' in the hierarchy for the class '{1}'", name, clazz.getSimpleName()));
- }
- public static Field findPrivateField(Class<?> clazz, @Nonnull String name) {
- if (clazz == null) {
- throw new NullPointerException("Class cannot be null!");
- }
- Class<?> nextClass = clazz;
- while (nextClass != null) {
- try {
- Field field = nextClass.getDeclaredField(name);
- if (!field.isAccessible()) {
- field.setAccessible(true);
- }
- return field;
- }
- catch (NoSuchFieldException e) {
- nextClass = nextClass.getSuperclass();
- }
- }
- throw new RuntimeException(FormatMFLF.format("Could not find the declared field '{0}' in the hierarchy for the class '{1}'", name, clazz.getSimpleName()));
- }
- public static <T> T getFieldValue(@Nullable Object instance, @Nonnull Field field) {
- try {
- return (T) field.get(instance);
- }
- catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- catch (IllegalArgumentException e) {
- throw new RuntimeException("The field was an instance method, but the specified instance the correct type", e);
- }
- catch (NullPointerException e) {
- throw new RuntimeException("The field was an instance method, but the specified instance was null", e);
- }
- catch (ClassCastException e) {
- throw new RuntimeException("The field return value could not be casted to the generic type", e);
- }
- }
- public static void setFieldValue(@Nullable Object instance, @Nonnull Field field, Object value) {
- try {
- field.set(instance, value);
- }
- catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- catch (IllegalArgumentException e) {
- throw new RuntimeException("The field was an instance field, but the specified instance the correct type. Or, the given value's type is incompatible with the field type", e);
- }
- catch (NullPointerException e) {
- throw new RuntimeException("The field was an instance field, but the specified instance was null", e);
- }
- catch (ClassCastException e) {
- throw new RuntimeException("The field return value could not be casted to the generic type", e);
- }
- }
- public static <T> T invokeMethod(@Nullable Object instance, @Nonnull Method method, Object... parameters) {
- try {
- return (T) method.invoke(instance, parameters);
- }
- catch (InvocationTargetException e) {
- throw new RuntimeException("The method threw an exception", e);
- }
- catch (IllegalAccessException e) {
- throw new RuntimeException("The method was inaccessible (protected/private)", e);
- }
- catch (IllegalArgumentException e) {
- throw new RuntimeException("The method was either an instance method (but the specified instance wasn't of that type), or there was an incorrect parameter", e);
- }
- catch (NullPointerException e) {
- throw new RuntimeException("The method was an instance method, but the specified instance was null", e);
- }
- catch (ClassCastException e) {
- throw new RuntimeException("The method return value could not be casted to the generic type", e);
- }
- }
- public static <T> Constructor<T> getConstructor(@Nonnull Class<T> clazz, @Nonnull Class<?>... constructorParams) {
- try {
- return clazz.getConstructor(constructorParams);
- }
- catch (NoSuchMethodException e) {
- throw new RuntimeException(FormatMFLF.format("Could not find the constructor with {0} parameters", constructorParams.length), e);
- }
- }
- public static <T> T createInstance(Constructor<?> constructor, Object... params) {
- try {
- return (T) constructor.newInstance(params);
- }
- catch (IllegalAccessException e) {
- throw new RuntimeException("The found constructor was inaccessible (might be private or protected)", e);
- }
- catch (IllegalArgumentException e) {
- throw new RuntimeException("The found constructor required arguments that were not present, or were incorrect types", e);
- }
- catch (InstantiationException e) {
- throw new RuntimeException("The class cannot be instantiated (it might be abstract or an interface)", e);
- }
- catch (InvocationTargetException e) {
- throw new RuntimeException("The constructor threw an exception", e);
- }
- }
- public static <T> T createInstance(@Nonnull String className, Object... params) {
- Class<?>[] paramTypes = new Class[params.length];
- for (int i = 0, len = paramTypes.length; i < len; i++) {
- Object obj = paramTypes[i];
- if (obj == null) {
- throw new NullPointerException(FormatMFLF.format("Cannot auto-detect parameter types because a parameter was null (at index {0})", i));
- }
- paramTypes[i] = obj.getClass();
- }
- return createInstance(getConstructor(findClass(className), paramTypes), params);
- }
- public static <T> T createInstance0P(@Nonnull String className) {
- return createInstance(getConstructor(findClass(className)));
- }
- public static <T> T createInstance1P(@Nonnull String className, @Nonnull Object param) {
- return createInstance(getConstructor(findClass(className), param.getClass()), param);
- }
- public static <T> T createInstance0P(@Nonnull Constructor<?> constructor) {
- return createInstance(constructor);
- }
- public static <T> T createInstance1P(@Nonnull Constructor<?> constructor, @Nonnull Object param) {
- return createInstance(constructor, param);
- }
- public static <T> T getPrivateStaticFieldValue(@Nonnull Class<?> clazz, @Nonnull String field) {
- return getFieldValue(null, findPrivateField(clazz, field));
- }
- public static <T> T getPublicStaticFieldValue(@Nonnull Class<?> clazz, @Nonnull String field) {
- return getFieldValue(null, findPublicField(clazz, field));
- }
- public static <T> T getPrivateFieldValue(@Nonnull Object instance, @Nonnull String field) {
- return getFieldValue(instance, findPrivateField(instance.getClass(), field));
- }
- public static <T> T getPublicFieldValue(@Nonnull Object instance, @Nonnull String field) {
- return getFieldValue(instance, findPublicField(instance.getClass(), field));
- }
- public static void setPrivateFieldValue(@Nonnull Object instance, @Nonnull String field, Object value) {
- setFieldValue(instance, findPrivateField(instance.getClass(), field), value);
- }
- public static void setPublicFieldValue(@Nonnull Object instance, @Nonnull String field, Object value) {
- setFieldValue(instance, findPublicField(instance.getClass(), field), value);
- }
- public static <T> T invokePrivateMethod0P(@Nonnull Object instance, @Nonnull String method) {
- return invokeMethod(instance, findPrivateMethod(instance.getClass(), method));
- }
- public static <T> T invokePrivateMethod1P(@Nonnull Object instance, @Nonnull String method, @Nonnull Object parameter) {
- return invokeMethod(instance, findPrivateMethod(instance.getClass(), method, parameter.getClass()), parameter);
- }
- public static <T> T invokePublicMethod0P(@Nonnull Object instance, @Nonnull String method) {
- return invokeMethod(instance, findPrivateMethod(instance.getClass(), method));
- }
- public static <T> T invokePublicMethod1P(@Nonnull Object instance, @Nonnull String method, @Nonnull Object parameter) {
- return invokeMethod(instance, findPrivateMethod(instance.getClass(), method, parameter.getClass()), parameter);
- }
- public static <T> T invokePrivateStaticMethod0P(@Nonnull Class<?> clazz, @Nonnull String method) {
- return invokeMethod(null, findPrivateMethod(clazz, method));
- }
- public static <T> T invokePrivateStaticMethod1P(@Nonnull Class<?> clazz, @Nonnull String method, @Nonnull Object parameter) {
- return invokeMethod(null, findPrivateMethod(clazz, method, parameter.getClass()), parameter);
- }
- public static <T> T invokePublicStaticMethod0P(@Nonnull Class<?> clazz, @Nonnull String method) {
- return invokeMethod(null, findPrivateMethod(clazz, method));
- }
- public static <T> T invokePublicStaticMethod1P(@Nonnull Class<?> clazz, @Nonnull String method, @Nonnull Object parameter) {
- return invokeMethod(null, findPrivateMethod(clazz, method, parameter.getClass()), parameter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment