1 17 package org.apache.bcel.classfile; 18 19 import java.io.DataInputStream ; 20 import java.io.IOException ; 21 import org.apache.bcel.Constants; 22 import org.apache.bcel.generic.Type; 23 import org.apache.bcel.util.BCELComparator; 24 25 32 public final class Field extends FieldOrMethod { 33 34 private static BCELComparator _cmp = new BCELComparator() { 35 36 public boolean equals( Object o1, Object 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 o ) { 45 Field THIS = (Field) o; 46 return THIS.getSignature().hashCode() ^ THIS.getName().hashCode(); 47 } 48 }; 49 50 51 55 public Field(Field c) { 56 super(c); 57 } 58 59 60 64 Field(DataInputStream file, ConstantPool constant_pool) throws IOException , 65 ClassFormatException { 66 super(file, constant_pool); 67 } 68 69 70 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 90 public void accept( Visitor v ) { 91 v.visitField(this); 92 } 93 94 95 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 114 public final String toString() { 115 String name, signature, access; access = Utility.accessToString(access_flags); 118 access = access.equals("") ? "" : (access + " "); 119 signature = Utility.signatureToString(getSignature()); 120 name = getName(); 121 StringBuffer buf = new StringBuffer (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 140 public final Field copy( ConstantPool _constant_pool ) { 141 return (Field) copy_(_constant_pool); 142 } 143 144 145 148 public Type getType() { 149 return Type.getReturnType(getSignature()); 150 } 151 152 153 156 public static BCELComparator getComparator() { 157 return _cmp; 158 } 159 160 161 164 public static void setComparator( BCELComparator comparator ) { 165 _cmp = comparator; 166 } 167 168 169 176 public boolean equals( Object obj ) { 177 return _cmp.equals(this, obj); 178 } 179 180 181 187 public int hashCode() { 188 return _cmp.hashCode(this); 189 } 190 } 191 | Popular Tags |