From 640c986e2fc2b4f7d9627be5ac546a2f01560106 Mon Sep 17 00:00:00 2001 From: Flowsqy <47575244+Flowsqy@users.noreply.github.com> Date: Sat, 31 Dec 2022 03:24:42 +0100 Subject: [PATCH] Add ReflectionUtils --- .../shopchest/nms/ReflectionUtils.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 nms/interface/src/main/java/de/epiceric/shopchest/nms/ReflectionUtils.java diff --git a/nms/interface/src/main/java/de/epiceric/shopchest/nms/ReflectionUtils.java b/nms/interface/src/main/java/de/epiceric/shopchest/nms/ReflectionUtils.java new file mode 100644 index 0000000..9dfd4be --- /dev/null +++ b/nms/interface/src/main/java/de/epiceric/shopchest/nms/ReflectionUtils.java @@ -0,0 +1,36 @@ +package de.epiceric.shopchest.nms; + +import java.lang.reflect.Field; + +/** + * Reflection utility class + */ +public class ReflectionUtils { + + /** + * Unsafe cast, used to suppress warning for known type of generic class + * + * @param o The object to cast + * @param The type you want + * @return The same object but with the good type + */ + @SuppressWarnings("unchecked") + public static T forceCast(Object o) { + return (T) o; + } + + /** + * Get a private static field value + * + * @param clazz The {@link Class} to lookup + * @param fieldName The {@link Field} name + * @return The value the private static field + * @throws ReflectiveOperationException If the field can't be found + */ + public static Object getPrivateStaticFieldValue(Class clazz, String fieldName) throws ReflectiveOperationException { + final Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(null); + } + +}