KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 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.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.bcel.classfile.ClassParser;
26 import org.apache.bcel.classfile.JavaClass;
27 import org.apache.bcel.classfile.Method;
28 import org.apache.bcel.generic.CodeExceptionGen;
29 import org.apache.bcel.generic.InstructionHandle;
30 import org.apache.bcel.generic.ObjectType;
31
32 import edu.umd.cs.findbugs.SystemProperties;
33 import edu.umd.cs.findbugs.annotations.CheckForNull;
34
35 /**
36  * Dataflow analysis to determine the nesting of catch and finally
37  * blocks within a method.
38  *
39  * @see BlockType
40  * @author David Hovemeyer
41  */

42 public class BlockTypeAnalysis extends BasicAbstractDataflowAnalysis<BlockType> {
43     private DepthFirstSearch dfs;
44
45     /**
46      * Constructor.
47      *
48      * @param dfs a DepthFirstSearch for the method to be analyzed
49      */

50     public BlockTypeAnalysis(DepthFirstSearch dfs) {
51         this.dfs = dfs;
52     }
53
54     public BlockType createFact() {
55         return new BlockType();
56     }
57
58     public void copy(BlockType source, BlockType dest) {
59         dest.copyFrom(source);
60     }
61
62     public void initEntryFact(BlockType result) throws DataflowAnalysisException {
63         result.setNormal();
64     }
65
66     public void initResultFact(BlockType result) {
67         makeFactTop(result);
68     }
69
70     public void makeFactTop(BlockType fact) {
71         fact.setTop();
72     }
73     public boolean isTop(BlockType fact) {
74         return fact.isTop();
75     }
76     public boolean isForwards() {
77         return true;
78     }
79
80     public BlockOrder getBlockOrder(CFG cfg) {
81         return new ReversePostOrder(cfg, dfs);
82     }
83
84     public boolean same(BlockType fact1, BlockType fact2) {
85         return fact1.sameAs(fact2);
86     }
87
88     public void transfer(BasicBlock basicBlock, @CheckForNull InstructionHandle end, BlockType start, BlockType result)
89             throws DataflowAnalysisException {
90         result.copyFrom(start);
91
92         if (start.isValid()) {
93             if (basicBlock.isExceptionHandler()) {
94                 CodeExceptionGen exceptionGen = basicBlock.getExceptionGen();
95                 ObjectType catchType = exceptionGen.getCatchType();
96                 if (catchType == null) {
97                     // Probably a finally block, or a synchronized block
98
// exception-compensation catch block.
99
result.pushFinally();
100                 } else {
101                     // Catch type was explicitly specified:
102
// this is probably a programmer-written catch block
103
result.pushCatch();
104                 }
105             }
106         }
107     }
108
109     public void meetInto(BlockType fact, Edge edge, BlockType result) throws DataflowAnalysisException {
110         result.mergeWith(fact);
111     }
112
113     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
114         if (argv.length != 1) {
115             System.err.println("Usage: " + BlockTypeAnalysis.class.getName() + " <classfile>");
116             System.exit(1);
117         }
118         
119         DataflowTestDriver<BlockType, BlockTypeAnalysis> driver = new DataflowTestDriver<BlockType, BlockTypeAnalysis>() {
120             /* (non-Javadoc)
121              * @see edu.umd.cs.findbugs.ba.DataflowTestDriver#createDataflow(edu.umd.cs.findbugs.ba.ClassContext, org.apache.bcel.classfile.Method)
122              */

123             @Override JavaDoc
124             public Dataflow<BlockType, BlockTypeAnalysis> createDataflow(ClassContext classContext, Method method) throws CFGBuilderException, DataflowAnalysisException {
125                 return classContext.getBlockTypeDataflow(method);
126             }
127         };
128         
129         driver.execute(argv[0]);
130     }
131 }
132
133 // vim:ts=4
134
Popular Tags