author | jjg |
Mon, 19 Nov 2012 16:40:54 -0800 | |
changeset 14550 | 284da8fb4eaf |
parent 14046 | 8ef5d5b19998 |
child 14949 | 45f43822bbde |
permissions | -rw-r--r-- |
10 | 1 |
/* |
13844 | 2 |
* Copyright (c) 1999, 2012, 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; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
29 |
import com.sun.tools.javac.util.Name; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
30 |
|
10 | 31 |
|
32 |
/** A JVM class file. |
|
33 |
* |
|
34 |
* <p>Generic Java classfiles have one additional attribute for classes, |
|
35 |
* methods and fields: |
|
36 |
* <pre> |
|
37 |
* "Signature" (u4 attr-length, u2 signature-index) |
|
38 |
* </pre> |
|
39 |
* |
|
40 |
* <p>A signature gives the full Java type of a method or field. When |
|
41 |
* used as a class attribute, it indicates type parameters, followed |
|
42 |
* by supertype, followed by all interfaces. |
|
43 |
* <pre> |
|
44 |
* methodOrFieldSignature ::= type |
|
45 |
* classSignature ::= [ typeparams ] supertype { interfacetype } |
|
46 |
* </pre> |
|
47 |
* <p>The type syntax in signatures is extended as follows: |
|
13844 | 48 |
* <pre>{@literal |
10 | 49 |
* type ::= ... | classtype | methodtype | typevar |
50 |
* classtype ::= classsig { '.' classsig } |
|
51 |
* classig ::= 'L' name [typeargs] ';' |
|
52 |
* methodtype ::= [ typeparams ] '(' { type } ')' type |
|
53 |
* typevar ::= 'T' name ';' |
|
54 |
* typeargs ::= '<' type { type } '>' |
|
55 |
* typeparams ::= '<' typeparam { typeparam } '>' |
|
56 |
* typeparam ::= name ':' type |
|
13844 | 57 |
* }</pre> |
10 | 58 |
* <p>This class defines constants used in class files as well |
59 |
* as routines to convert between internal ``.'' and external ``/'' |
|
60 |
* separators in class names. |
|
61 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
62 |
* <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
|
63 |
* If you write code that depends on this, you do so at your own risk. |
10 | 64 |
* This code and its internal interfaces are subject to change or |
65 |
* deletion without notice.</b> */ |
|
66 |
public class ClassFile { |
|
67 |
||
68 |
public final static int JAVA_MAGIC = 0xCAFEBABE; |
|
69 |
||
70 |
// see Target |
|
71 |
public final static int CONSTANT_Utf8 = 1; |
|
72 |
public final static int CONSTANT_Unicode = 2; |
|
73 |
public final static int CONSTANT_Integer = 3; |
|
74 |
public final static int CONSTANT_Float = 4; |
|
75 |
public final static int CONSTANT_Long = 5; |
|
76 |
public final static int CONSTANT_Double = 6; |
|
77 |
public final static int CONSTANT_Class = 7; |
|
78 |
public final static int CONSTANT_String = 8; |
|
79 |
public final static int CONSTANT_Fieldref = 9; |
|
80 |
public final static int CONSTANT_Methodref = 10; |
|
81 |
public final static int CONSTANT_InterfaceMethodref = 11; |
|
82 |
public final static int CONSTANT_NameandType = 12; |
|
8042 | 83 |
public final static int CONSTANT_MethodHandle = 15; |
84 |
public final static int CONSTANT_MethodType = 16; |
|
85 |
public final static int CONSTANT_InvokeDynamic = 18; |
|
10 | 86 |
|
14046
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
87 |
public final static int REF_getField = 1; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
88 |
public final static int REF_getStatic = 2; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
89 |
public final static int REF_putField = 3; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
90 |
public final static int REF_putStatic = 4; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
91 |
public final static int REF_invokeVirtual = 5; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
92 |
public final static int REF_invokeStatic = 6; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
93 |
public final static int REF_invokeSpecial = 7; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
94 |
public final static int REF_newInvokeSpecial = 8; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
95 |
public final static int REF_invokeInterface = 9; |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
96 |
|
10 | 97 |
public final static int MAX_PARAMETERS = 0xff; |
98 |
public final static int MAX_DIMENSIONS = 0xff; |
|
99 |
public final static int MAX_CODE = 0xffff; |
|
100 |
public final static int MAX_LOCALS = 0xffff; |
|
101 |
public final static int MAX_STACK = 0xffff; |
|
102 |
||
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
103 |
public enum Version { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
104 |
V45_3(45, 3), // base level for all attributes |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
105 |
V49(49, 0), // JDK 1.5: enum, generics, annotations |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
106 |
V50(50, 0), // JDK 1.6: stackmaps |
14550 | 107 |
V51(51, 0), // JDK 1.7 |
108 |
V52(52, 0); // JDK 1.8: lambda, type annos, param names |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
109 |
Version(int major, int minor) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
110 |
this.major = major; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
111 |
this.minor = minor; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
112 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
113 |
public final int major, minor; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
114 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1264
diff
changeset
|
115 |
|
10 | 116 |
|
117 |
/************************************************************************ |
|
118 |
* String Translation Routines |
|
119 |
***********************************************************************/ |
|
120 |
||
121 |
/** Return internal representation of buf[offset..offset+len-1], |
|
122 |
* converting '/' to '.'. |
|
123 |
*/ |
|
124 |
public static byte[] internalize(byte[] buf, int offset, int len) { |
|
125 |
byte[] translated = new byte[len]; |
|
126 |
for (int j = 0; j < len; j++) { |
|
127 |
byte b = buf[offset + j]; |
|
128 |
if (b == '/') translated[j] = (byte) '.'; |
|
129 |
else translated[j] = b; |
|
130 |
} |
|
131 |
return translated; |
|
132 |
} |
|
133 |
||
134 |
/** Return internal representation of given name, |
|
135 |
* converting '/' to '.'. |
|
136 |
*/ |
|
137 |
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
|
138 |
return internalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); |
10 | 139 |
} |
140 |
||
141 |
/** Return external representation of buf[offset..offset+len-1], |
|
142 |
* converting '.' to '/'. |
|
143 |
*/ |
|
144 |
public static byte[] externalize(byte[] buf, int offset, int len) { |
|
145 |
byte[] translated = new byte[len]; |
|
146 |
for (int j = 0; j < len; j++) { |
|
147 |
byte b = buf[offset + j]; |
|
148 |
if (b == '.') translated[j] = (byte) '/'; |
|
149 |
else translated[j] = b; |
|
150 |
} |
|
151 |
return translated; |
|
152 |
} |
|
153 |
||
154 |
/** Return external representation of given name, |
|
155 |
* converting '/' to '.'. |
|
156 |
*/ |
|
157 |
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
|
158 |
return externalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); |
10 | 159 |
} |
160 |
||
161 |
/************************************************************************ |
|
162 |
* Name-and-type |
|
163 |
***********************************************************************/ |
|
164 |
||
165 |
/** A class for the name-and-type signature of a method or field. |
|
166 |
*/ |
|
167 |
public static class NameAndType { |
|
168 |
Name name; |
|
169 |
Type type; |
|
170 |
||
171 |
NameAndType(Name name, Type type) { |
|
172 |
this.name = name; |
|
173 |
this.type = type; |
|
174 |
} |
|
175 |
||
176 |
public boolean equals(Object other) { |
|
177 |
return |
|
178 |
other instanceof NameAndType && |
|
179 |
name == ((NameAndType) other).name && |
|
180 |
type.equals(((NameAndType) other).type); |
|
181 |
} |
|
182 |
||
183 |
public int hashCode() { |
|
184 |
return name.hashCode() * type.hashCode(); |
|
185 |
} |
|
186 |
} |
|
187 |
} |