author | psandoz |
Fri, 03 Nov 2017 10:01:08 -0700 | |
changeset 48081 | 89829dd3cc54 |
parent 47216 | 71c04702a3d5 |
child 48355 | 4944950606ef |
permissions | -rw-r--r-- |
10 | 1 |
/* |
48081 | 2 |
* Copyright (c) 1999, 2017, 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.code.Type; |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
29 |
import com.sun.tools.javac.code.Types; |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
30 |
import com.sun.tools.javac.code.Types.UniqueType; |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
31 |
import com.sun.tools.javac.util.Name; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
32 |
|
10 | 33 |
|
34 |
/** A JVM class file. |
|
35 |
* |
|
36 |
* <p>Generic Java classfiles have one additional attribute for classes, |
|
37 |
* methods and fields: |
|
38 |
* <pre> |
|
39 |
* "Signature" (u4 attr-length, u2 signature-index) |
|
40 |
* </pre> |
|
41 |
* |
|
42 |
* <p>A signature gives the full Java type of a method or field. When |
|
43 |
* used as a class attribute, it indicates type parameters, followed |
|
44 |
* by supertype, followed by all interfaces. |
|
45 |
* <pre> |
|
46 |
* methodOrFieldSignature ::= type |
|
47 |
* classSignature ::= [ typeparams ] supertype { interfacetype } |
|
48 |
* </pre> |
|
49 |
* <p>The type syntax in signatures is extended as follows: |
|
13844 | 50 |
* <pre>{@literal |
10 | 51 |
* type ::= ... | classtype | methodtype | typevar |
52 |
* classtype ::= classsig { '.' classsig } |
|
53 |
* classig ::= 'L' name [typeargs] ';' |
|
54 |
* methodtype ::= [ typeparams ] '(' { type } ')' type |
|
55 |
* typevar ::= 'T' name ';' |
|
56 |
* typeargs ::= '<' type { type } '>' |
|
57 |
* typeparams ::= '<' typeparam { typeparam } '>' |
|
58 |
* typeparam ::= name ':' type |
|
13844 | 59 |
* }</pre> |
10 | 60 |
* <p>This class defines constants used in class files as well |
61 |
* as routines to convert between internal ``.'' and external ``/'' |
|
62 |
* separators in class names. |
|
63 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
64 |
* <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
|
65 |
* If you write code that depends on this, you do so at your own risk. |
10 | 66 |
* This code and its internal interfaces are subject to change or |
67 |
* deletion without notice.</b> */ |
|
68 |
public class ClassFile { |
|
69 |
||
70 |
public final static int JAVA_MAGIC = 0xCAFEBABE; |
|
71 |
||
72 |
// see Target |
|
73 |
public final static int CONSTANT_Utf8 = 1; |
|
74 |
public final static int CONSTANT_Unicode = 2; |
|
75 |
public final static int CONSTANT_Integer = 3; |
|
76 |
public final static int CONSTANT_Float = 4; |
|
77 |
public final static int CONSTANT_Long = 5; |
|
78 |
public final static int CONSTANT_Double = 6; |
|
79 |
public final static int CONSTANT_Class = 7; |
|
80 |
public final static int CONSTANT_String = 8; |
|
81 |
public final static int CONSTANT_Fieldref = 9; |
|
82 |
public final static int CONSTANT_Methodref = 10; |
|
83 |
public final static int CONSTANT_InterfaceMethodref = 11; |
|
84 |
public final static int CONSTANT_NameandType = 12; |
|
8042 | 85 |
public final static int CONSTANT_MethodHandle = 15; |
86 |
public final static int CONSTANT_MethodType = 16; |
|
87 |
public final static int CONSTANT_InvokeDynamic = 18; |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
88 |
public final static int CONSTANT_Module = 19; |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
89 |
public final static int CONSTANT_Package = 20; |
10 | 90 |
|
14046
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
91 |
public final static int REF_getField = 1; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
92 |
public final static int REF_getStatic = 2; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
93 |
public final static int REF_putField = 3; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
94 |
public final static int REF_putStatic = 4; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
95 |
public final static int REF_invokeVirtual = 5; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
96 |
public final static int REF_invokeStatic = 6; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
97 |
public final static int REF_invokeSpecial = 7; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
98 |
public final static int REF_newInvokeSpecial = 8; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
99 |
public final static int REF_invokeInterface = 9; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
100 |
|
10 | 101 |
public final static int MAX_PARAMETERS = 0xff; |
102 |
public final static int MAX_DIMENSIONS = 0xff; |
|
103 |
public final static int MAX_CODE = 0xffff; |
|
104 |
public final static int MAX_LOCALS = 0xffff; |
|
105 |
public final static int MAX_STACK = 0xffff; |
|
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 |
114 |
V54(54, 0); // JDK 10 |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
115 |
Version(int major, int minor) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
116 |
this.major = major; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
117 |
this.minor = minor; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
118 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
119 |
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
|
120 |
|
12c0ca21f8dc
8042406: javac.jvm.ClassReader.readClassFile() is using Target to verify valid major versions
ntoda
parents:
14949
diff
changeset
|
121 |
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
|
122 |
/** 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
|
123 |
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
|
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 MAX = values()[values().length-1]; |
12c0ca21f8dc
8042406: javac.jvm.ClassReader.readClassFile() is using Target to verify valid major versions
ntoda
parents:
14949
diff
changeset
|
126 |
/** 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
|
127 |
public static Version MAX() { return MAX; } |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
128 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
129 |
|
10 | 130 |
|
131 |
/************************************************************************ |
|
132 |
* String Translation Routines |
|
133 |
***********************************************************************/ |
|
134 |
||
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
135 |
/** |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
136 |
* Return internal representation of buf[offset..offset+len-1], converting '/' to '.'. |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
137 |
* |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
138 |
* 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
|
139 |
* which defines "internal name" to be the form using "/" instead of "." |
10 | 140 |
*/ |
141 |
public static byte[] internalize(byte[] buf, int offset, int len) { |
|
142 |
byte[] translated = new byte[len]; |
|
143 |
for (int j = 0; j < len; j++) { |
|
144 |
byte b = buf[offset + j]; |
|
145 |
if (b == '/') translated[j] = (byte) '.'; |
|
146 |
else translated[j] = b; |
|
147 |
} |
|
148 |
return translated; |
|
149 |
} |
|
150 |
||
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
151 |
/** |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
152 |
* Return internal representation of given name, converting '/' to '.'. |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
153 |
* |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
154 |
* 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
|
155 |
* which defines "internal name" to be the form using "/" instead of "." |
10 | 156 |
*/ |
157 |
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
|
158 |
return internalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); |
10 | 159 |
} |
160 |
||
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
161 |
/** |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
162 |
* Return external representation of buf[offset..offset+len-1], converting '.' to '/'. |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
163 |
* |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
164 |
* 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
|
165 |
* which defines "internal name" to be the form using "/" instead of "." |
10 | 166 |
*/ |
167 |
public static byte[] externalize(byte[] buf, int offset, int len) { |
|
168 |
byte[] translated = new byte[len]; |
|
169 |
for (int j = 0; j < len; j++) { |
|
170 |
byte b = buf[offset + j]; |
|
171 |
if (b == '.') translated[j] = (byte) '/'; |
|
172 |
else translated[j] = b; |
|
173 |
} |
|
174 |
return translated; |
|
175 |
} |
|
176 |
||
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
177 |
/** |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
178 |
* Return external representation of given name, converting '/' to '.'. |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
179 |
* |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37940
diff
changeset
|
180 |
* 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
|
181 |
* which defines "internal name" to be the form using "/" instead of "." |
10 | 182 |
*/ |
183 |
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
|
184 |
return externalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); |
10 | 185 |
} |
186 |
||
187 |
/************************************************************************ |
|
188 |
* Name-and-type |
|
189 |
***********************************************************************/ |
|
190 |
||
191 |
/** A class for the name-and-type signature of a method or field. |
|
192 |
*/ |
|
193 |
public static class NameAndType { |
|
194 |
Name name; |
|
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
195 |
UniqueType uniqueType; |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
196 |
Types types; |
10 | 197 |
|
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
198 |
NameAndType(Name name, Type type, Types types) { |
10 | 199 |
this.name = name; |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
200 |
this.uniqueType = new UniqueType(type, types); |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
201 |
this.types = types; |
10 | 202 |
} |
203 |
||
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
204 |
void setType(Type type) { |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
205 |
this.uniqueType = new UniqueType(type, types); |
10 | 206 |
} |
207 |
||
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
208 |
@Override |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
209 |
public boolean equals(Object other) { |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
210 |
return (other instanceof NameAndType && |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
211 |
name == ((NameAndType) other).name && |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
212 |
uniqueType.equals(((NameAndType) other).uniqueType)); |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
213 |
} |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
214 |
|
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
215 |
@Override |
10 | 216 |
public int hashCode() { |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14550
diff
changeset
|
217 |
return name.hashCode() * uniqueType.hashCode(); |
10 | 218 |
} |
219 |
} |
|
220 |
} |