summaryrefslogtreecommitdiffstats
path: root/src/main/java/de/spline/kvm/utils/ReflectionUtils.java
blob: 03f3a9fe9f2b0e2df5314f157389dcadec936886 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package de.spline.kvm.utils;

import java.awt.PopupMenu;
import java.lang.reflect.Field;

import nn.pp.rc.RemoteConsoleApplet;
import nn.pp.rc.ServerConsolePanelBase;

public class ReflectionUtils
{
    /**
     * Set a field of an object with reflection. This ignores all access
     * permissions (f.e. private) and accesses the field by name (so it works
     * for strange field names).
     *
     * @param cls
     * @param instance
     * @param field
     * @param value
     */
    public static void setField(Class<?> cls, Object instance, String field, Object value)
    {
        try {
            Field f = cls.getDeclaredField(field);
            f.setAccessible(true);
            f.set(instance, value);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * Get a field of an object with reflection. This ignores all access
     * permissions (f.e. private) and accesses the field by name (so it works
     * for strange field names).
     *
     * @param cls
     * @param instance
     * @param field
     * @return
     */
    public static Object getField(Class<?> cls, Object instance, String field)
    {
        try {
            Field f = cls.getDeclaredField(field);
            f.setAccessible(true);
            return f.get(instance);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * Get the ServerConsolePanelBase field from the applet instance. We need
     * to use reflection here, because the obfuscation has produced strange
     * field names and we cannot access it using normal methods.
     *
     * @param applet RemoteConsoleApplet instance
     * @return ServerConsolePanelBase instance of this applet
     */
    public static ServerConsolePanelBase getConsolePanel(RemoteConsoleApplet applet)
    {
        return (ServerConsolePanelBase) ReflectionUtils.getField(RemoteConsoleApplet.class, applet, "else");
    }

    /**
     * Get the popup option menu from a ServerConsolePanelBase instance. The
     * field name is okay-ish here, but we also need to use reflection here
     * because this field in private.
     *
     * (We use this option menu to fix a silly bug: The menu is not displayed,
     * because the "label" is removed after initialization and this is causing
     * NullPointerExceptions.)
     *
     * @param consolePanel ServerConsolePanelBase instance
     * @return options menu
     */
    public static PopupMenu getOptionMenu(ServerConsolePanelBase consolePanel)
    {
        return (PopupMenu)ReflectionUtils.getField(ServerConsolePanelBase.class, consolePanel, "G");
    }
}