001/*-------------------------------------------------------------------------+ 002| | 003| Copyright (c) 2005-2019 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016| | 017+-------------------------------------------------------------------------*/ 018package org.conqat.lib.commons.error; 019 020import java.util.ArrayList; 021import java.util.List; 022 023import org.conqat.lib.commons.string.StringUtils; 024 025/** 026 * Utility functionality for exception handling. 027 */ 028public class ExceptionUtils { 029 030 /** 031 * Trims the given {@link StackOverflowError}'s stacktrace by omitting all 032 * redundant stack trace elements. 033 */ 034 public static void trimStackOverFlowTrace(StackOverflowError e) { 035 List<StackTraceElement> trimmedStackTrace = new ArrayList<>(); 036 StackTraceElement previousElement = null; 037 StackTraceElement[] stackTrace = e.getStackTrace(); 038 for (int i = 0; i < stackTrace.length; i++) { 039 StackTraceElement currentElement = stackTrace[i]; 040 if (!currentElement.equals(previousElement)) { 041 trimmedStackTrace.add(stackTrace[i]); 042 previousElement = currentElement; 043 } 044 } 045 e.setStackTrace(trimmedStackTrace.toArray(new StackTraceElement[trimmedStackTrace.size()])); 046 } 047 048 /** 049 * Obtains the stack trace of the given {@link Throwable} as a string. 050 */ 051 public static String getStacktraceAsString(Throwable t) { 052 return StringUtils.obtainStackTrace(t); 053 } 054 055}