KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2005, 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 package edu.umd.cs.findbugs.ba.npe;
20
21 import java.util.BitSet JavaDoc;
22
23 /**
24  * Method property recording which parameters are (or should be)
25  * non-null, meaning that null values should not be passed
26  * as their arguments.
27  *
28  * @author David Hovemeyer
29  */

30 public class ParameterNullnessProperty {
31     /**
32      * Maximum number of parameters that can be represented by a ParameterNullnessProperty.
33      */

34     public static final int MAX_PARAMS = 32;
35     
36     private int nonNullParamSet;
37     
38     /**
39      * Constructor.
40      * Parameters are all assumed not to be non-null.
41      */

42     public ParameterNullnessProperty() {
43         this.nonNullParamSet = 0;
44     }
45     
46     /**
47      * Get the non-null param bitset.
48      *
49      * @return the non-null param bitset
50      */

51     int getNonNullParamSet() {
52         return nonNullParamSet;
53     }
54     
55     /**
56      * Set the non-null param bitset.
57      *
58      * @param nonNullParamSet the non-null param bitset
59      */

60     void setNonNullParamSet(int nonNullParamSet) {
61         this.nonNullParamSet = nonNullParamSet;
62     }
63
64     /**
65      * Set the non-null param set from given BitSet.
66      *
67      * @param nonNullSet BitSet indicating which parameters are
68      * non-null
69      */

70     public void setNonNullParamSet(BitSet JavaDoc nonNullSet) {
71         for (int i = 0; i < 32; ++i) {
72             setNonNull(i, nonNullSet.get(i));
73         }
74     }
75     
76     /**
77      * Set whether or not a parameter might be non-null.
78      *
79      * @param param the parameter index
80      * @param nonNull true if the parameter might be non-null, false otherwise
81      */

82     public void setNonNull(int param, boolean nonNull) {
83         if (param < 0 || param > 31)
84             return;
85         if (nonNull) {
86             nonNullParamSet |= (1 << param);
87         } else {
88             nonNullParamSet &= ~(1 << param);
89         }
90     }
91     
92     /**
93      * Return whether or not a parameter might be non-null.
94      *
95      * @param param the parameter index
96      * @return true if the parameter might be non-null, false otherwise
97      */

98     public boolean isNonNull(int param) {
99         if (param < 0 || param > 31)
100             return false;
101         else
102             return (nonNullParamSet & (1 << param)) != 0;
103     }
104     
105     /**
106      * Given a bitset of null arguments passed to the method represented
107      * by this property, return a bitset indicating which null arguments
108      * correspond to an non-null param.
109      *
110      * @param nullArgSet bitset of null arguments
111      * @return bitset intersecting null arguments and non-null params
112      */

113     public BitSet JavaDoc getViolatedParamSet(BitSet JavaDoc nullArgSet) {
114         BitSet JavaDoc result = new BitSet JavaDoc();
115         for (int i = 0; i < 32; ++i) {
116             result.set(i, nullArgSet.get(i) && isNonNull(i));
117         }
118         return result;
119     }
120     
121     /**
122      * Return whether or not the set of non-null parameters
123      * is empty.
124      *
125      * @return true if the set is empty, false if it contains at least one parameter
126      */

127     public boolean isEmpty() {
128         return nonNullParamSet == 0;
129     }
130     
131     @Override JavaDoc
132     public String JavaDoc toString() {
133         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
134         
135         buf.append('{');
136         for (int i = 0; i < 32; ++i) {
137             if (isNonNull(i)) {
138                 if (buf.length() > 1)
139                     buf.append(',');
140                 buf.append(i);
141             }
142         }
143         buf.append('}');
144         
145         return buf.toString();
146     }
147
148     /**
149      * Intersect this set with the given set.
150      * Useful for summarizing the properties of multiple methods.
151      *
152      * @param targetDerefParamSet another set
153      */

154     public void intersectWith(ParameterNullnessProperty targetDerefParamSet) {
155         nonNullParamSet &= targetDerefParamSet.nonNullParamSet;
156     }
157
158     /**
159      * Make this object the same as the given one.
160      *
161      * @param other another ParameterNullnessProperty
162      */

163     public void copyFrom(ParameterNullnessProperty other) {
164         this.nonNullParamSet = other.nonNullParamSet;
165     }
166 }
167
Popular Tags