/
FieldAccess.java
374 lines (320 loc) · 12.7 KB
/
FieldAccess.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package net.bytebuddy.implementation.bytecode.member;
import net.bytebuddy.description.enumeration.EnumerationDescription;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.implementation.bytecode.assign.TypeCasting;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import static net.bytebuddy.matcher.ElementMatchers.named;
/**
* An access representation to a given field.
*/
public enum FieldAccess {
/**
* The representation of field access to a static field.
*/
STATIC(Opcodes.PUTSTATIC, Opcodes.GETSTATIC, StackSize.ZERO),
/**
* The representation of field access to an instance field.
*/
INSTANCE(Opcodes.PUTFIELD, Opcodes.GETFIELD, StackSize.SINGLE);
/**
* The opcode for setting a field value.
*/
private final int putterOpcode;
/**
* The opcode for getting a field value.
*/
private final int getterOpcode;
/**
* The amount of operand slots this field access operation consumes when it is applied before eventually
* adding new values onto the operand stack.
*/
private final int targetSizeChange;
/**
* Creates a new field access.
*
* @param putterOpcode The opcode for setting a field value.
* @param getterOpcode The opcode for getting a field value.
* @param targetSizeChange The amount of operand slots this field access operation consumes when it is applied
* before eventually adding new values onto the operand stack.
*/
FieldAccess(int putterOpcode, int getterOpcode, StackSize targetSizeChange) {
this.putterOpcode = putterOpcode;
this.getterOpcode = getterOpcode;
this.targetSizeChange = targetSizeChange.getSize();
}
/**
* Creates an accessor to read an enumeration value.
*
* @param enumerationDescription The description of the enumeration.
* @return A stack manipulation for reading the enumeration.
*/
public static StackManipulation forEnumeration(EnumerationDescription enumerationDescription) {
FieldList<FieldDescription.InDefinedShape> fieldList = enumerationDescription.getEnumerationType()
.getDeclaredFields()
.filter(named(enumerationDescription.getValue()));
return fieldList.size() != 1 || !fieldList.getOnly().isStatic() || !fieldList.getOnly().isPublic() || !fieldList.getOnly().isEnum()
? StackManipulation.Illegal.INSTANCE
: STATIC.new AccessDispatcher(fieldList.getOnly()).getter();
}
/**
* Creates a field access representation for a given field.
*
* @param fieldDescription The field to be accessed.
* @return A field access definition for the given field.
*/
public static Defined forField(FieldDescription.InDefinedShape fieldDescription) {
return fieldDescription.isStatic()
? STATIC.new AccessDispatcher(fieldDescription)
: INSTANCE.new AccessDispatcher(fieldDescription);
}
/**
* Creates a field access representation for a given field. If the field's return type derives from its declared shape, the value
* is additionally casted to the generically resolved field.
*
* @param fieldDescription The field to be accessed.
* @return A field access definition for the given field.
*/
public static Defined forField(FieldDescription fieldDescription) {
FieldDescription.InDefinedShape declaredField = fieldDescription.asDefined();
return fieldDescription.getType().asErasure().equals(declaredField.getType().asErasure())
? forField(declaredField)
: OfGenericField.of(fieldDescription, forField(declaredField));
}
@Override
public String toString() {
return "FieldAccess." + name();
}
/**
* Representation of a field access for which a getter and a putter can be created.
*/
public interface Defined {
/**
* Creates a getter representation for a given field.
*
* @return A stack manipulation representing the retrieval of a field value.
*/
StackManipulation getter();
/**
* Creates a putter representation for a given field.
*
* @return A stack manipulation representing the setting of a field value.
*/
StackManipulation putter();
}
/**
* A dispatcher for implementing a generic read or write access on a field.
*/
protected static class OfGenericField implements Defined {
/**
* The resolved generic field type.
*/
private final TypeDescription targetType;
/**
* An accessor for the field in its defined shape.
*/
private final Defined defined;
/**
* Creates a new dispatcher for a generic field.
*
* @param targetType The resolved generic field type.
* @param defined An accessor for the field in its defined shape.
*/
protected OfGenericField(TypeDescription targetType, Defined defined) {
this.targetType = targetType;
this.defined = defined;
}
/**
* Creates a generic access dispatcher for a given field.
*
* @param fieldDescription The field that is being accessed.
* @param fieldAccess A field accessor for the field in its defined shape.
* @return A field access dispatcher for the given field.
*/
protected static Defined of(FieldDescription fieldDescription, Defined fieldAccess) {
return new OfGenericField(fieldDescription.getType().asErasure(), fieldAccess);
}
@Override
public StackManipulation getter() {
return new StackManipulation.Compound(defined.getter(), TypeCasting.to(targetType));
}
@Override
public StackManipulation putter() {
return defined.putter();
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
OfGenericField that = (OfGenericField) other;
return targetType.equals(that.targetType) && defined.equals(that.defined);
}
@Override
public int hashCode() {
int result = targetType.hashCode();
result = 31 * result + defined.hashCode();
return result;
}
@Override
public String toString() {
return "FieldAccess.OfGenericField{" +
"targetType=" + targetType +
", defined=" + defined +
'}';
}
}
/**
* A dispatcher for implementing a non-generic read or write access on a field.
*/
protected class AccessDispatcher implements Defined {
/**
* A description of the accessed field.
*/
private final FieldDescription.InDefinedShape fieldDescription;
/**
* Creates a new access dispatcher.
*
* @param fieldDescription A description of the accessed field.
*/
protected AccessDispatcher(FieldDescription.InDefinedShape fieldDescription) {
this.fieldDescription = fieldDescription;
}
@Override
public StackManipulation getter() {
return new FieldGetInstruction();
}
@Override
public StackManipulation putter() {
return new FieldPutInstruction();
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& FieldAccess.this.equals(((AccessDispatcher) other).getFieldAccess())
&& fieldDescription.equals(((AccessDispatcher) other).fieldDescription);
}
@Override
public int hashCode() {
return fieldDescription.hashCode() + 31 * FieldAccess.this.hashCode();
}
@Override
public String toString() {
return "FieldAccess.AccessDispatcher{" +
"fieldAccess=" + FieldAccess.this +
", fieldDescription=" + fieldDescription +
'}';
}
/**
* Returns the outer instance.
*
* @return The outer instance.
*/
private FieldAccess getFieldAccess() {
return FieldAccess.this;
}
/**
* An abstract base implementation for accessing a field value.
*/
private abstract class AbstractFieldInstruction implements StackManipulation {
@Override
public boolean isValid() {
return true;
}
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
methodVisitor.visitFieldInsn(getOpcode(),
fieldDescription.getDeclaringType().getInternalName(),
fieldDescription.getInternalName(),
fieldDescription.getDescriptor());
return resolveSize(fieldDescription.getType().getStackSize());
}
/**
* Returns the opcode for implementing the field access.
*
* @return The opcode for implementing the field access.
*/
protected abstract int getOpcode();
/**
* Resolves the actual size of this field access operation.
*
* @param fieldSize The size of the accessed field.
* @return The size of the field access operation based on the field's size.
*/
protected abstract Size resolveSize(StackSize fieldSize);
}
/**
* A reading field access operation.
*/
protected class FieldGetInstruction extends AbstractFieldInstruction {
@Override
protected int getOpcode() {
return getterOpcode;
}
@Override
protected Size resolveSize(StackSize fieldSize) {
int sizeChange = fieldSize.getSize() - targetSizeChange;
return new Size(sizeChange, sizeChange);
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& getAccessDispatcher().equals(((FieldGetInstruction) other).getAccessDispatcher());
}
@Override
public int hashCode() {
return getAccessDispatcher().hashCode() + 7;
}
@Override
public String toString() {
return "FieldAccess.AccessDispatcher.FieldGetInstruction{fieldDescription=" + fieldDescription + '}';
}
/**
* Returns the outer instance.
*
* @return The outer instance.
*/
private AccessDispatcher getAccessDispatcher() {
return AccessDispatcher.this;
}
}
/**
* A writing field access operation.
*/
protected class FieldPutInstruction extends AbstractFieldInstruction {
@Override
protected int getOpcode() {
return putterOpcode;
}
@Override
protected Size resolveSize(StackSize fieldSize) {
return new Size(-1 * (fieldSize.getSize() + targetSizeChange), 0);
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& getAccessDispatcher().equals(((FieldPutInstruction) other).getAccessDispatcher());
}
@Override
public int hashCode() {
return getAccessDispatcher().hashCode() + 14;
}
@Override
public String toString() {
return "FieldAccess.AccessDispatcher.FieldPutInstruction{fieldDescription=" + fieldDescription + '}';
}
/**
* Returns the outer instance.
*
* @return The outer instance.
*/
private AccessDispatcher getAccessDispatcher() {
return AccessDispatcher.this;
}
}
}
}