How to support older Java dependencies after methods have been removed?

59 Views Asked by At

I am writing a plugin for Minecraft which uses WorldEdit. There is a class BlockVector3 with methods getBlockX() and getX() which are deprecated and marked for removal, to be replaced by x().

The problem is that if I use the new method, users who are using an older version of WorldEdit will no longer be able to use my plugin. And if I retain the deprecated method, it will soon cease to function with the latest dependency.

I could write two different plugins. However, is there a way to work around this particular issue using reflection or some other introspection?

1

There are 1 best solutions below

0
Elikill58 On

There are multiple ways to do this.

  1. One class per version, tricky but easy.

It's a common way to do it. Mostly, people do an interface like this :

public interface WorldEditAdapter {
   void load();
   void doSomething(double x, double y, double z);
}

Then, create a new class for each version like this:

public class WorldEdit3 implements WorldEditAdapter {
   public void load() {
       // load everything related to WorldEdit 3
   }
}

public class WorldEdit4 implements WorldEditAdapter {
   public void load() {
       // load everything related to WorldEdit 4
   }
}

Then, in the onEnable(), firstly check for which version is used, then instanciate it with new WorldEdit3() for example, then save it as WorldEditAdapter. Now, your full plugin will use WorldEditAdapter.


About how to use method not available, if you don't have compilation plugin, maybe you can import multiple file to "fake" the existance of method for 2 versions. This could be tricky.

You can also use reflection as you mentionned. It's not preferable. To use it, do:

myObject.getClass().getDeclaredMethod("getX").invoke(myObject); // equivalent of myObject.getX();

Static call of method:

MyClass.class.getDeclaredMethod("myMethod").invoke(null); // equivalent of MyClass.myMethod();
  1. Gradle/Maven: sub projects.

With gradle and maven it would be easier. You can do solution 1 but by putting the WorldEdit3 in different subproject loaded by the main one. It would be easier to manage, but it's not your case.