Util Mirror: Reflection made easy

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

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

    Phasesaber

    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.
    Code:
    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.
    Code:
    import static mirror.Mirror;
    This allows you to use the main method in Mirror without calling the class.
    Code:
    $(i);
    Now let's call the split method (pretending it is a private method).
    Code:
    String[] a = $(i).getMethod("split", String.class).invoke(" ");
    Just like if we called
    Code:
    i.split(" ");
    Want to change a field? It's even easier!
    Code:
    //           name, value
    $(i).setField("hash", -5);
    And let's get the field!
    Code:
    //                <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).
    Code:
    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