I have recently run into a bit of trouble while trying to make one of my bukkit plugins version independent using reflection. The thing is, I know how to make an instance of a class by using it's name, but I don't know how to make use of the class name as a type for a method or constructor parameter. In other words, I want to transform this:
public class Foo{
private Goat goat;
public Foo(Goat goat){
this.goat=goat;
}
Into something like this:
public class Foo{
private Class.forName("mypackage.Goat") goat;
public Foo(Class.forName("mypackage.Goat") goat){
this.goat=goat;
}
Honestly I don't know if it's even possible, but it would help a lot in making my development faster.
EDIT:
My situation is a bit more complicated than the one I presented before. That's the reason I can't actually use generics. The 'Goat' type I illustrated above is actually a class inside a package unknown to me, so I only know the class name. Therefore, I have to make use of the Class.forName method along with a small algorithm that detects the current bukkit version and appends it to the package name I already know, so net.minecraft.server would become net.minecraft.server.v1_8_R1 In other words, I only know the class name at runtime. I know, I suck at explaining...
Aucun commentaire:
Enregistrer un commentaire