http://blog.sina.com.cn/s/blog_4a4f9fb50101eyfp.html
package org.wikijava.reflection;
public class MethodName { public static void main(String[] args) {
MethodName methodName = new MethodName();
String clazz = Thread.currentThread() .getStackTrace()[1].getClassName();
String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
System.out.println("class name: " + clazz + " Method Name " + method);
methodName.anotherMethod(); }
private void anotherMethod() {
String clazz = this.getClass().getName();
String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
System.out.println("class name: " + clazz + " Method Name " + method); } }
java中获取当前代码行方法名
String method = Thread.currentThread().getStackTrace()[1].getMethodName();
String key = TextUtil.isEmpty(method)?"null":method.toLowerCase().replace("get", "").replace("set", "");
settings.edit().putString(key, lontitude+"").commit();
android获取当前代码行方法名:
String method = Thread.currentThread().getStackTrace()[2].getMethodName();
String key = TextUtil.isEmpty(method)?"null":method.toLowerCase().replace("get", "").replace("set", "");
settings.edit().putString(key, lontitude+"").commit();