KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > annotation > nullcheck > NullPointerColorer


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jennifer Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.jimple.toolkits.annotation.nullcheck;
21
22 import soot.*;
23 import soot.tagkit.*;
24 import soot.toolkits.graph.*;
25 import java.util.*;
26 import soot.toolkits.scalar.*;
27 import soot.jimple.*;
28
29 public class NullPointerColorer extends BodyTransformer {
30
31     public NullPointerColorer( Singletons.Global g ) {}
32     public static NullPointerColorer v() { return G.v().soot_jimple_toolkits_annotation_nullcheck_NullPointerColorer(); }
33
34     protected void internalTransform (Body b, String JavaDoc phaseName, Map options) {
35         
36         BranchedRefVarsAnalysis analysis = new BranchedRefVarsAnalysis (
37                 new ExceptionalUnitGraph(b));
38
39         Iterator it = b.getUnits().iterator();
40
41         while (it.hasNext()) {
42             Stmt s = (Stmt)it.next();
43             
44             Iterator usesIt = s.getUseBoxes().iterator();
45             FlowSet beforeSet = (FlowSet)analysis.getFlowBefore(s);
46                 
47             while (usesIt.hasNext()) {
48                 ValueBox vBox = (ValueBox)usesIt.next();
49                 addColorTags(vBox, beforeSet, s, analysis);
50             }
51
52             Iterator defsIt = s.getDefBoxes().iterator();
53             FlowSet afterSet = (FlowSet)analysis.getFallFlowAfter(s);
54
55             while (defsIt.hasNext()){
56                 ValueBox vBox = (ValueBox)defsIt.next();
57                 addColorTags(vBox, afterSet, s, analysis);
58             }
59         }
60
61         Iterator keysIt = b.getMethod().getDeclaringClass().getTags().iterator();
62         boolean keysAdded = false;
63         while (keysIt.hasNext()){
64             Object JavaDoc next = keysIt.next();
65             if (next instanceof KeyTag){
66                 if (((KeyTag)next).analysisType().equals("NullCheckTag")){
67                     keysAdded = true;
68                 }
69             }
70         }
71         if (!keysAdded){
72             b.getMethod().getDeclaringClass().addTag(new KeyTag(ColorTag.RED, "Nullness: Null", "NullCheckTag"));
73             b.getMethod().getDeclaringClass().addTag(new KeyTag(ColorTag.GREEN, "Nullness: Not Null", "NullCheckTag"));
74             b.getMethod().getDeclaringClass().addTag(new KeyTag(ColorTag.BLUE, "Nullness: Nullness Unknown", "NullCheckTag"));
75         }
76     }
77     
78     private void addColorTags(ValueBox vBox, FlowSet set, Stmt s, BranchedRefVarsAnalysis analysis){
79         
80         Value val = vBox.getValue();
81         if (val.getType() instanceof RefLikeType) {
82             //G.v().out.println(val+": "+val.getClass().toString());
83

84             int vInfo = analysis.anyRefInfo(val, set);
85
86             switch (vInfo) {
87                 case 1 : {
88                     // analysis.kNull
89
s.addTag(new StringTag(val+": Null", "NullCheckTag"));
90                     vBox.addTag(new ColorTag(ColorTag.RED, "NullCheckTag"));
91                     break;
92                          }
93                 case 2 : {
94                     // analysis.kNonNull
95
s.addTag(new StringTag(val+": NonNull", "NullCheckTag"));
96                     vBox.addTag(new ColorTag(ColorTag.GREEN, "NullCheckTag"));
97                     break;
98                          }
99                 case 99 : {
100                     // analysis.KTop:
101
s.addTag(new StringTag(val+": Nullness Unknown", "NullCheckTag"));
102                     vBox.addTag(new ColorTag(ColorTag.BLUE, "NullCheckTag"));
103                     break;
104                           }
105                 case 0 : {
106                     // analysis.kBottom
107
s.addTag(new StringTag(val+": Nullness Unknown", "NullCheckTag"));
108                     vBox.addTag(new ColorTag(ColorTag.BLUE, "NullCheckTag"));
109                     break;
110                          }
111             }
112         }
113         else {
114             
115         }
116     }
117 }
118
Popular Tags