-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Util.java
324 lines (287 loc) · 12.4 KB
/
Util.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
/*
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.classfile.impl;
import java.lang.classfile.CodeBuilder;
import java.lang.classfile.CustomAttribute;
import java.lang.classfile.FieldBuilder;
import java.lang.classfile.MethodBuilder;
import java.lang.classfile.PseudoInstruction;
import java.lang.classfile.constantpool.PoolEntry;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
import java.util.AbstractList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.lang.classfile.Attribute;
import java.lang.classfile.AttributeMapper;
import java.lang.classfile.Attributes;
import java.lang.classfile.BufWriter;
import java.lang.classfile.ClassFile;
import java.lang.classfile.Opcode;
import java.lang.classfile.constantpool.ClassEntry;
import java.lang.classfile.constantpool.ModuleEntry;
import java.lang.classfile.constantpool.NameAndTypeEntry;
import java.lang.constant.ModuleDesc;
import java.lang.reflect.AccessFlag;
import jdk.internal.access.SharedSecrets;
import static java.lang.classfile.ClassFile.ACC_STATIC;
import java.lang.classfile.attribute.CodeAttribute;
import java.lang.classfile.components.ClassPrinter;
import java.nio.ByteBuffer;
import java.util.function.Consumer;
/**
* Helper to create and manipulate type descriptors, where type descriptors are
* represented as JVM type descriptor strings and symbols are represented as
* name strings
*/
public class Util {
private Util() {
}
public static <T> Consumer<Consumer<T>> writingAll(Iterable<T> container) {
record ForEachConsumer<T>(Iterable<T> container) implements Consumer<Consumer<T>> {
@Override
public void accept(Consumer<T> consumer) {
container.forEach(consumer);
}
}
return new ForEachConsumer<>(container);
}
public static Consumer<MethodBuilder> buildingCode(Consumer<? super CodeBuilder> codeHandler) {
record WithCodeMethodHandler(Consumer<? super CodeBuilder> codeHandler) implements Consumer<MethodBuilder> {
@Override
public void accept(MethodBuilder builder) {
builder.withCode(codeHandler);
}
}
return new WithCodeMethodHandler(codeHandler);
}
public static Consumer<FieldBuilder> buildingFlags(int flags) {
record WithFlagFieldHandler(int flags) implements Consumer<FieldBuilder> {
@Override
public void accept(FieldBuilder builder) {
builder.withFlags(flags);
}
}
return new WithFlagFieldHandler(flags);
}
private static final int ATTRIBUTE_STABILITY_COUNT = AttributeMapper.AttributeStability.values().length;
public static boolean isAttributeAllowed(final Attribute<?> attr,
final ClassFile.AttributesProcessingOption processingOption) {
return attr instanceof BoundAttribute
? ATTRIBUTE_STABILITY_COUNT - attr.attributeMapper().stability().ordinal() > processingOption.ordinal()
: true;
}
public static int parameterSlots(MethodTypeDesc mDesc) {
int count = 0;
for (int i = 0; i < mDesc.parameterCount(); i++) {
count += slotSize(mDesc.parameterType(i));
}
return count;
}
public static int[] parseParameterSlots(int flags, MethodTypeDesc mDesc) {
int[] result = new int[mDesc.parameterCount()];
int count = ((flags & ACC_STATIC) != 0) ? 0 : 1;
for (int i = 0; i < result.length; i++) {
result[i] = count;
count += slotSize(mDesc.parameterType(i));
}
return result;
}
public static int maxLocals(int flags, MethodTypeDesc mDesc) {
int count = ((flags & ACC_STATIC) != 0) ? 0 : 1;
for (int i = 0; i < mDesc.parameterCount(); i++) {
count += slotSize(mDesc.parameterType(i));
}
return count;
}
/**
* Converts a descriptor of classes or interfaces into
* a binary name. Rejects primitive types or arrays.
* This is an inverse of {@link ClassDesc#of(String)}.
*/
public static String toBinaryName(ClassDesc cd) {
return toInternalName(cd).replace('/', '.');
}
public static String toInternalName(ClassDesc cd) {
var desc = cd.descriptorString();
if (desc.charAt(0) == 'L')
return desc.substring(1, desc.length() - 1);
throw new IllegalArgumentException(desc);
}
public static ClassDesc toClassDesc(String classInternalNameOrArrayDesc) {
return classInternalNameOrArrayDesc.charAt(0) == '['
? ClassDesc.ofDescriptor(classInternalNameOrArrayDesc)
: ClassDesc.ofInternalName(classInternalNameOrArrayDesc);
}
public static<T, U> List<U> mappedList(List<? extends T> list, Function<T, U> mapper) {
return new AbstractList<>() {
@Override
public U get(int index) {
return mapper.apply(list.get(index));
}
@Override
public int size() {
return list.size();
}
};
}
public static List<ClassEntry> entryList(List<? extends ClassDesc> list) {
var result = new Object[list.size()]; // null check
for (int i = 0; i < result.length; i++) {
result[i] = TemporaryConstantPool.INSTANCE.classEntry(list.get(i));
}
return SharedSecrets.getJavaUtilCollectionAccess().listFromTrustedArray(result);
}
public static List<ModuleEntry> moduleEntryList(List<? extends ModuleDesc> list) {
var result = new Object[list.size()]; // null check
for (int i = 0; i < result.length; i++) {
result[i] = TemporaryConstantPool.INSTANCE.moduleEntry(TemporaryConstantPool.INSTANCE.utf8Entry(list.get(i).name()));
}
return SharedSecrets.getJavaUtilCollectionAccess().listFromTrustedArray(result);
}
public static void checkKind(Opcode op, Opcode.Kind k) {
if (op.kind() != k)
throw new IllegalArgumentException(
String.format("Wrong opcode kind specified; found %s(%s), expected %s", op, op.kind(), k));
}
public static int flagsToBits(AccessFlag.Location location, Collection<AccessFlag> flags) {
int i = 0;
for (AccessFlag f : flags) {
if (!f.locations().contains(location)) {
throw new IllegalArgumentException("unexpected flag: " + f + " use in target location: " + location);
}
i |= f.mask();
}
return i;
}
public static int flagsToBits(AccessFlag.Location location, AccessFlag... flags) {
int i = 0;
for (AccessFlag f : flags) {
if (!f.locations().contains(location)) {
throw new IllegalArgumentException("unexpected flag: " + f + " use in target location: " + location);
}
i |= f.mask();
}
return i;
}
public static boolean has(AccessFlag.Location location, int flagsMask, AccessFlag flag) {
return (flag.mask() & flagsMask) == flag.mask() && flag.locations().contains(location);
}
public static ClassDesc fieldTypeSymbol(NameAndTypeEntry nat) {
return ((AbstractPoolEntry.NameAndTypeEntryImpl)nat).fieldTypeSymbol();
}
public static MethodTypeDesc methodTypeSymbol(NameAndTypeEntry nat) {
return ((AbstractPoolEntry.NameAndTypeEntryImpl)nat).methodTypeSymbol();
}
@SuppressWarnings("unchecked")
private static <T extends Attribute<T>> void writeAttribute(BufWriterImpl writer, Attribute<?> attr) {
if (attr instanceof CustomAttribute<?> ca) {
var mapper = (AttributeMapper<T>) ca.attributeMapper();
mapper.writeAttribute(writer, (T) ca);
} else {
assert attr instanceof BoundAttribute || attr instanceof UnboundAttribute;
((Writable) attr).writeTo(writer);
}
}
public static void writeAttributes(BufWriterImpl buf, List<? extends Attribute<?>> list) {
buf.writeU2(list.size());
for (var e : list) {
writeAttribute(buf, e);
}
}
static void writeList(BufWriterImpl buf, List<Writable> list) {
buf.writeU2(list.size());
for (var e : list) {
e.writeTo(buf);
}
}
public static int slotSize(ClassDesc desc) {
return switch (desc.descriptorString().charAt(0)) {
case 'V' -> 0;
case 'D','J' -> 2;
default -> 1;
};
}
public static boolean isDoubleSlot(ClassDesc desc) {
char ch = desc.descriptorString().charAt(0);
return ch == 'D' || ch == 'J';
}
public static void dumpMethod(SplitConstantPool cp,
ClassDesc cls,
String methodName,
MethodTypeDesc methodDesc,
int acc,
ByteBuffer bytecode,
Consumer<String> dump) {
// try to dump debug info about corrupted bytecode
try {
var cc = ClassFile.of();
var clm = cc.parse(cc.build(cp.classEntry(cls), cp, clb ->
clb.withMethod(methodName, methodDesc, acc, mb ->
((DirectMethodBuilder)mb).writeAttribute(new UnboundAttribute.AdHocAttribute<CodeAttribute>(Attributes.code()) {
@Override
public void writeBody(BufWriterImpl b) {
b.writeU2(-1);//max stack
b.writeU2(-1);//max locals
b.writeInt(bytecode.limit());
b.writeBytes(bytecode.array(), 0, bytecode.limit());
b.writeU2(0);//exception handlers
b.writeU2(0);//attributes
}
}))));
ClassPrinter.toYaml(clm.methods().get(0).code().get(), ClassPrinter.Verbosity.TRACE_ALL, dump);
} catch (Error | Exception _) {
// fallback to bytecode hex dump
bytecode.rewind();
while (bytecode.position() < bytecode.limit()) {
dump.accept("%n%04x:".formatted(bytecode.position()));
for (int i = 0; i < 16 && bytecode.position() < bytecode.limit(); i++) {
dump.accept(" %02x".formatted(bytecode.get()));
}
}
}
}
public static void writeListIndices(BufWriter writer, List<? extends PoolEntry> list) {
writer.writeU2(list.size());
for (PoolEntry info : list) {
writer.writeIndex(info);
}
}
public static boolean writeLocalVariable(BufWriterImpl buf, PseudoInstruction lvOrLvt) {
return ((WritableLocalVariable) lvOrLvt).writeLocalTo(buf);
}
/**
* A generic interface for objects to write to a
* buf writer. Do not implement unless necessary,
* as this writeTo is public, which can be troublesome.
*/
interface Writable {
void writeTo(BufWriterImpl writer);
}
interface WritableLocalVariable {
boolean writeLocalTo(BufWriterImpl buf);
}
}