KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > CheckCalls


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2005, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
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.detect;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.apache.bcel.classfile.Method;
26 import org.apache.bcel.generic.Instruction;
27 import org.apache.bcel.generic.InvokeInstruction;
28
29 import edu.umd.cs.findbugs.BugReporter;
30 import edu.umd.cs.findbugs.Detector;
31 import edu.umd.cs.findbugs.SystemProperties;
32 import edu.umd.cs.findbugs.ba.CFG;
33 import edu.umd.cs.findbugs.ba.CFGBuilderException;
34 import edu.umd.cs.findbugs.ba.ClassContext;
35 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
36 import edu.umd.cs.findbugs.ba.Hierarchy;
37 import edu.umd.cs.findbugs.ba.JavaClassAndMethod;
38 import edu.umd.cs.findbugs.ba.Location;
39 import edu.umd.cs.findbugs.ba.SignatureConverter;
40
41 /**
42  * This is just for debugging method call resolution.
43  *
44  * @author David Hovemeyer
45  */

46 public class CheckCalls implements Detector {
47     
48     private static final String JavaDoc METHOD = SystemProperties.getProperty("checkcalls.method");
49     private static final String JavaDoc TARGET_METHOD = SystemProperties.getProperty("checkcalls.targetmethod");
50     
51     BugReporter bugReporter;
52     
53     public CheckCalls(BugReporter bugReporter) {
54         this.bugReporter = bugReporter;
55     }
56
57     /* (non-Javadoc)
58      * @see edu.umd.cs.findbugs.Detector#visitClassContext(edu.umd.cs.findbugs.ba.ClassContext)
59      */

60     public void visitClassContext(ClassContext classContext) {
61         Method[] methodList = classContext.getJavaClass().getMethods();
62         for (Method method : methodList) {
63             if (method.getCode() == null)
64                 continue;
65
66             //System.out.println("--> " + method.getName());
67
if (METHOD != null && !method.getName().equals(METHOD))
68                 continue;
69
70             try {
71                 System.out.println("Analyzing " +
72                         SignatureConverter.convertMethodSignature(classContext.getJavaClass(), method)
73                 );
74                 analyzeMethod(classContext, method);
75             } catch (CFGBuilderException e) {
76                 bugReporter.logError("Error", e);
77             } catch (DataflowAnalysisException e) {
78                 bugReporter.logError("Error", e);
79             } catch (ClassNotFoundException JavaDoc e) {
80                 bugReporter.reportMissingClass(e);
81             }
82         }
83     }
84
85     private void analyzeMethod(ClassContext classContext, Method method)
86             throws CFGBuilderException, ClassNotFoundException JavaDoc, DataflowAnalysisException {
87         CFG cfg = classContext.getCFG(method);
88         for (Iterator JavaDoc<Location> i = cfg.locationIterator(); i.hasNext();) {
89             Location location = i.next();
90             Instruction ins = location.getHandle().getInstruction();
91
92             if (ins instanceof InvokeInstruction) {
93                 if (TARGET_METHOD != null
94                         && !((InvokeInstruction)ins).getMethodName(classContext.getConstantPoolGen()).equals(TARGET_METHOD))
95                     continue;
96                 
97                 System.out.println("\n*******************************************************\n");
98                 
99                 System.out.println("Method invocation: " + location.getHandle());
100                 System.out.println("\tInvoking: " +
101                         SignatureConverter.convertMethodSignature((InvokeInstruction)ins,classContext.getConstantPoolGen()));
102                 
103                 JavaClassAndMethod proto = Hierarchy.findInvocationLeastUpperBound(
104                         (InvokeInstruction) ins,
105                         classContext.getConstantPoolGen());
106                 if (proto == null) {
107                     System.out.println("\tUnknown prototype method");
108                 } else {
109                     System.out.println("\tPrototype method: class=" +
110                             proto.getJavaClass().getClassName() + ", method=" +
111                             proto.getMethod());
112                 }
113                 Set JavaDoc<JavaClassAndMethod> calledMethodSet = Hierarchy.resolveMethodCallTargets(
114                         (InvokeInstruction) ins,
115                         classContext.getTypeDataflow(method).getFactAtLocation(location),
116                         classContext.getConstantPoolGen()
117                 );
118                 System.out.println("\tTarget method set: " + calledMethodSet);
119             }
120         }
121     }
122
123     /* (non-Javadoc)
124      * @see edu.umd.cs.findbugs.Detector#report()
125      */

126     public void report() {
127     }
128
129 }
130
Popular Tags