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 public static BCELComparator<Field> getComparator() {
57 return bcelComparator;
58 }
59
60
61
62
63 public static void setComparator(final BCELComparator<Field> comparator) {
64 bcelComparator = comparator;
65 }
66
67
68
69
70
71
72 Field(final DataInput file, final ConstantPool constantPool) throws IOException, ClassFormatException {
73 super(file, constantPool);
74 }
75
76
77
78
79
80
81
82 public Field(final Field c) {
83 super(c);
84 }
85
86
87
88
89
90
91
92
93 public Field(final int accessFlags, final int nameIndex, final int signatureIndex, final Attribute[] attributes, final ConstantPool constantPool) {
94 super(accessFlags, nameIndex, signatureIndex, attributes, constantPool);
95 }
96
97
98
99
100
101
102
103 @Override
104 public void accept(final Visitor v) {
105 v.visitField(this);
106 }
107
108
109
110
111 public Field copy(final ConstantPool constantPool) {
112 return (Field) copy_(constantPool);
113 }
114
115
116
117
118
119
120
121 @Override
122 public boolean equals(final Object obj) {
123 return obj instanceof Field && bcelComparator.equals(this, (Field) obj);
124 }
125
126
127
128
129 public ConstantValue getConstantValue() {
130 for (final Attribute attribute : super.getAttributes()) {
131 if (attribute.getTag() == Const.ATTR_CONSTANT_VALUE) {
132 return (ConstantValue) attribute;
133 }
134 }
135 return null;
136 }
137
138
139
140
141
142
143 public Type getType() {
144 return Type.getType(getSignature());
145 }
146
147
148
149
150
151
152
153 @Override
154 public int hashCode() {
155 return bcelComparator.hashCode(this);
156 }
157
158
159
160
161
162
163 @Override
164 public String toString() {
165 String name;
166 String signature;
167 String access;
168
169
170 access = Utility.accessToString(super.getAccessFlags());
171 access = access.isEmpty() ? "" : access + " ";
172 signature = Utility.signatureToString(getSignature());
173 name = getName();
174 final StringBuilder buf = new StringBuilder(64);
175 buf.append(access).append(signature).append(" ").append(name);
176 final ConstantValue cv = getConstantValue();
177 if (cv != null) {
178 buf.append(" = ").append(cv);
179 }
180 for (final Attribute attribute : super.getAttributes()) {
181 if (!(attribute instanceof ConstantValue)) {
182 buf.append(" [").append(attribute).append("]");
183 }
184 }
185 return buf.toString();
186 }
187 }