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.bcel;
21:
22: import org.apache.bcel.classfile.JavaClass;
23: import org.apache.bcel.classfile.Method;
24: import org.apache.bcel.generic.MethodGen;
25:
26: import edu.umd.cs.findbugs.Detector2;
27: import edu.umd.cs.findbugs.ba.CFG;
28: import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
29: import edu.umd.cs.findbugs.classfile.ClassDescriptor;
30: import edu.umd.cs.findbugs.classfile.Global;
31: import edu.umd.cs.findbugs.classfile.IAnalysisCache;
32: import edu.umd.cs.findbugs.classfile.MethodDescriptor;
33:
34: /**
35: * Base class for detectors that analyze CFG (and/or use CFG-based analyses).
36: *
37: * @author David Hovemeyer
38: */
39: public abstract class CFGDetector implements Detector2 {
40:
41: /* (non-Javadoc)
42: * @see edu.umd.cs.findbugs.Detector2#finishPass()
43: */
44: public void finishPass() {
45: }
46:
47: /* (non-Javadoc)
48: * @see edu.umd.cs.findbugs.Detector2#getDetectorClassName()
49: */
50: public String getDetectorClassName() {
51: return getClass().getName();
52: }
53:
54: /* (non-Javadoc)
55: * @see edu.umd.cs.findbugs.Detector2#visitClass(edu.umd.cs.findbugs.classfile.ClassDescriptor)
56: */
57: public void visitClass(ClassDescriptor classDescriptor)
58: throws CheckedAnalysisException {
59: IAnalysisCache analysisCache = Global.getAnalysisCache();
60:
61: JavaClass jclass = analysisCache.getClassAnalysis(
62: JavaClass.class, classDescriptor);
63:
64: for (Method method : jclass.getMethods()) {
65: if (method.getCode() == null) {
66: continue;
67: }
68:
69: MethodDescriptor methodDescriptor = BCELUtil
70: .getMethodDescriptor(jclass, method);
71:
72: // Try to get MethodGen. If we can't get one,
73: // then this method should be skipped.
74: MethodGen methodGen = analysisCache.getMethodAnalysis(
75: MethodGen.class, methodDescriptor);
76: if (methodGen == null) {
77: continue;
78: }
79:
80: CFG cfg = analysisCache.getMethodAnalysis(CFG.class,
81: methodDescriptor);
82: visitMethodCFG(methodDescriptor, cfg);
83: }
84: }
85:
86: /**
87: * Visit the CFG (control flow graph) of a method to be analyzed.
88: * Should be overridded by subclasses.
89: *
90: * @param methodDescriptor
91: * @param cfg
92: * @throws CheckedAnalysisException
93: */
94: protected abstract void visitMethodCFG(
95: MethodDescriptor methodDescriptor, CFG cfg)
96: throws CheckedAnalysisException;
97:
98: }
|