i have run bit of trouble while trying make 1 of bukkit plugins version independent using reflection. thing is, know how make instance of class using it's name, don't know how make use of class name type method or constructor parameter. in other words, want transform this:
public class foo{ private goat goat; public foo(goat goat){ this.goat=goat; }
into this:
public class foo{ private class.forname("mypackage.goat") goat; public foo(class.forname("mypackage.goat") goat){ this.goat=goat; }
honestly don't know if it's possible, lot in making development faster.
edit:
my situation bit more complicated 1 presented before. that's reason can't use generics. 'goat' type illustrated above class inside package unknown me, know class name. therefore, have make use of class.forname method along small algorithm detects current bukkit version , appends package name know, net.minecraft.server become net.minecraft.server.v1_8_r1 in other words, know class name @ runtime. know, suck @ explaining...
updated answer:
you've clarified in comments know type name at runtime.
the type of field (and constructor parameter) goat
set @ compile time. way use runtime information create foo
class that: generate @ runtime. there's no mechanism declaring placeholder filled in string later.
if absolutely need that, prior art call on tomcat (which compiles jsps on-the-fly) , vert.x (which generates classes on-the-fly). it's going non-trivial (unless you're shelling out javac
, of course).
original answer:
you can generics:
public class foo<t> { private t goat; public foo(t goat){ this.goat=goat; } }
usage:
foo<goat> f = new foo<>(); // or new foo<goat>() (earlier java versions didn't support <> form)
now, f
expects goat
goat
.
it's when you're using collections framework:
list<string> strings = new list<string>(); list<date> dates = new list<date>(); // ...
Comments
Post a Comment