常用辅助类

public class ClassHelper {

	/**
	 * 获取当前正在执行的方法名
	 * 
	 * @return
	 */
	public static String getCurrentThreadMethodName() {
		StackTraceElement[] stack = Thread.currentThread().getStackTrace();
		StackTraceElement stackTraceElement = stack[2];
		String className = stackTraceElement.getClassName();
		String methodName = stackTraceElement.getMethodName();
		String s = className + "." + methodName;
		return s;
	}

	/**
	 * 获取类路径
	 * 
	 * @return
	 */
	public static String getClassPath() {
		String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		return path.replace("\\", "/");
	}

	/**
	 * 获取当前应用的根目录,不含最后的斜线
	 * 
	 * @return
	 */
	public static String getBasePath() {
		String classPath = getClassPath();
		int webInfoIndex = classPath.indexOf("/WEB-INF");
		if (webInfoIndex > -1) {
			return classPath.substring(0, webInfoIndex);
		} else {
			return classPath.substring(0, classPath.indexOf("/target/classes"));
		}
	}

	/**
	 * 获取当前应用的名字
	 * 
	 * @return
	 */
	public static String getAppName() {
		String basePath = getBasePath();
		int index = basePath.lastIndexOf("/") + 1;
		return getBasePath().substring(index);
	}
}

  

posted @ 2018-10-23 17:06  漫漫人生路总会错几步  阅读(136)  评论(0编辑  收藏  举报