bobmarley12345

java reflection helper

Oct 5th, 2021
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.19 KB | None | 0 0
  1. package reghzy.carrottools.utils.reflect;
  2.  
  3. import reghzy.carrottools.utils.text.FormatMFLF;
  4.  
  5. import javax.annotation.Nonnull;
  6. import javax.annotation.Nullable;
  7. import java.lang.reflect.Constructor;
  8. import java.lang.reflect.Field;
  9. import java.lang.reflect.InvocationTargetException;
  10. import java.lang.reflect.Method;
  11.  
  12. /**
  13.  * A class to cleanly use reflection without having to use try catch blocks everywhere
  14.  */
  15. public class Reflect {
  16.     /**
  17.      * Casts the object value to another value (generic)
  18.      *
  19.      * <p>
  20.      * This should not return null, unless you give a null value,
  21.      * or are daft enough to try to cast an object that wasn't remapped :-)
  22.      * </p>
  23.      */
  24.     public static <T> T cast(Object value) {
  25.         if (value == null) {
  26.             return null;
  27.         }
  28.  
  29.         try {
  30.             return (T) value;
  31.         }
  32.         catch (ClassCastException e) {
  33.             throw new RuntimeException("Given value was probably not remapped", e);
  34.         }
  35.     }
  36.  
  37.     public static Class<?> findClass(String className) {
  38.         try {
  39.             return Class.forName(className);
  40.         }
  41.         catch (ClassNotFoundException e) {
  42.             throw new RuntimeException(FormatMFLF.format("The class '{0}' could not be found", className), e);
  43.         }
  44.     }
  45.  
  46.     public static Method findPublicMethod(Class<?> clazz, @Nonnull String name, Class<?>... parameterTypes) {
  47.         if (clazz == null) {
  48.             throw new NullPointerException("Class cannot be null!");
  49.         }
  50.  
  51.         Class<?> nextClass = clazz;
  52.         while (nextClass != null) {
  53.             try {
  54.                 return nextClass.getMethod(name, parameterTypes);
  55.             }
  56.             catch (NoSuchMethodException e) {
  57.                 nextClass = nextClass.getSuperclass();
  58.             }
  59.         }
  60.  
  61.         throw new RuntimeException(FormatMFLF.format("Could not find the method '{0}' in the hierarchy for the class '{1}'", name, clazz.getSimpleName()));
  62.     }
  63.  
  64.     public static Method findPrivateMethod(Class<?> clazz, @Nonnull String name, Class<?>... parameterTypes) {
  65.         if (clazz == null) {
  66.             throw new NullPointerException("Class cannot be null!");
  67.         }
  68.  
  69.         Class<?> nextClass = clazz;
  70.         while (nextClass != null) {
  71.             try {
  72.                 Method method = nextClass.getDeclaredMethod(name, parameterTypes);
  73.                 if (!method.isAccessible()) {
  74.                     method.setAccessible(true);
  75.                 }
  76.  
  77.                 return method;
  78.             }
  79.             catch (NoSuchMethodException e) {
  80.                 nextClass = nextClass.getSuperclass();
  81.             }
  82.         }
  83.  
  84.         throw new RuntimeException(FormatMFLF.format("Could not find the declared method '{0}' in the hierarchy for the class '{1}'", name, clazz.getSimpleName()));
  85.     }
  86.  
  87.     public static Field findPublicField(Class<?> clazz, @Nonnull String name) {
  88.         if (clazz == null) {
  89.             throw new NullPointerException("Class cannot be null!");
  90.         }
  91.  
  92.         Class<?> nextClass = clazz;
  93.         while (nextClass != null) {
  94.             try {
  95.                 return nextClass.getField(name);
  96.             }
  97.             catch (NoSuchFieldException e) {
  98.                 nextClass = nextClass.getSuperclass();
  99.             }
  100.         }
  101.  
  102.         throw new RuntimeException(FormatMFLF.format("Could not find the field '{0}' in the hierarchy for the class '{1}'", name, clazz.getSimpleName()));
  103.     }
  104.  
  105.     public static Field findPrivateField(Class<?> clazz, @Nonnull String name) {
  106.         if (clazz == null) {
  107.             throw new NullPointerException("Class cannot be null!");
  108.         }
  109.  
  110.         Class<?> nextClass = clazz;
  111.         while (nextClass != null) {
  112.             try {
  113.                 Field field = nextClass.getDeclaredField(name);
  114.                 if (!field.isAccessible()) {
  115.                     field.setAccessible(true);
  116.                 }
  117.  
  118.                 return field;
  119.             }
  120.             catch (NoSuchFieldException e) {
  121.                 nextClass = nextClass.getSuperclass();
  122.             }
  123.         }
  124.  
  125.         throw new RuntimeException(FormatMFLF.format("Could not find the declared field '{0}' in the hierarchy for the class '{1}'", name, clazz.getSimpleName()));
  126.     }
  127.  
  128.     public static <T> T getFieldValue(@Nullable Object instance, @Nonnull Field field) {
  129.         try {
  130.             return (T) field.get(instance);
  131.         }
  132.         catch (IllegalAccessException e) {
  133.             throw new RuntimeException(e);
  134.         }
  135.         catch (IllegalArgumentException e) {
  136.             throw new RuntimeException("The field was an instance method, but the specified instance the correct type", e);
  137.         }
  138.         catch (NullPointerException e) {
  139.             throw new RuntimeException("The field was an instance method, but the specified instance was null", e);
  140.         }
  141.         catch (ClassCastException e) {
  142.             throw new RuntimeException("The field return value could not be casted to the generic type", e);
  143.         }
  144.     }
  145.  
  146.     public static void setFieldValue(@Nullable Object instance, @Nonnull Field field, Object value) {
  147.         try {
  148.             field.set(instance, value);
  149.         }
  150.         catch (IllegalAccessException e) {
  151.             throw new RuntimeException(e);
  152.         }
  153.         catch (IllegalArgumentException e) {
  154.             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);
  155.         }
  156.         catch (NullPointerException e) {
  157.             throw new RuntimeException("The field was an instance field, but the specified instance was null", e);
  158.         }
  159.         catch (ClassCastException e) {
  160.             throw new RuntimeException("The field return value could not be casted to the generic type", e);
  161.         }
  162.     }
  163.  
  164.     public static <T> T invokeMethod(@Nullable Object instance, @Nonnull Method method, Object... parameters) {
  165.         try {
  166.             return (T) method.invoke(instance, parameters);
  167.         }
  168.         catch (InvocationTargetException e) {
  169.             throw new RuntimeException("The method threw an exception", e);
  170.         }
  171.         catch (IllegalAccessException e) {
  172.             throw new RuntimeException("The method was inaccessible (protected/private)", e);
  173.         }
  174.         catch (IllegalArgumentException e) {
  175.             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);
  176.         }
  177.         catch (NullPointerException e) {
  178.             throw new RuntimeException("The method was an instance method, but the specified instance was null", e);
  179.         }
  180.         catch (ClassCastException e) {
  181.             throw new RuntimeException("The method return value could not be casted to the generic type", e);
  182.         }
  183.     }
  184.  
  185.     public static <T> Constructor<T> getConstructor(@Nonnull Class<T> clazz, @Nonnull Class<?>... constructorParams) {
  186.         try {
  187.             return clazz.getConstructor(constructorParams);
  188.         }
  189.         catch (NoSuchMethodException e) {
  190.             throw new RuntimeException(FormatMFLF.format("Could not find the constructor with {0} parameters", constructorParams.length), e);
  191.         }
  192.     }
  193.  
  194.     public static <T> T createInstance(Constructor<?> constructor, Object... params) {
  195.         try {
  196.             return (T) constructor.newInstance(params);
  197.         }
  198.         catch (IllegalAccessException e) {
  199.             throw new RuntimeException("The found constructor was inaccessible (might be private or protected)", e);
  200.         }
  201.         catch (IllegalArgumentException e) {
  202.             throw new RuntimeException("The found constructor required arguments that were not present, or were incorrect types", e);
  203.         }
  204.         catch (InstantiationException e) {
  205.             throw new RuntimeException("The class cannot be instantiated (it might be abstract or an interface)", e);
  206.         }
  207.         catch (InvocationTargetException e) {
  208.             throw new RuntimeException("The constructor threw an exception", e);
  209.         }
  210.     }
  211.  
  212.     public static <T> T createInstance(@Nonnull String className, Object... params) {
  213.        Class<?>[] paramTypes = new Class[params.length];
  214.         for (int i = 0, len = paramTypes.length; i < len; i++) {
  215.             Object obj = paramTypes[i];
  216.             if (obj == null) {
  217.                 throw new NullPointerException(FormatMFLF.format("Cannot auto-detect parameter types because a parameter was null (at index {0})", i));
  218.             }
  219.  
  220.             paramTypes[i] = obj.getClass();
  221.         }
  222.  
  223.         return createInstance(getConstructor(findClass(className), paramTypes), params);
  224.     }
  225.  
  226.     public static <T> T createInstance0P(@Nonnull String className) {
  227.         return createInstance(getConstructor(findClass(className)));
  228.     }
  229.  
  230.     public static <T> T createInstance1P(@Nonnull String className, @Nonnull Object param) {
  231.         return createInstance(getConstructor(findClass(className), param.getClass()), param);
  232.     }
  233.  
  234.     public static <T> T createInstance0P(@Nonnull Constructor<?> constructor) {
  235.         return createInstance(constructor);
  236.     }
  237.  
  238.     public static <T> T createInstance1P(@Nonnull Constructor<?> constructor, @Nonnull Object param) {
  239.         return createInstance(constructor, param);
  240.     }
  241.  
  242.     public static <T> T getPrivateStaticFieldValue(@Nonnull Class<?> clazz, @Nonnull String field) {
  243.         return getFieldValue(null, findPrivateField(clazz, field));
  244.     }
  245.  
  246.     public static <T> T getPublicStaticFieldValue(@Nonnull Class<?> clazz, @Nonnull String field) {
  247.         return getFieldValue(null, findPublicField(clazz, field));
  248.     }
  249.  
  250.     public static <T> T getPrivateFieldValue(@Nonnull Object instance, @Nonnull String field) {
  251.         return getFieldValue(instance, findPrivateField(instance.getClass(), field));
  252.     }
  253.  
  254.     public static <T> T getPublicFieldValue(@Nonnull Object instance, @Nonnull String field) {
  255.         return getFieldValue(instance, findPublicField(instance.getClass(), field));
  256.     }
  257.  
  258.     public static void setPrivateFieldValue(@Nonnull Object instance, @Nonnull String field, Object value) {
  259.         setFieldValue(instance, findPrivateField(instance.getClass(), field), value);
  260.     }
  261.  
  262.     public static void setPublicFieldValue(@Nonnull Object instance, @Nonnull String field, Object value) {
  263.         setFieldValue(instance, findPublicField(instance.getClass(), field), value);
  264.     }
  265.  
  266.     public static <T> T invokePrivateMethod0P(@Nonnull Object instance, @Nonnull String method) {
  267.         return invokeMethod(instance, findPrivateMethod(instance.getClass(), method));
  268.     }
  269.  
  270.     public static <T> T invokePrivateMethod1P(@Nonnull Object instance, @Nonnull String method, @Nonnull Object parameter) {
  271.         return invokeMethod(instance, findPrivateMethod(instance.getClass(), method, parameter.getClass()), parameter);
  272.     }
  273.  
  274.     public static <T> T invokePublicMethod0P(@Nonnull Object instance, @Nonnull String method) {
  275.         return invokeMethod(instance, findPrivateMethod(instance.getClass(), method));
  276.     }
  277.  
  278.     public static <T> T invokePublicMethod1P(@Nonnull Object instance, @Nonnull String method, @Nonnull Object parameter) {
  279.         return invokeMethod(instance, findPrivateMethod(instance.getClass(), method, parameter.getClass()), parameter);
  280.     }
  281.  
  282.     public static <T> T invokePrivateStaticMethod0P(@Nonnull Class<?> clazz, @Nonnull String method) {
  283.         return invokeMethod(null, findPrivateMethod(clazz, method));
  284.     }
  285.  
  286.     public static <T> T invokePrivateStaticMethod1P(@Nonnull Class<?> clazz, @Nonnull String method, @Nonnull Object parameter) {
  287.         return invokeMethod(null, findPrivateMethod(clazz, method, parameter.getClass()), parameter);
  288.     }
  289.  
  290.     public static <T> T invokePublicStaticMethod0P(@Nonnull Class<?> clazz, @Nonnull String method) {
  291.         return invokeMethod(null, findPrivateMethod(clazz, method));
  292.     }
  293.  
  294.     public static <T> T invokePublicStaticMethod1P(@Nonnull Class<?> clazz, @Nonnull String method, @Nonnull Object parameter) {
  295.         return invokeMethod(null, findPrivateMethod(clazz, method, parameter.getClass()), parameter);
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment