KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > classfile > Field


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.bcel.classfile;
18
19 import java.io.DataInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import org.apache.bcel.Constants;
22 import org.apache.bcel.generic.Type;
23 import org.apache.bcel.util.BCELComparator;
24
25 /**
26  * This class represents the field info structure, i.e., the representation
27  * for a variable in the class. See JVM specification for details.
28  *
29  * @version $Id: Field.java 386056 2006-03-15 11:31:56Z tcurdt $
30  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
31  */

32 public final class Field extends FieldOrMethod {
33
34     private static BCELComparator _cmp = new BCELComparator() {
35
36         public boolean equals( Object JavaDoc o1, Object JavaDoc o2 ) {
37             Field THIS = (Field) o1;
38             Field THAT = (Field) o2;
39             return THIS.getName().equals(THAT.getName())
40                     && THIS.getSignature().equals(THAT.getSignature());
41         }
42
43
44         public int hashCode( Object JavaDoc o ) {
45             Field THIS = (Field) o;
46             return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
47         }
48     };
49
50
51     /**
52      * Initialize from another object. Note that both objects use the same
53      * references (shallow copy). Use clone() for a physical copy.
54      */

55     public Field(Field c) {
56         super(c);
57     }
58
59
60     /**
61      * Construct object from file stream.
62      * @param file Input stream
63      */

64     Field(DataInputStream JavaDoc file, ConstantPool constant_pool) throws IOException JavaDoc,
65             ClassFormatException {
66         super(file, constant_pool);
67     }
68
69
70     /**
71      * @param access_flags Access rights of field
72      * @param name_index Points to field name in constant pool
73      * @param signature_index Points to encoded signature
74      * @param attributes Collection of attributes
75      * @param constant_pool Array of constants
76      */

77     public Field(int access_flags, int name_index, int signature_index, Attribute[] attributes,
78             ConstantPool constant_pool) {
79         super(access_flags, name_index, signature_index, attributes, constant_pool);
80     }
81
82
83     /**
84      * Called by objects that are traversing the nodes of the tree implicitely
85      * defined by the contents of a Java class. I.e., the hierarchy of methods,
86      * fields, attributes, etc. spawns a tree of objects.
87      *
88      * @param v Visitor object
89      */

90     public void accept( Visitor v ) {
91         v.visitField(this);
92     }
93
94
95     /**
96      * @return constant value associated with this field (may be null)
97      */

98     public final ConstantValue getConstantValue() {
99         for (int i = 0; i < attributes_count; i++) {
100             if (attributes[i].getTag() == Constants.ATTR_CONSTANT_VALUE) {
101                 return (ConstantValue) attributes[i];
102             }
103         }
104         return null;
105     }
106
107
108     /**
109      * Return string representation close to declaration format,
110      * `public static final short MAX = 100', e.g..
111      *
112      * @return String representation of field, including the signature.
113      */

114     public final String JavaDoc toString() {
115         String JavaDoc name, signature, access; // Short cuts to constant pool
116
// Get names from constant pool
117
access = Utility.accessToString(access_flags);
118         access = access.equals("") ? "" : (access + " ");
119         signature = Utility.signatureToString(getSignature());
120         name = getName();
121         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(64);
122         buf.append(access).append(signature).append(" ").append(name);
123         ConstantValue cv = getConstantValue();
124         if (cv != null) {
125             buf.append(" = ").append(cv);
126         }
127         for (int i = 0; i < attributes_count; i++) {
128             Attribute a = attributes[i];
129             if (!(a instanceof ConstantValue)) {
130                 buf.append(" [").append(a.toString()).append("]");
131             }
132         }
133         return buf.toString();
134     }
135
136
137     /**
138      * @return deep copy of this field
139      */

140     public final Field copy( ConstantPool _constant_pool ) {
141         return (Field) copy_(_constant_pool);
142     }
143
144
145     /**
146      * @return type of field
147      */

148     public Type getType() {
149         return Type.getReturnType(getSignature());
150     }
151
152
153     /**
154      * @return Comparison strategy object
155      */

156     public static BCELComparator getComparator() {
157         return _cmp;
158     }
159
160
161     /**
162      * @param comparator Comparison strategy object
163      */

164     public static void setComparator( BCELComparator comparator ) {
165         _cmp = comparator;
166     }
167
168
169     /**
170      * Return value as defined by given BCELComparator strategy.
171      * By default two Field objects are said to be equal when
172      * their names and signatures are equal.
173      *
174      * @see java.lang.Object#equals(java.lang.Object)
175      */

176     public boolean equals( Object JavaDoc obj ) {
177         return _cmp.equals(this, obj);
178     }
179
180
181     /**
182      * Return value as defined by given BCELComparator strategy.
183      * By default return the hashcode of the field's name XOR signature.
184      *
185      * @see java.lang.Object#hashCode()
186      */

187     public int hashCode() {
188         return _cmp.hashCode(this);
189     }
190 }
191
Popular Tags