01: /*
02: * FindBugs - Find Bugs in Java programs
03: * Copyright (C) 2003-2007 University of Maryland
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: */
19:
20: package edu.umd.cs.findbugs;
21:
22: import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
23: import edu.umd.cs.findbugs.classfile.ClassDescriptor;
24:
25: /**
26: * A special Detector2 class designed to run some JUnit test code.
27: * Only used by FindBugsTestCase.
28: *
29: * @author David Hovemeyer
30: * @see FindBugsTestCase
31: */
32: public class JUnitDetectorAdapter implements Detector2 {
33:
34: private Throwable throwable;
35: private boolean testExecuted;
36:
37: private static InheritableThreadLocal<JUnitDetectorAdapter> instance = new InheritableThreadLocal<JUnitDetectorAdapter>();
38: private static InheritableThreadLocal<RunnableWithExceptions> runnableInstance = new InheritableThreadLocal<RunnableWithExceptions>();
39:
40: public JUnitDetectorAdapter(BugReporter bugReporter) {
41: instance.set(this );
42: }
43:
44: public static JUnitDetectorAdapter instance() {
45: return instance.get();
46: }
47:
48: /**
49: * @param runnable The runnable to set.
50: */
51: public static void setRunnable(RunnableWithExceptions runnable) {
52: runnableInstance.set(runnable);
53: }
54:
55: public void finishTest() throws Exception {
56: if (throwable instanceof Exception)
57: throw (Exception) throwable;
58: if (throwable instanceof Error)
59: throw (Error) throwable;
60: if (throwable != null)
61: throw new Error(throwable);
62:
63: }
64:
65: /* (non-Javadoc)
66: * @see edu.umd.cs.findbugs.Detector2#finishPass()
67: */
68: public void finishPass() {
69: }
70:
71: /* (non-Javadoc)
72: * @see edu.umd.cs.findbugs.Detector2#getDetectorClassName()
73: */
74: public String getDetectorClassName() {
75: return this .getClass().getName();
76: }
77:
78: /* (non-Javadoc)
79: * @see edu.umd.cs.findbugs.Detector2#visitClass(edu.umd.cs.findbugs.classfile.ClassDescriptor)
80: */
81: public void visitClass(ClassDescriptor classDescriptor)
82: throws CheckedAnalysisException {
83: // Only execute the test once
84: if (testExecuted) {
85: return;
86: }
87: testExecuted = true;
88:
89: try {
90: runnableInstance.get().run();
91: } catch (Throwable e) {
92: // e.printStackTrace();
93: throwable = e;
94: }
95: }
96:
97: }
|