Are plugins that require the modification of (a) .class file(s) allowed?

Discussion in 'Plugin Development' started by Fifteen, Jan 20, 2011.

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

    Fifteen

    So, I've created a kinda simple plugin a few days ago, but Mojang changed a variable from "public" to "private", and modifying this variable is essential in order to make my plugin (and even possible future Bukkit features) work.

    Now, my question is, am I allowed to make a plugin that requires a minor change to a .class file?

    Bump. Oh, and do I need to modify the .class file of CraftBukkit? Now that I think about it, as long as the modified file is referenced you don't need to modify the actual server file... right? I'm kind of a noob on how Java references things.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 6, 2016
  2. Offline

    Afforess

    Technically there is no restriction on what you can or can not modify in a plugin, but modifying classes in CB or MC may prove to be difficult.
     
  3. Offline

    Fifteen

    'kay, thanks. Oh, and I've already changed what I need to change in the .class file.
     
  4. Offline

    Mixcoatl

    Modifying .class files is not very nice. For one thing, it forces you to find or develop a mechanism to distribute the files you modified. It also puts you in the business of maintaining that modified file.

    In my humble opinion, a better solution to your specific problem would be to use reflection to access the private field. The Java reflection API will allow you to inspect or set the field in spite of its private access. Professionally speaking, this would still be considered "in poor taste," but sometimes your hand is forced.

    If you choose to do this, I would suggest testing your code especially rigorously -- much more so than usual.
     
  5. Offline

    Fifteen

    I have tried reflection, but for some reason Eclipse still said that the variable "is not visible".
     
  6. Offline

    Mixcoatl

    Reflection is not a feature of Eclipse. It's a feature of the JDK. Perhaps you're thinking of inspection. Reflection is the programmatic access of meta-information. Here's an example:
    Code:
    // Here's a class with a private field.
    class PrivateStuff {
        private double value = 0.0;
    }
    
    Later on in your code:
    Code:
    final PrivateStuff p = new PrivateStuff();
    
    // Get a Field object representing PrivateStuff.value
    Field valueField = p.getClass().getDeclaredField("value");
    
    // Instruct Java that you wish to disregard access modifiers (protected, private, etc).
    valueField.setAccessible(true);
    
    // Get the value of p.value as an object.
    double valueObject = valueField.getDouble(p);
    
    // Set the value of p.value to PI.
    valueField.setDouble(p, 3.14159);
    Note: I have omitted some exception handling for the sake of brevity. It will be obvious when you do this which exceptions need to be handled.
     
Thread Status:
Not open for further replies.

Share This Page