30
31 import org.graalvm.compiler.replacements.classfile.ClassfileConstant.Utf8;
32
33 import jdk.vm.ci.meta.ResolvedJavaMethod;
34 import jdk.vm.ci.meta.ResolvedJavaType;
35
36 /**
37 * Container for objects representing the {@code Code} attributes parsed from a class file.
38 *
39 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.3">Code
40 * attributes</a>
41 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4">Constant
42 * Pool</a>
43 */
44 public class Classfile {
45
46 private final ResolvedJavaType type;
47 private final List<ClassfileBytecode> codeAttributes;
48
49 private static final int MAJOR_VERSION_JAVA_MIN = 51; // JDK7
50 private static final int MAJOR_VERSION_JAVA_MAX = 55; // JDK11
51 private static final int MAGIC = 0xCAFEBABE;
52
53 /**
54 * Creates a {@link Classfile} by parsing the class file bytes for {@code type} loadable from
55 * {@code context}.
56 *
57 * @throws NoClassDefFoundError if there is an IO error while parsing the class file
58 */
59 public Classfile(ResolvedJavaType type, DataInputStream stream, ClassfileBytecodeProvider context) throws IOException {
60 this.type = type;
61
62 // magic
63 int magic = stream.readInt();
64 assert magic == MAGIC;
65
66 int minor = stream.readUnsignedShort();
67 int major = stream.readUnsignedShort();
68 if (major < MAJOR_VERSION_JAVA_MIN || major > MAJOR_VERSION_JAVA_MAX) {
69 throw new UnsupportedClassVersionError("Unsupported class file version: " + major + "." + minor);
70 }
|
30
31 import org.graalvm.compiler.replacements.classfile.ClassfileConstant.Utf8;
32
33 import jdk.vm.ci.meta.ResolvedJavaMethod;
34 import jdk.vm.ci.meta.ResolvedJavaType;
35
36 /**
37 * Container for objects representing the {@code Code} attributes parsed from a class file.
38 *
39 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.3">Code
40 * attributes</a>
41 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4">Constant
42 * Pool</a>
43 */
44 public class Classfile {
45
46 private final ResolvedJavaType type;
47 private final List<ClassfileBytecode> codeAttributes;
48
49 private static final int MAJOR_VERSION_JAVA_MIN = 51; // JDK7
50 private static final int MAJOR_VERSION_JAVA_MAX = 56; // JDK12
51 private static final int MAGIC = 0xCAFEBABE;
52
53 /**
54 * Creates a {@link Classfile} by parsing the class file bytes for {@code type} loadable from
55 * {@code context}.
56 *
57 * @throws NoClassDefFoundError if there is an IO error while parsing the class file
58 */
59 public Classfile(ResolvedJavaType type, DataInputStream stream, ClassfileBytecodeProvider context) throws IOException {
60 this.type = type;
61
62 // magic
63 int magic = stream.readInt();
64 assert magic == MAGIC;
65
66 int minor = stream.readUnsignedShort();
67 int major = stream.readUnsignedShort();
68 if (major < MAJOR_VERSION_JAVA_MIN || major > MAJOR_VERSION_JAVA_MAX) {
69 throw new UnsupportedClassVersionError("Unsupported class file version: " + major + "." + minor);
70 }
|