43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.compiler.replacements.classfile;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.bytecode.Bytecodes.GETFIELD;
|
|
26 |
import static org.graalvm.compiler.bytecode.Bytecodes.GETSTATIC;
|
|
27 |
import static org.graalvm.compiler.bytecode.Bytecodes.PUTFIELD;
|
|
28 |
import static org.graalvm.compiler.bytecode.Bytecodes.PUTSTATIC;
|
|
29 |
|
|
30 |
import java.io.DataInputStream;
|
|
31 |
import java.io.IOException;
|
|
32 |
|
|
33 |
import org.graalvm.compiler.bytecode.Bytecodes;
|
|
34 |
import org.graalvm.compiler.debug.GraalError;
|
|
35 |
|
|
36 |
import jdk.vm.ci.meta.JavaConstant;
|
|
37 |
import jdk.vm.ci.meta.ResolvedJavaField;
|
|
38 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
39 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
40 |
|
|
41 |
abstract class ClassfileConstant {
|
|
42 |
|
|
43 |
// @formatter:off
|
|
44 |
public static final byte CONSTANT_Utf8 = 1;
|
|
45 |
public static final byte CONSTANT_Integer = 3;
|
|
46 |
public static final byte CONSTANT_Float = 4;
|
|
47 |
public static final byte CONSTANT_Long = 5;
|
|
48 |
public static final byte CONSTANT_Double = 6;
|
|
49 |
public static final byte CONSTANT_Class = 7;
|
|
50 |
public static final byte CONSTANT_Fieldref = 9;
|
|
51 |
public static final byte CONSTANT_String = 8;
|
|
52 |
public static final byte CONSTANT_Methodref = 10;
|
|
53 |
public static final byte CONSTANT_InterfaceMethodref = 11;
|
|
54 |
public static final byte CONSTANT_NameAndType = 12;
|
|
55 |
public static final byte CONSTANT_MethodHandle = 15;
|
|
56 |
public static final byte CONSTANT_MethodType = 16;
|
48861
|
57 |
public static final byte CONSTANT_Dynamic = 17;
|
43972
|
58 |
public static final byte CONSTANT_InvokeDynamic = 18;
|
|
59 |
// @formatter:on
|
|
60 |
|
|
61 |
final byte tag;
|
|
62 |
|
|
63 |
ClassfileConstant(byte tag) {
|
|
64 |
this.tag = tag;
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Loads the type, if any, referenced at a specified entry.
|
|
69 |
*
|
|
70 |
* @param cp
|
|
71 |
* @param index
|
|
72 |
* @param opcode
|
|
73 |
*/
|
|
74 |
public void loadReferencedType(ClassfileConstantPool cp, int index, int opcode) {
|
|
75 |
}
|
|
76 |
|
|
77 |
@Override
|
|
78 |
public String toString() {
|
|
79 |
return getClass().getSimpleName();
|
|
80 |
}
|
|
81 |
|
|
82 |
static class ClassRef extends ClassfileConstant {
|
|
83 |
|
|
84 |
final int nameIndex;
|
|
85 |
private ResolvedJavaType type;
|
|
86 |
|
|
87 |
ClassRef(DataInputStream stream) throws IOException {
|
|
88 |
super(CONSTANT_Class);
|
|
89 |
this.nameIndex = stream.readUnsignedShort();
|
|
90 |
}
|
|
91 |
|
|
92 |
@Override
|
|
93 |
public void loadReferencedType(ClassfileConstantPool cp, int index, int opcode) {
|
|
94 |
resolve(cp);
|
|
95 |
}
|
|
96 |
|
|
97 |
public ResolvedJavaType resolve(ClassfileConstantPool cp) throws GraalError {
|
|
98 |
if (type == null) {
|
|
99 |
String typeDescriptor = cp.get(Utf8.class, nameIndex).value;
|
|
100 |
ClassfileBytecodeProvider context = cp.context;
|
|
101 |
type = context.metaAccess.lookupJavaType(context.resolveToClass(typeDescriptor));
|
|
102 |
}
|
|
103 |
return type;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
static class MemberRef extends ClassfileConstant {
|
|
108 |
|
|
109 |
final int classIndex;
|
|
110 |
final int nameAndTypeIndex;
|
|
111 |
|
|
112 |
MemberRef(byte tag, DataInputStream stream) throws IOException {
|
|
113 |
super(tag);
|
|
114 |
this.classIndex = stream.readUnsignedShort();
|
|
115 |
this.nameAndTypeIndex = stream.readUnsignedShort();
|
|
116 |
}
|
|
117 |
|
|
118 |
@Override
|
|
119 |
public void loadReferencedType(ClassfileConstantPool cp, int index, int opcode) {
|
|
120 |
cp.get(ClassRef.class, classIndex).loadReferencedType(cp, classIndex, opcode);
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
static class ExecutableRef extends MemberRef {
|
|
125 |
private ResolvedJavaMethod method;
|
|
126 |
|
|
127 |
ExecutableRef(byte tag, DataInputStream stream) throws IOException {
|
|
128 |
super(tag, stream);
|
|
129 |
}
|
|
130 |
|
|
131 |
ResolvedJavaMethod resolve(ClassfileConstantPool cp, int opcode) {
|
|
132 |
if (method == null) {
|
|
133 |
ResolvedJavaType cls = cp.get(ClassRef.class, classIndex).resolve(cp);
|
|
134 |
NameAndType nameAndType = cp.get(NameAndType.class, nameAndTypeIndex);
|
|
135 |
String name = nameAndType.getName(cp);
|
|
136 |
String type = nameAndType.getType(cp);
|
|
137 |
|
|
138 |
if (opcode == Bytecodes.INVOKEINTERFACE) {
|
46344
|
139 |
method = resolveMethod(cp.context, cls, name, type, false);
|
43972
|
140 |
if (method == null) {
|
|
141 |
throw new NoSuchMethodError(cls.toJavaName() + "." + name + type);
|
|
142 |
}
|
|
143 |
if (!method.isPublic() || !(method.getDeclaringClass().isInterface() || method.getDeclaringClass().isJavaLangObject())) {
|
|
144 |
throw new IncompatibleClassChangeError("cannot invokeinterface " + method.format("%H.%n(%P)%R"));
|
|
145 |
}
|
|
146 |
} else if (opcode == Bytecodes.INVOKEVIRTUAL || opcode == Bytecodes.INVOKESPECIAL) {
|
46344
|
147 |
method = resolveMethod(cp.context, cls, name, type, false);
|
43972
|
148 |
if (method == null) {
|
|
149 |
throw new NoSuchMethodError(cls.toJavaName() + "." + name + type);
|
|
150 |
}
|
|
151 |
} else {
|
|
152 |
assert opcode == Bytecodes.INVOKESTATIC;
|
46344
|
153 |
method = resolveMethod(cp.context, cls, name, type, true);
|
43972
|
154 |
if (method == null) {
|
|
155 |
throw new NoSuchMethodError(cls.toJavaName() + "." + name + type);
|
|
156 |
}
|
|
157 |
}
|
|
158 |
}
|
|
159 |
return method;
|
|
160 |
}
|
|
161 |
}
|
|
162 |
|
|
163 |
static class MethodRef extends ExecutableRef {
|
|
164 |
|
|
165 |
MethodRef(DataInputStream stream) throws IOException {
|
|
166 |
super(CONSTANT_Methodref, stream);
|
|
167 |
}
|
|
168 |
}
|
|
169 |
|
|
170 |
static class InterfaceMethodRef extends ExecutableRef {
|
|
171 |
InterfaceMethodRef(DataInputStream stream) throws IOException {
|
|
172 |
super(CONSTANT_InterfaceMethodref, stream);
|
|
173 |
}
|
|
174 |
}
|
|
175 |
|
|
176 |
static class FieldRef extends MemberRef {
|
|
177 |
private ResolvedJavaField field;
|
|
178 |
|
|
179 |
FieldRef(DataInputStream stream) throws IOException {
|
|
180 |
super(CONSTANT_Fieldref, stream);
|
|
181 |
}
|
|
182 |
|
|
183 |
ResolvedJavaField resolve(ClassfileConstantPool cp, int opcode) {
|
|
184 |
if (field == null) {
|
|
185 |
ResolvedJavaType cls = cp.get(ClassRef.class, classIndex).resolve(cp);
|
|
186 |
NameAndType nameAndType = cp.get(NameAndType.class, nameAndTypeIndex);
|
|
187 |
String name = nameAndType.getName(cp);
|
|
188 |
String type = nameAndType.getType(cp);
|
|
189 |
assert opcode == GETFIELD || opcode == GETSTATIC || opcode == PUTFIELD || opcode == PUTSTATIC : opcode;
|
46344
|
190 |
field = resolveField(cp.context, cls, name, type, opcode == GETSTATIC || opcode == PUTSTATIC);
|
43972
|
191 |
if (field == null) {
|
|
192 |
throw new NoSuchFieldError(cls.toJavaName() + "." + name + " " + type);
|
|
193 |
}
|
|
194 |
}
|
|
195 |
return field;
|
|
196 |
}
|
|
197 |
}
|
|
198 |
|
|
199 |
static class Primitive extends ClassfileConstant {
|
|
200 |
final JavaConstant value;
|
|
201 |
|
|
202 |
Primitive(byte tag, JavaConstant value) {
|
|
203 |
super(tag);
|
|
204 |
this.value = value;
|
|
205 |
}
|
|
206 |
}
|
|
207 |
|
|
208 |
static class StringRef extends ClassfileConstant {
|
|
209 |
|
|
210 |
final int stringIndex;
|
|
211 |
JavaConstant value;
|
|
212 |
|
|
213 |
StringRef(DataInputStream stream) throws IOException {
|
|
214 |
super(ClassfileConstant.CONSTANT_String);
|
|
215 |
this.stringIndex = stream.readUnsignedShort();
|
|
216 |
}
|
|
217 |
|
|
218 |
JavaConstant getValue(ClassfileConstantPool pool) {
|
|
219 |
if (value == null) {
|
|
220 |
value = pool.context.snippetReflection.forObject(pool.lookupUtf8(stringIndex));
|
|
221 |
}
|
|
222 |
return value;
|
|
223 |
}
|
|
224 |
}
|
|
225 |
|
|
226 |
static class NameAndType extends ClassfileConstant {
|
|
227 |
|
|
228 |
final int nameIndex;
|
|
229 |
final int typeIndex;
|
|
230 |
private String name;
|
|
231 |
private String type;
|
|
232 |
|
|
233 |
NameAndType(DataInputStream stream) throws IOException {
|
|
234 |
super(ClassfileConstant.CONSTANT_NameAndType);
|
|
235 |
this.nameIndex = stream.readUnsignedShort();
|
|
236 |
this.typeIndex = stream.readUnsignedShort();
|
|
237 |
}
|
|
238 |
|
|
239 |
public String getName(ClassfileConstantPool cp) {
|
|
240 |
if (name == null) {
|
|
241 |
name = cp.get(Utf8.class, nameIndex).value;
|
|
242 |
}
|
|
243 |
return name;
|
|
244 |
}
|
|
245 |
|
|
246 |
public String getType(ClassfileConstantPool cp) {
|
|
247 |
if (type == null) {
|
|
248 |
type = cp.get(Utf8.class, typeIndex).value;
|
|
249 |
}
|
|
250 |
return type;
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
static class Utf8 extends ClassfileConstant {
|
|
255 |
final String value;
|
|
256 |
|
|
257 |
Utf8(String value) {
|
|
258 |
super(CONSTANT_Utf8);
|
|
259 |
this.value = value;
|
|
260 |
}
|
|
261 |
}
|
|
262 |
|
|
263 |
static class Unsupported extends ClassfileConstant {
|
|
264 |
final String name;
|
|
265 |
|
|
266 |
Unsupported(byte tag, String name) {
|
|
267 |
super(tag);
|
|
268 |
this.name = name;
|
|
269 |
}
|
|
270 |
|
|
271 |
@Override
|
|
272 |
public void loadReferencedType(ClassfileConstantPool cp, int index, int opcode) {
|
|
273 |
throw new GraalError("Resolution of " + name + " constant pool entries not supported by " + ClassfileBytecodeProvider.class.getSimpleName());
|
|
274 |
}
|
|
275 |
}
|
|
276 |
|
46344
|
277 |
static ResolvedJavaMethod resolveMethod(ClassfileBytecodeProvider context, ResolvedJavaType c, String name, String descriptor, boolean isStatic) {
|
|
278 |
ResolvedJavaMethod method = context.findMethod(c, name, descriptor, isStatic);
|
43972
|
279 |
if (method != null) {
|
|
280 |
return method;
|
|
281 |
}
|
|
282 |
if (!c.isJavaLangObject() && !c.isInterface()) {
|
46344
|
283 |
method = resolveMethod(context, c.getSuperclass(), name, descriptor, isStatic);
|
43972
|
284 |
if (method != null) {
|
|
285 |
return method;
|
|
286 |
}
|
|
287 |
}
|
|
288 |
for (ResolvedJavaType i : c.getInterfaces()) {
|
46344
|
289 |
method = resolveMethod(context, i, name, descriptor, isStatic);
|
43972
|
290 |
if (method != null) {
|
|
291 |
return method;
|
|
292 |
}
|
|
293 |
}
|
|
294 |
return null;
|
|
295 |
}
|
|
296 |
|
46344
|
297 |
static ResolvedJavaField resolveField(ClassfileBytecodeProvider context, ResolvedJavaType c, String name, String fieldType, boolean isStatic) {
|
|
298 |
ResolvedJavaField field = context.findField(c, name, fieldType, isStatic);
|
43972
|
299 |
if (field != null) {
|
|
300 |
return field;
|
|
301 |
}
|
|
302 |
if (!c.isJavaLangObject() && !c.isInterface()) {
|
46344
|
303 |
field = resolveField(context, c.getSuperclass(), name, fieldType, isStatic);
|
43972
|
304 |
if (field != null) {
|
|
305 |
return field;
|
|
306 |
}
|
|
307 |
}
|
|
308 |
for (ResolvedJavaType i : c.getInterfaces()) {
|
46344
|
309 |
field = resolveField(context, i, name, fieldType, isStatic);
|
43972
|
310 |
if (field != null) {
|
|
311 |
return field;
|
|
312 |
}
|
|
313 |
}
|
|
314 |
return null;
|
|
315 |
}
|
|
316 |
}
|