Reputation: 21
My issue is that depending on Lib version (.jar dependency) I use superclass constructor changes from Integer to String for arguments.
public class GlowEnchant
extends EnchantmentWrapper {
public GlowEnchant(int id) {
super(id);
}
public String getName() {
return "Glow";
}
This is calling older version, but with new EnchantmentWrapper requires String to initiate constructor, and this shows with super that it's a wrong argument. I need to support both old and new Lib (half clients still prefer old one). How can this be done with Reflection (or anything that might be even simpler)?
Upvotes: 2
Views: 365
Reputation: 5463
Some thing like this:
public class GlowEnchant extends EnchantmentWrapper {
public GlowEnchant(int id) {
super(id);
}
public GlowEnchant(String s) {
super(s);
}
public String getName() {
return "Glow";
}
public static GlowEnchant create(int id) {
if (hasStringConstructor()) {
return new GlowEnchant(id + "");
} else
return new GlowEnchant(id);
}
private static boolean hasStringConstructor() {
try {
EnchantmentWrapper.class.getConstructor(String.class);
return true;
} catch (NoSuchMethodException | SecurityException e) {
}
return false;
}
}
Use a local copy of EnchantmentWrapper that includes both constructors for compilation.
Upvotes: 1