1 package org.junit.internal.runners.statements;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 
6 import org.junit.runners.model.FrameworkMethod;
7 import org.junit.runners.model.MultipleFailureException;
8 import org.junit.runners.model.Statement;
9 
10 public class RunAfters extends Statement {
11     private final Statement next;
12 
13     private final Object target;
14 
15     private final List<FrameworkMethod> afters;
16 
RunAfters(Statement next, List<FrameworkMethod> afters, Object target)17     public RunAfters(Statement next, List<FrameworkMethod> afters, Object target) {
18         this.next = next;
19         this.afters = afters;
20         this.target = target;
21     }
22 
23     @Override
evaluate()24     public void evaluate() throws Throwable {
25         List<Throwable> errors = new ArrayList<Throwable>();
26         try {
27             next.evaluate();
28         } catch (Throwable e) {
29             errors.add(e);
30         } finally {
31             for (FrameworkMethod each : afters) {
32                 try {
33                     invokeMethod(each);
34                 } catch (Throwable e) {
35                     errors.add(e);
36                 }
37             }
38         }
39         MultipleFailureException.assertEmpty(errors);
40     }
41 
42     /**
43      * @since 4.13
44      */
invokeMethod(FrameworkMethod method)45     protected void invokeMethod(FrameworkMethod method) throws Throwable {
46         method.invokeExplosively(target);
47     }
48 }