我需要获得一个唯一的方法标识符用作HashMap上的一个键。如何获取唯一的方法标识符?
我想做一些使用堆栈跟踪和反射和用户的方法签名。但问题是我没有找到一种方法来检索完整的方法签名(以避免方法超载)。
编辑
我想这somethink像这样工作的:
public class Class1 {
HashMap<String, Object> hm;
public Class1() {
hm = new HashMap<String, Object>();
}
public Object method() {
if (!containsKey()) {
Object value;
...
put(value);
}
return get();
}
public Object method(String arg1) {
if (!containsKey()) {
Object value;
...
put(value);
}
return get();
}
public Boolean containsKey() {
if (hm.containsKey(Util.getUniqueID(2)) {
return true;
} else {
return false;
}
}
public void put(Object value) {
hm.put(Util.getUniqueID(2), value);
}
public Object get() {
String key = Util.getUniqueID(2);
if (hm.containsKey(key) {
return hm.get(key);
} else {
return null;
}
}
}
class Util {
public static String getUniqueID(Integer depth) {
StackTraceElement element = Thread.currentThread().getStackTrace()[depth];
return element.getClassName() + ":" + element.getMethodName();
}
}
但问题是这两种方式,这一战略,将具有相同的ID。
我该如何解决?
将参数的类型添加到您的ID。 – Simulant 2014-10-17 12:33:10
我需要一种单身人士。 – GarouDan 2014-10-17 12:43:55