summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/ohmyvanillamc/ReflectionUtils.java
diff options
context:
space:
mode:
authorKeuin <[email protected]>2020-12-28 22:53:31 +0800
committerKeuin <[email protected]>2020-12-28 22:53:31 +0800
commit4dad5fc27145593c14425b3e673b924155903585 (patch)
tree3573b6ddb9170126d47f75a10b91c140c6fd9e98 /src/main/java/com/keuin/ohmyvanillamc/ReflectionUtils.java
parentf287ea9dbb54b96c15f441b7f72e7d6527704ded (diff)
Version 1.3.0: add switches for all disable features. Fixed the vanilla bug that the entity trackers sometimes spam in the console.
Diffstat (limited to 'src/main/java/com/keuin/ohmyvanillamc/ReflectionUtils.java')
-rw-r--r--src/main/java/com/keuin/ohmyvanillamc/ReflectionUtils.java59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/ohmyvanillamc/ReflectionUtils.java b/src/main/java/com/keuin/ohmyvanillamc/ReflectionUtils.java
new file mode 100644
index 0000000..253c89d
--- /dev/null
+++ b/src/main/java/com/keuin/ohmyvanillamc/ReflectionUtils.java
@@ -0,0 +1,59 @@
+package com.keuin.ohmyvanillamc;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * @Author 落叶飞翔的蜗牛
+ * @Date 2018/3/10
+ * @Description 常用反射函数
+ */
+public final class ReflectionUtils {
+
+ /**
+ * 获取私有成员变量的值
+ *
+ * @param instance
+ * @param filedName
+ * @return
+ */
+ public static Object getPrivateField(Object instance, String filedName) throws NoSuchFieldException, IllegalAccessException {
+ Field field = instance.getClass().getDeclaredField(filedName);
+ field.setAccessible(true);
+ return field.get(instance);
+ }
+
+ /**
+ * 设置私有成员的值
+ *
+ * @param instance
+ * @param fieldName
+ * @param value
+ * @throws NoSuchFieldException
+ * @throws IllegalAccessException
+ */
+ public static void setPrivateField(Object instance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
+ Field field = instance.getClass().getDeclaredField(fieldName);
+ field.setAccessible(true);
+ field.set(instance, value);
+ }
+
+ /**
+ * 访问私有方法
+ *
+ * @param instance
+ * @param methodName
+ * @param classes
+ * @param args
+ * @return
+ * @throws NoSuchMethodException
+ * @throws InvocationTargetException
+ * @throws IllegalAccessException
+ */
+ public static Object invokePrivateMethod(Object instance, String methodName, Class[] classes, Object... args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
+ Method method = instance.getClass().getDeclaredMethod(methodName, classes);
+ method.setAccessible(true);
+ return method.invoke(instance, args);
+ }
+} \ No newline at end of file