• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockito.internal.exceptions.stacktrace;
7 
8 import org.mockito.exceptions.stacktrace.StackTraceCleaner;
9 import org.mockito.internal.configuration.plugins.Plugins;
10 
11 import java.io.Serializable;
12 import java.util.ArrayList;
13 import java.util.List;
14 
15 public class StackTraceFilter implements Serializable {
16 
17     static final long serialVersionUID = -5499819791513105700L;
18 
19     private static final StackTraceCleaner CLEANER =
20             Plugins.getStackTraceCleanerProvider().getStackTraceCleaner(new DefaultStackTraceCleaner());
21 
22     /**
23      * Example how the filter works (+/- means good/bad):
24      * [a+, b+, c-, d+, e+, f-, g+] -> [a+, b+, d+, e+, g+]
25      * Basically removes all bad from the middle.
26      * <strike>If any good are in the middle of bad those are also removed.</strike>
27      */
filter(StackTraceElement[] target, boolean keepTop)28     public StackTraceElement[] filter(StackTraceElement[] target, boolean keepTop) {
29         //TODO: profile
30         //TODO: investigate "keepTop" commit history - no effect!
31         final List<StackTraceElement> filtered = new ArrayList<StackTraceElement>();
32         for (StackTraceElement element : target) {
33             if (CLEANER.isIn(element)) {
34                 filtered.add(element);
35             }
36         }
37         StackTraceElement[] result = new StackTraceElement[filtered.size()];
38         return filtered.toArray(result);
39     }
40 }
41