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