author | jjg |
Fri, 04 Mar 2011 11:33:37 -0800 | |
changeset 8630 | cd9eefe597f6 |
parent 8242 | 3873b4aaf4a8 |
child 9077 | 6ee59e40b258 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
8031 | 2 |
* Copyright (c) 1999, 2011, 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 |
||
28 |
import java.io.*; |
|
29 |
import java.net.URI; |
|
3782 | 30 |
import java.net.URISyntaxException; |
10 | 31 |
import java.nio.CharBuffer; |
4077
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
32 |
import java.util.Arrays; |
10 | 33 |
import java.util.EnumSet; |
34 |
import java.util.HashMap; |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
35 |
import java.util.HashSet; |
10 | 36 |
import java.util.Map; |
37 |
import java.util.Set; |
|
38 |
import javax.lang.model.SourceVersion; |
|
39 |
import javax.tools.JavaFileObject; |
|
40 |
import javax.tools.JavaFileManager; |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
41 |
import javax.tools.JavaFileManager.Location; |
10 | 42 |
import javax.tools.StandardJavaFileManager; |
43 |
||
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
44 |
import static javax.tools.StandardLocation.*; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
45 |
|
10 | 46 |
import com.sun.tools.javac.comp.Annotate; |
47 |
import com.sun.tools.javac.code.*; |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
48 |
import com.sun.tools.javac.code.Lint.LintCategory; |
10 | 49 |
import com.sun.tools.javac.code.Type.*; |
50 |
import com.sun.tools.javac.code.Symbol.*; |
|
51 |
import com.sun.tools.javac.code.Symtab; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
52 |
import com.sun.tools.javac.file.BaseFileObject; |
10 | 53 |
import com.sun.tools.javac.util.*; |
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
54 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
10 | 55 |
|
56 |
import static com.sun.tools.javac.code.Flags.*; |
|
57 |
import static com.sun.tools.javac.code.Kinds.*; |
|
58 |
import static com.sun.tools.javac.code.TypeTags.*; |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
59 |
import static com.sun.tools.javac.jvm.ClassFile.*; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
60 |
import static com.sun.tools.javac.jvm.ClassFile.Version.*; |
10 | 61 |
|
6721 | 62 |
import static com.sun.tools.javac.main.OptionName.*; |
63 |
||
10 | 64 |
/** This class provides operations to read a classfile into an internal |
65 |
* representation. The internal representation is anchored in a |
|
66 |
* ClassSymbol which contains in its scope symbol representations |
|
67 |
* for all other definitions in the classfile. Top-level Classes themselves |
|
68 |
* appear as members of the scopes of PackageSymbols. |
|
69 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
70 |
* <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
|
71 |
* If you write code that depends on this, you do so at your own risk. |
10 | 72 |
* This code and its internal interfaces are subject to change or |
73 |
* deletion without notice.</b> |
|
74 |
*/ |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
75 |
public class ClassReader implements Completer { |
10 | 76 |
/** The context key for the class reader. */ |
77 |
protected static final Context.Key<ClassReader> classReaderKey = |
|
78 |
new Context.Key<ClassReader>(); |
|
79 |
||
7842 | 80 |
public static final int INITIAL_BUFFER_SIZE = 0x0fff0; |
81 |
||
10 | 82 |
Annotate annotate; |
83 |
||
84 |
/** Switch: verbose output. |
|
85 |
*/ |
|
86 |
boolean verbose; |
|
87 |
||
88 |
/** Switch: check class file for correct minor version, unrecognized |
|
89 |
* attributes. |
|
90 |
*/ |
|
91 |
boolean checkClassFile; |
|
92 |
||
93 |
/** Switch: read constant pool and code sections. This switch is initially |
|
94 |
* set to false but can be turned on from outside. |
|
95 |
*/ |
|
96 |
public boolean readAllOfClassFile = false; |
|
97 |
||
98 |
/** Switch: read GJ signature information. |
|
99 |
*/ |
|
100 |
boolean allowGenerics; |
|
101 |
||
102 |
/** Switch: read varargs attribute. |
|
103 |
*/ |
|
104 |
boolean allowVarargs; |
|
105 |
||
106 |
/** Switch: allow annotations. |
|
107 |
*/ |
|
108 |
boolean allowAnnotations; |
|
109 |
||
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
110 |
/** Switch: allow simplified varargs. |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
111 |
*/ |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
112 |
boolean allowSimplifiedVarargs; |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
113 |
|
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
114 |
/** Lint option: warn about classfile issues |
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
115 |
*/ |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
116 |
boolean lintClassfile; |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
117 |
|
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
118 |
|
10 | 119 |
/** Switch: preserve parameter names from the variable table. |
120 |
*/ |
|
121 |
public boolean saveParameterNames; |
|
122 |
||
123 |
/** |
|
124 |
* Switch: cache completion failures unless -XDdev is used |
|
125 |
*/ |
|
126 |
private boolean cacheCompletionFailure; |
|
127 |
||
128 |
/** |
|
129 |
* Switch: prefer source files instead of newer when both source |
|
130 |
* and class are available |
|
131 |
**/ |
|
132 |
public boolean preferSource; |
|
133 |
||
134 |
/** The log to use for verbose output |
|
135 |
*/ |
|
136 |
final Log log; |
|
137 |
||
138 |
/** The symbol table. */ |
|
139 |
Symtab syms; |
|
140 |
||
141 |
Types types; |
|
142 |
||
143 |
/** The name table. */ |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
939
diff
changeset
|
144 |
final Names names; |
10 | 145 |
|
146 |
/** Force a completion failure on this name |
|
147 |
*/ |
|
148 |
final Name completionFailureName; |
|
149 |
||
150 |
/** Access to files |
|
151 |
*/ |
|
152 |
private final JavaFileManager fileManager; |
|
153 |
||
169 | 154 |
/** Factory for diagnostics |
155 |
*/ |
|
156 |
JCDiagnostic.Factory diagFactory; |
|
157 |
||
10 | 158 |
/** Can be reassigned from outside: |
159 |
* the completer to be used for ".java" files. If this remains unassigned |
|
160 |
* ".java" files will not be loaded. |
|
161 |
*/ |
|
162 |
public SourceCompleter sourceCompleter = null; |
|
163 |
||
164 |
/** A hashtable containing the encountered top-level and member classes, |
|
165 |
* indexed by flat names. The table does not contain local classes. |
|
166 |
*/ |
|
167 |
private Map<Name,ClassSymbol> classes; |
|
168 |
||
169 |
/** A hashtable containing the encountered packages. |
|
170 |
*/ |
|
171 |
private Map<Name, PackageSymbol> packages; |
|
172 |
||
173 |
/** The current scope where type variables are entered. |
|
174 |
*/ |
|
175 |
protected Scope typevars; |
|
176 |
||
177 |
/** The path name of the class file currently being read. |
|
178 |
*/ |
|
179 |
protected JavaFileObject currentClassFile = null; |
|
180 |
||
181 |
/** The class or method currently being read. |
|
182 |
*/ |
|
183 |
protected Symbol currentOwner = null; |
|
184 |
||
185 |
/** The buffer containing the currently read class file. |
|
186 |
*/ |
|
7842 | 187 |
byte[] buf = new byte[INITIAL_BUFFER_SIZE]; |
10 | 188 |
|
189 |
/** The current input pointer. |
|
190 |
*/ |
|
191 |
int bp; |
|
192 |
||
193 |
/** The objects of the constant pool. |
|
194 |
*/ |
|
195 |
Object[] poolObj; |
|
196 |
||
197 |
/** For every constant pool entry, an index into buf where the |
|
198 |
* defining section of the entry is found. |
|
199 |
*/ |
|
200 |
int[] poolIdx; |
|
201 |
||
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
202 |
/** The major version number of the class file being read. */ |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
203 |
int majorVersion; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
204 |
/** The minor version number of the class file being read. */ |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
205 |
int minorVersion; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
206 |
|
4077
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
207 |
/** A table to hold the constant pool indices for method parameter |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
208 |
* names, as given in LocalVariableTable attributes. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
209 |
*/ |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
210 |
int[] parameterNameIndices; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
211 |
|
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
212 |
/** |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
213 |
* Whether or not any parameter names have been found. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
214 |
*/ |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
215 |
boolean haveParameterNameIndices; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
216 |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
217 |
/** |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
218 |
* The set of attribute names for which warnings have been generated for the current class |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
219 |
*/ |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
220 |
Set<Name> warnedAttrs = new HashSet<Name>(); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
221 |
|
10 | 222 |
/** Get the ClassReader instance for this invocation. */ |
223 |
public static ClassReader instance(Context context) { |
|
224 |
ClassReader instance = context.get(classReaderKey); |
|
225 |
if (instance == null) |
|
226 |
instance = new ClassReader(context, true); |
|
227 |
return instance; |
|
228 |
} |
|
229 |
||
230 |
/** Initialize classes and packages, treating this as the definitive classreader. */ |
|
231 |
public void init(Symtab syms) { |
|
232 |
init(syms, true); |
|
233 |
} |
|
234 |
||
235 |
/** Initialize classes and packages, optionally treating this as |
|
236 |
* the definitive classreader. |
|
237 |
*/ |
|
238 |
private void init(Symtab syms, boolean definitive) { |
|
239 |
if (classes != null) return; |
|
240 |
||
241 |
if (definitive) { |
|
8032 | 242 |
Assert.check(packages == null || packages == syms.packages); |
10 | 243 |
packages = syms.packages; |
8032 | 244 |
Assert.check(classes == null || classes == syms.classes); |
10 | 245 |
classes = syms.classes; |
246 |
} else { |
|
247 |
packages = new HashMap<Name, PackageSymbol>(); |
|
248 |
classes = new HashMap<Name, ClassSymbol>(); |
|
249 |
} |
|
250 |
||
251 |
packages.put(names.empty, syms.rootPackage); |
|
252 |
syms.rootPackage.completer = this; |
|
253 |
syms.unnamedPackage.completer = this; |
|
254 |
} |
|
255 |
||
256 |
/** Construct a new class reader, optionally treated as the |
|
257 |
* definitive classreader for this invocation. |
|
258 |
*/ |
|
259 |
protected ClassReader(Context context, boolean definitive) { |
|
260 |
if (definitive) context.put(classReaderKey, this); |
|
261 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
939
diff
changeset
|
262 |
names = Names.instance(context); |
10 | 263 |
syms = Symtab.instance(context); |
264 |
types = Types.instance(context); |
|
265 |
fileManager = context.get(JavaFileManager.class); |
|
266 |
if (fileManager == null) |
|
267 |
throw new AssertionError("FileManager initialization error"); |
|
169 | 268 |
diagFactory = JCDiagnostic.Factory.instance(context); |
10 | 269 |
|
270 |
init(syms, definitive); |
|
271 |
log = Log.instance(context); |
|
272 |
||
273 |
Options options = Options.instance(context); |
|
274 |
annotate = Annotate.instance(context); |
|
6721 | 275 |
verbose = options.isSet(VERBOSE); |
276 |
checkClassFile = options.isSet("-checkclassfile"); |
|
10 | 277 |
Source source = Source.instance(context); |
278 |
allowGenerics = source.allowGenerics(); |
|
279 |
allowVarargs = source.allowVarargs(); |
|
280 |
allowAnnotations = source.allowAnnotations(); |
|
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
281 |
allowSimplifiedVarargs = source.allowSimplifiedVarargs(); |
6721 | 282 |
saveParameterNames = options.isSet("save-parameter-names"); |
283 |
cacheCompletionFailure = options.isUnset("dev"); |
|
10 | 284 |
preferSource = "source".equals(options.get("-Xprefer")); |
285 |
||
286 |
completionFailureName = |
|
6721 | 287 |
options.isSet("failcomplete") |
10 | 288 |
? names.fromString(options.get("failcomplete")) |
289 |
: null; |
|
290 |
||
291 |
typevars = new Scope(syms.noSymbol); |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
292 |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
293 |
lintClassfile = Lint.instance(context).isEnabled(LintCategory.CLASSFILE); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
294 |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
295 |
initAttributeReaders(); |
10 | 296 |
} |
297 |
||
298 |
/** Add member to class unless it is synthetic. |
|
299 |
*/ |
|
300 |
private void enterMember(ClassSymbol c, Symbol sym) { |
|
301 |
if ((sym.flags_field & (SYNTHETIC|BRIDGE)) != SYNTHETIC) |
|
302 |
c.members_field.enter(sym); |
|
303 |
} |
|
304 |
||
305 |
/************************************************************************ |
|
306 |
* Error Diagnoses |
|
307 |
***********************************************************************/ |
|
308 |
||
169 | 309 |
|
310 |
public class BadClassFile extends CompletionFailure { |
|
10 | 311 |
private static final long serialVersionUID = 0; |
312 |
||
169 | 313 |
public BadClassFile(TypeSymbol sym, JavaFileObject file, JCDiagnostic diag) { |
314 |
super(sym, createBadClassFileDiagnostic(file, diag)); |
|
10 | 315 |
} |
316 |
} |
|
169 | 317 |
// where |
318 |
private JCDiagnostic createBadClassFileDiagnostic(JavaFileObject file, JCDiagnostic diag) { |
|
319 |
String key = (file.getKind() == JavaFileObject.Kind.SOURCE |
|
320 |
? "bad.source.file.header" : "bad.class.file.header"); |
|
321 |
return diagFactory.fragment(key, file, diag); |
|
322 |
} |
|
10 | 323 |
|
324 |
public BadClassFile badClassFile(String key, Object... args) { |
|
325 |
return new BadClassFile ( |
|
326 |
currentOwner.enclClass(), |
|
327 |
currentClassFile, |
|
169 | 328 |
diagFactory.fragment(key, args)); |
10 | 329 |
} |
330 |
||
331 |
/************************************************************************ |
|
332 |
* Buffer Access |
|
333 |
***********************************************************************/ |
|
334 |
||
335 |
/** Read a character. |
|
336 |
*/ |
|
337 |
char nextChar() { |
|
338 |
return (char)(((buf[bp++] & 0xFF) << 8) + (buf[bp++] & 0xFF)); |
|
339 |
} |
|
340 |
||
3149 | 341 |
/** Read a byte. |
342 |
*/ |
|
343 |
byte nextByte() { |
|
344 |
return buf[bp++]; |
|
345 |
} |
|
346 |
||
10 | 347 |
/** Read an integer. |
348 |
*/ |
|
349 |
int nextInt() { |
|
350 |
return |
|
351 |
((buf[bp++] & 0xFF) << 24) + |
|
352 |
((buf[bp++] & 0xFF) << 16) + |
|
353 |
((buf[bp++] & 0xFF) << 8) + |
|
354 |
(buf[bp++] & 0xFF); |
|
355 |
} |
|
356 |
||
357 |
/** Extract a character at position bp from buf. |
|
358 |
*/ |
|
359 |
char getChar(int bp) { |
|
360 |
return |
|
361 |
(char)(((buf[bp] & 0xFF) << 8) + (buf[bp+1] & 0xFF)); |
|
362 |
} |
|
363 |
||
364 |
/** Extract an integer at position bp from buf. |
|
365 |
*/ |
|
366 |
int getInt(int bp) { |
|
367 |
return |
|
368 |
((buf[bp] & 0xFF) << 24) + |
|
369 |
((buf[bp+1] & 0xFF) << 16) + |
|
370 |
((buf[bp+2] & 0xFF) << 8) + |
|
371 |
(buf[bp+3] & 0xFF); |
|
372 |
} |
|
373 |
||
374 |
||
375 |
/** Extract a long integer at position bp from buf. |
|
376 |
*/ |
|
377 |
long getLong(int bp) { |
|
378 |
DataInputStream bufin = |
|
379 |
new DataInputStream(new ByteArrayInputStream(buf, bp, 8)); |
|
380 |
try { |
|
381 |
return bufin.readLong(); |
|
382 |
} catch (IOException e) { |
|
383 |
throw new AssertionError(e); |
|
384 |
} |
|
385 |
} |
|
386 |
||
387 |
/** Extract a float at position bp from buf. |
|
388 |
*/ |
|
389 |
float getFloat(int bp) { |
|
390 |
DataInputStream bufin = |
|
391 |
new DataInputStream(new ByteArrayInputStream(buf, bp, 4)); |
|
392 |
try { |
|
393 |
return bufin.readFloat(); |
|
394 |
} catch (IOException e) { |
|
395 |
throw new AssertionError(e); |
|
396 |
} |
|
397 |
} |
|
398 |
||
399 |
/** Extract a double at position bp from buf. |
|
400 |
*/ |
|
401 |
double getDouble(int bp) { |
|
402 |
DataInputStream bufin = |
|
403 |
new DataInputStream(new ByteArrayInputStream(buf, bp, 8)); |
|
404 |
try { |
|
405 |
return bufin.readDouble(); |
|
406 |
} catch (IOException e) { |
|
407 |
throw new AssertionError(e); |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
/************************************************************************ |
|
412 |
* Constant Pool Access |
|
413 |
***********************************************************************/ |
|
414 |
||
415 |
/** Index all constant pool entries, writing their start addresses into |
|
416 |
* poolIdx. |
|
417 |
*/ |
|
418 |
void indexPool() { |
|
419 |
poolIdx = new int[nextChar()]; |
|
420 |
poolObj = new Object[poolIdx.length]; |
|
421 |
int i = 1; |
|
422 |
while (i < poolIdx.length) { |
|
423 |
poolIdx[i++] = bp; |
|
424 |
byte tag = buf[bp++]; |
|
425 |
switch (tag) { |
|
426 |
case CONSTANT_Utf8: case CONSTANT_Unicode: { |
|
427 |
int len = nextChar(); |
|
428 |
bp = bp + len; |
|
429 |
break; |
|
430 |
} |
|
431 |
case CONSTANT_Class: |
|
432 |
case CONSTANT_String: |
|
8042 | 433 |
case CONSTANT_MethodType: |
10 | 434 |
bp = bp + 2; |
435 |
break; |
|
8042 | 436 |
case CONSTANT_MethodHandle: |
437 |
bp = bp + 3; |
|
438 |
break; |
|
10 | 439 |
case CONSTANT_Fieldref: |
440 |
case CONSTANT_Methodref: |
|
441 |
case CONSTANT_InterfaceMethodref: |
|
442 |
case CONSTANT_NameandType: |
|
443 |
case CONSTANT_Integer: |
|
444 |
case CONSTANT_Float: |
|
8042 | 445 |
case CONSTANT_InvokeDynamic: |
10 | 446 |
bp = bp + 4; |
447 |
break; |
|
448 |
case CONSTANT_Long: |
|
449 |
case CONSTANT_Double: |
|
450 |
bp = bp + 8; |
|
451 |
i++; |
|
452 |
break; |
|
453 |
default: |
|
454 |
throw badClassFile("bad.const.pool.tag.at", |
|
455 |
Byte.toString(tag), |
|
456 |
Integer.toString(bp -1)); |
|
457 |
} |
|
458 |
} |
|
459 |
} |
|
460 |
||
461 |
/** Read constant pool entry at start address i, use pool as a cache. |
|
462 |
*/ |
|
463 |
Object readPool(int i) { |
|
464 |
Object result = poolObj[i]; |
|
465 |
if (result != null) return result; |
|
466 |
||
467 |
int index = poolIdx[i]; |
|
468 |
if (index == 0) return null; |
|
469 |
||
470 |
byte tag = buf[index]; |
|
471 |
switch (tag) { |
|
472 |
case CONSTANT_Utf8: |
|
473 |
poolObj[i] = names.fromUtf(buf, index + 3, getChar(index + 1)); |
|
474 |
break; |
|
475 |
case CONSTANT_Unicode: |
|
476 |
throw badClassFile("unicode.str.not.supported"); |
|
477 |
case CONSTANT_Class: |
|
478 |
poolObj[i] = readClassOrType(getChar(index + 1)); |
|
479 |
break; |
|
480 |
case CONSTANT_String: |
|
481 |
// FIXME: (footprint) do not use toString here |
|
482 |
poolObj[i] = readName(getChar(index + 1)).toString(); |
|
483 |
break; |
|
484 |
case CONSTANT_Fieldref: { |
|
485 |
ClassSymbol owner = readClassSymbol(getChar(index + 1)); |
|
486 |
NameAndType nt = (NameAndType)readPool(getChar(index + 3)); |
|
487 |
poolObj[i] = new VarSymbol(0, nt.name, nt.type, owner); |
|
488 |
break; |
|
489 |
} |
|
490 |
case CONSTANT_Methodref: |
|
491 |
case CONSTANT_InterfaceMethodref: { |
|
492 |
ClassSymbol owner = readClassSymbol(getChar(index + 1)); |
|
493 |
NameAndType nt = (NameAndType)readPool(getChar(index + 3)); |
|
494 |
poolObj[i] = new MethodSymbol(0, nt.name, nt.type, owner); |
|
495 |
break; |
|
496 |
} |
|
497 |
case CONSTANT_NameandType: |
|
498 |
poolObj[i] = new NameAndType( |
|
499 |
readName(getChar(index + 1)), |
|
500 |
readType(getChar(index + 3))); |
|
501 |
break; |
|
502 |
case CONSTANT_Integer: |
|
503 |
poolObj[i] = getInt(index + 1); |
|
504 |
break; |
|
505 |
case CONSTANT_Float: |
|
506 |
poolObj[i] = new Float(getFloat(index + 1)); |
|
507 |
break; |
|
508 |
case CONSTANT_Long: |
|
509 |
poolObj[i] = new Long(getLong(index + 1)); |
|
510 |
break; |
|
511 |
case CONSTANT_Double: |
|
512 |
poolObj[i] = new Double(getDouble(index + 1)); |
|
513 |
break; |
|
8042 | 514 |
case CONSTANT_MethodHandle: |
515 |
skipBytes(4); |
|
516 |
break; |
|
517 |
case CONSTANT_MethodType: |
|
518 |
skipBytes(3); |
|
519 |
break; |
|
520 |
case CONSTANT_InvokeDynamic: |
|
521 |
skipBytes(5); |
|
522 |
break; |
|
10 | 523 |
default: |
524 |
throw badClassFile("bad.const.pool.tag", Byte.toString(tag)); |
|
525 |
} |
|
526 |
return poolObj[i]; |
|
527 |
} |
|
528 |
||
529 |
/** Read signature and convert to type. |
|
530 |
*/ |
|
531 |
Type readType(int i) { |
|
532 |
int index = poolIdx[i]; |
|
533 |
return sigToType(buf, index + 3, getChar(index + 1)); |
|
534 |
} |
|
535 |
||
536 |
/** If name is an array type or class signature, return the |
|
537 |
* corresponding type; otherwise return a ClassSymbol with given name. |
|
538 |
*/ |
|
539 |
Object readClassOrType(int i) { |
|
540 |
int index = poolIdx[i]; |
|
541 |
int len = getChar(index + 1); |
|
542 |
int start = index + 3; |
|
8032 | 543 |
Assert.check(buf[start] == '[' || buf[start + len - 1] != ';'); |
10 | 544 |
// by the above assertion, the following test can be |
545 |
// simplified to (buf[start] == '[') |
|
546 |
return (buf[start] == '[' || buf[start + len - 1] == ';') |
|
547 |
? (Object)sigToType(buf, start, len) |
|
548 |
: (Object)enterClass(names.fromUtf(internalize(buf, start, |
|
549 |
len))); |
|
550 |
} |
|
551 |
||
552 |
/** Read signature and convert to type parameters. |
|
553 |
*/ |
|
554 |
List<Type> readTypeParams(int i) { |
|
555 |
int index = poolIdx[i]; |
|
556 |
return sigToTypeParams(buf, index + 3, getChar(index + 1)); |
|
557 |
} |
|
558 |
||
559 |
/** Read class entry. |
|
560 |
*/ |
|
561 |
ClassSymbol readClassSymbol(int i) { |
|
562 |
return (ClassSymbol) (readPool(i)); |
|
563 |
} |
|
564 |
||
565 |
/** Read name. |
|
566 |
*/ |
|
567 |
Name readName(int i) { |
|
568 |
return (Name) (readPool(i)); |
|
569 |
} |
|
570 |
||
571 |
/************************************************************************ |
|
572 |
* Reading Types |
|
573 |
***********************************************************************/ |
|
574 |
||
575 |
/** The unread portion of the currently read type is |
|
576 |
* signature[sigp..siglimit-1]. |
|
577 |
*/ |
|
578 |
byte[] signature; |
|
579 |
int sigp; |
|
580 |
int siglimit; |
|
581 |
boolean sigEnterPhase = false; |
|
582 |
||
583 |
/** Convert signature to type, where signature is a byte array segment. |
|
584 |
*/ |
|
585 |
Type sigToType(byte[] sig, int offset, int len) { |
|
586 |
signature = sig; |
|
587 |
sigp = offset; |
|
588 |
siglimit = offset + len; |
|
589 |
return sigToType(); |
|
590 |
} |
|
591 |
||
592 |
/** Convert signature to type, where signature is implicit. |
|
593 |
*/ |
|
594 |
Type sigToType() { |
|
595 |
switch ((char) signature[sigp]) { |
|
596 |
case 'T': |
|
597 |
sigp++; |
|
598 |
int start = sigp; |
|
599 |
while (signature[sigp] != ';') sigp++; |
|
600 |
sigp++; |
|
601 |
return sigEnterPhase |
|
602 |
? Type.noType |
|
603 |
: findTypeVar(names.fromUtf(signature, start, sigp - 1 - start)); |
|
604 |
case '+': { |
|
605 |
sigp++; |
|
606 |
Type t = sigToType(); |
|
607 |
return new WildcardType(t, BoundKind.EXTENDS, |
|
608 |
syms.boundClass); |
|
609 |
} |
|
610 |
case '*': |
|
611 |
sigp++; |
|
612 |
return new WildcardType(syms.objectType, BoundKind.UNBOUND, |
|
613 |
syms.boundClass); |
|
614 |
case '-': { |
|
615 |
sigp++; |
|
616 |
Type t = sigToType(); |
|
617 |
return new WildcardType(t, BoundKind.SUPER, |
|
618 |
syms.boundClass); |
|
619 |
} |
|
620 |
case 'B': |
|
621 |
sigp++; |
|
622 |
return syms.byteType; |
|
623 |
case 'C': |
|
624 |
sigp++; |
|
625 |
return syms.charType; |
|
626 |
case 'D': |
|
627 |
sigp++; |
|
628 |
return syms.doubleType; |
|
629 |
case 'F': |
|
630 |
sigp++; |
|
631 |
return syms.floatType; |
|
632 |
case 'I': |
|
633 |
sigp++; |
|
634 |
return syms.intType; |
|
635 |
case 'J': |
|
636 |
sigp++; |
|
637 |
return syms.longType; |
|
638 |
case 'L': |
|
639 |
{ |
|
640 |
// int oldsigp = sigp; |
|
641 |
Type t = classSigToType(); |
|
642 |
if (sigp < siglimit && signature[sigp] == '.') |
|
643 |
throw badClassFile("deprecated inner class signature syntax " + |
|
644 |
"(please recompile from source)"); |
|
645 |
/* |
|
646 |
System.err.println(" decoded " + |
|
647 |
new String(signature, oldsigp, sigp-oldsigp) + |
|
648 |
" => " + t + " outer " + t.outer()); |
|
649 |
*/ |
|
650 |
return t; |
|
651 |
} |
|
652 |
case 'S': |
|
653 |
sigp++; |
|
654 |
return syms.shortType; |
|
655 |
case 'V': |
|
656 |
sigp++; |
|
657 |
return syms.voidType; |
|
658 |
case 'Z': |
|
659 |
sigp++; |
|
660 |
return syms.booleanType; |
|
661 |
case '[': |
|
662 |
sigp++; |
|
663 |
return new ArrayType(sigToType(), syms.arrayClass); |
|
664 |
case '(': |
|
665 |
sigp++; |
|
666 |
List<Type> argtypes = sigToTypes(')'); |
|
667 |
Type restype = sigToType(); |
|
668 |
List<Type> thrown = List.nil(); |
|
669 |
while (signature[sigp] == '^') { |
|
670 |
sigp++; |
|
671 |
thrown = thrown.prepend(sigToType()); |
|
672 |
} |
|
673 |
return new MethodType(argtypes, |
|
674 |
restype, |
|
675 |
thrown.reverse(), |
|
676 |
syms.methodClass); |
|
677 |
case '<': |
|
678 |
typevars = typevars.dup(currentOwner); |
|
679 |
Type poly = new ForAll(sigToTypeParams(), sigToType()); |
|
680 |
typevars = typevars.leave(); |
|
681 |
return poly; |
|
682 |
default: |
|
683 |
throw badClassFile("bad.signature", |
|
684 |
Convert.utf2string(signature, sigp, 10)); |
|
685 |
} |
|
686 |
} |
|
687 |
||
688 |
byte[] signatureBuffer = new byte[0]; |
|
689 |
int sbp = 0; |
|
690 |
/** Convert class signature to type, where signature is implicit. |
|
691 |
*/ |
|
692 |
Type classSigToType() { |
|
693 |
if (signature[sigp] != 'L') |
|
694 |
throw badClassFile("bad.class.signature", |
|
695 |
Convert.utf2string(signature, sigp, 10)); |
|
696 |
sigp++; |
|
697 |
Type outer = Type.noType; |
|
698 |
int startSbp = sbp; |
|
699 |
||
700 |
while (true) { |
|
701 |
final byte c = signature[sigp++]; |
|
702 |
switch (c) { |
|
703 |
||
704 |
case ';': { // end |
|
705 |
ClassSymbol t = enterClass(names.fromUtf(signatureBuffer, |
|
706 |
startSbp, |
|
707 |
sbp - startSbp)); |
|
708 |
if (outer == Type.noType) |
|
709 |
outer = t.erasure(types); |
|
710 |
else |
|
711 |
outer = new ClassType(outer, List.<Type>nil(), t); |
|
712 |
sbp = startSbp; |
|
713 |
return outer; |
|
714 |
} |
|
715 |
||
716 |
case '<': // generic arguments |
|
717 |
ClassSymbol t = enterClass(names.fromUtf(signatureBuffer, |
|
718 |
startSbp, |
|
719 |
sbp - startSbp)); |
|
720 |
outer = new ClassType(outer, sigToTypes('>'), t) { |
|
721 |
boolean completed = false; |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
722 |
@Override |
10 | 723 |
public Type getEnclosingType() { |
724 |
if (!completed) { |
|
725 |
completed = true; |
|
726 |
tsym.complete(); |
|
727 |
Type enclosingType = tsym.type.getEnclosingType(); |
|
728 |
if (enclosingType != Type.noType) { |
|
729 |
List<Type> typeArgs = |
|
730 |
super.getEnclosingType().allparams(); |
|
731 |
List<Type> typeParams = |
|
732 |
enclosingType.allparams(); |
|
733 |
if (typeParams.length() != typeArgs.length()) { |
|
734 |
// no "rare" types |
|
735 |
super.setEnclosingType(types.erasure(enclosingType)); |
|
736 |
} else { |
|
737 |
super.setEnclosingType(types.subst(enclosingType, |
|
738 |
typeParams, |
|
739 |
typeArgs)); |
|
740 |
} |
|
741 |
} else { |
|
742 |
super.setEnclosingType(Type.noType); |
|
743 |
} |
|
744 |
} |
|
745 |
return super.getEnclosingType(); |
|
746 |
} |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
747 |
@Override |
10 | 748 |
public void setEnclosingType(Type outer) { |
749 |
throw new UnsupportedOperationException(); |
|
750 |
} |
|
751 |
}; |
|
752 |
switch (signature[sigp++]) { |
|
753 |
case ';': |
|
754 |
if (sigp < signature.length && signature[sigp] == '.') { |
|
755 |
// support old-style GJC signatures |
|
756 |
// The signature produced was |
|
757 |
// Lfoo/Outer<Lfoo/X;>;.Lfoo/Outer$Inner<Lfoo/Y;>; |
|
758 |
// rather than say |
|
759 |
// Lfoo/Outer<Lfoo/X;>.Inner<Lfoo/Y;>; |
|
760 |
// so we skip past ".Lfoo/Outer$" |
|
761 |
sigp += (sbp - startSbp) + // "foo/Outer" |
|
762 |
3; // ".L" and "$" |
|
763 |
signatureBuffer[sbp++] = (byte)'$'; |
|
764 |
break; |
|
765 |
} else { |
|
766 |
sbp = startSbp; |
|
767 |
return outer; |
|
768 |
} |
|
769 |
case '.': |
|
770 |
signatureBuffer[sbp++] = (byte)'$'; |
|
771 |
break; |
|
772 |
default: |
|
773 |
throw new AssertionError(signature[sigp-1]); |
|
774 |
} |
|
775 |
continue; |
|
776 |
||
777 |
case '.': |
|
778 |
signatureBuffer[sbp++] = (byte)'$'; |
|
779 |
continue; |
|
780 |
case '/': |
|
781 |
signatureBuffer[sbp++] = (byte)'.'; |
|
782 |
continue; |
|
783 |
default: |
|
784 |
signatureBuffer[sbp++] = c; |
|
785 |
continue; |
|
786 |
} |
|
787 |
} |
|
788 |
} |
|
789 |
||
790 |
/** Convert (implicit) signature to list of types |
|
791 |
* until `terminator' is encountered. |
|
792 |
*/ |
|
793 |
List<Type> sigToTypes(char terminator) { |
|
794 |
List<Type> head = List.of(null); |
|
795 |
List<Type> tail = head; |
|
796 |
while (signature[sigp] != terminator) |
|
797 |
tail = tail.setTail(List.of(sigToType())); |
|
798 |
sigp++; |
|
799 |
return head.tail; |
|
800 |
} |
|
801 |
||
802 |
/** Convert signature to type parameters, where signature is a byte |
|
803 |
* array segment. |
|
804 |
*/ |
|
805 |
List<Type> sigToTypeParams(byte[] sig, int offset, int len) { |
|
806 |
signature = sig; |
|
807 |
sigp = offset; |
|
808 |
siglimit = offset + len; |
|
809 |
return sigToTypeParams(); |
|
810 |
} |
|
811 |
||
812 |
/** Convert signature to type parameters, where signature is implicit. |
|
813 |
*/ |
|
814 |
List<Type> sigToTypeParams() { |
|
815 |
List<Type> tvars = List.nil(); |
|
816 |
if (signature[sigp] == '<') { |
|
817 |
sigp++; |
|
818 |
int start = sigp; |
|
819 |
sigEnterPhase = true; |
|
820 |
while (signature[sigp] != '>') |
|
821 |
tvars = tvars.prepend(sigToTypeParam()); |
|
822 |
sigEnterPhase = false; |
|
823 |
sigp = start; |
|
824 |
while (signature[sigp] != '>') |
|
825 |
sigToTypeParam(); |
|
826 |
sigp++; |
|
827 |
} |
|
828 |
return tvars.reverse(); |
|
829 |
} |
|
830 |
||
831 |
/** Convert (implicit) signature to type parameter. |
|
832 |
*/ |
|
833 |
Type sigToTypeParam() { |
|
834 |
int start = sigp; |
|
835 |
while (signature[sigp] != ':') sigp++; |
|
836 |
Name name = names.fromUtf(signature, start, sigp - start); |
|
837 |
TypeVar tvar; |
|
838 |
if (sigEnterPhase) { |
|
839 |
tvar = new TypeVar(name, currentOwner, syms.botType); |
|
840 |
typevars.enter(tvar.tsym); |
|
841 |
} else { |
|
842 |
tvar = (TypeVar)findTypeVar(name); |
|
843 |
} |
|
844 |
List<Type> bounds = List.nil(); |
|
845 |
Type st = null; |
|
846 |
if (signature[sigp] == ':' && signature[sigp+1] == ':') { |
|
847 |
sigp++; |
|
848 |
st = syms.objectType; |
|
849 |
} |
|
850 |
while (signature[sigp] == ':') { |
|
851 |
sigp++; |
|
852 |
bounds = bounds.prepend(sigToType()); |
|
853 |
} |
|
854 |
if (!sigEnterPhase) { |
|
855 |
types.setBounds(tvar, bounds.reverse(), st); |
|
856 |
} |
|
857 |
return tvar; |
|
858 |
} |
|
859 |
||
860 |
/** Find type variable with given name in `typevars' scope. |
|
861 |
*/ |
|
862 |
Type findTypeVar(Name name) { |
|
863 |
Scope.Entry e = typevars.lookup(name); |
|
864 |
if (e.scope != null) { |
|
865 |
return e.sym.type; |
|
866 |
} else { |
|
867 |
if (readingClassAttr) { |
|
868 |
// While reading the class attribute, the supertypes |
|
869 |
// might refer to a type variable from an enclosing element |
|
870 |
// (method or class). |
|
871 |
// If the type variable is defined in the enclosing class, |
|
872 |
// we can actually find it in |
|
873 |
// currentOwner.owner.type.getTypeArguments() |
|
874 |
// However, until we have read the enclosing method attribute |
|
875 |
// we don't know for sure if this owner is correct. It could |
|
876 |
// be a method and there is no way to tell before reading the |
|
877 |
// enclosing method attribute. |
|
878 |
TypeVar t = new TypeVar(name, currentOwner, syms.botType); |
|
879 |
missingTypeVariables = missingTypeVariables.prepend(t); |
|
880 |
// System.err.println("Missing type var " + name); |
|
881 |
return t; |
|
882 |
} |
|
883 |
throw badClassFile("undecl.type.var", name); |
|
884 |
} |
|
885 |
} |
|
886 |
||
887 |
/************************************************************************ |
|
888 |
* Reading Attributes |
|
889 |
***********************************************************************/ |
|
890 |
||
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
891 |
protected enum AttributeKind { CLASS, MEMBER }; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
892 |
protected abstract class AttributeReader { |
7632
7fa75ac531bb
7005856: avoid name clash for langtools when building on MacOS
jjg
parents:
7624
diff
changeset
|
893 |
AttributeReader(Name name, ClassFile.Version version, Set<AttributeKind> kinds) { |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
894 |
this.name = name; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
895 |
this.version = version; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
896 |
this.kinds = kinds; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
897 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
898 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
899 |
boolean accepts(AttributeKind kind) { |
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
900 |
if (kinds.contains(kind)) { |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
901 |
if (majorVersion > version.major || (majorVersion == version.major && minorVersion >= version.minor)) |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
902 |
return true; |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
903 |
|
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
904 |
if (lintClassfile && !warnedAttrs.contains(name)) { |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
905 |
JavaFileObject prev = log.useSource(currentClassFile); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
906 |
try { |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
907 |
log.warning(LintCategory.CLASSFILE, (DiagnosticPosition) null, "future.attr", |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
908 |
name, version.major, version.minor, majorVersion, minorVersion); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
909 |
} finally { |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
910 |
log.useSource(prev); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
911 |
} |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
912 |
warnedAttrs.add(name); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
913 |
} |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
914 |
} |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
915 |
return false; |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
916 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
917 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
918 |
abstract void read(Symbol sym, int attrLen); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
919 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
920 |
final Name name; |
7632
7fa75ac531bb
7005856: avoid name clash for langtools when building on MacOS
jjg
parents:
7624
diff
changeset
|
921 |
final ClassFile.Version version; |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
922 |
final Set<AttributeKind> kinds; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
923 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
924 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
925 |
protected Set<AttributeKind> CLASS_ATTRIBUTE = |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
926 |
EnumSet.of(AttributeKind.CLASS); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
927 |
protected Set<AttributeKind> MEMBER_ATTRIBUTE = |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
928 |
EnumSet.of(AttributeKind.MEMBER); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
929 |
protected Set<AttributeKind> CLASS_OR_MEMBER_ATTRIBUTE = |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
930 |
EnumSet.of(AttributeKind.CLASS, AttributeKind.MEMBER); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
931 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
932 |
protected Map<Name, AttributeReader> attributeReaders = new HashMap<Name, AttributeReader>(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
933 |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
934 |
private void initAttributeReaders() { |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
935 |
AttributeReader[] readers = { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
936 |
// v45.3 attributes |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
937 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
938 |
new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
939 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
940 |
if (readAllOfClassFile || saveParameterNames) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
941 |
((MethodSymbol)sym).code = readCode(sym); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
942 |
else |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
943 |
bp = bp + attrLen; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
944 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
945 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
946 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
947 |
new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
948 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
949 |
Object v = readPool(nextChar()); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
950 |
// Ignore ConstantValue attribute if field not final. |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
951 |
if ((sym.flags() & FINAL) != 0) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
952 |
((VarSymbol) sym).setData(v); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
953 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
954 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
955 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
956 |
new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
957 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
958 |
sym.flags_field |= DEPRECATED; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
959 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
960 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
961 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
962 |
new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
963 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
964 |
int nexceptions = nextChar(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
965 |
List<Type> thrown = List.nil(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
966 |
for (int j = 0; j < nexceptions; j++) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
967 |
thrown = thrown.prepend(readClassSymbol(nextChar()).type); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
968 |
if (sym.type.getThrownTypes().isEmpty()) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
969 |
sym.type.asMethodType().thrown = thrown.reverse(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
970 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
971 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
972 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
973 |
new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
974 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
975 |
ClassSymbol c = (ClassSymbol) sym; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
976 |
readInnerClasses(c); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
977 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
978 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
979 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
980 |
new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
981 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
982 |
int newbp = bp + attrLen; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
983 |
if (saveParameterNames) { |
4077
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
984 |
// Pick up parameter names from the variable table. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
985 |
// Parameter names are not explicitly identified as such, |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
986 |
// but all parameter name entries in the LocalVariableTable |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
987 |
// have a start_pc of 0. Therefore, we record the name |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
988 |
// indicies of all slots with a start_pc of zero in the |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
989 |
// parameterNameIndicies array. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
990 |
// Note that this implicitly honors the JVMS spec that |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
991 |
// there may be more than one LocalVariableTable, and that |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
992 |
// there is no specified ordering for the entries. |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
993 |
int numEntries = nextChar(); |
4077
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
994 |
for (int i = 0; i < numEntries; i++) { |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
995 |
int start_pc = nextChar(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
996 |
int length = nextChar(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
997 |
int nameIndex = nextChar(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
998 |
int sigIndex = nextChar(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
999 |
int register = nextChar(); |
4077
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1000 |
if (start_pc == 0) { |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1001 |
// ensure array large enough |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1002 |
if (register >= parameterNameIndices.length) { |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1003 |
int newSize = Math.max(register, parameterNameIndices.length + 8); |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1004 |
parameterNameIndices = |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1005 |
Arrays.copyOf(parameterNameIndices, newSize); |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1006 |
} |
4077
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1007 |
parameterNameIndices[register] = nameIndex; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1008 |
haveParameterNameIndices = true; |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1009 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1010 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1011 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1012 |
bp = newbp; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1013 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1014 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1015 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1016 |
new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1017 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1018 |
ClassSymbol c = (ClassSymbol) sym; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1019 |
Name n = readName(nextChar()); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1020 |
c.sourcefile = new SourceFileObject(n, c.flatname); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1021 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1022 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1023 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1024 |
new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1025 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1026 |
// bridge methods are visible when generics not enabled |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1027 |
if (allowGenerics || (sym.flags_field & BRIDGE) == 0) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1028 |
sym.flags_field |= SYNTHETIC; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1029 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1030 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1031 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1032 |
// standard v49 attributes |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1033 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1034 |
new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1035 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1036 |
int newbp = bp + attrLen; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1037 |
readEnclosingMethodAttr(sym); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1038 |
bp = newbp; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1039 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1040 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1041 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1042 |
new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1043 |
@Override |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1044 |
boolean accepts(AttributeKind kind) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1045 |
return super.accepts(kind) && allowGenerics; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1046 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1047 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1048 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1049 |
if (sym.kind == TYP) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1050 |
ClassSymbol c = (ClassSymbol) sym; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1051 |
readingClassAttr = true; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1052 |
try { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1053 |
ClassType ct1 = (ClassType)c.type; |
8032 | 1054 |
Assert.check(c == currentOwner); |
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1055 |
ct1.typarams_field = readTypeParams(nextChar()); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1056 |
ct1.supertype_field = sigToType(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1057 |
ListBuffer<Type> is = new ListBuffer<Type>(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1058 |
while (sigp != siglimit) is.append(sigToType()); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1059 |
ct1.interfaces_field = is.toList(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1060 |
} finally { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1061 |
readingClassAttr = false; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1062 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1063 |
} else { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1064 |
List<Type> thrown = sym.type.getThrownTypes(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1065 |
sym.type = readType(nextChar()); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1066 |
//- System.err.println(" # " + sym.type); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1067 |
if (sym.kind == MTH && sym.type.getThrownTypes().isEmpty()) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1068 |
sym.type.asMethodType().thrown = thrown; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1069 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1070 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1071 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1072 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1073 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1074 |
// v49 annotation attributes |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1075 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1076 |
new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1077 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1078 |
attachAnnotationDefault(sym); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1079 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1080 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1081 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1082 |
new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1083 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1084 |
attachAnnotations(sym); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1085 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1086 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1087 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1088 |
new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1089 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1090 |
attachParameterAnnotations(sym); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1091 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1092 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1093 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1094 |
new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1095 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1096 |
attachAnnotations(sym); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1097 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1098 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1099 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1100 |
new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1101 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1102 |
attachParameterAnnotations(sym); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1103 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1104 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1105 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1106 |
// additional "legacy" v49 attributes, superceded by flags |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1107 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1108 |
new AttributeReader(names.Annotation, V49, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1109 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1110 |
if (allowAnnotations) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1111 |
sym.flags_field |= ANNOTATION; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1112 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1113 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1114 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1115 |
new AttributeReader(names.Bridge, V49, MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1116 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1117 |
sym.flags_field |= BRIDGE; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1118 |
if (!allowGenerics) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1119 |
sym.flags_field &= ~SYNTHETIC; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1120 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1121 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1122 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1123 |
new AttributeReader(names.Enum, V49, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1124 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1125 |
sym.flags_field |= ENUM; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1126 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1127 |
}, |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1128 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1129 |
new AttributeReader(names.Varargs, V49, CLASS_OR_MEMBER_ATTRIBUTE) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1130 |
void read(Symbol sym, int attrLen) { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1131 |
if (allowVarargs) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1132 |
sym.flags_field |= VARARGS; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1133 |
} |
3149 | 1134 |
}, |
1135 |
||
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1136 |
// The following attributes for a Code attribute are not currently handled |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1137 |
// StackMapTable |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1138 |
// SourceDebugExtension |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1139 |
// LineNumberTable |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1140 |
// LocalVariableTypeTable |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1141 |
}; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1142 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1143 |
for (AttributeReader r: readers) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1144 |
attributeReaders.put(r.name, r); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1145 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1146 |
|
10 | 1147 |
/** Report unrecognized attribute. |
1148 |
*/ |
|
1149 |
void unrecognized(Name attrName) { |
|
1150 |
if (checkClassFile) |
|
1151 |
printCCF("ccf.unrecognized.attribute", attrName); |
|
1152 |
} |
|
1153 |
||
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1154 |
|
10 | 1155 |
|
1156 |
void readEnclosingMethodAttr(Symbol sym) { |
|
1157 |
// sym is a nested class with an "Enclosing Method" attribute |
|
1158 |
// remove sym from it's current owners scope and place it in |
|
1159 |
// the scope specified by the attribute |
|
1160 |
sym.owner.members().remove(sym); |
|
1161 |
ClassSymbol self = (ClassSymbol)sym; |
|
1162 |
ClassSymbol c = readClassSymbol(nextChar()); |
|
1163 |
NameAndType nt = (NameAndType)readPool(nextChar()); |
|
1164 |
||
1165 |
MethodSymbol m = findMethod(nt, c.members_field, self.flags()); |
|
1166 |
if (nt != null && m == null) |
|
1167 |
throw badClassFile("bad.enclosing.method", self); |
|
1168 |
||
1169 |
self.name = simpleBinaryName(self.flatname, c.flatname) ; |
|
1170 |
self.owner = m != null ? m : c; |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
939
diff
changeset
|
1171 |
if (self.name.isEmpty()) |
5012
405341b6e9c5
6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
4872
diff
changeset
|
1172 |
self.fullname = names.empty; |
10 | 1173 |
else |
1174 |
self.fullname = ClassSymbol.formFullName(self.name, self.owner); |
|
1175 |
||
1176 |
if (m != null) { |
|
1177 |
((ClassType)sym.type).setEnclosingType(m.type); |
|
1178 |
} else if ((self.flags_field & STATIC) == 0) { |
|
1179 |
((ClassType)sym.type).setEnclosingType(c.type); |
|
1180 |
} else { |
|
1181 |
((ClassType)sym.type).setEnclosingType(Type.noType); |
|
1182 |
} |
|
1183 |
enterTypevars(self); |
|
1184 |
if (!missingTypeVariables.isEmpty()) { |
|
1185 |
ListBuffer<Type> typeVars = new ListBuffer<Type>(); |
|
1186 |
for (Type typevar : missingTypeVariables) { |
|
1187 |
typeVars.append(findTypeVar(typevar.tsym.name)); |
|
1188 |
} |
|
1189 |
foundTypeVariables = typeVars.toList(); |
|
1190 |
} else { |
|
1191 |
foundTypeVariables = List.nil(); |
|
1192 |
} |
|
1193 |
} |
|
1194 |
||
1195 |
// See java.lang.Class |
|
1196 |
private Name simpleBinaryName(Name self, Name enclosing) { |
|
1197 |
String simpleBinaryName = self.toString().substring(enclosing.toString().length()); |
|
1198 |
if (simpleBinaryName.length() < 1 || simpleBinaryName.charAt(0) != '$') |
|
1199 |
throw badClassFile("bad.enclosing.method", self); |
|
1200 |
int index = 1; |
|
1201 |
while (index < simpleBinaryName.length() && |
|
1202 |
isAsciiDigit(simpleBinaryName.charAt(index))) |
|
1203 |
index++; |
|
1204 |
return names.fromString(simpleBinaryName.substring(index)); |
|
1205 |
} |
|
1206 |
||
1207 |
private MethodSymbol findMethod(NameAndType nt, Scope scope, long flags) { |
|
1208 |
if (nt == null) |
|
1209 |
return null; |
|
1210 |
||
1211 |
MethodType type = nt.type.asMethodType(); |
|
1212 |
||
1213 |
for (Scope.Entry e = scope.lookup(nt.name); e.scope != null; e = e.next()) |
|
1214 |
if (e.sym.kind == MTH && isSameBinaryType(e.sym.type.asMethodType(), type)) |
|
1215 |
return (MethodSymbol)e.sym; |
|
1216 |
||
1217 |
if (nt.name != names.init) |
|
1218 |
// not a constructor |
|
1219 |
return null; |
|
1220 |
if ((flags & INTERFACE) != 0) |
|
1221 |
// no enclosing instance |
|
1222 |
return null; |
|
1223 |
if (nt.type.getParameterTypes().isEmpty()) |
|
1224 |
// no parameters |
|
1225 |
return null; |
|
1226 |
||
1227 |
// A constructor of an inner class. |
|
1228 |
// Remove the first argument (the enclosing instance) |
|
1229 |
nt.type = new MethodType(nt.type.getParameterTypes().tail, |
|
1230 |
nt.type.getReturnType(), |
|
1231 |
nt.type.getThrownTypes(), |
|
1232 |
syms.methodClass); |
|
1233 |
// Try searching again |
|
1234 |
return findMethod(nt, scope, flags); |
|
1235 |
} |
|
1236 |
||
1237 |
/** Similar to Types.isSameType but avoids completion */ |
|
1238 |
private boolean isSameBinaryType(MethodType mt1, MethodType mt2) { |
|
1239 |
List<Type> types1 = types.erasure(mt1.getParameterTypes()) |
|
1240 |
.prepend(types.erasure(mt1.getReturnType())); |
|
1241 |
List<Type> types2 = mt2.getParameterTypes().prepend(mt2.getReturnType()); |
|
1242 |
while (!types1.isEmpty() && !types2.isEmpty()) { |
|
1243 |
if (types1.head.tsym != types2.head.tsym) |
|
1244 |
return false; |
|
1245 |
types1 = types1.tail; |
|
1246 |
types2 = types2.tail; |
|
1247 |
} |
|
1248 |
return types1.isEmpty() && types2.isEmpty(); |
|
1249 |
} |
|
1250 |
||
1251 |
/** |
|
1252 |
* Character.isDigit answers <tt>true</tt> to some non-ascii |
|
1253 |
* digits. This one does not. <b>copied from java.lang.Class</b> |
|
1254 |
*/ |
|
1255 |
private static boolean isAsciiDigit(char c) { |
|
1256 |
return '0' <= c && c <= '9'; |
|
1257 |
} |
|
1258 |
||
1259 |
/** Read member attributes. |
|
1260 |
*/ |
|
1261 |
void readMemberAttrs(Symbol sym) { |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1262 |
readAttrs(sym, AttributeKind.MEMBER); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1263 |
} |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1264 |
|
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1265 |
void readAttrs(Symbol sym, AttributeKind kind) { |
10 | 1266 |
char ac = nextChar(); |
1267 |
for (int i = 0; i < ac; i++) { |
|
1268 |
Name attrName = readName(nextChar()); |
|
1269 |
int attrLen = nextInt(); |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1270 |
AttributeReader r = attributeReaders.get(attrName); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1271 |
if (r != null && r.accepts(kind)) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1272 |
r.read(sym, attrLen); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1273 |
else { |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1274 |
unrecognized(attrName); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1275 |
bp = bp + attrLen; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1276 |
} |
10 | 1277 |
} |
1278 |
} |
|
1279 |
||
1280 |
private boolean readingClassAttr = false; |
|
1281 |
private List<Type> missingTypeVariables = List.nil(); |
|
1282 |
private List<Type> foundTypeVariables = List.nil(); |
|
1283 |
||
1284 |
/** Read class attributes. |
|
1285 |
*/ |
|
1286 |
void readClassAttrs(ClassSymbol c) { |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1287 |
readAttrs(c, AttributeKind.CLASS); |
10 | 1288 |
} |
1289 |
||
1290 |
/** Read code block. |
|
1291 |
*/ |
|
1292 |
Code readCode(Symbol owner) { |
|
1293 |
nextChar(); // max_stack |
|
1294 |
nextChar(); // max_locals |
|
1295 |
final int code_length = nextInt(); |
|
1296 |
bp += code_length; |
|
1297 |
final char exception_table_length = nextChar(); |
|
1298 |
bp += exception_table_length * 8; |
|
1299 |
readMemberAttrs(owner); |
|
1300 |
return null; |
|
1301 |
} |
|
1302 |
||
1303 |
/************************************************************************ |
|
1304 |
* Reading Java-language annotations |
|
1305 |
***********************************************************************/ |
|
1306 |
||
1307 |
/** Attach annotations. |
|
1308 |
*/ |
|
1309 |
void attachAnnotations(final Symbol sym) { |
|
1310 |
int numAttributes = nextChar(); |
|
1311 |
if (numAttributes != 0) { |
|
1312 |
ListBuffer<CompoundAnnotationProxy> proxies = |
|
1313 |
new ListBuffer<CompoundAnnotationProxy>(); |
|
1314 |
for (int i = 0; i<numAttributes; i++) { |
|
1315 |
CompoundAnnotationProxy proxy = readCompoundAnnotation(); |
|
1316 |
if (proxy.type.tsym == syms.proprietaryType.tsym) |
|
1317 |
sym.flags_field |= PROPRIETARY; |
|
1318 |
else |
|
1319 |
proxies.append(proxy); |
|
8241
4161b56e0d20
7017414: before the move of JSR 292 to package java.lang.invoke, javac must recognize the new package
mcimadamore
parents:
8042
diff
changeset
|
1320 |
if (majorVersion >= V51.major && |
4161b56e0d20
7017414: before the move of JSR 292 to package java.lang.invoke, javac must recognize the new package
mcimadamore
parents:
8042
diff
changeset
|
1321 |
(proxy.type.tsym == syms.polymorphicSignatureType.tsym || |
4161b56e0d20
7017414: before the move of JSR 292 to package java.lang.invoke, javac must recognize the new package
mcimadamore
parents:
8042
diff
changeset
|
1322 |
proxy.type.tsym == syms.transientPolymorphicSignatureType.tsym)) { |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6143
diff
changeset
|
1323 |
sym.flags_field |= POLYMORPHIC_SIGNATURE; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6143
diff
changeset
|
1324 |
} |
10 | 1325 |
} |
1326 |
annotate.later(new AnnotationCompleter(sym, proxies.toList())); |
|
1327 |
} |
|
1328 |
} |
|
1329 |
||
1330 |
/** Attach parameter annotations. |
|
1331 |
*/ |
|
1332 |
void attachParameterAnnotations(final Symbol method) { |
|
1333 |
final MethodSymbol meth = (MethodSymbol)method; |
|
1334 |
int numParameters = buf[bp++] & 0xFF; |
|
1335 |
List<VarSymbol> parameters = meth.params(); |
|
1336 |
int pnum = 0; |
|
1337 |
while (parameters.tail != null) { |
|
1338 |
attachAnnotations(parameters.head); |
|
1339 |
parameters = parameters.tail; |
|
1340 |
pnum++; |
|
1341 |
} |
|
1342 |
if (pnum != numParameters) { |
|
1343 |
throw badClassFile("bad.runtime.invisible.param.annotations", meth); |
|
1344 |
} |
|
1345 |
} |
|
1346 |
||
1347 |
/** Attach the default value for an annotation element. |
|
1348 |
*/ |
|
1349 |
void attachAnnotationDefault(final Symbol sym) { |
|
1350 |
final MethodSymbol meth = (MethodSymbol)sym; // only on methods |
|
1351 |
final Attribute value = readAttributeValue(); |
|
1352 |
annotate.later(new AnnotationDefaultCompleter(meth, value)); |
|
1353 |
} |
|
1354 |
||
1355 |
Type readTypeOrClassSymbol(int i) { |
|
1356 |
// support preliminary jsr175-format class files |
|
1357 |
if (buf[poolIdx[i]] == CONSTANT_Class) |
|
1358 |
return readClassSymbol(i).type; |
|
1359 |
return readType(i); |
|
1360 |
} |
|
1361 |
Type readEnumType(int i) { |
|
1362 |
// support preliminary jsr175-format class files |
|
1363 |
int index = poolIdx[i]; |
|
1364 |
int length = getChar(index + 1); |
|
1365 |
if (buf[index + length + 2] != ';') |
|
1366 |
return enterClass(readName(i)).type; |
|
1367 |
return readType(i); |
|
1368 |
} |
|
1369 |
||
1370 |
CompoundAnnotationProxy readCompoundAnnotation() { |
|
1371 |
Type t = readTypeOrClassSymbol(nextChar()); |
|
1372 |
int numFields = nextChar(); |
|
1373 |
ListBuffer<Pair<Name,Attribute>> pairs = |
|
1374 |
new ListBuffer<Pair<Name,Attribute>>(); |
|
1375 |
for (int i=0; i<numFields; i++) { |
|
1376 |
Name name = readName(nextChar()); |
|
1377 |
Attribute value = readAttributeValue(); |
|
1378 |
pairs.append(new Pair<Name,Attribute>(name, value)); |
|
1379 |
} |
|
1380 |
return new CompoundAnnotationProxy(t, pairs.toList()); |
|
1381 |
} |
|
1382 |
||
1383 |
Attribute readAttributeValue() { |
|
1384 |
char c = (char) buf[bp++]; |
|
1385 |
switch (c) { |
|
1386 |
case 'B': |
|
1387 |
return new Attribute.Constant(syms.byteType, readPool(nextChar())); |
|
1388 |
case 'C': |
|
1389 |
return new Attribute.Constant(syms.charType, readPool(nextChar())); |
|
1390 |
case 'D': |
|
1391 |
return new Attribute.Constant(syms.doubleType, readPool(nextChar())); |
|
1392 |
case 'F': |
|
1393 |
return new Attribute.Constant(syms.floatType, readPool(nextChar())); |
|
1394 |
case 'I': |
|
1395 |
return new Attribute.Constant(syms.intType, readPool(nextChar())); |
|
1396 |
case 'J': |
|
1397 |
return new Attribute.Constant(syms.longType, readPool(nextChar())); |
|
1398 |
case 'S': |
|
1399 |
return new Attribute.Constant(syms.shortType, readPool(nextChar())); |
|
1400 |
case 'Z': |
|
1401 |
return new Attribute.Constant(syms.booleanType, readPool(nextChar())); |
|
1402 |
case 's': |
|
1403 |
return new Attribute.Constant(syms.stringType, readPool(nextChar()).toString()); |
|
1404 |
case 'e': |
|
1405 |
return new EnumAttributeProxy(readEnumType(nextChar()), readName(nextChar())); |
|
1406 |
case 'c': |
|
1407 |
return new Attribute.Class(types, readTypeOrClassSymbol(nextChar())); |
|
1408 |
case '[': { |
|
1409 |
int n = nextChar(); |
|
1410 |
ListBuffer<Attribute> l = new ListBuffer<Attribute>(); |
|
1411 |
for (int i=0; i<n; i++) |
|
1412 |
l.append(readAttributeValue()); |
|
1413 |
return new ArrayAttributeProxy(l.toList()); |
|
1414 |
} |
|
1415 |
case '@': |
|
1416 |
return readCompoundAnnotation(); |
|
1417 |
default: |
|
1418 |
throw new AssertionError("unknown annotation tag '" + c + "'"); |
|
1419 |
} |
|
1420 |
} |
|
1421 |
||
1422 |
interface ProxyVisitor extends Attribute.Visitor { |
|
1423 |
void visitEnumAttributeProxy(EnumAttributeProxy proxy); |
|
1424 |
void visitArrayAttributeProxy(ArrayAttributeProxy proxy); |
|
1425 |
void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy); |
|
1426 |
} |
|
1427 |
||
1428 |
static class EnumAttributeProxy extends Attribute { |
|
1429 |
Type enumType; |
|
1430 |
Name enumerator; |
|
1431 |
public EnumAttributeProxy(Type enumType, Name enumerator) { |
|
1432 |
super(null); |
|
1433 |
this.enumType = enumType; |
|
1434 |
this.enumerator = enumerator; |
|
1435 |
} |
|
1436 |
public void accept(Visitor v) { ((ProxyVisitor)v).visitEnumAttributeProxy(this); } |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1437 |
@Override |
10 | 1438 |
public String toString() { |
1439 |
return "/*proxy enum*/" + enumType + "." + enumerator; |
|
1440 |
} |
|
1441 |
} |
|
1442 |
||
1443 |
static class ArrayAttributeProxy extends Attribute { |
|
1444 |
List<Attribute> values; |
|
1445 |
ArrayAttributeProxy(List<Attribute> values) { |
|
1446 |
super(null); |
|
1447 |
this.values = values; |
|
1448 |
} |
|
1449 |
public void accept(Visitor v) { ((ProxyVisitor)v).visitArrayAttributeProxy(this); } |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1450 |
@Override |
10 | 1451 |
public String toString() { |
1452 |
return "{" + values + "}"; |
|
1453 |
} |
|
1454 |
} |
|
1455 |
||
1456 |
/** A temporary proxy representing a compound attribute. |
|
1457 |
*/ |
|
1458 |
static class CompoundAnnotationProxy extends Attribute { |
|
1459 |
final List<Pair<Name,Attribute>> values; |
|
1460 |
public CompoundAnnotationProxy(Type type, |
|
1461 |
List<Pair<Name,Attribute>> values) { |
|
1462 |
super(type); |
|
1463 |
this.values = values; |
|
1464 |
} |
|
1465 |
public void accept(Visitor v) { ((ProxyVisitor)v).visitCompoundAnnotationProxy(this); } |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1466 |
@Override |
10 | 1467 |
public String toString() { |
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
1468 |
StringBuilder buf = new StringBuilder(); |
10 | 1469 |
buf.append("@"); |
1470 |
buf.append(type.tsym.getQualifiedName()); |
|
1471 |
buf.append("/*proxy*/{"); |
|
1472 |
boolean first = true; |
|
1473 |
for (List<Pair<Name,Attribute>> v = values; |
|
1474 |
v.nonEmpty(); v = v.tail) { |
|
1475 |
Pair<Name,Attribute> value = v.head; |
|
1476 |
if (!first) buf.append(","); |
|
1477 |
first = false; |
|
1478 |
buf.append(value.fst); |
|
1479 |
buf.append("="); |
|
1480 |
buf.append(value.snd); |
|
1481 |
} |
|
1482 |
buf.append("}"); |
|
1483 |
return buf.toString(); |
|
1484 |
} |
|
1485 |
} |
|
1486 |
||
3149 | 1487 |
/** A temporary proxy representing a type annotation. |
1488 |
*/ |
|
1489 |
static class TypeAnnotationProxy { |
|
1490 |
final CompoundAnnotationProxy compound; |
|
1491 |
final TypeAnnotationPosition position; |
|
1492 |
public TypeAnnotationProxy(CompoundAnnotationProxy compound, |
|
1493 |
TypeAnnotationPosition position) { |
|
1494 |
this.compound = compound; |
|
1495 |
this.position = position; |
|
1496 |
} |
|
1497 |
} |
|
1498 |
||
10 | 1499 |
class AnnotationDeproxy implements ProxyVisitor { |
1500 |
private ClassSymbol requestingOwner = currentOwner.kind == MTH |
|
1501 |
? currentOwner.enclClass() : (ClassSymbol)currentOwner; |
|
1502 |
||
1503 |
List<Attribute.Compound> deproxyCompoundList(List<CompoundAnnotationProxy> pl) { |
|
1504 |
// also must fill in types!!!! |
|
1505 |
ListBuffer<Attribute.Compound> buf = |
|
1506 |
new ListBuffer<Attribute.Compound>(); |
|
1507 |
for (List<CompoundAnnotationProxy> l = pl; l.nonEmpty(); l=l.tail) { |
|
1508 |
buf.append(deproxyCompound(l.head)); |
|
1509 |
} |
|
1510 |
return buf.toList(); |
|
1511 |
} |
|
1512 |
||
1513 |
Attribute.Compound deproxyCompound(CompoundAnnotationProxy a) { |
|
1514 |
ListBuffer<Pair<Symbol.MethodSymbol,Attribute>> buf = |
|
1515 |
new ListBuffer<Pair<Symbol.MethodSymbol,Attribute>>(); |
|
1516 |
for (List<Pair<Name,Attribute>> l = a.values; |
|
1517 |
l.nonEmpty(); |
|
1518 |
l = l.tail) { |
|
1519 |
MethodSymbol meth = findAccessMethod(a.type, l.head.fst); |
|
1520 |
buf.append(new Pair<Symbol.MethodSymbol,Attribute> |
|
1521 |
(meth, deproxy(meth.type.getReturnType(), l.head.snd))); |
|
1522 |
} |
|
1523 |
return new Attribute.Compound(a.type, buf.toList()); |
|
1524 |
} |
|
1525 |
||
1526 |
MethodSymbol findAccessMethod(Type container, Name name) { |
|
1527 |
CompletionFailure failure = null; |
|
1528 |
try { |
|
1529 |
for (Scope.Entry e = container.tsym.members().lookup(name); |
|
1530 |
e.scope != null; |
|
1531 |
e = e.next()) { |
|
1532 |
Symbol sym = e.sym; |
|
1533 |
if (sym.kind == MTH && sym.type.getParameterTypes().length() == 0) |
|
1534 |
return (MethodSymbol) sym; |
|
1535 |
} |
|
1536 |
} catch (CompletionFailure ex) { |
|
1537 |
failure = ex; |
|
1538 |
} |
|
1539 |
// The method wasn't found: emit a warning and recover |
|
1540 |
JavaFileObject prevSource = log.useSource(requestingOwner.classfile); |
|
1541 |
try { |
|
1542 |
if (failure == null) { |
|
1543 |
log.warning("annotation.method.not.found", |
|
1544 |
container, |
|
1545 |
name); |
|
1546 |
} else { |
|
1547 |
log.warning("annotation.method.not.found.reason", |
|
1548 |
container, |
|
1549 |
name, |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
815
diff
changeset
|
1550 |
failure.getDetailValue());//diagnostic, if present |
10 | 1551 |
} |
1552 |
} finally { |
|
1553 |
log.useSource(prevSource); |
|
1554 |
} |
|
1555 |
// Construct a new method type and symbol. Use bottom |
|
1556 |
// type (typeof null) as return type because this type is |
|
1557 |
// a subtype of all reference types and can be converted |
|
1558 |
// to primitive types by unboxing. |
|
1559 |
MethodType mt = new MethodType(List.<Type>nil(), |
|
1560 |
syms.botType, |
|
1561 |
List.<Type>nil(), |
|
1562 |
syms.methodClass); |
|
1563 |
return new MethodSymbol(PUBLIC | ABSTRACT, name, mt, container.tsym); |
|
1564 |
} |
|
1565 |
||
1566 |
Attribute result; |
|
1567 |
Type type; |
|
1568 |
Attribute deproxy(Type t, Attribute a) { |
|
1569 |
Type oldType = type; |
|
1570 |
try { |
|
1571 |
type = t; |
|
1572 |
a.accept(this); |
|
1573 |
return result; |
|
1574 |
} finally { |
|
1575 |
type = oldType; |
|
1576 |
} |
|
1577 |
} |
|
1578 |
||
1579 |
// implement Attribute.Visitor below |
|
1580 |
||
1581 |
public void visitConstant(Attribute.Constant value) { |
|
1582 |
// assert value.type == type; |
|
1583 |
result = value; |
|
1584 |
} |
|
1585 |
||
1586 |
public void visitClass(Attribute.Class clazz) { |
|
1587 |
result = clazz; |
|
1588 |
} |
|
1589 |
||
1590 |
public void visitEnum(Attribute.Enum e) { |
|
1591 |
throw new AssertionError(); // shouldn't happen |
|
1592 |
} |
|
1593 |
||
1594 |
public void visitCompound(Attribute.Compound compound) { |
|
1595 |
throw new AssertionError(); // shouldn't happen |
|
1596 |
} |
|
1597 |
||
1598 |
public void visitArray(Attribute.Array array) { |
|
1599 |
throw new AssertionError(); // shouldn't happen |
|
1600 |
} |
|
1601 |
||
1602 |
public void visitError(Attribute.Error e) { |
|
1603 |
throw new AssertionError(); // shouldn't happen |
|
1604 |
} |
|
1605 |
||
1606 |
public void visitEnumAttributeProxy(EnumAttributeProxy proxy) { |
|
1607 |
// type.tsym.flatName() should == proxy.enumFlatName |
|
1608 |
TypeSymbol enumTypeSym = proxy.enumType.tsym; |
|
1609 |
VarSymbol enumerator = null; |
|
1610 |
for (Scope.Entry e = enumTypeSym.members().lookup(proxy.enumerator); |
|
1611 |
e.scope != null; |
|
1612 |
e = e.next()) { |
|
1613 |
if (e.sym.kind == VAR) { |
|
1614 |
enumerator = (VarSymbol)e.sym; |
|
1615 |
break; |
|
1616 |
} |
|
1617 |
} |
|
1618 |
if (enumerator == null) { |
|
1619 |
log.error("unknown.enum.constant", |
|
1620 |
currentClassFile, enumTypeSym, proxy.enumerator); |
|
1621 |
result = new Attribute.Error(enumTypeSym.type); |
|
1622 |
} else { |
|
1623 |
result = new Attribute.Enum(enumTypeSym.type, enumerator); |
|
1624 |
} |
|
1625 |
} |
|
1626 |
||
1627 |
public void visitArrayAttributeProxy(ArrayAttributeProxy proxy) { |
|
1628 |
int length = proxy.values.length(); |
|
1629 |
Attribute[] ats = new Attribute[length]; |
|
1630 |
Type elemtype = types.elemtype(type); |
|
1631 |
int i = 0; |
|
1632 |
for (List<Attribute> p = proxy.values; p.nonEmpty(); p = p.tail) { |
|
1633 |
ats[i++] = deproxy(elemtype, p.head); |
|
1634 |
} |
|
1635 |
result = new Attribute.Array(type, ats); |
|
1636 |
} |
|
1637 |
||
1638 |
public void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy) { |
|
1639 |
result = deproxyCompound(proxy); |
|
1640 |
} |
|
1641 |
} |
|
1642 |
||
1643 |
class AnnotationDefaultCompleter extends AnnotationDeproxy implements Annotate.Annotator { |
|
1644 |
final MethodSymbol sym; |
|
1645 |
final Attribute value; |
|
1646 |
final JavaFileObject classFile = currentClassFile; |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1647 |
@Override |
10 | 1648 |
public String toString() { |
1649 |
return " ClassReader store default for " + sym.owner + "." + sym + " is " + value; |
|
1650 |
} |
|
1651 |
AnnotationDefaultCompleter(MethodSymbol sym, Attribute value) { |
|
1652 |
this.sym = sym; |
|
1653 |
this.value = value; |
|
1654 |
} |
|
1655 |
// implement Annotate.Annotator.enterAnnotation() |
|
1656 |
public void enterAnnotation() { |
|
1657 |
JavaFileObject previousClassFile = currentClassFile; |
|
1658 |
try { |
|
1659 |
currentClassFile = classFile; |
|
1660 |
sym.defaultValue = deproxy(sym.type.getReturnType(), value); |
|
1661 |
} finally { |
|
1662 |
currentClassFile = previousClassFile; |
|
1663 |
} |
|
1664 |
} |
|
1665 |
} |
|
1666 |
||
1667 |
class AnnotationCompleter extends AnnotationDeproxy implements Annotate.Annotator { |
|
1668 |
final Symbol sym; |
|
1669 |
final List<CompoundAnnotationProxy> l; |
|
1670 |
final JavaFileObject classFile; |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1671 |
@Override |
10 | 1672 |
public String toString() { |
1673 |
return " ClassReader annotate " + sym.owner + "." + sym + " with " + l; |
|
1674 |
} |
|
1675 |
AnnotationCompleter(Symbol sym, List<CompoundAnnotationProxy> l) { |
|
1676 |
this.sym = sym; |
|
1677 |
this.l = l; |
|
1678 |
this.classFile = currentClassFile; |
|
1679 |
} |
|
1680 |
// implement Annotate.Annotator.enterAnnotation() |
|
1681 |
public void enterAnnotation() { |
|
1682 |
JavaFileObject previousClassFile = currentClassFile; |
|
1683 |
try { |
|
1684 |
currentClassFile = classFile; |
|
1685 |
List<Attribute.Compound> newList = deproxyCompoundList(l); |
|
1686 |
sym.attributes_field = ((sym.attributes_field == null) |
|
1687 |
? newList |
|
1688 |
: newList.prependList(sym.attributes_field)); |
|
1689 |
} finally { |
|
1690 |
currentClassFile = previousClassFile; |
|
1691 |
} |
|
1692 |
} |
|
1693 |
} |
|
1694 |
||
1695 |
||
1696 |
/************************************************************************ |
|
1697 |
* Reading Symbols |
|
1698 |
***********************************************************************/ |
|
1699 |
||
1700 |
/** Read a field. |
|
1701 |
*/ |
|
1702 |
VarSymbol readField() { |
|
1703 |
long flags = adjustFieldFlags(nextChar()); |
|
1704 |
Name name = readName(nextChar()); |
|
1705 |
Type type = readType(nextChar()); |
|
1706 |
VarSymbol v = new VarSymbol(flags, name, type, currentOwner); |
|
1707 |
readMemberAttrs(v); |
|
1708 |
return v; |
|
1709 |
} |
|
1710 |
||
1711 |
/** Read a method. |
|
1712 |
*/ |
|
1713 |
MethodSymbol readMethod() { |
|
1714 |
long flags = adjustMethodFlags(nextChar()); |
|
1715 |
Name name = readName(nextChar()); |
|
1716 |
Type type = readType(nextChar()); |
|
1717 |
if (name == names.init && currentOwner.hasOuterInstance()) { |
|
1718 |
// Sometimes anonymous classes don't have an outer |
|
1719 |
// instance, however, there is no reliable way to tell so |
|
1720 |
// we never strip this$n |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
939
diff
changeset
|
1721 |
if (!currentOwner.name.isEmpty()) |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1722 |
type = new MethodType(adjustMethodParams(flags, type.getParameterTypes()), |
10 | 1723 |
type.getReturnType(), |
1724 |
type.getThrownTypes(), |
|
1725 |
syms.methodClass); |
|
1726 |
} |
|
1727 |
MethodSymbol m = new MethodSymbol(flags, name, type, currentOwner); |
|
4077
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1728 |
if (saveParameterNames) |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1729 |
initParameterNames(m); |
10 | 1730 |
Symbol prevOwner = currentOwner; |
1731 |
currentOwner = m; |
|
1732 |
try { |
|
1733 |
readMemberAttrs(m); |
|
1734 |
} finally { |
|
1735 |
currentOwner = prevOwner; |
|
1736 |
} |
|
4077
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1737 |
if (saveParameterNames) |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1738 |
setParameterNames(m, type); |
10 | 1739 |
return m; |
1740 |
} |
|
1741 |
||
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1742 |
private List<Type> adjustMethodParams(long flags, List<Type> args) { |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1743 |
boolean isVarargs = (flags & VARARGS) != 0; |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1744 |
if (isVarargs) { |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1745 |
Type varargsElem = args.last(); |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1746 |
ListBuffer<Type> adjustedArgs = ListBuffer.lb(); |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1747 |
for (Type t : args) { |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1748 |
adjustedArgs.append(t != varargsElem ? |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1749 |
t : |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1750 |
((ArrayType)t).makeVarargs()); |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1751 |
} |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1752 |
args = adjustedArgs.toList(); |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1753 |
} |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1754 |
return args.tail; |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1755 |
} |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7632
diff
changeset
|
1756 |
|
4077
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1757 |
/** |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1758 |
* Init the parameter names array. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1759 |
* Parameter names are currently inferred from the names in the |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1760 |
* LocalVariableTable attributes of a Code attribute. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1761 |
* (Note: this means parameter names are currently not available for |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1762 |
* methods without a Code attribute.) |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1763 |
* This method initializes an array in which to store the name indexes |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1764 |
* of parameter names found in LocalVariableTable attributes. It is |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1765 |
* slightly supersized to allow for additional slots with a start_pc of 0. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1766 |
*/ |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1767 |
void initParameterNames(MethodSymbol sym) { |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1768 |
// make allowance for synthetic parameters. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1769 |
final int excessSlots = 4; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1770 |
int expectedParameterSlots = |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1771 |
Code.width(sym.type.getParameterTypes()) + excessSlots; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1772 |
if (parameterNameIndices == null |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1773 |
|| parameterNameIndices.length < expectedParameterSlots) { |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1774 |
parameterNameIndices = new int[expectedParameterSlots]; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1775 |
} else |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1776 |
Arrays.fill(parameterNameIndices, 0); |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1777 |
haveParameterNameIndices = false; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1778 |
} |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1779 |
|
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1780 |
/** |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1781 |
* Set the parameter names for a symbol from the name index in the |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1782 |
* parameterNameIndicies array. The type of the symbol may have changed |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1783 |
* while reading the method attributes (see the Signature attribute). |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1784 |
* This may be because of generic information or because anonymous |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1785 |
* synthetic parameters were added. The original type (as read from |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1786 |
* the method descriptor) is used to help guess the existence of |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1787 |
* anonymous synthetic parameters. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1788 |
* On completion, sym.savedParameter names will either be null (if |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1789 |
* no parameter names were found in the class file) or will be set to a |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1790 |
* list of names, one per entry in sym.type.getParameterTypes, with |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1791 |
* any missing names represented by the empty name. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1792 |
*/ |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1793 |
void setParameterNames(MethodSymbol sym, Type jvmType) { |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1794 |
// if no names were found in the class file, there's nothing more to do |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1795 |
if (!haveParameterNameIndices) |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1796 |
return; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1797 |
|
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1798 |
int firstParam = ((sym.flags() & STATIC) == 0) ? 1 : 0; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1799 |
// the code in readMethod may have skipped the first parameter when |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1800 |
// setting up the MethodType. If so, we make a corresponding allowance |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1801 |
// here for the position of the first parameter. Note that this |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1802 |
// assumes the skipped parameter has a width of 1 -- i.e. it is not |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1803 |
// a double width type (long or double.) |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1804 |
if (sym.name == names.init && currentOwner.hasOuterInstance()) { |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1805 |
// Sometimes anonymous classes don't have an outer |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1806 |
// instance, however, there is no reliable way to tell so |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1807 |
// we never strip this$n |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1808 |
if (!currentOwner.name.isEmpty()) |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1809 |
firstParam += 1; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1810 |
} |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1811 |
|
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1812 |
if (sym.type != jvmType) { |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1813 |
// reading the method attributes has caused the symbol's type to |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1814 |
// be changed. (i.e. the Signature attribute.) This may happen if |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1815 |
// there are hidden (synthetic) parameters in the descriptor, but |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1816 |
// not in the Signature. The position of these hidden parameters |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1817 |
// is unspecified; for now, assume they are at the beginning, and |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1818 |
// so skip over them. The primary case for this is two hidden |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1819 |
// parameters passed into Enum constructors. |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1820 |
int skip = Code.width(jvmType.getParameterTypes()) |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1821 |
- Code.width(sym.type.getParameterTypes()); |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1822 |
firstParam += skip; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1823 |
} |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1824 |
List<Name> paramNames = List.nil(); |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1825 |
int index = firstParam; |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1826 |
for (Type t: sym.type.getParameterTypes()) { |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1827 |
int nameIdx = (index < parameterNameIndices.length |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1828 |
? parameterNameIndices[index] : 0); |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1829 |
Name name = nameIdx == 0 ? names.empty : readName(nameIdx); |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1830 |
paramNames = paramNames.prepend(name); |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1831 |
index += Code.width(t); |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1832 |
} |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1833 |
sym.savedParameterNames = paramNames.reverse(); |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1834 |
} |
0096541a388e
6889255: javac MethodSymbol throws NPE if ClassReader does not read parameter names correctly
jjg
parents:
4073
diff
changeset
|
1835 |
|
8042 | 1836 |
/** |
1837 |
* skip n bytes |
|
1838 |
*/ |
|
1839 |
void skipBytes(int n) { |
|
1840 |
bp = bp + n; |
|
1841 |
} |
|
1842 |
||
10 | 1843 |
/** Skip a field or method |
1844 |
*/ |
|
1845 |
void skipMember() { |
|
1846 |
bp = bp + 6; |
|
1847 |
char ac = nextChar(); |
|
1848 |
for (int i = 0; i < ac; i++) { |
|
1849 |
bp = bp + 2; |
|
1850 |
int attrLen = nextInt(); |
|
1851 |
bp = bp + attrLen; |
|
1852 |
} |
|
1853 |
} |
|
1854 |
||
1855 |
/** Enter type variables of this classtype and all enclosing ones in |
|
1856 |
* `typevars'. |
|
1857 |
*/ |
|
1858 |
protected void enterTypevars(Type t) { |
|
1859 |
if (t.getEnclosingType() != null && t.getEnclosingType().tag == CLASS) |
|
1860 |
enterTypevars(t.getEnclosingType()); |
|
1861 |
for (List<Type> xs = t.getTypeArguments(); xs.nonEmpty(); xs = xs.tail) |
|
1862 |
typevars.enter(xs.head.tsym); |
|
1863 |
} |
|
1864 |
||
1865 |
protected void enterTypevars(Symbol sym) { |
|
1866 |
if (sym.owner.kind == MTH) { |
|
1867 |
enterTypevars(sym.owner); |
|
1868 |
enterTypevars(sym.owner.owner); |
|
1869 |
} |
|
1870 |
enterTypevars(sym.type); |
|
1871 |
} |
|
1872 |
||
1873 |
/** Read contents of a given class symbol `c'. Both external and internal |
|
1874 |
* versions of an inner class are read. |
|
1875 |
*/ |
|
1876 |
void readClass(ClassSymbol c) { |
|
1877 |
ClassType ct = (ClassType)c.type; |
|
1878 |
||
1879 |
// allocate scope for members |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8241
diff
changeset
|
1880 |
c.members_field = new Scope(c); |
10 | 1881 |
|
1882 |
// prepare type variable table |
|
1883 |
typevars = typevars.dup(currentOwner); |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1884 |
if (ct.getEnclosingType().tag == CLASS) |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1885 |
enterTypevars(ct.getEnclosingType()); |
10 | 1886 |
|
1887 |
// read flags, or skip if this is an inner class |
|
1888 |
long flags = adjustClassFlags(nextChar()); |
|
1889 |
if (c.owner.kind == PCK) c.flags_field = flags; |
|
1890 |
||
1891 |
// read own class name and check that it matches |
|
1892 |
ClassSymbol self = readClassSymbol(nextChar()); |
|
1893 |
if (c != self) |
|
1894 |
throw badClassFile("class.file.wrong.class", |
|
1895 |
self.flatname); |
|
1896 |
||
1897 |
// class attributes must be read before class |
|
1898 |
// skip ahead to read class attributes |
|
1899 |
int startbp = bp; |
|
1900 |
nextChar(); |
|
1901 |
char interfaceCount = nextChar(); |
|
1902 |
bp += interfaceCount * 2; |
|
1903 |
char fieldCount = nextChar(); |
|
1904 |
for (int i = 0; i < fieldCount; i++) skipMember(); |
|
1905 |
char methodCount = nextChar(); |
|
1906 |
for (int i = 0; i < methodCount; i++) skipMember(); |
|
1907 |
readClassAttrs(c); |
|
1908 |
||
1909 |
if (readAllOfClassFile) { |
|
1910 |
for (int i = 1; i < poolObj.length; i++) readPool(i); |
|
1911 |
c.pool = new Pool(poolObj.length, poolObj); |
|
1912 |
} |
|
1913 |
||
1914 |
// reset and read rest of classinfo |
|
1915 |
bp = startbp; |
|
1916 |
int n = nextChar(); |
|
1917 |
if (ct.supertype_field == null) |
|
1918 |
ct.supertype_field = (n == 0) |
|
1919 |
? Type.noType |
|
1920 |
: readClassSymbol(n).erasure(types); |
|
1921 |
n = nextChar(); |
|
1922 |
List<Type> is = List.nil(); |
|
1923 |
for (int i = 0; i < n; i++) { |
|
1924 |
Type _inter = readClassSymbol(nextChar()).erasure(types); |
|
1925 |
is = is.prepend(_inter); |
|
1926 |
} |
|
1927 |
if (ct.interfaces_field == null) |
|
1928 |
ct.interfaces_field = is.reverse(); |
|
1929 |
||
8032 | 1930 |
Assert.check(fieldCount == nextChar()); |
10 | 1931 |
for (int i = 0; i < fieldCount; i++) enterMember(c, readField()); |
8032 | 1932 |
Assert.check(methodCount == nextChar()); |
10 | 1933 |
for (int i = 0; i < methodCount; i++) enterMember(c, readMethod()); |
1934 |
||
1935 |
typevars = typevars.leave(); |
|
1936 |
} |
|
1937 |
||
1938 |
/** Read inner class info. For each inner/outer pair allocate a |
|
1939 |
* member class. |
|
1940 |
*/ |
|
1941 |
void readInnerClasses(ClassSymbol c) { |
|
1942 |
int n = nextChar(); |
|
1943 |
for (int i = 0; i < n; i++) { |
|
1944 |
nextChar(); // skip inner class symbol |
|
1945 |
ClassSymbol outer = readClassSymbol(nextChar()); |
|
1946 |
Name name = readName(nextChar()); |
|
1947 |
if (name == null) name = names.empty; |
|
1948 |
long flags = adjustClassFlags(nextChar()); |
|
1949 |
if (outer != null) { // we have a member class |
|
1950 |
if (name == names.empty) |
|
1951 |
name = names.one; |
|
1952 |
ClassSymbol member = enterClass(name, outer); |
|
1953 |
if ((flags & STATIC) == 0) { |
|
1954 |
((ClassType)member.type).setEnclosingType(outer.type); |
|
1955 |
if (member.erasure_field != null) |
|
1956 |
((ClassType)member.erasure_field).setEnclosingType(types.erasure(outer.type)); |
|
1957 |
} |
|
1958 |
if (c == outer) { |
|
1959 |
member.flags_field = flags; |
|
1960 |
enterMember(c, member); |
|
1961 |
} |
|
1962 |
} |
|
1963 |
} |
|
1964 |
} |
|
1965 |
||
1966 |
/** Read a class file. |
|
1967 |
*/ |
|
1968 |
private void readClassFile(ClassSymbol c) throws IOException { |
|
1969 |
int magic = nextInt(); |
|
1970 |
if (magic != JAVA_MAGIC) |
|
1971 |
throw badClassFile("illegal.start.of.class.file"); |
|
1972 |
||
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1973 |
minorVersion = nextChar(); |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
1974 |
majorVersion = nextChar(); |
10 | 1975 |
int maxMajor = Target.MAX().majorVersion; |
1976 |
int maxMinor = Target.MAX().minorVersion; |
|
1977 |
if (majorVersion > maxMajor || |
|
1978 |
majorVersion * 1000 + minorVersion < |
|
1979 |
Target.MIN().majorVersion * 1000 + Target.MIN().minorVersion) |
|
1980 |
{ |
|
1981 |
if (majorVersion == (maxMajor + 1)) |
|
1982 |
log.warning("big.major.version", |
|
1983 |
currentClassFile, |
|
1984 |
majorVersion, |
|
1985 |
maxMajor); |
|
1986 |
else |
|
1987 |
throw badClassFile("wrong.version", |
|
1988 |
Integer.toString(majorVersion), |
|
1989 |
Integer.toString(minorVersion), |
|
1990 |
Integer.toString(maxMajor), |
|
1991 |
Integer.toString(maxMinor)); |
|
1992 |
} |
|
1993 |
else if (checkClassFile && |
|
1994 |
majorVersion == maxMajor && |
|
1995 |
minorVersion > maxMinor) |
|
1996 |
{ |
|
1997 |
printCCF("found.later.version", |
|
1998 |
Integer.toString(minorVersion)); |
|
1999 |
} |
|
2000 |
indexPool(); |
|
2001 |
if (signatureBuffer.length < bp) { |
|
2002 |
int ns = Integer.highestOneBit(bp) << 1; |
|
2003 |
signatureBuffer = new byte[ns]; |
|
2004 |
} |
|
2005 |
readClass(c); |
|
2006 |
} |
|
2007 |
||
2008 |
/************************************************************************ |
|
2009 |
* Adjusting flags |
|
2010 |
***********************************************************************/ |
|
2011 |
||
2012 |
long adjustFieldFlags(long flags) { |
|
2013 |
return flags; |
|
2014 |
} |
|
2015 |
long adjustMethodFlags(long flags) { |
|
2016 |
if ((flags & ACC_BRIDGE) != 0) { |
|
2017 |
flags &= ~ACC_BRIDGE; |
|
2018 |
flags |= BRIDGE; |
|
2019 |
if (!allowGenerics) |
|
2020 |
flags &= ~SYNTHETIC; |
|
2021 |
} |
|
2022 |
if ((flags & ACC_VARARGS) != 0) { |
|
2023 |
flags &= ~ACC_VARARGS; |
|
2024 |
flags |= VARARGS; |
|
2025 |
} |
|
2026 |
return flags; |
|
2027 |
} |
|
2028 |
long adjustClassFlags(long flags) { |
|
2029 |
return flags & ~ACC_SUPER; // SUPER and SYNCHRONIZED bits overloaded |
|
2030 |
} |
|
2031 |
||
2032 |
/************************************************************************ |
|
2033 |
* Loading Classes |
|
2034 |
***********************************************************************/ |
|
2035 |
||
2036 |
/** Define a new class given its name and owner. |
|
2037 |
*/ |
|
2038 |
public ClassSymbol defineClass(Name name, Symbol owner) { |
|
2039 |
ClassSymbol c = new ClassSymbol(0, name, owner); |
|
2040 |
if (owner.kind == PCK) |
|
8032 | 2041 |
Assert.checkNull(classes.get(c.flatname), c); |
10 | 2042 |
c.completer = this; |
2043 |
return c; |
|
2044 |
} |
|
2045 |
||
2046 |
/** Create a new toplevel or member class symbol with given name |
|
2047 |
* and owner and enter in `classes' unless already there. |
|
2048 |
*/ |
|
2049 |
public ClassSymbol enterClass(Name name, TypeSymbol owner) { |
|
2050 |
Name flatname = TypeSymbol.formFlatName(name, owner); |
|
2051 |
ClassSymbol c = classes.get(flatname); |
|
2052 |
if (c == null) { |
|
2053 |
c = defineClass(name, owner); |
|
2054 |
classes.put(flatname, c); |
|
2055 |
} else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) { |
|
2056 |
// reassign fields of classes that might have been loaded with |
|
2057 |
// their flat names. |
|
2058 |
c.owner.members().remove(c); |
|
2059 |
c.name = name; |
|
2060 |
c.owner = owner; |
|
2061 |
c.fullname = ClassSymbol.formFullName(name, owner); |
|
2062 |
} |
|
2063 |
return c; |
|
2064 |
} |
|
2065 |
||
2066 |
/** |
|
2067 |
* Creates a new toplevel class symbol with given flat name and |
|
2068 |
* given class (or source) file. |
|
2069 |
* |
|
2070 |
* @param flatName a fully qualified binary class name |
|
2071 |
* @param classFile the class file or compilation unit defining |
|
2072 |
* the class (may be {@code null}) |
|
2073 |
* @return a newly created class symbol |
|
2074 |
* @throws AssertionError if the class symbol already exists |
|
2075 |
*/ |
|
2076 |
public ClassSymbol enterClass(Name flatName, JavaFileObject classFile) { |
|
2077 |
ClassSymbol cs = classes.get(flatName); |
|
2078 |
if (cs != null) { |
|
2079 |
String msg = Log.format("%s: completer = %s; class file = %s; source file = %s", |
|
2080 |
cs.fullname, |
|
2081 |
cs.completer, |
|
2082 |
cs.classfile, |
|
2083 |
cs.sourcefile); |
|
2084 |
throw new AssertionError(msg); |
|
2085 |
} |
|
2086 |
Name packageName = Convert.packagePart(flatName); |
|
2087 |
PackageSymbol owner = packageName.isEmpty() |
|
2088 |
? syms.unnamedPackage |
|
2089 |
: enterPackage(packageName); |
|
2090 |
cs = defineClass(Convert.shortName(flatName), owner); |
|
2091 |
cs.classfile = classFile; |
|
2092 |
classes.put(flatName, cs); |
|
2093 |
return cs; |
|
2094 |
} |
|
2095 |
||
2096 |
/** Create a new member or toplevel class symbol with given flat name |
|
2097 |
* and enter in `classes' unless already there. |
|
2098 |
*/ |
|
2099 |
public ClassSymbol enterClass(Name flatname) { |
|
2100 |
ClassSymbol c = classes.get(flatname); |
|
2101 |
if (c == null) |
|
2102 |
return enterClass(flatname, (JavaFileObject)null); |
|
2103 |
else |
|
2104 |
return c; |
|
2105 |
} |
|
2106 |
||
2107 |
private boolean suppressFlush = false; |
|
2108 |
||
2109 |
/** Completion for classes to be loaded. Before a class is loaded |
|
2110 |
* we make sure its enclosing class (if any) is loaded. |
|
2111 |
*/ |
|
2112 |
public void complete(Symbol sym) throws CompletionFailure { |
|
2113 |
if (sym.kind == TYP) { |
|
2114 |
ClassSymbol c = (ClassSymbol)sym; |
|
2115 |
c.members_field = new Scope.ErrorScope(c); // make sure it's always defined |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
2116 |
boolean saveSuppressFlush = suppressFlush; |
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
2117 |
suppressFlush = true; |
10 | 2118 |
try { |
2119 |
completeOwners(c.owner); |
|
2120 |
completeEnclosing(c); |
|
2121 |
} finally { |
|
2513
1cbbf23b22f9
6817950: refactor ClassReader to improve attribute handling
jjg
parents:
1260
diff
changeset
|
2122 |
suppressFlush = saveSuppressFlush; |
10 | 2123 |
} |
2124 |
fillIn(c); |
|
2125 |
} else if (sym.kind == PCK) { |
|
2126 |
PackageSymbol p = (PackageSymbol)sym; |
|
2127 |
try { |
|
2128 |
fillIn(p); |
|
2129 |
} catch (IOException ex) { |
|
2130 |
throw new CompletionFailure(sym, ex.getLocalizedMessage()).initCause(ex); |
|
2131 |
} |
|
2132 |
} |
|
2133 |
if (!filling && !suppressFlush) |
|
2134 |
annotate.flush(); // finish attaching annotations |
|
2135 |
} |
|
2136 |
||
2137 |
/** complete up through the enclosing package. */ |
|
2138 |
private void completeOwners(Symbol o) { |
|
2139 |
if (o.kind != PCK) completeOwners(o.owner); |
|
2140 |
o.complete(); |
|
2141 |
} |
|
2142 |
||
2143 |
/** |
|
2144 |
* Tries to complete lexically enclosing classes if c looks like a |
|
2145 |
* nested class. This is similar to completeOwners but handles |
|
2146 |
* the situation when a nested class is accessed directly as it is |
|
2147 |
* possible with the Tree API or javax.lang.model.*. |
|
2148 |
*/ |
|
2149 |
private void completeEnclosing(ClassSymbol c) { |
|
2150 |
if (c.owner.kind == PCK) { |
|
2151 |
Symbol owner = c.owner; |
|
2152 |
for (Name name : Convert.enclosingCandidates(Convert.shortName(c.name))) { |
|
2153 |
Symbol encl = owner.members().lookup(name).sym; |
|
2154 |
if (encl == null) |
|
2155 |
encl = classes.get(TypeSymbol.formFlatName(name, owner)); |
|
2156 |
if (encl != null) |
|
2157 |
encl.complete(); |
|
2158 |
} |
|
2159 |
} |
|
2160 |
} |
|
2161 |
||
2162 |
/** We can only read a single class file at a time; this |
|
2163 |
* flag keeps track of when we are currently reading a class |
|
2164 |
* file. |
|
2165 |
*/ |
|
2166 |
private boolean filling = false; |
|
2167 |
||
2168 |
/** Fill in definition of class `c' from corresponding class or |
|
2169 |
* source file. |
|
2170 |
*/ |
|
2171 |
private void fillIn(ClassSymbol c) { |
|
2172 |
if (completionFailureName == c.fullname) { |
|
2173 |
throw new CompletionFailure(c, "user-selected completion failure by class name"); |
|
2174 |
} |
|
2175 |
currentOwner = c; |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7330
diff
changeset
|
2176 |
warnedAttrs.clear(); |
10 | 2177 |
JavaFileObject classfile = c.classfile; |
2178 |
if (classfile != null) { |
|
2179 |
JavaFileObject previousClassFile = currentClassFile; |
|
2180 |
try { |
|
8032 | 2181 |
if (filling) { |
2182 |
Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile); |
|
2183 |
} |
|
10 | 2184 |
currentClassFile = classfile; |
2185 |
if (verbose) { |
|
8630 | 2186 |
log.printVerbose("loading", currentClassFile.toString()); |
10 | 2187 |
} |
2188 |
if (classfile.getKind() == JavaFileObject.Kind.CLASS) { |
|
2189 |
filling = true; |
|
2190 |
try { |
|
2191 |
bp = 0; |
|
2192 |
buf = readInputStream(buf, classfile.openInputStream()); |
|
2193 |
readClassFile(c); |
|
2194 |
if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) { |
|
2195 |
List<Type> missing = missingTypeVariables; |
|
2196 |
List<Type> found = foundTypeVariables; |
|
2197 |
missingTypeVariables = List.nil(); |
|
2198 |
foundTypeVariables = List.nil(); |
|
2199 |
filling = false; |
|
2200 |
ClassType ct = (ClassType)currentOwner.type; |
|
2201 |
ct.supertype_field = |
|
2202 |
types.subst(ct.supertype_field, missing, found); |
|
2203 |
ct.interfaces_field = |
|
2204 |
types.subst(ct.interfaces_field, missing, found); |
|
2205 |
} else if (missingTypeVariables.isEmpty() != |
|
2206 |
foundTypeVariables.isEmpty()) { |
|
2207 |
Name name = missingTypeVariables.head.tsym.name; |
|
2208 |
throw badClassFile("undecl.type.var", name); |
|
2209 |
} |
|
2210 |
} finally { |
|
2211 |
missingTypeVariables = List.nil(); |
|
2212 |
foundTypeVariables = List.nil(); |
|
2213 |
filling = false; |
|
2214 |
} |
|
2215 |
} else { |
|
2216 |
if (sourceCompleter != null) { |
|
2217 |
sourceCompleter.complete(c); |
|
2218 |
} else { |
|
2219 |
throw new IllegalStateException("Source completer required to read " |
|
2220 |
+ classfile.toUri()); |
|
2221 |
} |
|
2222 |
} |
|
2223 |
return; |
|
2224 |
} catch (IOException ex) { |
|
2225 |
throw badClassFile("unable.to.access.file", ex.getMessage()); |
|
2226 |
} finally { |
|
2227 |
currentClassFile = previousClassFile; |
|
2228 |
} |
|
2229 |
} else { |
|
169 | 2230 |
JCDiagnostic diag = |
2231 |
diagFactory.fragment("class.file.not.found", c.flatname); |
|
10 | 2232 |
throw |
169 | 2233 |
newCompletionFailure(c, diag); |
10 | 2234 |
} |
2235 |
} |
|
2236 |
// where |
|
2237 |
private static byte[] readInputStream(byte[] buf, InputStream s) throws IOException { |
|
2238 |
try { |
|
2239 |
buf = ensureCapacity(buf, s.available()); |
|
2240 |
int r = s.read(buf); |
|
2241 |
int bp = 0; |
|
2242 |
while (r != -1) { |
|
2243 |
bp += r; |
|
2244 |
buf = ensureCapacity(buf, bp); |
|
2245 |
r = s.read(buf, bp, buf.length - bp); |
|
2246 |
} |
|
2247 |
return buf; |
|
2248 |
} finally { |
|
2249 |
try { |
|
2250 |
s.close(); |
|
2251 |
} catch (IOException e) { |
|
2252 |
/* Ignore any errors, as this stream may have already |
|
2253 |
* thrown a related exception which is the one that |
|
2254 |
* should be reported. |
|
2255 |
*/ |
|
2256 |
} |
|
2257 |
} |
|
2258 |
} |
|
7842 | 2259 |
/* |
2260 |
* ensureCapacity will increase the buffer as needed, taking note that |
|
2261 |
* the new buffer will always be greater than the needed and never |
|
2262 |
* exactly equal to the needed size or bp. If equal then the read (above) |
|
2263 |
* will infinitely loop as buf.length - bp == 0. |
|
2264 |
*/ |
|
10 | 2265 |
private static byte[] ensureCapacity(byte[] buf, int needed) { |
7842 | 2266 |
if (buf.length <= needed) { |
10 | 2267 |
byte[] old = buf; |
2268 |
buf = new byte[Integer.highestOneBit(needed) << 1]; |
|
2269 |
System.arraycopy(old, 0, buf, 0, old.length); |
|
2270 |
} |
|
2271 |
return buf; |
|
2272 |
} |
|
2273 |
/** Static factory for CompletionFailure objects. |
|
2274 |
* In practice, only one can be used at a time, so we share one |
|
2275 |
* to reduce the expense of allocating new exception objects. |
|
2276 |
*/ |
|
169 | 2277 |
private CompletionFailure newCompletionFailure(TypeSymbol c, |
2278 |
JCDiagnostic diag) { |
|
10 | 2279 |
if (!cacheCompletionFailure) { |
2280 |
// log.warning("proc.messager", |
|
2281 |
// Log.getLocalizedString("class.file.not.found", c.flatname)); |
|
2282 |
// c.debug.printStackTrace(); |
|
169 | 2283 |
return new CompletionFailure(c, diag); |
10 | 2284 |
} else { |
2285 |
CompletionFailure result = cachedCompletionFailure; |
|
2286 |
result.sym = c; |
|
169 | 2287 |
result.diag = diag; |
10 | 2288 |
return result; |
2289 |
} |
|
2290 |
} |
|
2291 |
private CompletionFailure cachedCompletionFailure = |
|
169 | 2292 |
new CompletionFailure(null, (JCDiagnostic) null); |
10 | 2293 |
{ |
2294 |
cachedCompletionFailure.setStackTrace(new StackTraceElement[0]); |
|
2295 |
} |
|
2296 |
||
2297 |
/** Load a toplevel class with given fully qualified name |
|
2298 |
* The class is entered into `classes' only if load was successful. |
|
2299 |
*/ |
|
2300 |
public ClassSymbol loadClass(Name flatname) throws CompletionFailure { |
|
2301 |
boolean absent = classes.get(flatname) == null; |
|
2302 |
ClassSymbol c = enterClass(flatname); |
|
2303 |
if (c.members_field == null && c.completer != null) { |
|
2304 |
try { |
|
2305 |
c.complete(); |
|
2306 |
} catch (CompletionFailure ex) { |
|
2307 |
if (absent) classes.remove(flatname); |
|
2308 |
throw ex; |
|
2309 |
} |
|
2310 |
} |
|
2311 |
return c; |
|
2312 |
} |
|
2313 |
||
2314 |
/************************************************************************ |
|
2315 |
* Loading Packages |
|
2316 |
***********************************************************************/ |
|
2317 |
||
2318 |
/** Check to see if a package exists, given its fully qualified name. |
|
2319 |
*/ |
|
2320 |
public boolean packageExists(Name fullname) { |
|
2321 |
return enterPackage(fullname).exists(); |
|
2322 |
} |
|
2323 |
||
2324 |
/** Make a package, given its fully qualified name. |
|
2325 |
*/ |
|
2326 |
public PackageSymbol enterPackage(Name fullname) { |
|
2327 |
PackageSymbol p = packages.get(fullname); |
|
2328 |
if (p == null) { |
|
8032 | 2329 |
Assert.check(!fullname.isEmpty(), "rootPackage missing!"); |
10 | 2330 |
p = new PackageSymbol( |
2331 |
Convert.shortName(fullname), |
|
2332 |
enterPackage(Convert.packagePart(fullname))); |
|
2333 |
p.completer = this; |
|
2334 |
packages.put(fullname, p); |
|
2335 |
} |
|
2336 |
return p; |
|
2337 |
} |
|
2338 |
||
2339 |
/** Make a package, given its unqualified name and enclosing package. |
|
2340 |
*/ |
|
2341 |
public PackageSymbol enterPackage(Name name, PackageSymbol owner) { |
|
2342 |
return enterPackage(TypeSymbol.formFullName(name, owner)); |
|
2343 |
} |
|
2344 |
||
2345 |
/** Include class corresponding to given class file in package, |
|
2346 |
* unless (1) we already have one the same kind (.class or .java), or |
|
2347 |
* (2) we have one of the other kind, and the given class file |
|
2348 |
* is older. |
|
2349 |
*/ |
|
2350 |
protected void includeClassFile(PackageSymbol p, JavaFileObject file) { |
|
2351 |
if ((p.flags_field & EXISTS) == 0) |
|
2352 |
for (Symbol q = p; q != null && q.kind == PCK; q = q.owner) |
|
2353 |
q.flags_field |= EXISTS; |
|
2354 |
JavaFileObject.Kind kind = file.getKind(); |
|
2355 |
int seen; |
|
2356 |
if (kind == JavaFileObject.Kind.CLASS) |
|
2357 |
seen = CLASS_SEEN; |
|
2358 |
else |
|
2359 |
seen = SOURCE_SEEN; |
|
2360 |
String binaryName = fileManager.inferBinaryName(currentLoc, file); |
|
2361 |
int lastDot = binaryName.lastIndexOf("."); |
|
2362 |
Name classname = names.fromString(binaryName.substring(lastDot + 1)); |
|
2363 |
boolean isPkgInfo = classname == names.package_info; |
|
2364 |
ClassSymbol c = isPkgInfo |
|
2365 |
? p.package_info |
|
2366 |
: (ClassSymbol) p.members_field.lookup(classname).sym; |
|
2367 |
if (c == null) { |
|
2368 |
c = enterClass(classname, p); |
|
2369 |
if (c.classfile == null) // only update the file if's it's newly created |
|
2370 |
c.classfile = file; |
|
2371 |
if (isPkgInfo) { |
|
2372 |
p.package_info = c; |
|
2373 |
} else { |
|
2374 |
if (c.owner == p) // it might be an inner class |
|
2375 |
p.members_field.enter(c); |
|
2376 |
} |
|
2377 |
} else if (c.classfile != null && (c.flags_field & seen) == 0) { |
|
2378 |
// if c.classfile == null, we are currently compiling this class |
|
2379 |
// and no further action is necessary. |
|
2380 |
// if (c.flags_field & seen) != 0, we have already encountered |
|
2381 |
// a file of the same kind; again no further action is necessary. |
|
2382 |
if ((c.flags_field & (CLASS_SEEN | SOURCE_SEEN)) != 0) |
|
2383 |
c.classfile = preferredFileObject(file, c.classfile); |
|
2384 |
} |
|
2385 |
c.flags_field |= seen; |
|
2386 |
} |
|
2387 |
||
2388 |
/** Implement policy to choose to derive information from a source |
|
2389 |
* file or a class file when both are present. May be overridden |
|
2390 |
* by subclasses. |
|
2391 |
*/ |
|
2392 |
protected JavaFileObject preferredFileObject(JavaFileObject a, |
|
2393 |
JavaFileObject b) { |
|
2394 |
||
2395 |
if (preferSource) |
|
2396 |
return (a.getKind() == JavaFileObject.Kind.SOURCE) ? a : b; |
|
2397 |
else { |
|
2398 |
long adate = a.getLastModified(); |
|
2399 |
long bdate = b.getLastModified(); |
|
2400 |
// 6449326: policy for bad lastModifiedTime in ClassReader |
|
2401 |
//assert adate >= 0 && bdate >= 0; |
|
2402 |
return (adate > bdate) ? a : b; |
|
2403 |
} |
|
2404 |
} |
|
2405 |
||
2406 |
/** |
|
2407 |
* specifies types of files to be read when filling in a package symbol |
|
2408 |
*/ |
|
2409 |
protected EnumSet<JavaFileObject.Kind> getPackageFileKinds() { |
|
2410 |
return EnumSet.of(JavaFileObject.Kind.CLASS, JavaFileObject.Kind.SOURCE); |
|
2411 |
} |
|
2412 |
||
2413 |
/** |
|
2414 |
* this is used to support javadoc |
|
2415 |
*/ |
|
2416 |
protected void extraFileActions(PackageSymbol pack, JavaFileObject fe) { |
|
2417 |
} |
|
2418 |
||
2419 |
protected Location currentLoc; // FIXME |
|
2420 |
||
2421 |
private boolean verbosePath = true; |
|
2422 |
||
2423 |
/** Load directory of package into members scope. |
|
2424 |
*/ |
|
2425 |
private void fillIn(PackageSymbol p) throws IOException { |
|
2426 |
if (p.members_field == null) p.members_field = new Scope(p); |
|
2427 |
String packageName = p.fullname.toString(); |
|
2428 |
||
2429 |
Set<JavaFileObject.Kind> kinds = getPackageFileKinds(); |
|
2430 |
||
2431 |
fillIn(p, PLATFORM_CLASS_PATH, |
|
2432 |
fileManager.list(PLATFORM_CLASS_PATH, |
|
2433 |
packageName, |
|
2434 |
EnumSet.of(JavaFileObject.Kind.CLASS), |
|
2435 |
false)); |
|
2436 |
||
2437 |
Set<JavaFileObject.Kind> classKinds = EnumSet.copyOf(kinds); |
|
2438 |
classKinds.remove(JavaFileObject.Kind.SOURCE); |
|
2439 |
boolean wantClassFiles = !classKinds.isEmpty(); |
|
2440 |
||
2441 |
Set<JavaFileObject.Kind> sourceKinds = EnumSet.copyOf(kinds); |
|
2442 |
sourceKinds.remove(JavaFileObject.Kind.CLASS); |
|
2443 |
boolean wantSourceFiles = !sourceKinds.isEmpty(); |
|
2444 |
||
2445 |
boolean haveSourcePath = fileManager.hasLocation(SOURCE_PATH); |
|
2446 |
||
2447 |
if (verbose && verbosePath) { |
|
2448 |
if (fileManager instanceof StandardJavaFileManager) { |
|
2449 |
StandardJavaFileManager fm = (StandardJavaFileManager)fileManager; |
|
2450 |
if (haveSourcePath && wantSourceFiles) { |
|
2451 |
List<File> path = List.nil(); |
|
2452 |
for (File file : fm.getLocation(SOURCE_PATH)) { |
|
2453 |
path = path.prepend(file); |
|
2454 |
} |
|
8630 | 2455 |
log.printVerbose("sourcepath", path.reverse().toString()); |
10 | 2456 |
} else if (wantSourceFiles) { |
2457 |
List<File> path = List.nil(); |
|
2458 |
for (File file : fm.getLocation(CLASS_PATH)) { |
|
2459 |
path = path.prepend(file); |
|
2460 |
} |
|
8630 | 2461 |
log.printVerbose("sourcepath", path.reverse().toString()); |
10 | 2462 |
} |
2463 |
if (wantClassFiles) { |
|
2464 |
List<File> path = List.nil(); |
|
2465 |
for (File file : fm.getLocation(PLATFORM_CLASS_PATH)) { |
|
2466 |
path = path.prepend(file); |
|
2467 |
} |
|
2468 |
for (File file : fm.getLocation(CLASS_PATH)) { |
|
2469 |
path = path.prepend(file); |
|
2470 |
} |
|
8630 | 2471 |
log.printVerbose("classpath", path.reverse().toString()); |
10 | 2472 |
} |
2473 |
} |
|
2474 |
} |
|
2475 |
||
2476 |
if (wantSourceFiles && !haveSourcePath) { |
|
2477 |
fillIn(p, CLASS_PATH, |
|
2478 |
fileManager.list(CLASS_PATH, |
|
2479 |
packageName, |
|
2480 |
kinds, |
|
2481 |
false)); |
|
2482 |
} else { |
|
2483 |
if (wantClassFiles) |
|
2484 |
fillIn(p, CLASS_PATH, |
|
2485 |
fileManager.list(CLASS_PATH, |
|
2486 |
packageName, |
|
2487 |
classKinds, |
|
2488 |
false)); |
|
2489 |
if (wantSourceFiles) |
|
2490 |
fillIn(p, SOURCE_PATH, |
|
2491 |
fileManager.list(SOURCE_PATH, |
|
2492 |
packageName, |
|
2493 |
sourceKinds, |
|
2494 |
false)); |
|
2495 |
} |
|
2496 |
verbosePath = false; |
|
2497 |
} |
|
2498 |
// where |
|
2499 |
private void fillIn(PackageSymbol p, |
|
2500 |
Location location, |
|
2501 |
Iterable<JavaFileObject> files) |
|
2502 |
{ |
|
2503 |
currentLoc = location; |
|
2504 |
for (JavaFileObject fo : files) { |
|
2505 |
switch (fo.getKind()) { |
|
2506 |
case CLASS: |
|
2507 |
case SOURCE: { |
|
2508 |
// TODO pass binaryName to includeClassFile |
|
2509 |
String binaryName = fileManager.inferBinaryName(currentLoc, fo); |
|
2510 |
String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1); |
|
2511 |
if (SourceVersion.isIdentifier(simpleName) || |
|
2512 |
simpleName.equals("package-info")) |
|
2513 |
includeClassFile(p, fo); |
|
2514 |
break; |
|
2515 |
} |
|
2516 |
default: |
|
2517 |
extraFileActions(p, fo); |
|
2518 |
} |
|
2519 |
} |
|
2520 |
} |
|
2521 |
||
2522 |
/** Output for "-checkclassfile" option. |
|
2523 |
* @param key The key to look up the correct internationalized string. |
|
2524 |
* @param arg An argument for substitution into the output string. |
|
2525 |
*/ |
|
2526 |
private void printCCF(String key, Object arg) { |
|
6143
79b7dee406cc
6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents:
5857
diff
changeset
|
2527 |
log.printNoteLines(key, arg); |
10 | 2528 |
} |
2529 |
||
2530 |
||
2531 |
public interface SourceCompleter { |
|
2532 |
void complete(ClassSymbol sym) |
|
2533 |
throws CompletionFailure; |
|
2534 |
} |
|
2535 |
||
2536 |
/** |
|
2537 |
* A subclass of JavaFileObject for the sourcefile attribute found in a classfile. |
|
2538 |
* The attribute is only the last component of the original filename, so is unlikely |
|
2539 |
* to be valid as is, so operations other than those to access the name throw |
|
2540 |
* UnsupportedOperationException |
|
2541 |
*/ |
|
2542 |
private static class SourceFileObject extends BaseFileObject { |
|
2543 |
||
2544 |
/** The file's name. |
|
2545 |
*/ |
|
2546 |
private Name name; |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
731
diff
changeset
|
2547 |
private Name flatname; |
10 | 2548 |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
731
diff
changeset
|
2549 |
public SourceFileObject(Name name, Name flatname) { |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
731
diff
changeset
|
2550 |
super(null); // no file manager; never referenced for this file object |
10 | 2551 |
this.name = name; |
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
731
diff
changeset
|
2552 |
this.flatname = flatname; |
10 | 2553 |
} |
2554 |
||
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2555 |
@Override |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2556 |
public URI toUri() { |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2557 |
try { |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2558 |
return new URI(null, name.toString(), null); |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2559 |
} catch (URISyntaxException e) { |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2560 |
throw new CannotCreateUriError(name.toString(), e); |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2561 |
} |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2562 |
} |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2563 |
|
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2564 |
@Override |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2565 |
public String getName() { |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2566 |
return name.toString(); |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2567 |
} |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2568 |
|
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2569 |
@Override |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2570 |
public String getShortName() { |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2571 |
return getName(); |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2572 |
} |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2573 |
|
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2574 |
@Override |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2575 |
public JavaFileObject.Kind getKind() { |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2576 |
return getKind(getName()); |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2577 |
} |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2578 |
|
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2579 |
@Override |
10 | 2580 |
public InputStream openInputStream() { |
2581 |
throw new UnsupportedOperationException(); |
|
2582 |
} |
|
2583 |
||
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2584 |
@Override |
10 | 2585 |
public OutputStream openOutputStream() { |
2586 |
throw new UnsupportedOperationException(); |
|
2587 |
} |
|
2588 |
||
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2589 |
@Override |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2590 |
public CharBuffer getCharContent(boolean ignoreEncodingErrors) { |
10 | 2591 |
throw new UnsupportedOperationException(); |
2592 |
} |
|
2593 |
||
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2594 |
@Override |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2595 |
public Reader openReader(boolean ignoreEncodingErrors) { |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2596 |
throw new UnsupportedOperationException(); |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2597 |
} |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2598 |
|
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2599 |
@Override |
10 | 2600 |
public Writer openWriter() { |
2601 |
throw new UnsupportedOperationException(); |
|
2602 |
} |
|
2603 |
||
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2604 |
@Override |
10 | 2605 |
public long getLastModified() { |
2606 |
throw new UnsupportedOperationException(); |
|
2607 |
} |
|
2608 |
||
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2609 |
@Override |
10 | 2610 |
public boolean delete() { |
2611 |
throw new UnsupportedOperationException(); |
|
2612 |
} |
|
2613 |
||
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2614 |
@Override |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2615 |
protected String inferBinaryName(Iterable<? extends File> path) { |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2616 |
return flatname.toString(); |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2617 |
} |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2618 |
|
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2619 |
@Override |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2620 |
public boolean isNameCompatible(String simpleName, JavaFileObject.Kind kind) { |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3782
diff
changeset
|
2621 |
return true; // fail-safe mode |
10 | 2622 |
} |
2623 |
||
4073
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
2624 |
/** |
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
2625 |
* Check if two file objects are equal. |
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
2626 |
* SourceFileObjects are just placeholder objects for the value of a |
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
2627 |
* SourceFile attribute, and do not directly represent specific files. |
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
2628 |
* Two SourceFileObjects are equal if their names are equal. |
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
2629 |
*/ |
10 | 2630 |
@Override |
2631 |
public boolean equals(Object other) { |
|
4073
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
2632 |
if (this == other) |
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
2633 |
return true; |
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
2634 |
|
10 | 2635 |
if (!(other instanceof SourceFileObject)) |
2636 |
return false; |
|
4073
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
2637 |
|
10 | 2638 |
SourceFileObject o = (SourceFileObject) other; |
2639 |
return name.equals(o.name); |
|
2640 |
} |
|
2641 |
||
2642 |
@Override |
|
2643 |
public int hashCode() { |
|
2644 |
return name.hashCode(); |
|
2645 |
} |
|
2646 |
} |
|
2647 |
} |