Java get parameter values through reflection -
i have set of classes similar. i'm using reflection access , invoke choose method below. need able mylist
parameter. i've tried using proxies but, i'm beginner java , don't understand how implement , use them.
public int choose(list<object> mylist, object card, object color, state state) { int answer = -1; // sort through mylist , set answer index of desired object return answer; }
is there better way parameter in java without using aspectj? , if so, provide example?
thanks help.
class state { } class myclass { public int choose(list<object> mylist, object card, object color, state state) { int answer = -1; system.out.println("mylist has " + mylist.size() + " elements"); (object o : mylist) { system.out.println("element -> " + o); } // sort through mylist , set answer index of desired object return answer; } } public static int invokechoose(object obj) throws nosuchmethodexception, invocationtargetexception, illegalaccessexception { method m = obj.getclass().getdeclaredmethod("choose", list.class, object.class, object.class, state.class); list<object> somelist = new arraylist<>(); somelist.add(integer.valueof(0)); somelist.add(integer.valueof(1)); somelist.add(integer.valueof(2)); object result = m.invoke(obj, somelist, new object(), new object(), new state()); return ((number)result).intvalue(); } public static void tryit() { myclass c = new myclass(); try { int result = invokechoose(c); system.out.println("the result is: " + result); } catch (throwable e) { e.printstacktrace(); } }
output:
mylist has 3 elements element -> 0 element -> 1 element -> 2 result is: -1
Comments
Post a Comment