1 /*
2 * Copyright (c) 2016, 2017, 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 */
29 import java.util.List;
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;
50 private static final int MAJOR_VERSION_JAVA_MAX = 54;
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 }
71
72 ClassfileConstantPool cp = new ClassfileConstantPool(stream, context);
73
74 // access_flags, this_class, super_class
75 skipFully(stream, 6);
76
77 // interfaces
78 skipFully(stream, stream.readUnsignedShort() * 2);
79
80 // fields
81 skipFields(stream);
82
83 // methods
84 codeAttributes = readMethods(stream, cp);
85
86 // attributes
87 skipAttributes(stream);
88 }
|
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 */
29 import java.util.List;
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_JAVA7 = 51;
50 private static final int MAJOR_VERSION_JAVA10 = 54;
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_JAVA7 || major > MAJOR_VERSION_JAVA10) {
69 throw new UnsupportedClassVersionError("Unsupported class file version: " + major + "." + minor);
70 }
71
72 ClassfileConstantPool cp = new ClassfileConstantPool(stream, context);
73
74 // access_flags, this_class, super_class
75 skipFully(stream, 6);
76
77 // interfaces
78 skipFully(stream, stream.readUnsignedShort() * 2);
79
80 // fields
81 skipFields(stream);
82
83 // methods
84 codeAttributes = readMethods(stream, cp);
85
86 // attributes
87 skipAttributes(stream);
88 }
|