Add ReflectionUtils

This commit is contained in:
Flowsqy 2022-12-31 03:24:42 +01:00
parent 6e76f07876
commit 640c986e2f

View File

@ -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 <T> The type you want
* @return The same object but with the good type
*/
@SuppressWarnings("unchecked")
public static <T> 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);
}
}