Util Mirror: Reflection made easy

Discussion in 'Resources' started by Phasesaber, Apr 8, 2015.

Thread Status:
Not open for further replies.
  1. Offline


    Yo! Here's a tool that makes Reflection super simple, it's called Mirror. If you don't like it, change it on GitHub.

    Let's say you have an object.
    String i = "This is some text."
    And you want to call some private methods on it. With Mirror, you can!

    The first thing you want to do is statically import Mirror.
    import static mirror.Mirror;
    This allows you to use the main method in Mirror without calling the class.
    Now let's call the split method (pretending it is a private method).
    String[] a = $(i).getMethod("split", String.class).invoke(" ");
    Just like if we called
    i.split(" ");
    Want to change a field? It's even easier!
    //           name, value
    $(i).setField("hash", -5);
    And let's get the field!
    //                <typeToReturn>    name
    int hash = $(i).<Integer>getField("hash"); //returns -5
    Now, what on earth does this have to do with Bukkit?
    Here's a simple example of getting a Player's Connection (thanks @Skionz).
    public Object getConnection(Player p){
        Object nmsPlayer = $(p).getMethod("getHandle").invoke();
        return $(nmsPlayer).<Object>getField("playerconnection");
    All the code is on GitHub.
    Last edited: Apr 9, 2015
Thread Status:
Not open for further replies.

Share This Page