KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > npe2 > IfNullCondition


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, 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.npe2;
21
22 import org.apache.bcel.Constants;
23
24 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
25 import edu.umd.cs.findbugs.ba.Edge;
26 import edu.umd.cs.findbugs.ba.EdgeTypes;
27 import edu.umd.cs.findbugs.ba.Location;
28 import edu.umd.cs.findbugs.ba.vna.ValueNumber;
29 import edu.umd.cs.findbugs.ba.vna.ValueNumberFrame;
30
31 /**
32  * @author David Hovemeyer
33  */

34 public class IfNullCondition extends Condition {
35     private ValueNumber valueNumber;
36     private Decision ifcmpDecision;
37     private Decision fallThroughDecision;
38     
39     public IfNullCondition(Location location) {
40         super(location);
41     }
42
43     /* (non-Javadoc)
44      * @see edu.umd.cs.findbugs.ba.npe2.Condition#getDecision(edu.umd.cs.findbugs.ba.Edge)
45      */

46     @Override JavaDoc
47     public Decision getDecision(Edge edge) {
48         return edge.getType() == EdgeTypes.IFCMP_EDGE ? ifcmpDecision : fallThroughDecision;
49     }
50
51     /* (non-Javadoc)
52      * @see edu.umd.cs.findbugs.ba.npe2.Condition#getValueNumber()
53      */

54     @Override JavaDoc
55     public ValueNumber getValueNumber() {
56         return valueNumber;
57     }
58
59     /* (non-Javadoc)
60      * @see edu.umd.cs.findbugs.ba.npe2.Condition#refresh(edu.umd.cs.findbugs.ba.vna.ValueNumberFrame, edu.umd.cs.findbugs.ba.npe2.DefinitelyNullSet)
61      */

62     @Override JavaDoc
63     public void refresh(ValueNumberFrame vnaFrame, DefinitelyNullSet definitelyNullSet) throws DataflowAnalysisException {
64         valueNumber = vnaFrame.getTopValue();
65         
66         NullnessValue nullnessValue = definitelyNullSet.getNulllessValue(valueNumber);
67         short opcode = getLocation().getHandle().getInstruction().getOpcode();
68         
69         if (nullnessValue.isDefinitelyNull() || nullnessValue.isDefinitelyNotNull()) {
70             // Comparison is redundant.
71

72             boolean ifcmpFeasible = nullnessValue.isDefinitelyNull() == (opcode == Constants.IFNULL);
73             ifcmpDecision = new Decision(
74                     ifcmpFeasible,
75                     ifcmpFeasible ? nullnessValue.toCheckedValue() : null
76             );
77             
78             boolean fallThroughFeasible = nullnessValue.isDefinitelyNull() != (opcode == Constants.IFNONNULL);
79             fallThroughDecision = new Decision(
80                     fallThroughFeasible,
81                     fallThroughFeasible ? nullnessValue.toCheckedValue() : null
82             );
83             
84             return;
85         }
86         
87         NullnessValue definitelyNull = NullnessValue.definitelyNullValue().toCheckedValue();
88         NullnessValue definitelyNotNull = NullnessValue.definitelyNotNullValue().toCheckedValue();
89
90         // Nullness is unknown, assume both branches are feasible.
91
ifcmpDecision = new Decision(
92                 true,
93                 (opcode == Constants.IFNULL) ? definitelyNull : definitelyNotNull
94         );
95         fallThroughDecision = new Decision(
96                 true,
97                 (opcode == Constants.IFNULL) ? definitelyNotNull : definitelyNull
98         );
99     }
100 }
101
Popular Tags