vendredi 18 mars 2016

Getting an instance of an object from a Field, Reflection

I am trying to modify some content that is located in a place where I have to use reflection to get to. The thing is that I want to get that values (which happens to be a list) as a Field and then change it. When I get the field do I get a reference (copy) of the Field or do I get the actual field (meaning if I edit it It will change from wherever I took it from) ?

  //Get command map
    Field field = this.getServer().getClass().getDeclaredField("commandMap");
    field.setAccessible(true);
    CommandMap cmdmap = (CommandMap) field.get(this.getServer());
    //Get commands map
    Field f = cmdmap.getClass().getDeclaredField("knownCommands");
    f.setAccessible(true);
    Map<String, Command> kcmds = (Map<String, Command>) f.get(cmdmap);
    //Unregister all
    for(String s: cmdToUnregister)
    {
        for(String fp : kcmds.keySet())
        {
            if(s.equalsIgnoreCase(kcmds.get(fp).getName()))
            {
                kcmds.remove(fp);
            }
        }
    }
    //Re set fields
} catch (IllegalArgumentException | SecurityException | IllegalAccessException | NoSuchFieldException ex) {
    Logger.getLogger(DivinityRealm.class.getName()).log(Level.SEVERE, null, ex);
}

I tried this, it didn't work obiously. Whoever I don't know if the problem is my way of using Reflection or the fact that I am using a wrong approach?





Aucun commentaire:

Enregistrer un commentaire