1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.classfile;
18
19 import java.io.DataInput;
20 import java.io.IOException;
21 import java.util.Objects;
22
23 import org.apache.bcel.Const;
24 import org.apache.bcel.generic.Type;
25 import org.apache.bcel.util.BCELComparator;
26
27
28
29
30
31 public final class Field extends FieldOrMethod {
32
33
34
35
36
37
38 public static final Field[] EMPTY_ARRAY = {};
39
40 private static BCELComparator<Field> bcelComparator = new BCELComparator<Field>() {
41
42 @Override
43 public boolean equals(final Field a, final Field b) {
44 return a == b || a != null && b != null && Objects.equals(a.getName(), b.getName()) && Objects.equals(a.getSignature(), b.getSignature());
45 }
46
47 @Override
48 public int hashCode(final Field o) {
49 return o != null ? Objects.hash(o.getSignature(), o.getName()) : 0;
50 }
51 };
52
53
54
55
56 static final Field[] EMPTY_FIELD_ARRAY = {};
57
58
59
60
61 public static BCELComparator<Field> getComparator() {
62 return bcelComparator;
63 }
64
65
66
67
68 public static void setComparator(final BCELComparator<Field> comparator) {
69 bcelComparator = comparator;
70 }
71
72
73
74
75
76
77 Field(final DataInput file, final ConstantPool constantPool) throws IOException, ClassFormatException {
78 super(file, constantPool);
79 }
80
81
82
83
84
85
86
87 public Field(final Field c) {
88 super(c);
89 }
90
91
92
93
94
95
96
97
98 public Field(final int accessFlags, final int nameIndex, final int signatureIndex, final Attribute[] attributes, final ConstantPool constantPool) {
99 super(accessFlags, nameIndex, signatureIndex, attributes, constantPool);
100 }
101
102
103
104
105
106
107
108 @Override
109 public void accept(final Visitor v) {
110 v.visitField(this);
111 }
112
113
114
115
116 public Field copy(final ConstantPool constantPool) {
117 return (Field) copy_(constantPool);
118 }
119
120
121
122
123
124
125
126 @Override
127 public boolean equals(final Object obj) {
128 return obj instanceof Field && bcelComparator.equals(this, (Field) obj);
129 }
130
131
132
133
134 public ConstantValue getConstantValue() {
135 for (final Attribute attribute : super.getAttributes()) {
136 if (attribute.getTag() == Const.ATTR_CONSTANT_VALUE) {
137 return (ConstantValue) attribute;
138 }
139 }
140 return null;
141 }
142
143
144
145
146
147
148 public Type getType() {
149 return Type.getType(getSignature());
150 }
151
152
153
154
155
156
157
158 @Override
159 public int hashCode() {
160 return bcelComparator.hashCode(this);
161 }
162
163
164
165
166
167
168 @Override
169 public String toString() {
170 String name;
171 String signature;
172 String access;
173
174
175 access = Utility.accessToString(super.getAccessFlags());
176 access = access.isEmpty() ? "" : access + " ";
177 signature = Utility.signatureToString(getSignature());
178 name = getName();
179 final StringBuilder buf = new StringBuilder(64);
180 buf.append(access).append(signature).append(" ").append(name);
181 final ConstantValue cv = getConstantValue();
182 if (cv != null) {
183 buf.append(" = ").append(cv);
184 }
185 for (final Attribute attribute : super.getAttributes()) {
186 if (!(attribute instanceof ConstantValue)) {
187 buf.append(" [").append(attribute).append("]");
188 }
189 }
190 return buf.toString();
191 }
192 }