KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > vna > AvailableLoad


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.vna;
21
22 import edu.umd.cs.findbugs.ba.InstanceField;
23 import edu.umd.cs.findbugs.ba.StaticField;
24 import edu.umd.cs.findbugs.ba.XField;
25
26 /**
27  * An AvailableLoad indicates a field and (optionally) object reference
28  * for which a value is available. It is used to implement
29  * redundant load elimination and forward substitution in ValueNumberAnalysis.
30  * The idea is that programmers often reload fields unnecessarily when they
31  * "know" that the value will not change. In order to deduce the intended
32  * meaning of such code, our analyses need to figure out that such
33  * loads return the same value.
34  * <p/>
35  * <p> AvailableLoad objects may be used as keys in both hash and tree
36  * sets and maps.
37  *
38  * @author David Hovemeyer
39  * @see ValueNumberAnalysis
40  */

41 public class AvailableLoad implements Comparable JavaDoc<AvailableLoad> {
42     private final ValueNumber reference;
43     private final XField field;
44
45     /**
46      * Constructor from static field.
47      *
48      * @param staticField the StaticField
49      */

50     public AvailableLoad(StaticField staticField) {
51         this.reference = null;
52         this.field = staticField;
53     }
54
55     /**
56      * Constructor from object reference and instance field.
57      *
58      * @param reference the ValueNumber of the object reference
59      * @param field the InstanceField
60      */

61     public AvailableLoad(ValueNumber reference, InstanceField field) {
62         if (reference == null) throw new IllegalArgumentException JavaDoc();
63         this.reference = reference;
64         this.field = field;
65     }
66
67     /**
68      * Get the ValueNumber of the object reference.
69      *
70      * @return the ValueNumber, or null if this is a an available
71      * static field load
72      */

73     public ValueNumber getReference() {
74         return reference;
75     }
76
77     public boolean matchesReference(ValueNumber v) {
78         if (v == reference) return true;
79         if (reference== null) return false;
80         return reference.equals(v);
81     }
82     /**
83      * Get the field for which a load is available.
84      *
85      * @return the XField
86      */

87     public XField getField() {
88         return field;
89     }
90
91     public int compareTo(AvailableLoad other) {
92         int cmp = field.compareTo(other.field);
93         if (cmp != 0)
94             return cmp;
95         else if (reference == other.reference)
96             return 0;
97         else if (reference == null)
98             return -1;
99         else if (other.reference == null)
100             return 1;
101         else
102             return reference.compareTo(other.reference);
103     }
104
105     @Override JavaDoc
106          public int hashCode() {
107         return (reference == null ? 0 : reference.hashCode()) ^ field.hashCode();
108     }
109
110     @Override JavaDoc
111          public boolean equals(Object JavaDoc o) {
112         if (o == null || this.getClass() != o.getClass())
113             return false;
114         AvailableLoad other = (AvailableLoad) o;
115         return (reference == other.reference ||
116                 (reference != null && other.reference != null && reference.equals(other.reference)))
117                 && field.equals(other.field);
118     }
119
120     @Override JavaDoc
121          public String JavaDoc toString() {
122         return (reference == null ? "" : reference.getNumber() + ".") + field;
123     }
124 }
125
126 // vim:ts=4
127
Popular Tags