001: /*
002: * Bytecode Analysis Framework
003: * Copyright (C) 2005, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package edu.umd.cs.findbugs.ba.npe;
020:
021: import java.util.BitSet;
022:
023: /**
024: * Method property recording which parameters are (or should be)
025: * non-null, meaning that null values should not be passed
026: * as their arguments.
027: *
028: * @author David Hovemeyer
029: */
030: public class ParameterNullnessProperty {
031: /**
032: * Maximum number of parameters that can be represented by a ParameterNullnessProperty.
033: */
034: public static final int MAX_PARAMS = 32;
035:
036: private int nonNullParamSet;
037:
038: /**
039: * Constructor.
040: * Parameters are all assumed not to be non-null.
041: */
042: public ParameterNullnessProperty() {
043: this .nonNullParamSet = 0;
044: }
045:
046: /**
047: * Get the non-null param bitset.
048: *
049: * @return the non-null param bitset
050: */
051: int getNonNullParamSet() {
052: return nonNullParamSet;
053: }
054:
055: /**
056: * Set the non-null param bitset.
057: *
058: * @param nonNullParamSet the non-null param bitset
059: */
060: void setNonNullParamSet(int nonNullParamSet) {
061: this .nonNullParamSet = nonNullParamSet;
062: }
063:
064: /**
065: * Set the non-null param set from given BitSet.
066: *
067: * @param nonNullSet BitSet indicating which parameters are
068: * non-null
069: */
070: public void setNonNullParamSet(BitSet nonNullSet) {
071: for (int i = 0; i < 32; ++i) {
072: setNonNull(i, nonNullSet.get(i));
073: }
074: }
075:
076: /**
077: * Set whether or not a parameter might be non-null.
078: *
079: * @param param the parameter index
080: * @param nonNull true if the parameter might be non-null, false otherwise
081: */
082: public void setNonNull(int param, boolean nonNull) {
083: if (param < 0 || param > 31)
084: return;
085: if (nonNull) {
086: nonNullParamSet |= (1 << param);
087: } else {
088: nonNullParamSet &= ~(1 << param);
089: }
090: }
091:
092: /**
093: * Return whether or not a parameter might be non-null.
094: *
095: * @param param the parameter index
096: * @return true if the parameter might be non-null, false otherwise
097: */
098: public boolean isNonNull(int param) {
099: 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 getViolatedParamSet(BitSet nullArgSet) {
114: BitSet result = new BitSet();
115: for (int i = 0; i < 32; ++i) {
116: result.set(i, nullArgSet.get(i) && isNonNull(i));
117: }
118: return result;
119: }
120:
121: public BitSet getAsBitSet() {
122: BitSet result = new BitSet();
123: if (isEmpty())
124: return result;
125: for (int i = 0; i < 32; ++i) {
126: result.set(i, isNonNull(i));
127: }
128: return result;
129: }
130:
131: /**
132: * Return whether or not the set of non-null parameters
133: * is empty.
134: *
135: * @return true if the set is empty, false if it contains at least one parameter
136: */
137: public boolean isEmpty() {
138: return nonNullParamSet == 0;
139: }
140:
141: @Override
142: public String toString() {
143: StringBuffer buf = new StringBuffer();
144:
145: buf.append('{');
146: for (int i = 0; i < 32; ++i) {
147: if (isNonNull(i)) {
148: if (buf.length() > 1)
149: buf.append(',');
150: buf.append(i);
151: }
152: }
153: buf.append('}');
154:
155: return buf.toString();
156: }
157:
158: /**
159: * Intersect this set with the given set.
160: * Useful for summarizing the properties of multiple methods.
161: *
162: * @param targetDerefParamSet another set
163: */
164: public void intersectWith(
165: ParameterNullnessProperty targetDerefParamSet) {
166: nonNullParamSet &= targetDerefParamSet.nonNullParamSet;
167: }
168:
169: /**
170: * Make this object the same as the given one.
171: *
172: * @param other another ParameterNullnessProperty
173: */
174: public void copyFrom(ParameterNullnessProperty other) {
175: this.nonNullParamSet = other.nonNullParamSet;
176: }
177: }
|