Guest User

Untitled

a guest
Jan 6th, 2022
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. package ru.cubinexnetwork.utility;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.Method;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8.  
  9. public final class ReflectionUtil {
  10. public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes) throws NoSuchMethodException {
  11. Class<?>[] primitiveTypes = DataType.getPrimitive(parameterTypes);
  12. Constructor[] arrayOfConstructor;
  13. int i;
  14. byte b;
  15. for (arrayOfConstructor = (Constructor[])clazz.getConstructors(), i = arrayOfConstructor.length, b = 0; b < i; ) {
  16. Constructor<?> constructor = arrayOfConstructor[b];
  17. if (DataType.compare(DataType.getPrimitive(constructor.getParameterTypes()), primitiveTypes)) {
  18. b++;
  19. continue;
  20. }
  21. return constructor;
  22. }
  23. throw new NoSuchMethodException("There is no such constructor in this class with the specified parameter types");
  24. }
  25.  
  26. public static Object getValue(Object instance, Class<?> clazz, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
  27. return getField(clazz, declared, fieldName).get(instance);
  28. }
  29.  
  30. public static Field getField(Class<?> clazz, boolean declared, String fieldName) throws NoSuchFieldException, SecurityException {
  31. Field field = declared ? clazz.getDeclaredField(fieldName) : clazz.getField(fieldName);
  32. field.setAccessible(true);
  33. return field;
  34. }
  35.  
  36. public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
  37. Class<?>[] primitiveTypes = DataType.getPrimitive(parameterTypes);
  38. Method[] arrayOfMethod;
  39. int i;
  40. byte b;
  41. for (arrayOfMethod = clazz.getMethods(), i = arrayOfMethod.length, b = 0; b < i; ) {
  42. Method method = arrayOfMethod[b];
  43. if (!method.getName().equals(methodName) || DataType.compare(DataType.getPrimitive(method.getParameterTypes()), primitiveTypes)) {
  44. b++;
  45. continue;
  46. }
  47. return method;
  48. }
  49. throw new NoSuchMethodException("There is no such method in this class with the specified name and parameter types");
  50. }
  51.  
  52. public static void setField(Object object, String field, Object fieldValue) {
  53. Field f = null;
  54. Class<?> clazz = object.getClass();
  55. try {
  56. f = clazz.getDeclaredField(field);
  57. f.setAccessible(true);
  58. f.set(object, fieldValue);
  59. } catch (IllegalAccessException|NoSuchFieldException e) {
  60. e.printStackTrace();
  61. } finally {
  62. f.setAccessible(false);
  63. }
  64. }
  65.  
  66. public static <T> T getField(Class clazz, Object object, String field, Class<T> fieldType) {
  67. return (T)getField(clazz, object, field);
  68. }
  69.  
  70. public static void setValue(Object instance, String fieldName, Object value) throws Exception {
  71. Field field = instance.getClass().getDeclaredField(fieldName);
  72. field.setAccessible(true);
  73. field.set(instance, value);
  74. }
  75.  
  76. public static void setValue(Object instance, boolean declared, String fieldName, Object value) throws Exception {
  77. Field field;
  78. if (declared) {
  79. field = instance.getClass().getDeclaredField(fieldName);
  80. } else {
  81. field = instance.getClass().getField(fieldName);
  82. }
  83. field.setAccessible(true);
  84. field.set(instance, value);
  85. }
  86.  
  87. public static Object getField(Class clazz, Object object, String field) {
  88. Field f = null;
  89. try {
  90. f = clazz.getDeclaredField(field);
  91. f.setAccessible(true);
  92. Object object2 = f.get(object);
  93. return object2;
  94. } catch (IllegalAccessException|NoSuchFieldException e) {
  95. e.printStackTrace();
  96. Object var5_6 = null;
  97. return var5_6;
  98. } finally {
  99. f.setAccessible(false);
  100. }
  101. }
  102.  
  103. public enum DataType {
  104. BYTE((String)byte.class, Byte.class),
  105. SHORT((String)short.class, Short.class),
  106. INTEGER((String)int.class, Integer.class),
  107. LONG((String)long.class, Long.class),
  108. CHARACTER((String)char.class, Character.class),
  109. FLOAT((String)float.class, Float.class),
  110. DOUBLE((String)double.class, Double.class),
  111. BOOLEAN((String)boolean.class, Boolean.class);
  112.  
  113. private static final Map<Class<?>, DataType> CLASS_MAP = new HashMap<>();
  114.  
  115. private final Class<?> primitive;
  116.  
  117. private final Class<?> reference;
  118.  
  119. DataType(Class<?> primitive, Class<?> reference) {
  120. this.primitive = primitive;
  121. this.reference = reference;
  122. }
  123.  
  124. public Class<?> getPrimitive() {
  125. return this.primitive;
  126. }
  127.  
  128. public Class<?> getReference() {
  129. return this.reference;
  130. }
  131.  
  132. public static DataType fromClass(Class<?> clazz) {
  133. return CLASS_MAP.get(clazz);
  134. }
  135.  
  136. public static Class<?> getPrimitive(Class<?> clazz) {
  137. DataType type = fromClass(clazz);
  138. return (type == null) ? clazz : type.getPrimitive();
  139. }
  140.  
  141. public static Class<?> getReference(Class<?> clazz) {
  142. DataType type = fromClass(clazz);
  143. return (type == null) ? clazz : type.getReference();
  144. }
  145.  
  146. public static Class<?>[] getPrimitive(Class<?>[] classes) {
  147. int length = (classes == null) ? 0 : classes.length;
  148. Class[] types = new Class[length];
  149. for (int index = 0; index < length; index++)
  150. types[index] = getPrimitive(classes[index]);
  151. return types;
  152. }
  153.  
  154. public static Class<?>[] getReference(Class<?>[] classes) {
  155. int length = (classes == null) ? 0 : classes.length;
  156. Class[] types = new Class[length];
  157. for (int index = 0; index < length; index++)
  158. types[index] = getReference(classes[index]);
  159. return types;
  160. }
  161.  
  162. public static Class<?>[] getPrimitive(Object[] objects) {
  163. int length = (objects == null) ? 0 : objects.length;
  164. Class[] types = new Class[length];
  165. for (int index = 0; index < length; index++)
  166. types[index] = getPrimitive(objects[index].getClass());
  167. return types;
  168. }
  169.  
  170. public static Class<?>[] getReference(Object[] objects) {
  171. int length = (objects == null) ? 0 : objects.length;
  172. Class[] types = new Class[length];
  173. for (int index = 0; index < length; index++)
  174. types[index] = getReference(objects[index].getClass());
  175. return types;
  176. }
  177.  
  178. public static boolean compare(Class<?>[] primary, Class<?>[] secondary) {
  179. if (primary == null || secondary == null || primary.length != secondary.length)
  180. return true;
  181. for (int index = 0; index < primary.length; ) {
  182. Class<?> primaryClass = primary[index];
  183. Class<?> secondaryClass = secondary[index];
  184. if (primaryClass.equals(secondaryClass) || primaryClass.isAssignableFrom(secondaryClass)) {
  185. index++;
  186. continue;
  187. }
  188. return true;
  189. }
  190. return false;
  191. }
  192.  
  193. static {
  194. for (DataType type : values()) {
  195. CLASS_MAP.put(type.primitive, type);
  196. CLASS_MAP.put(type.reference, type);
  197. }
  198. }
  199. }
  200. }
  201.  
Advertisement
Add Comment
Please, Sign In to add comment