package com.xxx.xxxx; import java.lang.reflect.Method; public class test { public static void main(String[] args) throws ClassNotFoundException{ System.out.print(getTraceInfo()); } public static String getTraceInfo() throws ClassNotFoundException{ StringBuffer sb = new StringBuffer(); StackTraceElement[] stacks = new Throwable().getStackTrace(); int stacksLen = stacks.length; System.out.println(stacksLen); // Class clazz = Class.forName("java.lang.StringBuffer"); Class<?> clazz = Class.forName(StackTraceElement.class.getName()); // Class<?> clazz = Class.forName(test.class.getName()); Method[] methods = clazz.getMethods(); for (Method method : methods) { String methodName = method.getName(); System.out.println("方法名称:" + methodName); Class<?>[] parameterTypes = method.getParameterTypes(); for (Class<?> clas : parameterTypes) { String parameterName = clas.getName(); System.out.println("参数名称:" + parameterName); } System.out.println("*****************************"); } sb.append("class: " ).append(stacks[1].getClassName()).append("; method: ").append(stacks[1].getMethodName()).append("; number: ").append(stacks[1].getLineNumber()); return sb.toString(); } }