1. StackTraceElement StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); for (StackTraceElement element : stackTrace) { String className = element.getClassName(); String methodName = element.getMethodName(); String fileName = element.getFileName(); int lineNumber = element.getLineNumber(); // ... } 2. AspectJ @Aspect public class MyAspect { @Before("execution(* com.example.MyClass.myMethod(..))") public void beforeAdvice(JoinPoint joinPoint) { Signature signature = joinPoint.getSignature(); String className = signature.getDeclaringTypeName(); String methodName = signature.getName(); int lineNumber = joinPoint.getSourceLocation().getLine(); // ... } } 3. JDI (Java Debug Interface) VirtualMachineManager virtualMachineManager = Bootstrap.virtualMachineManager(); List<AttachingConnector> connectors = virtualMachineManager.attachingConnectors(); AttachingConnector connector = connectors.get(0); VirtualMachine virtualMachine = connector.attach(connector.defaultArguments()); List<ReferenceType> classes = virtualMachine.allClasses(); for (ReferenceType referenceType : classes) { List<Location> locations = referenceType.allLineLocations(); for (Location location : locations) { String className = location.sourcePath(); String methodName = location.method().name(); int lineNumber = location.lineNumber(); // ... } }


上一篇:
下一篇:
切换中文