KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > ca > CallListAnalysis


1 /*
2  * Bytecode Analysis Framework
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 package edu.umd.cs.findbugs.ba.ca;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.bcel.classfile.Method;
26 import org.apache.bcel.generic.ConstantPoolGen;
27 import org.apache.bcel.generic.Instruction;
28 import org.apache.bcel.generic.InstructionHandle;
29 import org.apache.bcel.generic.InvokeInstruction;
30
31 import edu.umd.cs.findbugs.ba.AbstractDataflowAnalysis;
32 import edu.umd.cs.findbugs.ba.BasicBlock;
33 import edu.umd.cs.findbugs.ba.BlockOrder;
34 import edu.umd.cs.findbugs.ba.CFG;
35 import edu.umd.cs.findbugs.ba.CFGBuilderException;
36 import edu.umd.cs.findbugs.ba.ClassContext;
37 import edu.umd.cs.findbugs.ba.Dataflow;
38 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
39 import edu.umd.cs.findbugs.ba.DataflowTestDriver;
40 import edu.umd.cs.findbugs.ba.DepthFirstSearch;
41 import edu.umd.cs.findbugs.ba.Edge;
42 import edu.umd.cs.findbugs.ba.Location;
43 import edu.umd.cs.findbugs.ba.ReversePostOrder;
44
45 public class CallListAnalysis extends AbstractDataflowAnalysis<CallList> {
46     private CFG cfg;
47     private DepthFirstSearch dfs;
48     //private ConstantPoolGen cpg;
49
private Map JavaDoc<InstructionHandle, Call> callMap;
50     
51     public CallListAnalysis(CFG cfg, DepthFirstSearch dfs, ConstantPoolGen cpg) {
52         this.cfg = cfg;
53         this.dfs = dfs;
54         //this.cpg = cpg;
55
this.callMap = buildCallMap(cfg, cpg);
56     }
57     
58     private static Map JavaDoc<InstructionHandle, Call> buildCallMap(CFG cfg, ConstantPoolGen cpg) {
59         Map JavaDoc<InstructionHandle, Call> callMap = new HashMap JavaDoc<InstructionHandle, Call>();
60         
61         for (Iterator JavaDoc<Location> i = cfg.locationIterator(); i.hasNext();) {
62             InstructionHandle handle = i.next().getHandle();
63             Instruction ins = handle.getInstruction();
64             
65             if (ins instanceof InvokeInstruction) {
66                 InvokeInstruction inv = (InvokeInstruction) ins;
67                 Call call = new Call(inv.getClassName(cpg), inv.getName(cpg), inv.getSignature(cpg));
68                 callMap.put(handle, call);
69             }
70         }
71         
72         return callMap;
73     }
74     
75     public void initEntryFact(CallList fact) {
76         fact.clear();
77     }
78     
79     public void initResultFact(CallList fact) {
80         fact.setTop();
81     }
82     
83     public boolean isForwards() {
84         return true;
85     }
86
87     public BlockOrder getBlockOrder(CFG cfg) {
88         return new ReversePostOrder(cfg, dfs);
89     }
90     
91     public void makeFactTop(CallList fact) {
92         fact.setTop();
93     }
94     
95     public boolean isTop(CallList fact) {
96         return fact.isTop();
97     }
98     
99     public CallList createFact() {
100         return new CallList();
101     }
102     
103     public boolean same(CallList a, CallList b) {
104         return a.equals(b);
105     }
106     
107     public void meetInto(CallList start, Edge edge, CallList result)
108             throws DataflowAnalysisException {
109         CallList merge = CallList.merge(start, result);
110         result.copyFrom(merge);
111     }
112     
113     public void copy(CallList source, CallList dest) {
114         dest.copyFrom(source);
115     }
116
117     @Override JavaDoc
118          public void transferInstruction(
119             InstructionHandle handle, BasicBlock basicBlock, CallList fact) throws DataflowAnalysisException {
120         Call call = callMap.get(handle);
121         if (call != null) {
122             fact.add(call);
123         }
124     }
125     
126     @Override JavaDoc
127          public boolean isFactValid(CallList fact) {
128         return fact.isValid();
129     }
130     
131     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
132         if (argv.length != 1) {
133             System.err.println("Usage: " + CallListAnalysis.class.getName() + " <class file>");
134             System.exit(1);
135         }
136         
137         DataflowTestDriver<CallList, CallListAnalysis> driver =
138             new DataflowTestDriver<CallList, CallListAnalysis>() {
139                 @Override JavaDoc
140                                  public Dataflow<CallList, CallListAnalysis> createDataflow(
141                         ClassContext classContext,
142                         Method method) throws CFGBuilderException, DataflowAnalysisException {
143                     CallListAnalysis analysis = new CallListAnalysis(
144                             classContext.getCFG(method),
145                             classContext.getDepthFirstSearch(method),
146                             classContext.getConstantPoolGen());
147                     Dataflow<CallList, CallListAnalysis> dataflow =
148                         new Dataflow<CallList, CallListAnalysis>(analysis.cfg, analysis);
149                         
150                     dataflow.execute();
151                     
152                     return dataflow;
153                 }
154             };
155     
156         driver.execute(argv[0]);
157     }
158 }
159
Popular Tags