Tuesday 15 July 2014

java - How to get a unique method identifier? -



java - How to get a unique method identifier? -

i'm needing unique method identifier utilize key on hashmap.

i'm trying using stacktrace , reflection , user method signature. problem didn´t find way retrive finish method signature (to avoid methods overload).

edited

i somethink works:

public class class1 { hashmap<string, object> hm; public class1() { hm = new hashmap<string, object>(); } public object method() { if (!containskey()) { object value; ... put(value); } homecoming get(); } public object method(string arg1) { if (!containskey()) { object value; ... put(value); } homecoming get(); } public boolean containskey() { if (hm.containskey(util.getuniqueid(2)) { homecoming true; } else { homecoming false; } } public void put(object value) { hm.put(util.getuniqueid(2), value); } public object get() { string key = util.getuniqueid(2); if (hm.containskey(key) { homecoming hm.get(key); } else { homecoming null; } } } class util { public static string getuniqueid(integer depth) { stacktraceelement element = thread.currentthread().getstacktrace()[depth]; homecoming element.getclassname() + ":" + element.getmethodname(); } }

but problem 2 methods, strategy, have same id.

how can work around?

you can append + ":" + element.getlinenumber() you'd still have worry case 2 overloaded methods set on 1 long line.

looking @ stacktraceelement methods, doesn't seem possible unique method identifier way. besides, code not readable in opinion.

i'd suggest seek more explicit , do

if (hm.containskey("getvalue(int)") { ... }

or similar.

java methods uniqueidentifier

No comments:

Post a Comment