Here you can find the source of printStackTrace(StackTraceElement[] elements)
public static List<String> printStackTrace(StackTraceElement[] elements)
//package com.java2s; /* Copyright Francesco Andreuzzi Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class Main { private static final String DOT = "."; private static final String TAB = "\t"; private static final String NEWLINE = "\n"; private static final String START_LABEL = "--- start"; private static final String END_LABEL = "--- end"; public static List<String> printStackTrace(StackTraceElement[] elements) { List<String> stackTrace = new ArrayList<>(); String last = null;/*w w w. j av a 2 s.co m*/ boolean lastWasTabbed = false; for (StackTraceElement element : elements) { if (last == null) { stackTrace.add(element.toString()); last = element.toString(); continue; } String current = element.toString(); int currentFirstPoint = current.indexOf(DOT); String after = current.substring(currentFirstPoint + 1); int currentSecondPoint = after.indexOf(DOT); if (currentSecondPoint == -1) currentSecondPoint = currentFirstPoint; int lastFirstPoint = last.indexOf(DOT); after = last.substring(lastFirstPoint + 1); int lastSecondPoint = after.indexOf(DOT); if (lastSecondPoint == -1) lastSecondPoint = lastFirstPoint; if (current.substring(0, currentSecondPoint).equals( last.substring(0, lastSecondPoint))) { if (lastWasTabbed) { stackTrace.add(TAB + current); } else { stackTrace.add(current); } } else { if (lastWasTabbed) { stackTrace.add(current); lastWasTabbed = false; } else { stackTrace.add(TAB + current); lastWasTabbed = true; } } last = current; } return stackTrace; } public static void printStackTrace(StackTraceElement[] elements, PrintWriter writer) { if (writer == null) { return; } List<String> stackTrace = printStackTrace(elements); writer.write(START_LABEL); for (String s : stackTrace) { writer.write(s); } writer.write(END_LABEL); writer.write(NEWLINE); } }