KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > npe > IsNullConditionDecision


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.npe;
21
22 import edu.umd.cs.findbugs.annotations.CheckForNull;
23 import edu.umd.cs.findbugs.ba.EdgeTypes;
24 import edu.umd.cs.findbugs.ba.vna.ValueNumber;
25 import edu.umd.cs.findbugs.util.Strings;
26 import edu.umd.cs.findbugs.util
27
.Util;
28 /**
29  * A control decision which resulted in information being gained
30  * about whether a particular value is null or non-null
31  * on the IFCMP_EDGE and FALL_THROUGH_EDGE branches.
32  *
33  * @see IsNullValue
34  * @see IsNullValueFrame
35  * @see IsNullValueAnalysis
36  */

37 public class IsNullConditionDecision implements EdgeTypes {
38     private final @CheckForNull ValueNumber value;
39     private final @CheckForNull IsNullValue ifcmpDecision;
40     private final @CheckForNull IsNullValue fallThroughDecision;
41
42     /**
43      * Constructor.
44      *
45      * @param value the ValueNumber for which we have new information; null if no
46      * new information
47      * @param ifcmpDecision the decision for the IFCMP_EDGE; null if that edge is not feasible
48      * @param fallThroughDecision the decision for the FALL_THROUGH_EDGE; null if that edge is not feasible
49      */

50     public IsNullConditionDecision(@CheckForNull ValueNumber value, @CheckForNull IsNullValue ifcmpDecision, @CheckForNull IsNullValue fallThroughDecision) {
51         this.value = value;
52
53         // At least one of the edges must be feasible
54
assert !(ifcmpDecision == null && fallThroughDecision == null);
55         this.ifcmpDecision = ifcmpDecision;
56         this.fallThroughDecision = fallThroughDecision;
57     }
58
59     public int hashCode() {
60         return Util.nullSafeHashcode(value) + 5
61                 * Util.nullSafeHashcode(ifcmpDecision) + 17
62                 * Util.nullSafeHashcode(fallThroughDecision);
63     }
64
65     public boolean equals(Object JavaDoc o) {
66         if (!(o instanceof IsNullConditionDecision))
67             return false;
68         IsNullConditionDecision other = (IsNullConditionDecision) o;
69         return Util.nullSafeEquals(value, other.value)
70                 && Util.nullSafeEquals(ifcmpDecision, other.ifcmpDecision)
71                 && Util.nullSafeEquals(fallThroughDecision,
72                         other.fallThroughDecision);
73     }
74     /**
75      * Get the value about which the branch yields information.
76      */

77     public ValueNumber getValue() {
78         return value;
79     }
80
81     /**
82      * Determine whether or not the comparison is redundant.
83      */

84     public boolean isRedundant() {
85         return ifcmpDecision == null || fallThroughDecision == null;
86     }
87
88     /**
89      * Determine whether or not the given edge is feasible.
90      * An edge may be infeasible if the comparison is redundant (i.e.,
91      * can only be determined one way)
92      *
93      * @param edgeType the type of edge; must be IFCMP_EDGE or FALL_THROUGH_EDGE
94      * @return true if the edge is feasible, false if infeasible
95      */

96     public boolean isEdgeFeasible(int edgeType) {
97         IsNullValue decision = getDecision(edgeType);
98         return decision != null;
99     }
100
101     /**
102      * Get the decision reached about the value on outgoing edge of given type.
103      *
104      * @param edgeType the type of edge; must be IFCMP_EDGE or FALL_THROUGH_EDGE
105      * @return the IsNullValue representing the decision, or null
106      * if the edge is infeasible
107      */

108     public @CheckForNull IsNullValue getDecision(int edgeType) {
109         assert edgeType == IFCMP_EDGE || edgeType == FALL_THROUGH_EDGE;
110         if (edgeType == IFCMP_EDGE)
111             return ifcmpDecision;
112         else
113             return fallThroughDecision;
114     }
115
116     @Override JavaDoc
117     public String JavaDoc toString() {
118         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
119         buf.append(value != null ? value.toString() : "NoValue,");
120         buf.append("ifcmp=");
121         buf.append(ifcmpDecision != null ? Strings.trimComma(ifcmpDecision.toString()) : "INFEASIBLE");
122         buf.append(",fallthru=");
123         buf.append(fallThroughDecision != null ? Strings.trimComma(fallThroughDecision.toString()) : "INFEASIBLE");
124         return buf.toString();
125     }
126 }
127
128 // vim:ts=4
129
Popular Tags