1 package net.pterodactylus.sone;
3 import java.lang.reflect.Field;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6 import java.lang.reflect.Modifier;
9 * Utilities for testing.
11 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
13 public class TestUtil {
15 public static void setFinalField(Object object, String fieldName, Object value) {
17 Field clientCoreField = object.getClass().getField(fieldName);
18 clientCoreField.setAccessible(true);
19 Field modifiersField = Field.class.getDeclaredField("modifiers");
20 modifiersField.setAccessible(true);
21 modifiersField.setInt(clientCoreField, clientCoreField.getModifiers() & ~Modifier.FINAL);
22 clientCoreField.set(object, value);
23 } catch (NoSuchFieldException e) {
24 throw new RuntimeException(e);
25 } catch (IllegalAccessException e) {
26 throw new RuntimeException(e);
30 public static <T> T getPrivateField(Object object, String fieldName) {
32 Field field = object.getClass().getDeclaredField(fieldName);
33 field.setAccessible(true);
34 return (T) field.get(object);
35 } catch (NoSuchFieldException e) {
36 throw new RuntimeException(e);
37 } catch (IllegalAccessException e) {
38 throw new RuntimeException(e);
42 public static <T> T callPrivateMethod(Object object, String methodName) {
44 Method method = object.getClass().getDeclaredMethod(methodName, new Class[0]);
45 method.setAccessible(true);
46 return (T) method.invoke(object);
47 } catch (NoSuchMethodException e) {
48 throw new RuntimeException(e);
49 } catch (InvocationTargetException e) {
50 throw new RuntimeException(e);
51 } catch (IllegalAccessException e) {
52 throw new RuntimeException(e);