author | darcy |
Tue, 11 Jun 2019 16:45:20 -0700 | |
changeset 55382 | 30b1b7b4dd86 |
parent 54568 | b2ed96c35687 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
55382 | 2 |
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.jvm; |
|
27 |
||
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
28 |
import com.sun.tools.javac.util.Name; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
29 |
|
10 | 30 |
|
31 |
/** A JVM class file. |
|
32 |
* |
|
33 |
* <p>Generic Java classfiles have one additional attribute for classes, |
|
34 |
* methods and fields: |
|
35 |
* <pre> |
|
36 |
* "Signature" (u4 attr-length, u2 signature-index) |
|
37 |
* </pre> |
|
38 |
* |
|
39 |
* <p>A signature gives the full Java type of a method or field. When |
|
40 |
* used as a class attribute, it indicates type parameters, followed |
|
41 |
* by supertype, followed by all interfaces. |
|
42 |
* <pre> |
|
43 |
* methodOrFieldSignature ::= type |
|
44 |
* classSignature ::= [ typeparams ] supertype { interfacetype } |
|
45 |
* </pre> |
|
46 |
* <p>The type syntax in signatures is extended as follows: |
|
13844 | 47 |
* <pre>{@literal |
10 | 48 |
* type ::= ... | classtype | methodtype | typevar |
49 |
* classtype ::= classsig { '.' classsig } |
|
50 |
* classig ::= 'L' name [typeargs] ';' |
|
51 |
* methodtype ::= [ typeparams ] '(' { type } ')' type |
|
52 |
* typevar ::= 'T' name ';' |
|
53 |
* typeargs ::= '<' type { type } '>' |
|
54 |
* typeparams ::= '<' typeparam { typeparam } '>' |
|
55 |
* typeparam ::= name ':' type |
|
13844 | 56 |
* }</pre> |
10 | 57 |
* <p>This class defines constants used in class files as well |
58 |
* as routines to convert between internal ``.'' and external ``/'' |
|
59 |
* separators in class names. |
|
60 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
61 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
62 |
* If you write code that depends on this, you do so at your own risk. |
10 | 63 |
* This code and its internal interfaces are subject to change or |
64 |
* deletion without notice.</b> */ |
|
65 |
public class ClassFile { |
|
66 |
||
67 |
public final static int JAVA_MAGIC = 0xCAFEBABE; |
|
68 |
||
69 |
// see Target |
|
70 |
public final static int CONSTANT_Utf8 = 1; |
|
71 |
public final static int CONSTANT_Unicode = 2; |
|
72 |
public final static int CONSTANT_Integer = 3; |
|
73 |
public final static int CONSTANT_Float = 4; |
|
74 |
public final static int CONSTANT_Long = 5; |
|
75 |
public final static int CONSTANT_Double = 6; |
|
76 |
public final static int CONSTANT_Class = 7; |
|
77 |
public final static int CONSTANT_String = 8; |
|
78 |
public final static int CONSTANT_Fieldref = 9; |
|
79 |
public final static int CONSTANT_Methodref = 10; |
|
80 |
public final static int CONSTANT_InterfaceMethodref = 11; |
|
81 |
public final static int CONSTANT_NameandType = 12; |
|
8042 | 82 |
public final static int CONSTANT_MethodHandle = 15; |
83 |
public final static int CONSTANT_MethodType = 16; |
|
48826 | 84 |
public final static int CONSTANT_Dynamic = 17; |
8042 | 85 |
public final static int CONSTANT_InvokeDynamic = 18; |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
86 |
public final static int CONSTANT_Module = 19; |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
87 |
public final static int CONSTANT_Package = 20; |
10 | 88 |
|
14046
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
89 |
public final static int REF_getField = 1; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
90 |
public final static int REF_getStatic = 2; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
91 |
public final static int REF_putField = 3; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
92 |
public final static int REF_putStatic = 4; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
93 |
public final static int REF_invokeVirtual = 5; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
94 |
public final static int REF_invokeStatic = 6; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
95 |
public final static int REF_invokeSpecial = 7; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
96 |
public final static int REF_newInvokeSpecial = 8; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
97 |
public final static int REF_invokeInterface = 9; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
98 |
|
10 | 99 |
public final static int MAX_PARAMETERS = 0xff; |
100 |
public final static int MAX_DIMENSIONS = 0xff; |
|
101 |
public final static int MAX_CODE = 0xffff; |
|
102 |
public final static int MAX_LOCALS = 0xffff; |
|
103 |
public final static int MAX_STACK = 0xffff; |
|
104 |
||
49580
62b908c9f0e6
8199194: Add javac support for preview features
mcimadamore
parents:
48826
diff
changeset
|
105 |
public final static int PREVIEW_MINOR_VERSION = 0xffff; |
62b908c9f0e6
8199194: Add javac support for preview features
mcimadamore
parents:
48826
diff
changeset
|
106 |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
107 |
public enum Version { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
108 |
V45_3(45, 3), // base level for all attributes |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
109 |
V49(49, 0), // JDK 1.5: enum, generics, annotations |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
110 |
V50(50, 0), // JDK 1.6: stackmaps |
14550 | 111 |
V51(51, 0), // JDK 1.7 |
36526 | 112 |
V52(52, 0), // JDK 1.8: lambda, type annos, param names |
48081 | 113 |
V53(53, 0), // JDK 1.9: modules, indy string concat |
48355 | 114 |
V54(54, 0), // JDK 10 |
50892 | 115 |
V55(55, 0), // JDK 11: constant dynamic, nest mates |
53023 | 116 |
V56(56, 0), // JDK 12 |
55382 | 117 |
V57(57, 0), // JDK 13 |
118 |
V58(58, 0); // JDK 14 |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
119 |
Version(int major, int minor) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
120 |
this.major = major; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
121 |
this.minor = minor; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
122 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
123 |
public final int major, minor; |
24606
12c0ca21f8dc
8042406: javac.jvm.ClassReader.readClassFile() is using Target to verify valid major versions
ntoda
parents:
14949
diff
changeset
|
124 |
|
12c0ca21f8dc
8042406: javac.jvm.ClassReader.readClassFile() is using Target to verify valid major versions
ntoda
parents:
14949
diff
changeset
|
125 |
private static final Version MIN = values()[0]; |
12c0ca21f8dc
8042406: javac.jvm.ClassReader.readClassFile() is using Target to verify valid major versions
ntoda
parents:
14949
diff
changeset
|
126 |
/** Return the least version supported, MIN */ |
12c0ca21f8dc
8042406: javac.jvm.ClassReader.readClassFile() is using Target to verify valid major versions
ntoda
parents:
14949
diff
changeset
|
127 |
public static Version MIN() { return MIN; } |
12c0ca21f8dc
8042406: javac.jvm.ClassReader.readClassFile() is using Target to verify valid major versions
ntoda
parents:
14949
diff
changeset
|
128 |
|
12c0ca21f8dc
8042406: javac.jvm.ClassReader.readClassFile() is using Target to verify valid major versions
ntoda
parents:
14949
diff
changeset
|
129 |
private static final Version MAX = values()[values().length-1]; |
12c0ca21f8dc
8042406: javac.jvm.ClassReader.readClassFile() is using Target to verify valid major versions
ntoda
parents:
14949
diff
changeset
|
130 |
/** Return the largest version supported, MAX */ |
12c0ca21f8dc
8042406: javac.jvm.ClassReader.readClassFile() is using Target to verify valid major versions
ntoda
parents:
14949
diff
changeset
|
131 |
public static Version MAX() { return MAX; } |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
132 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
133 |
|
10 | 134 |
|
135 |
/************************************************************************ |
|
136 |
* String Translation Routines |
|
137 |
***********************************************************************/ |
|
138 |
||
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
139 |
/** |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
140 |
* Return internal representation of buf[offset..offset+len-1], converting '/' to '.'. |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
141 |
* |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
142 |
* Note: the naming is the inverse of that used by JVMS 4.2 The Internal Form Of Names, |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
143 |
* which defines "internal name" to be the form using "/" instead of "." |
10 | 144 |
*/ |
145 |
public static byte[] internalize(byte[] buf, int offset, int len) { |
|
146 |
byte[] translated = new byte[len]; |
|
147 |
for (int j = 0; j < len; j++) { |
|
148 |
byte b = buf[offset + j]; |
|
149 |
if (b == '/') translated[j] = (byte) '.'; |
|
150 |
else translated[j] = b; |
|
151 |
} |
|
152 |
return translated; |
|
153 |
} |
|
154 |
||
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
155 |
/** |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
156 |
* Return internal representation of given name, converting '/' to '.'. |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
157 |
* |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
158 |
* Note: the naming is the inverse of that used by JVMS 4.2 The Internal Form Of Names, |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
159 |
* which defines "internal name" to be the form using "/" instead of "." |
10 | 160 |
*/ |
161 |
public static byte[] internalize(Name name) { |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
10
diff
changeset
|
162 |
return internalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); |
10 | 163 |
} |
164 |
||
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
165 |
/** |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
166 |
* Return external representation of buf[offset..offset+len-1], converting '.' to '/'. |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
167 |
* |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
168 |
* Note: the naming is the inverse of that used by JVMS 4.2 The Internal Form Of Names, |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
169 |
* which defines "internal name" to be the form using "/" instead of "." |
10 | 170 |
*/ |
171 |
public static byte[] externalize(byte[] buf, int offset, int len) { |
|
172 |
byte[] translated = new byte[len]; |
|
173 |
for (int j = 0; j < len; j++) { |
|
174 |
byte b = buf[offset + j]; |
|
175 |
if (b == '.') translated[j] = (byte) '/'; |
|
176 |
else translated[j] = b; |
|
177 |
} |
|
178 |
return translated; |
|
179 |
} |
|
180 |
||
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
181 |
/** |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
182 |
* Return external representation of given name, converting '/' to '.'. |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
183 |
* |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
184 |
* Note: the naming is the inverse of that used by JVMS 4.2 The Internal Form Of Names, |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
185 |
* which defines "internal name" to be the form using "/" instead of "." |
10 | 186 |
*/ |
187 |
public static byte[] externalize(Name name) { |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
10
diff
changeset
|
188 |
return externalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); |
10 | 189 |
} |
190 |
} |