001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.mstat; 014 015import java.util.*; 016 017/** 018 * Stack trace utility methods. 019 */ 020public class ExceptionHasher { 021 022 private final String stopClass; 023 024 /** 025 * TODO 026 * 027 * @param stopClass TODO 028 */ 029 public ExceptionHasher(Class<?> stopClass) { 030 this.stopClass = stopClass == null ? null : stopClass.getName(); 031 } 032 033 /** 034 * Calculates a 16-bit hash for the specified throwable based on it's stack trace. 035 * 036 * @param t The throwable to calculate the stack trace on. 037 * @return A calculated hash. 038 */ 039 public int hash(Throwable t) { 040 int i = 0; 041 while (t != null) { 042 for (StackTraceElement e : t.getStackTrace()) { 043 if (e.getClassName().equals(stopClass)) 044 break; 045 if (e.getClassName().indexOf('$') == -1) 046 i ^= e.hashCode(); 047 } 048 t = t.getCause(); 049 } 050 return i; 051 } 052 053 /** 054 * TODO 055 * 056 * @param t TODO 057 * @return TODO 058 */ 059 public List<StackTraceElement> getStackTrace(Throwable t) { 060 List<StackTraceElement> l = new ArrayList<>(); 061 for (StackTraceElement e : t.getStackTrace()) { 062 if (e.getClassName().equals(stopClass)) 063 break; 064 l.add(e); 065 } 066 return Collections.unmodifiableList(l); 067 } 068}