KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > DataflowCFGPrinter


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2003,2004 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.ba;
21
22 import java.io.PrintStream JavaDoc;
23
24 import org.apache.bcel.generic.InstructionHandle;
25
26 /**
27  * CFGPrinter class which prints dataflow values at
28  * each basic block and instruction.
29  */

30 public class DataflowCFGPrinter <Fact, AnalysisType extends BasicAbstractDataflowAnalysis<Fact>> extends CFGPrinter {
31     private Dataflow<Fact, AnalysisType> dataflow;
32
33     public DataflowCFGPrinter(Dataflow<Fact, AnalysisType> dataflow) {
34         super(dataflow.getCFG());
35         this.dataflow = dataflow;
36
37         setIsForwards(dataflow.getAnalysis().isForwards());
38     }
39     
40     /* (non-Javadoc)
41      * @see edu.umd.cs.findbugs.ba.CFGPrinter#edgeAnnotate(edu.umd.cs.findbugs.ba.Edge)
42      */

43     @Override JavaDoc
44     public String JavaDoc edgeAnnotate(Edge edge) {
45         String JavaDoc edgeAnnotation= "";
46         try {
47             edgeAnnotation = " " + dataflow.getAnalysis().factToString(dataflow.getAnalysis().getFactOnEdge(edge));
48         } catch (Throwable JavaDoc e) {
49             // ignore
50
}
51         return edgeAnnotation;
52     }
53
54     @Override JavaDoc
55     public String JavaDoc blockStartAnnotate(BasicBlock bb) {
56         boolean flip = isForwards() != dataflow.getAnalysis().isForwards();
57         Fact fact = flip ? dataflow.getResultFact(bb) : dataflow.getStartFact(bb);
58         
59         return " " + dataflow.getAnalysis().factToString(fact);
60     }
61
62     @Override JavaDoc
63     public String JavaDoc blockAnnotate(BasicBlock bb) {
64         boolean flip = isForwards() != dataflow.getAnalysis().isForwards();
65         Fact fact = flip ? dataflow.getStartFact(bb) : dataflow.getResultFact(bb) ;
66
67         return " " + dataflow.getAnalysis().factToString(fact);
68     }
69
70     @Override JavaDoc
71     public String JavaDoc instructionAnnotate(InstructionHandle handle, BasicBlock bb) {
72         try {
73             boolean flip = isForwards() != dataflow.getAnalysis().isForwards();
74             
75             Location loc =new Location(handle, bb);
76
77             Fact fact = flip
78                     ? dataflow.getAnalysis().getFactAfterLocation(loc)
79                     : dataflow.getAnalysis().getFactAtLocation(loc);
80             return " " + dataflow.getAnalysis().factToString(fact);
81         } catch (DataflowAnalysisException e) {
82             throw new IllegalStateException JavaDoc("Caught exception: " + e.toString());
83         }
84     }
85
86     /**
87      * Print CFG annotated with results from given dataflow analysis.
88      *
89      * @param <Fact> Dataflow fact type
90      * @param <AnalysisType> Dataflow analysis type
91      * @param dataflow dataflow driver
92      * @param out PrintStream to use
93      */

94     public static<Fact, AnalysisType extends BasicAbstractDataflowAnalysis<Fact>>
95     void printCFG(Dataflow<Fact, AnalysisType> dataflow, PrintStream JavaDoc out) {
96         DataflowCFGPrinter<Fact, AnalysisType> printer =
97             new DataflowCFGPrinter<Fact, AnalysisType>(dataflow);
98         printer.print(out);
99     }
100     
101 }
102
103 // vim:ts=4
104
Popular Tags