01: /*
02: * FindBugs - Find Bugs in Java programs
03: * Copyright (C) 2006, University of Maryland
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
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 java.util.Collection;
23: import java.util.LinkedList;
24:
25: import edu.umd.cs.findbugs.annotations.CheckForNull;
26: import edu.umd.cs.findbugs.ba.Location;
27: import edu.umd.cs.findbugs.ba.vna.ValueNumber;
28: import edu.umd.cs.findbugs.util.MultiMap;
29:
30: /**
31: * @author pugh
32: */
33: public class UsagesRequiringNonNullValues {
34:
35: public static class Pair {
36: public final ValueNumber vn;
37: public final PointerUsageRequiringNonNullValue pu;
38:
39: Pair(ValueNumber vn, PointerUsageRequiringNonNullValue pu) {
40: this .vn = vn;
41: this .pu = pu;
42: }
43:
44: @Override
45: public String toString() {
46: return vn.toString();
47: }
48: }
49:
50: MultiMap<Location, Pair> map = new MultiMap<Location, Pair>(
51: LinkedList.class);
52:
53: @Override
54: public String toString() {
55: StringBuffer buf = new StringBuffer();
56: for (Location loc : map.keySet()) {
57: buf.append(loc.getHandle().getPosition() + ":"
58: + loc.getHandle().getInstruction() + "\n");
59: for (Pair p : getPairs(loc)) {
60: buf.append(" ").append(p.vn).append("\n");
61: }
62: }
63: return buf.toString();
64: }
65:
66: public void add(Location loc, ValueNumber vn,
67: PointerUsageRequiringNonNullValue usage) {
68: Pair p = new Pair(vn, usage);
69: if (DerefFinder.DEBUG)
70: System.out
71: .println("At " + loc + " adding dereference " + p);
72:
73: map.add(loc, p);
74: }
75:
76: public @CheckForNull
77: PointerUsageRequiringNonNullValue get(Location loc, ValueNumber vn) {
78: PointerUsageRequiringNonNullValue secondBest = null;
79: for (Pair p : map.get(loc)) {
80: if (p.vn.equals(vn))
81: return p.pu;
82: else
83: secondBest = p.pu;
84: }
85: return secondBest;
86: }
87:
88: public Collection<? extends Pair> getPairs(Location loc) {
89: return map.get(loc);
90: }
91:
92: }
|