@@ -0,0 +1,2703 @@
+/*%0A * Copyright 2016    https://github.com/sdcuike Inc. %0A * All rights reserved.%0A *%0A * Licensed to the Apache Software Foundation (ASF) under one or more%0A * contributor license agreements.  See the NOTICE file distributed with%0A * this work for additional information regarding copyright ownership.%0A * The ASF licenses this file to You under the Apache License, Version 2.0%0A * (the %22License%22); you may not use this file except in compliance with%0A * the License.  You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage com.doctor.reflect;%0A%0Aimport java.io.PrintWriter;%0Aimport java.io.StringWriter;%0A%0A/**%0A * Call stack introspection%0A * %0A * @author sdcuike%0A *%0A *         Created At 2016%E5%B9%B48%E6%9C%8829%E6%97%A5 %E4%B8%8B%E5%8D%889:40:35%0A */%0Apublic class CallStackIntrospectionDemo %7B%0A    private static final MyLogger logger = new LoggerImpl();%0A%0A    public static void main(String%5B%5D args) %7B%0A        logger.logRecord(%22hello%22);%0A%0A        IllegalArgumentException exception = new IllegalArgumentException(%22IllegalArgumentException%22);%0A        logger.logProblem(%22throwable%22, exception);%0A    %7D%0A%0A    public interface MyLogger %7B%0A        // Types for log records%0A        int ERROR   = 0;%0A        int WARNING = 100;%0A        int STATUS  = 200;%0A        int DEBUG   = 300;%0A        int TRACE   = 400;%0A%0A        void logRecord(String message);%0A%0A        void logProblem(String message, Throwable throwable);%0A    %7D%0A%0A    public static class LoggerImpl implements MyLogger %7B%0A%0A        @Override%0A        public void logRecord(String message) %7B%0A            Throwable throwable = new Throwable();%0A            log(message, throwable.getStackTrace()%5B1%5D);%0A        %7D%0A%0A        @Override%0A        public void logProblem(String message, Throwable throwable) %7B%0A            StringWriter out = new StringWriter();%0A            PrintWriter writer = new PrintWriter(out);%0A            throwable.printStackTrace(writer);%0A            log(message + out.toString(), throwable.getStackTrace()%5B0%5D);%0A        %7D%0A%0A        private void log(String message, StackTraceElement stackTraceElement) %7B%0A            String className = stackTraceElement.getClassName();%0A            String methodName = stackTraceElement.getMethodName();%0A            int lineNumber = stackTraceElement.getLineNumber();%0A            System.out.println(String.join(%22  %22, %22%E6%A8%A1%E6%8B%9F%E6%89%93%E5%8D%B0%E6%97%A5%E5%BF%97:%22, methodName, className, %22%22 + lineNumber, message));%0A        %7D%0A%0A    %7D%0A%7D%0A

Call stack introspection