Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. package net.iz44kpvp.simpleutils.utils;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.Modifier;
  7.  
  8. import org.bukkit.Bukkit;
  9.  
  10.  
  11. public class ReflectionUtils {
  12.  
  13. public static final String serverVersion = null;
  14.  
  15. static {
  16. try {
  17. Class.forName("org.bukkit.Bukkit");
  18. setObject(ReflectionUtils.class, null, "serverVersion", Bukkit.getServer().getClass().getPackage().getName()
  19. .substring(Bukkit.getServer().getClass().getPackage().getName().lastIndexOf('.') + 1));
  20. } catch (Exception e) {
  21. }
  22. }
  23.  
  24. public static Field getField(Class<?> clazz, String fname) throws Exception {
  25. Field f = null;
  26. try {
  27. f = clazz.getDeclaredField(fname);
  28. } catch (Exception e) {
  29. f = clazz.getField(fname);
  30. }
  31. f.setAccessible(true);
  32. Field modifiers = Field.class.getDeclaredField("modifiers");
  33. modifiers.setAccessible(true);
  34. modifiers.setInt(f, f.getModifiers() & ~Modifier.FINAL);
  35. return f;
  36. }
  37.  
  38. public static <T> Field getField(Class<?> target, String name, Class<T> fieldType, int index) {
  39. for (final Field field : target.getDeclaredFields()) {
  40. if ((name == null || field.getName().equals(name)) && fieldType.isAssignableFrom(field.getType()) && index-- <= 0) {
  41. field.setAccessible(true);
  42. return field;
  43. }
  44. }
  45.  
  46. if (target.getSuperclass() != null)
  47. return getField(target.getSuperclass(), name, fieldType, index);
  48. throw new IllegalArgumentException("Cannot find field with type " + fieldType);
  49. }
  50.  
  51. public static Object getObject(Object obj, String fname) throws Exception {
  52. return getField(obj.getClass(), fname).get(obj);
  53. }
  54.  
  55. public static Object getObject(Class<?> clazz, Object obj, String fname) throws Exception {
  56. return getField(clazz, fname).get(obj);
  57. }
  58.  
  59. public static void setObject(Object obj, String fname, Object value) throws Exception {
  60. getField(obj.getClass(), fname).set(obj, value);
  61. }
  62.  
  63. public static void setObject(Class<?> clazz, Object obj, String fname, Object value) throws Exception {
  64. getField(clazz, fname).set(obj, value);
  65. }
  66.  
  67. public static Method getMethod(Class<?> clazz, String mname) throws Exception {
  68. Method m = null;
  69. try {
  70. m = clazz.getDeclaredMethod(mname);
  71. } catch (Exception e) {
  72. try {
  73. m = clazz.getMethod(mname);
  74. } catch (Exception ex) {
  75. for (Method me : clazz.getDeclaredMethods()) {
  76. if (me.getName().equalsIgnoreCase(mname))
  77. m = me;
  78. break;
  79. }
  80. if (m == null)
  81. for (Method me : clazz.getMethods()) {
  82. if (me.getName().equalsIgnoreCase(mname))
  83. m = me;
  84. break;
  85. }
  86. }
  87. }
  88. m.setAccessible(true);
  89. return m;
  90. }
  91.  
  92. public static Method getMethod(Class<?> clazz, String mname, Class<?>... args) throws Exception {
  93. Method m = null;
  94. try {
  95. m = clazz.getDeclaredMethod(mname, args);
  96. } catch (Exception e) {
  97. try {
  98. m = clazz.getMethod(mname, args);
  99. } catch (Exception ex) {
  100. for (Method me : clazz.getDeclaredMethods()) {
  101. if (me.getName().equalsIgnoreCase(mname))
  102. m = me;
  103. break;
  104. }
  105. if (m == null)
  106. for (Method me : clazz.getMethods()) {
  107. if (me.getName().equalsIgnoreCase(mname))
  108. m = me;
  109. break;
  110. }
  111. }
  112. }
  113. m.setAccessible(true);
  114. return m;
  115. }
  116.  
  117. public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... args) throws Exception {
  118. Constructor<?> c = clazz.getConstructor(args);
  119. c.setAccessible(true);
  120. return c;
  121. }
  122.  
  123. public static Enum<?> getEnum(Class<?> clazz, String enumname, String constant) throws Exception {
  124. Class<?> c = Class.forName(clazz.getName() + "$" + enumname);
  125. Enum<?>[] econstants = (Enum<?>[]) c.getEnumConstants();
  126. for (Enum<?> e : econstants) {
  127. if (e.name().equalsIgnoreCase(constant))
  128. return e;
  129. }
  130. throw new Exception("Enum constant not found " + constant);
  131. }
  132.  
  133. public static Enum<?> getEnum(Class<?> clazz, String constant) throws Exception {
  134. Class<?> c = Class.forName(clazz.getName());
  135. Enum<?>[] econstants = (Enum<?>[]) c.getEnumConstants();
  136. for (Enum<?> e : econstants) {
  137. if (e.name().equalsIgnoreCase(constant))
  138. return e;
  139. }
  140. throw new Exception("Enum constant not found " + constant);
  141. }
  142.  
  143. public static Class<?> getNMSClass(String clazz) throws Exception {
  144. return Class.forName("net.minecraft.server." + serverVersion + "." + clazz);
  145. }
  146.  
  147. public static Class<?> getBukkitClass(String clazz) throws Exception {
  148. return Class.forName("org.bukkit.craftbukkit." + serverVersion + "." + clazz);
  149. }
  150.  
  151. public static Object invokeMethod(Class<?> clazz, Object obj, String method, Class<?>[] args, Object... initargs)
  152. throws Exception {
  153. return getMethod(clazz, method, args).invoke(obj, initargs);
  154. }
  155.  
  156. public static Object invokeMethod(Class<?> clazz, Object obj, String method) throws Exception {
  157. return getMethod(clazz, method).invoke(obj, new Object[] {});
  158. }
  159.  
  160. public static Object invokeMethod(Class<?> clazz, Object obj, String method, Object... initargs) throws Exception {
  161. return getMethod(clazz, method).invoke(obj, initargs);
  162. }
  163.  
  164. public static Object invokeMethod(Object obj, String method) throws Exception {
  165. return getMethod(obj.getClass(), method).invoke(obj, new Object[] {});
  166. }
  167.  
  168. public static Object invokeMethod(Object obj, String method, Object[] initargs) throws Exception {
  169. return getMethod(obj.getClass(), method).invoke(obj, initargs);
  170. }
  171.  
  172. public static Object invokeConstructor(Class<?> clazz, Class<?>[] args, Object... initargs) throws Exception {
  173. return getConstructor(clazz, args).newInstance(initargs);
  174. }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement