summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/bukkit/support/Util.java
blob: 2f24d9a4083665b42bb439434c64158dc14fcaf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.bukkit.support;

import java.lang.reflect.Field;

public class Util {
    /*
    public static <T> T getInternalState(Object object, String fieldName) {
        return getInternalState(object.getClass(), object, fieldName);
    }
    */

    @SuppressWarnings("unchecked")
    public static <T> T getInternalState(Class<?> clazz, Object object, String fieldName) {
        Field field;
        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (SecurityException e) {
            throw new RuntimeException("Not allowed to access " + clazz, e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException("Unable to find field " + fieldName, e);
        }

        field.setAccessible(true);
        try {
            return (T) field.get(object);
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        }
        throw new RuntimeException("Unable to get internal value");
    }
}