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