Removing Repetition with Java Annotations -
is possible make following code cleaner less repetition using annotations? know possible java 8 closures, trying working on java 6/7
variable x = new variable(this,"hclass","hc"){ @override string getvalue(player p){ return getherofromplayer(p).getheroclass().getname(); } }; variable y = new variable(this,"hsecclass","hsc"){ @override string getvalue(player p){ return getherofromplayer(p).getsecondclass().getname(); } }; variable z = new variable(this,"hlevel","hl"){ @override string getvalue(player p){ return getherofromplayer(p).getlevel(); } }; variable = new variable(this,"hmastered","hma"){ @override string getvalue(player p){ return getherofromplayer(p).ismaster(getherofromplayer(p).getheroclass()) && (herosclass == null || getherofromplayer(p).ismaster(herosclass)) ? localetype.message_heroes_true.getval() : localetype.message_heroes_false.getval();; } }; this goes on time, added map, returns results lazily.
edit: hoping annotations allow me along lines of
@variable("hlevel","hl") string gethlevel(){getherofromplayer(p).getlevel();} edit: variable.java
abstract class variable { final private variablegroup vg; final private list<string> keys = new vector<string>(); variable(variablegroup vg,string...varargs){ this.vg = vg; (string s:varargs){ keys.add(s); } } abstract string getvalue(player p); }
based on comments can this
@variable("primary class") public string gethclass(player p) { return getherofromplayer(p).getheroclass().getname(); } @variable("primary class level") public int gethlevel(player p) { return getherofromplayer(p).gethlevel(); } @variable("secondary class") public string gethsecclass(player p) { return getherofromplayer(p).getsecondclass().getname(); } @variable("secondary class level") public int gethlevel(player p) { return getherofromplayer(p).gethseclevel(); } note: there no need fields return string. information can following
class heroclass = for(method method : heroclass.getmethods()) { variable var = method.getannotation(variable.class); if (var == null) continue; // ignore object.getclass() string description = var.value; // text display users string attributename = method.getname().substring(3); // cut "get" string initials = attributename.replaceall("[a-z]+", ""); }
Comments
Post a Comment