author | ntoda |
Thu, 31 Jul 2014 17:01:24 -0700 | |
changeset 25799 | 1afc4675dc75 |
parent 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.tools.java; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
import java.io.OutputStream; |
|
30 |
import java.io.PrintStream; |
|
31 |
import sun.tools.tree.Context; |
|
32 |
import sun.tools.tree.Vset; |
|
33 |
import sun.tools.tree.Expression; |
|
34 |
import sun.tools.tree.LocalMember; |
|
35 |
import sun.tools.tree.UplevelReference; |
|
36 |
||
37 |
/** |
|
38 |
* This class is a Java class definition |
|
39 |
* |
|
40 |
* WARNING: The contents of this source file are not part of any |
|
41 |
* supported API. Code that depends on them does so at its own risk: |
|
42 |
* they are subject to change or removal without notice. |
|
43 |
*/ |
|
44 |
public |
|
45 |
class ClassDefinition implements Constants { |
|
46 |
||
47 |
protected Object source; |
|
48 |
protected long where; |
|
49 |
protected int modifiers; |
|
50 |
protected Identifier localName; // for local classes |
|
51 |
protected ClassDeclaration declaration; |
|
52 |
protected IdentifierToken superClassId; |
|
53 |
protected IdentifierToken interfaceIds[]; |
|
54 |
protected ClassDeclaration superClass; |
|
55 |
protected ClassDeclaration interfaces[]; |
|
56 |
protected ClassDefinition outerClass; |
|
57 |
protected MemberDefinition outerMember; |
|
58 |
protected MemberDefinition innerClassMember; // field for me in outerClass |
|
59 |
protected MemberDefinition firstMember; |
|
60 |
protected MemberDefinition lastMember; |
|
61 |
protected boolean resolved; |
|
62 |
protected String documentation; |
|
63 |
protected boolean error; |
|
64 |
protected boolean nestError; |
|
65 |
protected UplevelReference references; |
|
66 |
protected boolean referencesFrozen; |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
67 |
private Hashtable<Identifier, MemberDefinition> fieldHash = new Hashtable<>(31); |
2 | 68 |
private int abstr; |
69 |
||
70 |
// Table of local and anonymous classes whose internal names are constructed |
|
71 |
// using the current class as a prefix. This is part of a fix for |
|
72 |
// bugid 4054523 and 4030421. See also 'Environment.getClassDefinition' |
|
73 |
// and 'BatchEnvironment.makeClassDefinition'. Allocated on demand. |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
74 |
private Hashtable<String, ClassDefinition> localClasses = null; |
2 | 75 |
private final int LOCAL_CLASSES_SIZE = 31; |
76 |
||
77 |
// The immediately surrounding context in which the class appears. |
|
78 |
// Set at the beginning of checking, upon entry to 'SourceClass.checkInternal'. |
|
79 |
// Null for classes that are not local or inside a local class. |
|
80 |
// At present, this field exists only for the benefit of 'resolveName' as part |
|
81 |
// of the fix for 4095716. |
|
82 |
protected Context classContext; |
|
83 |
||
84 |
// The saved class context is now also used in 'SourceClass.getAccessMember'. |
|
85 |
// Provide read-only access via this method. Part of fix for 4098093. |
|
86 |
public Context getClassContext() { |
|
87 |
return classContext; |
|
88 |
} |
|
89 |
||
90 |
||
91 |
/** |
|
92 |
* Constructor |
|
93 |
*/ |
|
94 |
protected ClassDefinition(Object source, long where, ClassDeclaration declaration, |
|
95 |
int modifiers, IdentifierToken superClass, IdentifierToken interfaces[]) { |
|
96 |
this.source = source; |
|
97 |
this.where = where; |
|
98 |
this.declaration = declaration; |
|
99 |
this.modifiers = modifiers; |
|
100 |
this.superClassId = superClass; |
|
101 |
this.interfaceIds = interfaces; |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Get the source of the class |
|
106 |
*/ |
|
107 |
public final Object getSource() { |
|
108 |
return source; |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Check if there were any errors in this class. |
|
113 |
*/ |
|
114 |
public final boolean getError() { |
|
115 |
return error; |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Mark this class to be erroneous. |
|
120 |
*/ |
|
121 |
public final void setError() { |
|
122 |
this.error = true; |
|
123 |
setNestError(); |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Check if there were any errors in our class nest. |
|
128 |
*/ |
|
129 |
public final boolean getNestError() { |
|
130 |
// Check to see if our error value is set, or if any of our |
|
131 |
// outer classes' error values are set. This will work in |
|
132 |
// conjunction with setError(), which sets the error value |
|
133 |
// of its outer class, to yield true is any of our nest |
|
134 |
// siblings has an error. This addresses bug 4111488: either |
|
135 |
// code should be generated for all classes in a nest, or |
|
136 |
// none of them. |
|
137 |
return nestError || ((outerClass != null) ? outerClass.getNestError() : false); |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* Mark this class, and all siblings in its class nest, to be |
|
142 |
* erroneous. |
|
143 |
*/ |
|
144 |
public final void setNestError() { |
|
145 |
this.nestError = true; |
|
146 |
if (outerClass != null) { |
|
147 |
// If we have an outer class, set it to be erroneous as well. |
|
148 |
// This will work in conjunction with getError(), which checks |
|
149 |
// the error value of its outer class, to set the whole class |
|
150 |
// nest to be erroneous. This address bug 4111488: either |
|
151 |
// code should be generated for all classes in a nest, or |
|
152 |
// none of them. |
|
153 |
outerClass.setNestError(); |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Get the position in the input |
|
159 |
*/ |
|
160 |
public final long getWhere() { |
|
161 |
return where; |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Get the class declaration |
|
166 |
*/ |
|
167 |
public final ClassDeclaration getClassDeclaration() { |
|
168 |
return declaration; |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Get the class' modifiers |
|
173 |
*/ |
|
174 |
public final int getModifiers() { |
|
175 |
return modifiers; |
|
176 |
} |
|
177 |
public final void subModifiers(int mod) { |
|
178 |
modifiers &= ~mod; |
|
179 |
} |
|
180 |
public final void addModifiers(int mod) { |
|
181 |
modifiers |= mod; |
|
182 |
} |
|
183 |
||
184 |
// *** DEBUG *** |
|
185 |
protected boolean supersCheckStarted = !(this instanceof sun.tools.javac.SourceClass); |
|
186 |
||
187 |
/** |
|
188 |
* Get the class' super class |
|
189 |
*/ |
|
190 |
public final ClassDeclaration getSuperClass() { |
|
191 |
/*--- |
|
192 |
if (superClass == null && superClassId != null) |
|
193 |
throw new CompilerError("getSuperClass "+superClassId); |
|
194 |
// There are obscure cases where null is the right answer, |
|
195 |
// in order to enable some error reporting later on. |
|
196 |
// For example: class T extends T.N { class N { } } |
|
197 |
---*/ |
|
198 |
||
199 |
// *** DEBUG *** |
|
200 |
// This method should not be called if the superclass has not been resolved. |
|
201 |
if (!supersCheckStarted) throw new CompilerError("unresolved super"); |
|
202 |
||
203 |
return superClass; |
|
204 |
} |
|
205 |
||
206 |
/** |
|
207 |
* Get the super class, and resolve names now if necessary. |
|
208 |
* |
|
209 |
* It is only possible to resolve names at this point if we are |
|
210 |
* a source class. The provision of this method at this level |
|
211 |
* in the class hierarchy is dubious, but see 'getInnerClass' below. |
|
212 |
* All other calls to 'getSuperClass(env)' appear in 'SourceClass'. |
|
213 |
* NOTE: An older definition of this method has been moved to |
|
214 |
* 'SourceClass', where it overrides this one. |
|
215 |
* |
|
216 |
* @see #resolveTypeStructure |
|
217 |
*/ |
|
218 |
||
219 |
public ClassDeclaration getSuperClass(Environment env) { |
|
220 |
return getSuperClass(); |
|
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Get the class' interfaces |
|
225 |
*/ |
|
226 |
public final ClassDeclaration getInterfaces()[] { |
|
227 |
if (interfaces == null) throw new CompilerError("getInterfaces"); |
|
228 |
return interfaces; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Get the class' enclosing class (or null if not inner) |
|
233 |
*/ |
|
234 |
public final ClassDefinition getOuterClass() { |
|
235 |
return outerClass; |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* Set the class' enclosing class. Must be done at most once. |
|
240 |
*/ |
|
241 |
protected final void setOuterClass(ClassDefinition outerClass) { |
|
242 |
if (this.outerClass != null) throw new CompilerError("setOuterClass"); |
|
243 |
this.outerClass = outerClass; |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* Set the class' enclosing current instance pointer. |
|
248 |
* Must be done at most once. |
|
249 |
*/ |
|
250 |
protected final void setOuterMember(MemberDefinition outerMember) { |
|
251 |
||
252 |
if (isStatic() || !isInnerClass()) throw new CompilerError("setOuterField"); |
|
253 |
if (this.outerMember != null) throw new CompilerError("setOuterField"); |
|
254 |
this.outerMember = outerMember; |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Tell if the class is inner. |
|
259 |
* This predicate also returns true for top-level nested types. |
|
260 |
* To test for a true inner class as seen by the programmer, |
|
261 |
* use <tt>!isTopLevel()</tt>. |
|
262 |
*/ |
|
263 |
public final boolean isInnerClass() { |
|
264 |
return outerClass != null; |
|
265 |
} |
|
266 |
||
267 |
/** |
|
268 |
* Tell if the class is a member of another class. |
|
269 |
* This is false for package members and for block-local classes. |
|
270 |
*/ |
|
271 |
public final boolean isMember() { |
|
272 |
return outerClass != null && !isLocal(); |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Tell if the class is "top-level", which is either a package member, |
|
277 |
* or a static member of another top-level class. |
|
278 |
*/ |
|
279 |
public final boolean isTopLevel() { |
|
280 |
return outerClass == null || isStatic() || isInterface(); |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* Tell if the class is local or inside a local class, |
|
285 |
* which means it cannot be mentioned outside of its file. |
|
286 |
*/ |
|
287 |
||
288 |
// The comment above is true only because M_LOCAL is set |
|
289 |
// whenever M_ANONYMOUS is. I think it is risky to assume that |
|
290 |
// isAnonymous(x) => isLocal(x). |
|
291 |
||
292 |
public final boolean isInsideLocal() { |
|
293 |
return isLocal() || |
|
294 |
(outerClass != null && outerClass.isInsideLocal()); |
|
295 |
} |
|
296 |
||
297 |
/** |
|
298 |
* Tell if the class is local or or anonymous class, or inside |
|
299 |
* such a class, which means it cannot be mentioned outside of |
|
300 |
* its file. |
|
301 |
*/ |
|
302 |
public final boolean isInsideLocalOrAnonymous() { |
|
303 |
return isLocal() || isAnonymous () || |
|
304 |
(outerClass != null && outerClass.isInsideLocalOrAnonymous()); |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* Return a simple identifier for this class (idNull if anonymous). |
|
309 |
*/ |
|
310 |
public Identifier getLocalName() { |
|
311 |
if (localName != null) { |
|
312 |
return localName; |
|
313 |
} |
|
314 |
// This is also the name of the innerClassMember, if any: |
|
315 |
return getName().getFlatName().getName(); |
|
316 |
} |
|
317 |
||
318 |
/** |
|
319 |
* Set the local name of a class. Must be a local class. |
|
320 |
*/ |
|
321 |
public void setLocalName(Identifier name) { |
|
322 |
if (isLocal()) { |
|
323 |
localName = name; |
|
324 |
} |
|
325 |
} |
|
326 |
||
327 |
/** |
|
328 |
* If inner, get the field for this class in the enclosing class |
|
329 |
*/ |
|
330 |
public final MemberDefinition getInnerClassMember() { |
|
331 |
if (outerClass == null) |
|
332 |
return null; |
|
333 |
if (innerClassMember == null) { |
|
334 |
// We must find the field in the outer class. |
|
335 |
Identifier nm = getName().getFlatName().getName(); |
|
336 |
for (MemberDefinition field = outerClass.getFirstMatch(nm); |
|
337 |
field != null; field = field.getNextMatch()) { |
|
338 |
if (field.isInnerClass()) { |
|
339 |
innerClassMember = field; |
|
340 |
break; |
|
341 |
} |
|
342 |
} |
|
343 |
if (innerClassMember == null) |
|
344 |
throw new CompilerError("getInnerClassField"); |
|
345 |
} |
|
346 |
return innerClassMember; |
|
347 |
} |
|
348 |
||
349 |
/** |
|
350 |
* If inner, return an innermost uplevel self pointer, if any exists. |
|
351 |
* Otherwise, return null. |
|
352 |
*/ |
|
353 |
public final MemberDefinition findOuterMember() { |
|
354 |
return outerMember; |
|
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* See if this is a (nested) static class. |
|
359 |
*/ |
|
360 |
public final boolean isStatic() { |
|
361 |
return (modifiers & ACC_STATIC) != 0; |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Get the class' top-level enclosing class |
|
366 |
*/ |
|
367 |
public final ClassDefinition getTopClass() { |
|
368 |
ClassDefinition p, q; |
|
369 |
for (p = this; (q = p.outerClass) != null; p = q) |
|
370 |
; |
|
371 |
return p; |
|
372 |
} |
|
373 |
||
374 |
/** |
|
375 |
* Get the class' first field or first match |
|
376 |
*/ |
|
377 |
public final MemberDefinition getFirstMember() { |
|
378 |
return firstMember; |
|
379 |
} |
|
380 |
public final MemberDefinition getFirstMatch(Identifier name) { |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
381 |
return fieldHash.get(name); |
2 | 382 |
} |
383 |
||
384 |
/** |
|
385 |
* Get the class' name |
|
386 |
*/ |
|
387 |
public final Identifier getName() { |
|
388 |
return declaration.getName(); |
|
389 |
} |
|
390 |
||
391 |
/** |
|
392 |
* Get the class' type |
|
393 |
*/ |
|
394 |
public final Type getType() { |
|
395 |
return declaration.getType(); |
|
396 |
} |
|
397 |
||
398 |
/** |
|
399 |
* Get the class' documentation |
|
400 |
*/ |
|
401 |
public String getDocumentation() { |
|
402 |
return documentation; |
|
403 |
} |
|
404 |
||
405 |
/** |
|
406 |
* Return true if the given documentation string contains a deprecation |
|
407 |
* paragraph. This is true if the string contains the tag @deprecated |
|
408 |
* is the first word in a line. |
|
409 |
*/ |
|
410 |
public static boolean containsDeprecated(String documentation) { |
|
411 |
if (documentation == null) { |
|
412 |
return false; |
|
413 |
} |
|
414 |
doScan: |
|
415 |
for (int scan = 0; |
|
416 |
(scan = documentation.indexOf(paraDeprecated, scan)) >= 0; |
|
417 |
scan += paraDeprecated.length()) { |
|
418 |
// make sure there is only whitespace between this word |
|
419 |
// and the beginning of the line |
|
420 |
for (int beg = scan-1; beg >= 0; beg--) { |
|
421 |
char ch = documentation.charAt(beg); |
|
422 |
if (ch == '\n' || ch == '\r') { |
|
423 |
break; // OK |
|
424 |
} |
|
425 |
if (!Character.isSpace(ch)) { |
|
426 |
continue doScan; |
|
427 |
} |
|
428 |
} |
|
429 |
// make sure the char after the word is space or end of line |
|
430 |
int end = scan+paraDeprecated.length(); |
|
431 |
if (end < documentation.length()) { |
|
432 |
char ch = documentation.charAt(end); |
|
433 |
if (!(ch == '\n' || ch == '\r') && !Character.isSpace(ch)) { |
|
434 |
continue doScan; |
|
435 |
} |
|
436 |
} |
|
437 |
return true; |
|
438 |
} |
|
439 |
return false; |
|
440 |
} |
|
441 |
||
442 |
public final boolean inSamePackage(ClassDeclaration c) { |
|
443 |
// find out if the class stored in c is defined in the same |
|
444 |
// package as the current class. |
|
445 |
return inSamePackage(c.getName().getQualifier()); |
|
446 |
} |
|
447 |
||
448 |
public final boolean inSamePackage(ClassDefinition c) { |
|
449 |
// find out if the class stored in c is defined in the same |
|
450 |
// package as the current class. |
|
451 |
return inSamePackage(c.getName().getQualifier()); |
|
452 |
} |
|
453 |
||
454 |
public final boolean inSamePackage(Identifier packageName) { |
|
455 |
return (getName().getQualifier().equals(packageName)); |
|
456 |
} |
|
457 |
||
458 |
/** |
|
459 |
* Checks |
|
460 |
*/ |
|
461 |
public final boolean isInterface() { |
|
462 |
return (getModifiers() & M_INTERFACE) != 0; |
|
463 |
} |
|
464 |
public final boolean isClass() { |
|
465 |
return (getModifiers() & M_INTERFACE) == 0; |
|
466 |
} |
|
467 |
public final boolean isPublic() { |
|
468 |
return (getModifiers() & M_PUBLIC) != 0; |
|
469 |
} |
|
470 |
public final boolean isPrivate() { |
|
471 |
return (getModifiers() & M_PRIVATE) != 0; |
|
472 |
} |
|
473 |
public final boolean isProtected() { |
|
474 |
return (getModifiers() & M_PROTECTED) != 0; |
|
475 |
} |
|
476 |
public final boolean isPackagePrivate() { |
|
477 |
return (modifiers & (M_PUBLIC | M_PRIVATE | M_PROTECTED)) == 0; |
|
478 |
} |
|
479 |
public final boolean isFinal() { |
|
480 |
return (getModifiers() & M_FINAL) != 0; |
|
481 |
} |
|
482 |
public final boolean isAbstract() { |
|
483 |
return (getModifiers() & M_ABSTRACT) != 0; |
|
484 |
} |
|
485 |
public final boolean isSynthetic() { |
|
486 |
return (getModifiers() & M_SYNTHETIC) != 0; |
|
487 |
} |
|
488 |
public final boolean isDeprecated() { |
|
489 |
return (getModifiers() & M_DEPRECATED) != 0; |
|
490 |
} |
|
491 |
public final boolean isAnonymous() { |
|
492 |
return (getModifiers() & M_ANONYMOUS) != 0; |
|
493 |
} |
|
494 |
public final boolean isLocal() { |
|
495 |
return (getModifiers() & M_LOCAL) != 0; |
|
496 |
} |
|
497 |
public final boolean hasConstructor() { |
|
498 |
return getFirstMatch(idInit) != null; |
|
499 |
} |
|
500 |
||
501 |
||
502 |
/** |
|
503 |
* Check to see if a class must be abstract. This method replaces |
|
504 |
* isAbstract(env) |
|
505 |
*/ |
|
506 |
public final boolean mustBeAbstract(Environment env) { |
|
507 |
// If it is declared abstract, return true. |
|
508 |
// (Fix for 4110534.) |
|
509 |
if (isAbstract()) { |
|
510 |
return true; |
|
511 |
} |
|
512 |
||
513 |
// Check to see if the class should have been declared to be |
|
514 |
// abstract. |
|
515 |
||
516 |
// We make sure that the inherited method collection has been |
|
517 |
// performed. |
|
518 |
collectInheritedMethods(env); |
|
519 |
||
520 |
// We check for any abstract methods inherited or declared |
|
521 |
// by this class. |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
522 |
Iterator<MemberDefinition> methods = getMethods(); |
2 | 523 |
while (methods.hasNext()) { |
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
524 |
MemberDefinition method = methods.next(); |
2 | 525 |
|
526 |
if (method.isAbstract()) { |
|
527 |
return true; |
|
528 |
} |
|
529 |
} |
|
530 |
||
531 |
// We check for hidden "permanently abstract" methods in |
|
532 |
// our superclasses. |
|
533 |
return getPermanentlyAbstractMethods().hasNext(); |
|
534 |
} |
|
535 |
||
536 |
/** |
|
537 |
* Check if this is a super class of another class |
|
538 |
*/ |
|
539 |
public boolean superClassOf(Environment env, ClassDeclaration otherClass) |
|
540 |
throws ClassNotFound { |
|
541 |
while (otherClass != null) { |
|
542 |
if (getClassDeclaration().equals(otherClass)) { |
|
543 |
return true; |
|
544 |
} |
|
545 |
otherClass = otherClass.getClassDefinition(env).getSuperClass(); |
|
546 |
} |
|
547 |
return false; |
|
548 |
} |
|
549 |
||
550 |
/** |
|
551 |
* Check if this is an enclosing class of another class |
|
552 |
*/ |
|
553 |
public boolean enclosingClassOf(ClassDefinition otherClass) { |
|
554 |
while ((otherClass = otherClass.getOuterClass()) != null) { |
|
555 |
if (this == otherClass) { |
|
556 |
return true; |
|
557 |
} |
|
558 |
} |
|
559 |
return false; |
|
560 |
} |
|
561 |
||
562 |
/** |
|
563 |
* Check if this is a sub class of another class |
|
564 |
*/ |
|
565 |
public boolean subClassOf(Environment env, ClassDeclaration otherClass) throws ClassNotFound { |
|
566 |
ClassDeclaration c = getClassDeclaration(); |
|
567 |
while (c != null) { |
|
568 |
if (c.equals(otherClass)) { |
|
569 |
return true; |
|
570 |
} |
|
571 |
c = c.getClassDefinition(env).getSuperClass(); |
|
572 |
} |
|
573 |
return false; |
|
574 |
} |
|
575 |
||
576 |
/** |
|
577 |
* Check if this class is implemented by another class |
|
578 |
*/ |
|
579 |
public boolean implementedBy(Environment env, ClassDeclaration c) throws ClassNotFound { |
|
580 |
for (; c != null ; c = c.getClassDefinition(env).getSuperClass()) { |
|
581 |
if (getClassDeclaration().equals(c)) { |
|
582 |
return true; |
|
583 |
} |
|
584 |
ClassDeclaration intf[] = c.getClassDefinition(env).getInterfaces(); |
|
585 |
for (int i = 0 ; i < intf.length ; i++) { |
|
586 |
if (implementedBy(env, intf[i])) { |
|
587 |
return true; |
|
588 |
} |
|
589 |
} |
|
590 |
} |
|
591 |
return false; |
|
592 |
} |
|
593 |
||
594 |
/** |
|
595 |
* Check to see if a class which implements interface `this' could |
|
596 |
* possibly implement the interface `intDef'. Note that the only |
|
597 |
* way that this can fail is if `this' and `intDef' have methods |
|
598 |
* which are of the same signature and different return types. This |
|
599 |
* method is used by Environment.explicitCast() to determine if a |
|
600 |
* cast between two interfaces is legal. |
|
601 |
* |
|
602 |
* This method should only be called on a class after it has been |
|
603 |
* basicCheck()'ed. |
|
604 |
*/ |
|
605 |
public boolean couldImplement(ClassDefinition intDef) { |
|
606 |
// Check to see if we could have done the necessary checks. |
|
607 |
if (!doInheritanceChecks) { |
|
608 |
throw new CompilerError("couldImplement: no checks"); |
|
609 |
} |
|
610 |
||
611 |
// This method should only be called for interfaces. |
|
612 |
if (!isInterface() || !intDef.isInterface()) { |
|
613 |
throw new CompilerError("couldImplement: not interface"); |
|
614 |
} |
|
615 |
||
616 |
// Make sure we are not called before we have collected our |
|
617 |
// inheritance information. |
|
618 |
if (allMethods == null) { |
|
619 |
throw new CompilerError("couldImplement: called early"); |
|
620 |
} |
|
621 |
||
622 |
// Get the other classes' methods. getMethods() in |
|
623 |
// general can return methods which are not visible to the |
|
624 |
// current package. We need to make sure that these do not |
|
625 |
// prevent this class from being implemented. |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
626 |
Iterator<MemberDefinition> otherMethods = intDef.getMethods(); |
2 | 627 |
|
628 |
while (otherMethods.hasNext()) { |
|
629 |
// Get one of the methods from intDef... |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
630 |
MemberDefinition method = otherMethods.next(); |
2 | 631 |
|
632 |
Identifier name = method.getName(); |
|
633 |
Type type = method.getType(); |
|
634 |
||
635 |
// See if we implement a method of the same signature... |
|
636 |
MemberDefinition myMethod = allMethods.lookupSig(name, type); |
|
637 |
||
638 |
//System.out.println("Comparing\n\t" + myMethod + |
|
639 |
// "\nand\n\t" + method); |
|
640 |
||
641 |
if (myMethod != null) { |
|
642 |
// We do. Make sure the methods have the same return type. |
|
643 |
if (!myMethod.sameReturnType(method)) { |
|
644 |
return false; |
|
645 |
} |
|
646 |
} |
|
647 |
} |
|
648 |
||
649 |
return true; |
|
650 |
} |
|
651 |
||
652 |
/** |
|
653 |
* Check if another class can be accessed from the 'extends' or 'implements' |
|
654 |
* clause of this class. |
|
655 |
*/ |
|
656 |
public boolean extendsCanAccess(Environment env, ClassDeclaration c) throws ClassNotFound { |
|
657 |
||
658 |
// Names in the 'extends' or 'implements' clause of an inner class |
|
659 |
// are checked as if they appeared in the body of the surrounding class. |
|
660 |
if (outerClass != null) { |
|
661 |
return outerClass.canAccess(env, c); |
|
662 |
} |
|
663 |
||
664 |
// We are a package member. |
|
665 |
||
666 |
ClassDefinition cdef = c.getClassDefinition(env); |
|
667 |
||
668 |
if (cdef.isLocal()) { |
|
669 |
// No locals should be in scope in the 'extends' or |
|
670 |
// 'implements' clause of a package member. |
|
671 |
throw new CompilerError("top local"); |
|
672 |
} |
|
673 |
||
674 |
if (cdef.isInnerClass()) { |
|
675 |
MemberDefinition f = cdef.getInnerClassMember(); |
|
676 |
||
677 |
// Access to public member is always allowed. |
|
678 |
if (f.isPublic()) { |
|
679 |
return true; |
|
680 |
} |
|
681 |
||
682 |
// Private access is ok only from the same class nest. This can |
|
683 |
// happen only if the class represented by 'this' encloses the inner |
|
684 |
// class represented by 'f'. |
|
685 |
if (f.isPrivate()) { |
|
686 |
return getClassDeclaration().equals(f.getTopClass().getClassDeclaration()); |
|
687 |
} |
|
688 |
||
689 |
// Protected or default access -- allow access if in same package. |
|
690 |
return getName().getQualifier().equals(f.getClassDeclaration().getName().getQualifier()); |
|
691 |
} |
|
692 |
||
693 |
// Access to public member is always allowed. |
|
694 |
if (cdef.isPublic()) { |
|
695 |
return true; |
|
696 |
} |
|
697 |
||
698 |
// Default access -- allow access if in same package. |
|
699 |
return getName().getQualifier().equals(c.getName().getQualifier()); |
|
700 |
} |
|
701 |
||
702 |
/** |
|
703 |
* Check if another class can be accessed from within the body of this class. |
|
704 |
*/ |
|
705 |
public boolean canAccess(Environment env, ClassDeclaration c) throws ClassNotFound { |
|
706 |
ClassDefinition cdef = c.getClassDefinition(env); |
|
707 |
||
708 |
if (cdef.isLocal()) { |
|
709 |
// if it's in scope, it's accessible |
|
710 |
return true; |
|
711 |
} |
|
712 |
||
713 |
if (cdef.isInnerClass()) { |
|
714 |
return canAccess(env, cdef.getInnerClassMember()); |
|
715 |
} |
|
716 |
||
717 |
// Public access is always ok |
|
718 |
if (cdef.isPublic()) { |
|
719 |
return true; |
|
720 |
} |
|
721 |
||
722 |
// It must be in the same package |
|
723 |
return getName().getQualifier().equals(c.getName().getQualifier()); |
|
724 |
} |
|
725 |
||
726 |
/** |
|
727 |
* Check if a field can be accessed from a class |
|
728 |
*/ |
|
729 |
||
730 |
public boolean canAccess(Environment env, MemberDefinition f) |
|
731 |
throws ClassNotFound { |
|
732 |
||
733 |
// Public access is always ok |
|
734 |
if (f.isPublic()) { |
|
735 |
return true; |
|
736 |
} |
|
737 |
// Protected access is ok from a subclass |
|
738 |
if (f.isProtected() && subClassOf(env, f.getClassDeclaration())) { |
|
739 |
return true; |
|
740 |
} |
|
741 |
// Private access is ok only from the same class nest |
|
742 |
if (f.isPrivate()) { |
|
743 |
return getTopClass().getClassDeclaration() |
|
744 |
.equals(f.getTopClass().getClassDeclaration()); |
|
745 |
} |
|
746 |
// It must be in the same package |
|
747 |
return getName().getQualifier().equals(f.getClassDeclaration().getName().getQualifier()); |
|
748 |
} |
|
749 |
||
750 |
/** |
|
751 |
* Check if a class is entitled to inline access to a class from |
|
752 |
* another class. |
|
753 |
*/ |
|
754 |
public boolean permitInlinedAccess(Environment env, ClassDeclaration c) |
|
755 |
throws ClassNotFound { |
|
756 |
||
757 |
return (env.opt() && c.equals(declaration)) || |
|
758 |
(env.opt_interclass() && canAccess(env, c)); |
|
759 |
} |
|
760 |
||
761 |
/** |
|
762 |
* Check if a class is entitled to inline access to a method from |
|
763 |
* another class. |
|
764 |
*/ |
|
765 |
public boolean permitInlinedAccess(Environment env, MemberDefinition f) |
|
766 |
throws ClassNotFound { |
|
767 |
return (env.opt() |
|
768 |
&& (f.clazz.getClassDeclaration().equals(declaration))) || |
|
769 |
(env.opt_interclass() && canAccess(env, f)); |
|
770 |
} |
|
771 |
||
772 |
/** |
|
773 |
* We know the the field is marked protected (and not public) and that |
|
774 |
* the field is visible (as per canAccess). Can we access the field as |
|
775 |
* <accessor>.<field>, where <accessor> has the type <accessorType>? |
|
776 |
* |
|
777 |
* Protected fields can only be accessed when the accessorType is a |
|
778 |
* subclass of the current class |
|
779 |
*/ |
|
780 |
public boolean protectedAccess(Environment env, MemberDefinition f, |
|
781 |
Type accessorType) |
|
782 |
throws ClassNotFound |
|
783 |
{ |
|
784 |
||
785 |
return |
|
786 |
// static protected fields are accessible |
|
787 |
f.isStatic() |
|
788 |
|| // allow array.clone() |
|
789 |
(accessorType.isType(TC_ARRAY) && (f.getName() == idClone) |
|
790 |
&& (f.getType().getArgumentTypes().length == 0)) |
|
791 |
|| // <accessorType> is a subtype of the current class |
|
792 |
(accessorType.isType(TC_CLASS) |
|
793 |
&& env.getClassDefinition(accessorType.getClassName()) |
|
794 |
.subClassOf(env, getClassDeclaration())) |
|
795 |
|| // we are accessing the field from a friendly class (same package) |
|
796 |
(getName().getQualifier() |
|
797 |
.equals(f.getClassDeclaration().getName().getQualifier())); |
|
798 |
} |
|
799 |
||
800 |
||
801 |
/** |
|
802 |
* Find or create an access method for a private member, |
|
803 |
* or return null if this is not possible. |
|
804 |
*/ |
|
805 |
public MemberDefinition getAccessMember(Environment env, Context ctx, |
|
806 |
MemberDefinition field, boolean isSuper) { |
|
807 |
throw new CompilerError("binary getAccessMember"); |
|
808 |
} |
|
809 |
||
810 |
/** |
|
811 |
* Find or create an update method for a private member, |
|
812 |
* or return null if this is not possible. |
|
813 |
*/ |
|
814 |
public MemberDefinition getUpdateMember(Environment env, Context ctx, |
|
815 |
MemberDefinition field, boolean isSuper) { |
|
816 |
throw new CompilerError("binary getUpdateMember"); |
|
817 |
} |
|
818 |
||
819 |
/** |
|
820 |
* Get a field from this class. Report ambiguous fields. |
|
821 |
* If no accessible field is found, this method may return an |
|
822 |
* inaccessible field to allow a useful error message. |
|
823 |
* |
|
824 |
* getVariable now takes the source class `source' as an argument. |
|
825 |
* This allows getVariable to check whether a field is inaccessible |
|
826 |
* before it signals that a field is ambiguous. The compiler used to |
|
827 |
* signal an ambiguity even when one of the fields involved was not |
|
828 |
* accessible. (bug 4053724) |
|
829 |
*/ |
|
830 |
public MemberDefinition getVariable(Environment env, |
|
831 |
Identifier nm, |
|
832 |
ClassDefinition source) |
|
833 |
throws AmbiguousMember, ClassNotFound { |
|
834 |
||
835 |
return getVariable0(env, nm, source, true, true); |
|
836 |
} |
|
837 |
||
838 |
/* |
|
839 |
* private fields are never inherited. package-private fields are |
|
840 |
* not inherited across package boundaries. To capture this, we |
|
841 |
* take two booleans as parameters: showPrivate indicates whether |
|
842 |
* we have passed a class boundary, and showPackage indicates whether |
|
843 |
* we have crossed a package boundary. |
|
844 |
*/ |
|
845 |
private MemberDefinition getVariable0(Environment env, |
|
846 |
Identifier nm, |
|
847 |
ClassDefinition source, |
|
848 |
boolean showPrivate, |
|
849 |
boolean showPackage) |
|
850 |
throws AmbiguousMember, ClassNotFound { |
|
851 |
||
852 |
// Check to see if this field is defined in the current class |
|
853 |
for (MemberDefinition member = getFirstMatch(nm); |
|
854 |
member != null; |
|
855 |
member = member.getNextMatch()) { |
|
856 |
if (member.isVariable()) { |
|
857 |
if ((showPrivate || !member.isPrivate()) && |
|
858 |
(showPackage || !member.isPackagePrivate())) { |
|
859 |
// It is defined in this class. |
|
860 |
return member; |
|
861 |
} else { |
|
862 |
// Even though this definition is not inherited, |
|
863 |
// it hides all definitions in supertypes. |
|
864 |
return null; |
|
865 |
} |
|
866 |
} |
|
867 |
} |
|
868 |
||
869 |
// Find the field in our superclass. |
|
870 |
ClassDeclaration sup = getSuperClass(); |
|
871 |
MemberDefinition field = null; |
|
872 |
if (sup != null) { |
|
873 |
field = |
|
874 |
sup.getClassDefinition(env) |
|
875 |
.getVariable0(env, nm, source, |
|
876 |
false, |
|
877 |
showPackage && inSamePackage(sup)); |
|
878 |
} |
|
879 |
||
880 |
// Find the field in our superinterfaces. |
|
881 |
for (int i = 0 ; i < interfaces.length ; i++) { |
|
882 |
// Try to look up the field in an interface. Since interfaces |
|
883 |
// only have public fields, the values of the two boolean |
|
884 |
// arguments are not important. |
|
885 |
MemberDefinition field2 = |
|
886 |
interfaces[i].getClassDefinition(env) |
|
887 |
.getVariable0(env, nm, source, true, true); |
|
888 |
||
889 |
if (field2 != null) { |
|
890 |
// If we have two different, accessible fields, then |
|
891 |
// we've found an ambiguity. |
|
892 |
if (field != null && |
|
893 |
source.canAccess(env, field) && |
|
894 |
field2 != field) { |
|
895 |
||
896 |
throw new AmbiguousMember(field2, field); |
|
897 |
} |
|
898 |
field = field2; |
|
899 |
} |
|
900 |
} |
|
901 |
return field; |
|
902 |
} |
|
903 |
||
904 |
/** |
|
905 |
* Tells whether to report a deprecation error for this class. |
|
906 |
*/ |
|
907 |
public boolean reportDeprecated(Environment env) { |
|
908 |
return (isDeprecated() |
|
909 |
|| (outerClass != null && outerClass.reportDeprecated(env))); |
|
910 |
} |
|
911 |
||
912 |
/** |
|
913 |
* Note that this class is being used somehow by <tt>ref</tt>. |
|
914 |
* Report deprecation errors, etc. |
|
915 |
*/ |
|
916 |
public void noteUsedBy(ClassDefinition ref, long where, Environment env) { |
|
917 |
// (Have this deal with canAccess() checks, too?) |
|
918 |
if (reportDeprecated(env)) { |
|
919 |
env.error(where, "warn.class.is.deprecated", this); |
|
920 |
} |
|
921 |
} |
|
922 |
||
923 |
/** |
|
924 |
* Get an inner class. |
|
925 |
* Look in supers but not outers. |
|
926 |
* (This is used directly to resolve expressions like "site.K", and |
|
927 |
* inside a loop to resolve lone names like "K" or the "K" in "K.L".) |
|
928 |
* |
|
929 |
* Called from 'Context' and 'FieldExpression' as well as this class. |
|
930 |
* |
|
931 |
* @see FieldExpression.checkCommon |
|
932 |
* @see resolveName |
|
933 |
*/ |
|
934 |
public MemberDefinition getInnerClass(Environment env, Identifier nm) |
|
935 |
throws ClassNotFound { |
|
936 |
// Note: AmbiguousClass will not be thrown unless and until |
|
937 |
// inner classes can be defined inside interfaces. |
|
938 |
||
939 |
// Check if it is defined in the current class |
|
940 |
for (MemberDefinition field = getFirstMatch(nm); |
|
941 |
field != null ; field = field.getNextMatch()) { |
|
942 |
if (field.isInnerClass()) { |
|
943 |
if (field.getInnerClass().isLocal()) { |
|
944 |
continue; // ignore this name; it is internally generated |
|
945 |
} |
|
946 |
return field; |
|
947 |
} |
|
948 |
} |
|
949 |
||
950 |
// Get it from the super class |
|
951 |
// It is likely that 'getSuperClass()' could be made to work here |
|
952 |
// but we would have to assure somehow that 'resolveTypeStructure' |
|
953 |
// has been called on the current class nest. Since we can get |
|
954 |
// here from 'resolveName', which is called from 'resolveSupers', |
|
955 |
// it is possible that the first attempt to resolve the superclass |
|
956 |
// will originate here, instead of in the call to 'getSuperClass' |
|
957 |
// in 'checkSupers'. See 'resolveTypeStructure', in which a call |
|
958 |
// to 'resolveSupers' precedes the call to 'checkSupers'. Why is |
|
959 |
// name resolution done twice, first in 'resolveName'? |
|
960 |
// NOTE: 'SourceMember.resolveTypeStructure' may initiate type |
|
961 |
// structure resolution for an inner class. Normally, this |
|
962 |
// occurs during the resolution of the outer class, but fields |
|
963 |
// added after the resolution of their containing class will |
|
964 |
// be resolved late -- see 'addMember(env,field)' below. |
|
965 |
// This should only happen for synthetic members, which should |
|
966 |
// never be an inner class. |
|
967 |
ClassDeclaration sup = getSuperClass(env); |
|
968 |
if (sup != null) |
|
969 |
return sup.getClassDefinition(env).getInnerClass(env, nm); |
|
970 |
||
971 |
return null; |
|
972 |
} |
|
973 |
||
974 |
/** |
|
975 |
* Lookup a method. This code implements the method lookup |
|
976 |
* mechanism specified in JLS 15.11.2. |
|
977 |
* |
|
978 |
* This mechanism cannot be used to lookup synthetic methods. |
|
979 |
*/ |
|
980 |
private MemberDefinition matchMethod(Environment env, |
|
981 |
ClassDefinition accessor, |
|
982 |
Identifier methodName, |
|
983 |
Type[] argumentTypes, |
|
984 |
boolean isAnonConstCall, |
|
985 |
Identifier accessPackage) |
|
986 |
throws AmbiguousMember, ClassNotFound { |
|
987 |
||
988 |
if (allMethods == null || !allMethods.isFrozen()) { |
|
989 |
// This may be too restrictive. |
|
990 |
throw new CompilerError("matchMethod called early"); |
|
991 |
// collectInheritedMethods(env); |
|
992 |
} |
|
993 |
||
994 |
// A tentative maximally specific method. |
|
995 |
MemberDefinition tentative = null; |
|
996 |
||
997 |
// A list of other methods which may be maximally specific too. |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
998 |
List<MemberDefinition> candidateList = null; |
2 | 999 |
|
1000 |
// Get all the methods inherited by this class which |
|
1001 |
// have the name `methodName'. |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1002 |
Iterator<MemberDefinition> methods = allMethods.lookupName(methodName); |
2 | 1003 |
|
1004 |
while (methods.hasNext()) { |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1005 |
MemberDefinition method = methods.next(); |
2 | 1006 |
|
1007 |
// See if this method is applicable. |
|
1008 |
if (!env.isApplicable(method, argumentTypes)) { |
|
1009 |
continue; |
|
1010 |
} |
|
1011 |
||
1012 |
// See if this method is accessible. |
|
1013 |
if (accessor != null) { |
|
1014 |
if (!accessor.canAccess(env, method)) { |
|
1015 |
continue; |
|
1016 |
} |
|
1017 |
} else if (isAnonConstCall) { |
|
1018 |
if (method.isPrivate() || |
|
1019 |
(method.isPackagePrivate() && |
|
1020 |
accessPackage != null && |
|
1021 |
!inSamePackage(accessPackage))) { |
|
1022 |
// For anonymous constructor accesses, we |
|
1023 |
// haven't yet built an accessing class. |
|
1024 |
// We disallow anonymous classes from seeing |
|
1025 |
// private/package-private inaccessible |
|
1026 |
// constructors in their superclass. |
|
1027 |
continue; |
|
1028 |
} |
|
1029 |
} else { |
|
1030 |
// If accessor is null, we assume that the access |
|
1031 |
// is allowed. Query: is this option used? |
|
1032 |
} |
|
1033 |
||
1034 |
if (tentative == null) { |
|
1035 |
// `method' becomes our tentative maximally specific match. |
|
1036 |
tentative = method; |
|
1037 |
} else { |
|
1038 |
if (env.isMoreSpecific(method, tentative)) { |
|
1039 |
// We have found a method which is a strictly better |
|
1040 |
// match than `tentative'. Replace it. |
|
1041 |
tentative = method; |
|
1042 |
} else { |
|
1043 |
// If this method could possibly be another |
|
1044 |
// maximally specific method, add it to our |
|
1045 |
// list of other candidates. |
|
1046 |
if (!env.isMoreSpecific(tentative,method)) { |
|
1047 |
if (candidateList == null) { |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1048 |
candidateList = new ArrayList<>(); |
2 | 1049 |
} |
1050 |
candidateList.add(method); |
|
1051 |
} |
|
1052 |
} |
|
1053 |
} |
|
1054 |
} |
|
1055 |
||
1056 |
if (tentative != null && candidateList != null) { |
|
1057 |
// Find out if our `tentative' match is a uniquely |
|
1058 |
// maximally specific. |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1059 |
Iterator<MemberDefinition> candidates = candidateList.iterator(); |
2 | 1060 |
while (candidates.hasNext()) { |
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1061 |
MemberDefinition method = candidates.next(); |
2 | 1062 |
if (!env.isMoreSpecific(tentative, method)) { |
1063 |
throw new AmbiguousMember(tentative, method); |
|
1064 |
} |
|
1065 |
} |
|
1066 |
} |
|
1067 |
||
1068 |
return tentative; |
|
1069 |
} |
|
1070 |
||
1071 |
/** |
|
1072 |
* Lookup a method. This code implements the method lookup |
|
1073 |
* mechanism specified in JLS 15.11.2. |
|
1074 |
* |
|
1075 |
* This mechanism cannot be used to lookup synthetic methods. |
|
1076 |
*/ |
|
1077 |
public MemberDefinition matchMethod(Environment env, |
|
1078 |
ClassDefinition accessor, |
|
1079 |
Identifier methodName, |
|
1080 |
Type[] argumentTypes) |
|
1081 |
throws AmbiguousMember, ClassNotFound { |
|
1082 |
||
1083 |
return matchMethod(env, accessor, methodName, |
|
1084 |
argumentTypes, false, null); |
|
1085 |
} |
|
1086 |
||
1087 |
/** |
|
1088 |
* Lookup a method. This code implements the method lookup |
|
1089 |
* mechanism specified in JLS 15.11.2. |
|
1090 |
* |
|
1091 |
* This mechanism cannot be used to lookup synthetic methods. |
|
1092 |
*/ |
|
1093 |
public MemberDefinition matchMethod(Environment env, |
|
1094 |
ClassDefinition accessor, |
|
1095 |
Identifier methodName) |
|
1096 |
throws AmbiguousMember, ClassNotFound { |
|
1097 |
||
1098 |
return matchMethod(env, accessor, methodName, |
|
1099 |
Type.noArgs, false, null); |
|
1100 |
} |
|
1101 |
||
1102 |
/** |
|
1103 |
* A version of matchMethod to be used only for constructors |
|
1104 |
* when we cannot pass in a sourceClass argument. We just assert |
|
1105 |
* our package name. |
|
1106 |
* |
|
1107 |
* This is used only for anonymous classes, where we have to look up |
|
1108 |
* a (potentially) protected constructor with no valid sourceClass |
|
1109 |
* parameter available. |
|
1110 |
*/ |
|
1111 |
public MemberDefinition matchAnonConstructor(Environment env, |
|
1112 |
Identifier accessPackage, |
|
1113 |
Type argumentTypes[]) |
|
1114 |
throws AmbiguousMember, ClassNotFound { |
|
1115 |
||
1116 |
return matchMethod(env, null, idInit, argumentTypes, |
|
1117 |
true, accessPackage); |
|
1118 |
} |
|
1119 |
||
1120 |
/** |
|
1121 |
* Find a method, ie: exact match in this class or any of the super |
|
1122 |
* classes. |
|
1123 |
* |
|
1124 |
* Only called by javadoc. For now I am holding off rewriting this |
|
1125 |
* code to rely on collectInheritedMethods(), as that code has |
|
1126 |
* not gotten along with javadoc in the past. |
|
1127 |
*/ |
|
1128 |
public MemberDefinition findMethod(Environment env, Identifier nm, Type t) |
|
1129 |
throws ClassNotFound { |
|
1130 |
// look in the current class |
|
1131 |
MemberDefinition f; |
|
1132 |
for (f = getFirstMatch(nm) ; f != null ; f = f.getNextMatch()) { |
|
1133 |
// Note that non-method types return false for equalArguments(). |
|
1134 |
if (f.getType().equalArguments(t)) { |
|
1135 |
return f; |
|
1136 |
} |
|
1137 |
} |
|
1138 |
||
1139 |
// constructors are not inherited |
|
1140 |
if (nm.equals(idInit)) { |
|
1141 |
return null; |
|
1142 |
} |
|
1143 |
||
1144 |
// look in the super class |
|
1145 |
ClassDeclaration sup = getSuperClass(); |
|
1146 |
if (sup == null) |
|
1147 |
return null; |
|
1148 |
||
1149 |
return sup.getClassDefinition(env).findMethod(env, nm, t); |
|
1150 |
} |
|
1151 |
||
1152 |
// We create a stub for this. Source classes do more work. |
|
1153 |
protected void basicCheck(Environment env) throws ClassNotFound { |
|
1154 |
// Do the outer class first. |
|
1155 |
if (outerClass != null) |
|
1156 |
outerClass.basicCheck(env); |
|
1157 |
} |
|
1158 |
||
1159 |
/** |
|
1160 |
* Check this class. |
|
1161 |
*/ |
|
1162 |
public void check(Environment env) throws ClassNotFound { |
|
1163 |
} |
|
1164 |
||
1165 |
public Vset checkLocalClass(Environment env, Context ctx, |
|
1166 |
Vset vset, ClassDefinition sup, |
|
1167 |
Expression args[], Type argTypes[] |
|
1168 |
) throws ClassNotFound { |
|
1169 |
throw new CompilerError("checkLocalClass"); |
|
1170 |
} |
|
1171 |
||
1172 |
//--------------------------------------------------------------- |
|
1173 |
// The non-synthetic methods defined in this class or in any |
|
1174 |
// of its parents (class or interface). This member is used |
|
1175 |
// to cache work done in collectInheritedMethods for use by |
|
1176 |
// getMethods() and matchMethod(). It should be accessed by |
|
1177 |
// no other method without forethought. |
|
1178 |
MethodSet allMethods = null; |
|
1179 |
||
1180 |
// One of our superclasses may contain an abstract method which |
|
1181 |
// we are unable to ever implement. This happens when there is |
|
1182 |
// a package-private abstract method in our parent and we are in |
|
1183 |
// a different package than our parent. In these cases, we |
|
1184 |
// keep a list of the "permanently abstract" or "unimplementable" |
|
1185 |
// methods so that we can correctly detect that this class is |
|
1186 |
// indeed abstract and so that we can give somewhat comprehensible |
|
1187 |
// error messages. |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1188 |
private List<MemberDefinition> permanentlyAbstractMethods = new ArrayList<>(); |
2 | 1189 |
|
1190 |
/** |
|
1191 |
* This method returns an Iterator of all abstract methods |
|
1192 |
* in our superclasses which we are unable to implement. |
|
1193 |
*/ |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1194 |
protected Iterator<MemberDefinition> getPermanentlyAbstractMethods() { |
2 | 1195 |
// This method can only be called after collectInheritedMethods. |
1196 |
if (allMethods == null) { |
|
1197 |
throw new CompilerError("isPermanentlyAbstract() called early"); |
|
1198 |
} |
|
1199 |
||
1200 |
return permanentlyAbstractMethods.iterator(); |
|
1201 |
} |
|
1202 |
||
1203 |
/** |
|
1204 |
* A flag used by turnOffInheritanceChecks() to indicate if |
|
1205 |
* inheritance checks are on or off. |
|
1206 |
*/ |
|
1207 |
protected static boolean doInheritanceChecks = true; |
|
1208 |
||
1209 |
/** |
|
1210 |
* This is a workaround to allow javadoc to turn off certain |
|
1211 |
* inheritance/override checks which interfere with javadoc |
|
1212 |
* badly. In the future it might be good to eliminate the |
|
1213 |
* shared sources of javadoc and javac to avoid the need for this |
|
1214 |
* sort of workaround. |
|
1215 |
*/ |
|
1216 |
public static void turnOffInheritanceChecks() { |
|
1217 |
doInheritanceChecks = false; |
|
1218 |
} |
|
1219 |
||
1220 |
/** |
|
1221 |
* Add all of the methods declared in or above `parent' to |
|
1222 |
* `allMethods', the set of methods in the current class. |
|
1223 |
* `myMethods' is the set of all methods declared in this |
|
1224 |
* class, and `mirandaMethods' is a repository for Miranda methods. |
|
1225 |
* If mirandaMethods is null, no mirandaMethods will be |
|
1226 |
* generated. |
|
1227 |
* |
|
1228 |
* For a definition of Miranda methods, see the comment above the |
|
1229 |
* method addMirandaMethods() which occurs later in this file. |
|
1230 |
*/ |
|
1231 |
private void collectOneClass(Environment env, |
|
1232 |
ClassDeclaration parent, |
|
1233 |
MethodSet myMethods, |
|
1234 |
MethodSet allMethods, |
|
1235 |
MethodSet mirandaMethods) { |
|
1236 |
||
1237 |
// System.out.println("Inheriting methods from " + parent); |
|
1238 |
||
1239 |
try { |
|
1240 |
ClassDefinition pClass = parent.getClassDefinition(env); |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1241 |
Iterator<MemberDefinition> methods = pClass.getMethods(env); |
2 | 1242 |
while (methods.hasNext()) { |
1243 |
MemberDefinition method = |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1244 |
methods.next(); |
2 | 1245 |
|
1246 |
// Private methods are not inherited. |
|
1247 |
// |
|
1248 |
// Constructors are not inherited. |
|
1249 |
// |
|
1250 |
// Any non-abstract methods in an interface come |
|
1251 |
// from java.lang.Object. This means that they |
|
1252 |
// should have already been added to allMethods |
|
1253 |
// when we walked our superclass lineage. |
|
1254 |
if (method.isPrivate() || |
|
1255 |
method.isConstructor() || |
|
1256 |
(pClass.isInterface() && !method.isAbstract())) { |
|
1257 |
||
1258 |
continue; |
|
1259 |
} |
|
1260 |
||
1261 |
// Get the components of the methods' signature. |
|
1262 |
Identifier name = method.getName(); |
|
1263 |
Type type = method.getType(); |
|
1264 |
||
1265 |
// Check for a method of the same signature which |
|
1266 |
// was locally declared. |
|
1267 |
MemberDefinition override = |
|
1268 |
myMethods.lookupSig(name, type); |
|
1269 |
||
1270 |
// Is this method inaccessible due to package-private |
|
1271 |
// visibility? |
|
1272 |
if (method.isPackagePrivate() && |
|
1273 |
!inSamePackage(method.getClassDeclaration())) { |
|
1274 |
||
1275 |
if (override != null && this instanceof |
|
1276 |
sun.tools.javac.SourceClass) { |
|
1277 |
// We give a warning when a class shadows an |
|
1278 |
// inaccessible package-private method from |
|
1279 |
// its superclass. This warning is meant |
|
1280 |
// to prevent people from relying on overriding |
|
1281 |
// when it does not happen. This warning should |
|
1282 |
// probably be removed to be consistent with the |
|
1283 |
// general "no warnings" policy of this |
|
1284 |
// compiler. |
|
1285 |
// |
|
1286 |
// The `instanceof' above is a hack so that only |
|
1287 |
// SourceClass generates this warning, not a |
|
1288 |
// BinaryClass, for example. |
|
1289 |
env.error(method.getWhere(), |
|
1290 |
"warn.no.override.access", |
|
1291 |
override, |
|
1292 |
override.getClassDeclaration(), |
|
1293 |
method.getClassDeclaration()); |
|
1294 |
} |
|
1295 |
||
1296 |
// If our superclass has a package-private abstract |
|
1297 |
// method that we have no access to, then we add |
|
1298 |
// this method to our list of permanently abstract |
|
1299 |
// methods. The idea is, since we cannot override |
|
1300 |
// the method, we can never make this class |
|
1301 |
// non-abstract. |
|
1302 |
if (method.isAbstract()) { |
|
1303 |
permanentlyAbstractMethods.add(method); |
|
1304 |
} |
|
1305 |
||
1306 |
// `method' is inaccessible. We do not inherit it. |
|
1307 |
continue; |
|
1308 |
} |
|
1309 |
||
1310 |
if (override != null) { |
|
1311 |
// `method' and `override' have the same signature. |
|
1312 |
// We are required to check that `override' is a |
|
1313 |
// legal override of `method' |
|
1314 |
||
1315 |
//System.out.println ("About to check override of " + |
|
1316 |
// method); |
|
1317 |
||
1318 |
override.checkOverride(env, method); |
|
1319 |
} else { |
|
1320 |
// In the absence of a definition in the class |
|
1321 |
// itself, we check to see if this definition |
|
1322 |
// can be successfully merged with any other |
|
1323 |
// inherited definitions. |
|
1324 |
||
1325 |
// Have we added a member of the same signature |
|
1326 |
// to `allMethods' already? |
|
1327 |
MemberDefinition formerMethod = |
|
1328 |
allMethods.lookupSig(name, type); |
|
1329 |
||
1330 |
// If the previous definition is nonexistent or |
|
1331 |
// ignorable, replace it. |
|
1332 |
if (formerMethod == null) { |
|
1333 |
//System.out.println("Added " + method + " to " + |
|
1334 |
// this); |
|
1335 |
||
1336 |
if (mirandaMethods != null && |
|
1337 |
pClass.isInterface() && !isInterface()) { |
|
1338 |
// Whenever a class inherits a method |
|
1339 |
// from an interface, that method is |
|
1340 |
// one of our "miranda" methods. Early |
|
1341 |
// VMs require that these methods be |
|
1342 |
// added as true members to the class |
|
1343 |
// to enable method lookup to work in the |
|
1344 |
// VM. |
|
1345 |
method = |
|
1346 |
new sun.tools.javac.SourceMember(method,this, |
|
1347 |
env); |
|
1348 |
mirandaMethods.add(method); |
|
1349 |
||
1350 |
//System.out.println("Added " + method + |
|
1351 |
// " to " + this + " as a Miranda"); |
|
1352 |
} |
|
1353 |
||
1354 |
// There is no previous inherited definition. |
|
1355 |
// Add `method' to `allMethods'. |
|
1356 |
allMethods.add(method); |
|
1357 |
} else if (isInterface() && |
|
1358 |
!formerMethod.isAbstract() && |
|
1359 |
method.isAbstract()) { |
|
1360 |
// If we are in an interface and we have inherited |
|
1361 |
// both an abstract method and a non-abstract method |
|
1362 |
// then we know that the non-abstract method is |
|
1363 |
// a placeholder from Object put in for type checking |
|
1364 |
// and the abstract method was already checked to |
|
1365 |
// be proper by our superinterface. |
|
1366 |
allMethods.replace(method); |
|
1367 |
||
1368 |
} else { |
|
1369 |
// Okay, `formerMethod' and `method' both have the |
|
1370 |
// same signature. See if they are compatible. |
|
1371 |
||
1372 |
//System.out.println ("About to check meet of " + |
|
1373 |
// method); |
|
1374 |
||
1375 |
if (!formerMethod.checkMeet(env, |
|
1376 |
method, |
|
1377 |
this.getClassDeclaration())) { |
|
1378 |
// The methods are incompatible. Skip to |
|
1379 |
// next method. |
|
1380 |
continue; |
|
1381 |
} |
|
1382 |
||
1383 |
if (formerMethod.couldOverride(env, method)) { |
|
1384 |
// Do nothing. The current definition |
|
1385 |
// is specific enough. |
|
1386 |
||
1387 |
//System.out.println("trivial meet of " + |
|
1388 |
// method); |
|
1389 |
continue; |
|
1390 |
} |
|
1391 |
||
1392 |
if (method.couldOverride(env, formerMethod)) { |
|
1393 |
// `method' is more specific than |
|
1394 |
// `formerMethod'. replace `formerMethod'. |
|
1395 |
||
1396 |
//System.out.println("new def of " + method); |
|
1397 |
if (mirandaMethods != null && |
|
1398 |
pClass.isInterface() && !isInterface()) { |
|
1399 |
// Whenever a class inherits a method |
|
1400 |
// from an interface, that method is |
|
1401 |
// one of our "miranda" methods. Early |
|
1402 |
// VMs require that these methods be |
|
1403 |
// added as true members to the class |
|
1404 |
// to enable method lookup to work in the |
|
1405 |
// VM. |
|
1406 |
method = |
|
1407 |
new sun.tools.javac.SourceMember(method, |
|
1408 |
this,env); |
|
1409 |
||
1410 |
mirandaMethods.replace(method); |
|
1411 |
||
1412 |
//System.out.println("Added " + method + |
|
1413 |
// " to " + this + " as a Miranda"); |
|
1414 |
} |
|
1415 |
||
1416 |
allMethods.replace(method); |
|
1417 |
||
1418 |
continue; |
|
1419 |
} |
|
1420 |
||
1421 |
// Neither method is more specific than the other. |
|
1422 |
// Oh well. We need to construct a nontrivial |
|
1423 |
// meet of the two methods. |
|
1424 |
// |
|
1425 |
// This is not yet implemented, so we give |
|
1426 |
// a message with a helpful workaround. |
|
1427 |
env.error(this.where, |
|
1428 |
"nontrivial.meet", method, |
|
1429 |
formerMethod.getClassDefinition(), |
|
1430 |
method.getClassDeclaration() |
|
1431 |
); |
|
1432 |
} |
|
1433 |
} |
|
1434 |
} |
|
1435 |
} catch (ClassNotFound ee) { |
|
1436 |
env.error(getWhere(), "class.not.found", ee.name, this); |
|
1437 |
} |
|
1438 |
} |
|
1439 |
||
1440 |
/** |
|
1441 |
* <p>Collect all methods defined in this class or inherited from |
|
1442 |
* any of our superclasses or interfaces. Look for any |
|
1443 |
* incompatible definitions. |
|
1444 |
* |
|
1445 |
* <p>This function is also responsible for collecting the |
|
1446 |
* <em>Miranda</em> methods for a class. For a definition of |
|
1447 |
* Miranda methods, see the comment in addMirandaMethods() |
|
1448 |
* below. |
|
1449 |
*/ |
|
1450 |
protected void collectInheritedMethods(Environment env) { |
|
1451 |
// The methods defined in this class. |
|
1452 |
MethodSet myMethods; |
|
1453 |
MethodSet mirandaMethods; |
|
1454 |
||
1455 |
//System.out.println("Called collectInheritedMethods() for " + |
|
1456 |
// this); |
|
1457 |
||
1458 |
if (allMethods != null) { |
|
1459 |
if (allMethods.isFrozen()) { |
|
1460 |
// We have already done the collection. No need to |
|
1461 |
// do it again. |
|
1462 |
return; |
|
1463 |
} else { |
|
1464 |
// We have run into a circular need to collect our methods. |
|
1465 |
// This should not happen at this stage. |
|
1466 |
throw new CompilerError("collectInheritedMethods()"); |
|
1467 |
} |
|
1468 |
} |
|
1469 |
||
1470 |
myMethods = new MethodSet(); |
|
1471 |
allMethods = new MethodSet(); |
|
1472 |
||
1473 |
// For testing, do not generate miranda methods. |
|
1474 |
if (env.version12()) { |
|
1475 |
mirandaMethods = null; |
|
1476 |
} else { |
|
1477 |
mirandaMethods = new MethodSet(); |
|
1478 |
} |
|
1479 |
||
1480 |
// Any methods defined in the current class get added |
|
1481 |
// to both the myMethods and the allMethods MethodSets. |
|
1482 |
||
1483 |
for (MemberDefinition member = getFirstMember(); |
|
1484 |
member != null; |
|
1485 |
member = member.nextMember) { |
|
1486 |
||
1487 |
// We only collect methods. Initializers are not relevant. |
|
1488 |
if (member.isMethod() && |
|
1489 |
!member.isInitializer()) { |
|
1490 |
||
1491 |
//System.out.println("Declared in " + this + ", " + member); |
|
1492 |
||
1493 |
//////////////////////////////////////////////////////////// |
|
1494 |
// PCJ 2003-07-30 modified the following code because with |
|
1495 |
// the covariant return type feature of the 1.5 compiler, |
|
1496 |
// there might be multiple methods with the same signature |
|
1497 |
// but different return types, and MethodSet doesn't |
|
1498 |
// support that. We use a new utility method that attempts |
|
1499 |
// to ensure that the appropriate method winds up in the |
|
1500 |
// MethodSet. See 4892308. |
|
1501 |
//////////////////////////////////////////////////////////// |
|
1502 |
// myMethods.add(member); |
|
1503 |
// allMethods.add(member); |
|
1504 |
//////////////////////////////////////////////////////////// |
|
1505 |
methodSetAdd(env, myMethods, member); |
|
1506 |
methodSetAdd(env, allMethods, member); |
|
1507 |
//////////////////////////////////////////////////////////// |
|
1508 |
} |
|
1509 |
} |
|
1510 |
||
1511 |
// We're ready to start adding inherited methods. First add |
|
1512 |
// the methods from our superclass. |
|
1513 |
||
1514 |
//System.out.println("About to start superclasses for " + this); |
|
1515 |
||
1516 |
ClassDeclaration scDecl = getSuperClass(env); |
|
1517 |
if (scDecl != null) { |
|
1518 |
collectOneClass(env, scDecl, |
|
1519 |
myMethods, allMethods, mirandaMethods); |
|
1520 |
||
1521 |
// Make sure that we add all unimplementable methods from our |
|
1522 |
// superclass to our list of unimplementable methods. |
|
1523 |
ClassDefinition sc = scDecl.getClassDefinition(); |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1524 |
Iterator<MemberDefinition> supIter = sc.getPermanentlyAbstractMethods(); |
2 | 1525 |
while (supIter.hasNext()) { |
1526 |
permanentlyAbstractMethods.add(supIter.next()); |
|
1527 |
} |
|
1528 |
} |
|
1529 |
||
1530 |
// Now we inherit all of the methods from our interfaces. |
|
1531 |
||
1532 |
//System.out.println("About to start interfaces for " + this); |
|
1533 |
||
1534 |
for (int i = 0; i < interfaces.length; i++) { |
|
1535 |
collectOneClass(env, interfaces[i], |
|
1536 |
myMethods, allMethods, mirandaMethods); |
|
1537 |
} |
|
1538 |
allMethods.freeze(); |
|
1539 |
||
1540 |
// Now we have collected all of our methods from our superclasses |
|
1541 |
// and interfaces into our `allMethods' member. Good. As a last |
|
1542 |
// task, we add our collected miranda methods to this class. |
|
1543 |
// |
|
1544 |
// If we do not add the mirandas to the class explicitly, there |
|
1545 |
// will be no code generated for them. |
|
1546 |
if (mirandaMethods != null && mirandaMethods.size() > 0) { |
|
1547 |
addMirandaMethods(env, mirandaMethods.iterator()); |
|
1548 |
} |
|
1549 |
} |
|
1550 |
||
1551 |
//////////////////////////////////////////////////////////// |
|
1552 |
// PCJ 2003-07-30 added this utility method to insulate |
|
1553 |
// MethodSet additions from the covariant return type |
|
1554 |
// feature of the 1.5 compiler. When there are multiple |
|
1555 |
// methods with the same signature and different return |
|
1556 |
// types to be added, we try to ensure that the one with |
|
1557 |
// the most specific return type winds up in the MethodSet. |
|
1558 |
// This logic was not put into MethodSet itself because it |
|
1559 |
// requires access to an Environment for type relationship |
|
1560 |
// checking. No error checking is performed here, but that |
|
1561 |
// should be OK because this code is only still used by |
|
1562 |
// rmic. See 4892308. |
|
1563 |
//////////////////////////////////////////////////////////// |
|
1564 |
private static void methodSetAdd(Environment env, |
|
1565 |
MethodSet methodSet, |
|
1566 |
MemberDefinition newMethod) |
|
1567 |
{ |
|
1568 |
MemberDefinition oldMethod = methodSet.lookupSig(newMethod.getName(), |
|
1569 |
newMethod.getType()); |
|
1570 |
if (oldMethod != null) { |
|
1571 |
Type oldReturnType = oldMethod.getType().getReturnType(); |
|
1572 |
Type newReturnType = newMethod.getType().getReturnType(); |
|
1573 |
try { |
|
1574 |
if (env.isMoreSpecific(newReturnType, oldReturnType)) { |
|
1575 |
methodSet.replace(newMethod); |
|
1576 |
} |
|
1577 |
} catch (ClassNotFound ignore) { |
|
1578 |
} |
|
1579 |
} else { |
|
1580 |
methodSet.add(newMethod); |
|
1581 |
} |
|
1582 |
} |
|
1583 |
//////////////////////////////////////////////////////////// |
|
1584 |
||
1585 |
/** |
|
1586 |
* Get an Iterator of all methods which could be accessed in an |
|
1587 |
* instance of this class. |
|
1588 |
*/ |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1589 |
public Iterator<MemberDefinition> getMethods(Environment env) { |
2 | 1590 |
if (allMethods == null) { |
1591 |
collectInheritedMethods(env); |
|
1592 |
} |
|
1593 |
return getMethods(); |
|
1594 |
} |
|
1595 |
||
1596 |
/** |
|
1597 |
* Get an Iterator of all methods which could be accessed in an |
|
1598 |
* instance of this class. Throw a compiler error if we haven't |
|
1599 |
* generated this information yet. |
|
1600 |
*/ |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1601 |
public Iterator<MemberDefinition> getMethods() { |
2 | 1602 |
if (allMethods == null) { |
1603 |
throw new CompilerError("getMethods: too early"); |
|
1604 |
} |
|
1605 |
return allMethods.iterator(); |
|
1606 |
} |
|
1607 |
||
1608 |
// In early VM's there was a bug -- the VM didn't walk the interfaces |
|
1609 |
// of a class looking for a method, they only walked the superclass |
|
1610 |
// chain. This meant that abstract methods defined only in interfaces |
|
1611 |
// were not being found. To fix this bug, a counter-bug was introduced |
|
1612 |
// in the compiler -- the so-called Miranda methods. If a class |
|
1613 |
// does not provide a definition for an abstract method in one of |
|
1614 |
// its interfaces then the compiler inserts one in the class artificially. |
|
1615 |
// That way the VM didn't have to bother looking at the interfaces. |
|
1616 |
// |
|
1617 |
// This is a problem. Miranda methods are not part of the specification. |
|
1618 |
// But they continue to be inserted so that old VM's can run new code. |
|
1619 |
// Someday, when the old VM's are gone, perhaps classes can be compiled |
|
1620 |
// without Miranda methods. Towards this end, the compiler has a |
|
1621 |
// flag, -nomiranda, which can turn off the creation of these methods. |
|
1622 |
// Eventually that behavior should become the default. |
|
1623 |
// |
|
1624 |
// Why are they called Miranda methods? Well the sentence "If the |
|
1625 |
// class is not able to provide a method, then one will be provided |
|
1626 |
// by the compiler" is very similar to the sentence "If you cannot |
|
1627 |
// afford an attorney, one will be provided by the court," -- one |
|
1628 |
// of the so-called "Miranda" rights in the United States. |
|
1629 |
||
1630 |
/** |
|
1631 |
* Add a list of methods to this class as miranda methods. This |
|
1632 |
* gets overridden with a meaningful implementation in SourceClass. |
|
1633 |
* BinaryClass should not need to do anything -- it should already |
|
1634 |
* have its miranda methods and, if it doesn't, then that doesn't |
|
1635 |
* affect our compilation. |
|
1636 |
*/ |
|
1637 |
protected void addMirandaMethods(Environment env, |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1638 |
Iterator<MemberDefinition> mirandas) { |
2 | 1639 |
// do nothing. |
1640 |
} |
|
1641 |
||
1642 |
//--------------------------------------------------------------- |
|
1643 |
||
1644 |
public void inlineLocalClass(Environment env) { |
|
1645 |
} |
|
1646 |
||
1647 |
/** |
|
1648 |
* We create a stub for this. Source classes do more work. |
|
1649 |
* Some calls from 'SourceClass.checkSupers' execute this method. |
|
1650 |
* @see sun.tools.javac.SourceClass#resolveTypeStructure |
|
1651 |
*/ |
|
1652 |
||
1653 |
public void resolveTypeStructure(Environment env) { |
|
1654 |
} |
|
1655 |
||
1656 |
/** |
|
1657 |
* Look up an inner class name, from somewhere inside this class. |
|
1658 |
* Since supers and outers are in scope, search them too. |
|
1659 |
* <p> |
|
1660 |
* If no inner class is found, env.resolveName() is then called, |
|
1661 |
* to interpret the ambient package and import directives. |
|
1662 |
* <p> |
|
1663 |
* This routine operates on a "best-efforts" basis. If |
|
1664 |
* at some point a class is not found, the partially-resolved |
|
1665 |
* identifier is returned. Eventually, someone else has to |
|
1666 |
* try to get the ClassDefinition and diagnose the ClassNotFound. |
|
1667 |
* <p> |
|
1668 |
* resolveName() looks at surrounding scopes, and hence |
|
1669 |
* pulling in both inherited and uplevel types. By contrast, |
|
1670 |
* resolveInnerClass() is intended only for interpreting |
|
1671 |
* explicitly qualified names, and so look only at inherited |
|
1672 |
* types. Also, resolveName() looks for package prefixes, |
|
1673 |
* which appear similar to "very uplevel" outer classes. |
|
1674 |
* <p> |
|
1675 |
* A similar (but more complex) name-lookup process happens |
|
1676 |
* when field and identifier expressions denoting qualified names |
|
1677 |
* are type-checked. The added complexity comes from the fact |
|
1678 |
* that variables may occur in such names, and take precedence |
|
1679 |
* over class and package names. |
|
1680 |
* <p> |
|
1681 |
* In the expression type-checker, resolveInnerClass() is paralleled |
|
1682 |
* by code in FieldExpression.checkAmbigName(), which also calls |
|
1683 |
* ClassDefinition.getInnerClass() to interpret names of the form |
|
1684 |
* "OuterClass.Inner" (and also outerObject.Inner). The checking |
|
1685 |
* of an identifier expression that fails to be a variable is referred |
|
1686 |
* directly to resolveName(). |
|
1687 |
*/ |
|
1688 |
public Identifier resolveName(Environment env, Identifier name) { |
|
1689 |
if (tracing) env.dtEvent("ClassDefinition.resolveName: " + name); |
|
1690 |
// This logic is pretty much exactly parallel to that of |
|
1691 |
// Environment.resolveName(). |
|
1692 |
if (name.isQualified()) { |
|
1693 |
// Try to resolve the first identifier component, |
|
1694 |
// because inner class names take precedence over |
|
1695 |
// package prefixes. (Cf. Environment.resolveName.) |
|
1696 |
Identifier rhead = resolveName(env, name.getHead()); |
|
1697 |
||
1698 |
if (rhead.hasAmbigPrefix()) { |
|
1699 |
// The first identifier component refers to an |
|
1700 |
// ambiguous class. Limp on. We throw away the |
|
1701 |
// rest of the classname as it is irrelevant. |
|
1702 |
// (part of solution for 4059855). |
|
1703 |
return rhead; |
|
1704 |
} |
|
1705 |
||
1706 |
if (!env.classExists(rhead)) { |
|
1707 |
return env.resolvePackageQualifiedName(name); |
|
1708 |
} |
|
1709 |
try { |
|
1710 |
return env.getClassDefinition(rhead). |
|
1711 |
resolveInnerClass(env, name.getTail()); |
|
1712 |
} catch (ClassNotFound ee) { |
|
1713 |
// return partially-resolved name someone else can fail on |
|
1714 |
return Identifier.lookupInner(rhead, name.getTail()); |
|
1715 |
} |
|
1716 |
} |
|
1717 |
||
1718 |
// This method used to fail to look for local classes, thus a |
|
1719 |
// reference to a local class within, e.g., the type of a member |
|
1720 |
// declaration, would fail to resolve if the immediately enclosing |
|
1721 |
// context was an inner class. The code added below is ugly, but |
|
1722 |
// it works, and is lifted from existing code in 'Context.resolveName' |
|
1723 |
// and 'Context.getClassCommon'. See the comments there about the design. |
|
1724 |
// Fixes 4095716. |
|
1725 |
||
1726 |
int ls = -2; |
|
1727 |
LocalMember lf = null; |
|
1728 |
if (classContext != null) { |
|
1729 |
lf = classContext.getLocalClass(name); |
|
1730 |
if (lf != null) { |
|
1731 |
ls = lf.getScopeNumber(); |
|
1732 |
} |
|
1733 |
} |
|
1734 |
||
1735 |
// Look for an unqualified name in enclosing scopes. |
|
1736 |
for (ClassDefinition c = this; c != null; c = c.outerClass) { |
|
1737 |
try { |
|
1738 |
MemberDefinition f = c.getInnerClass(env, name); |
|
1739 |
if (f != null && |
|
1740 |
(lf == null || classContext.getScopeNumber(c) > ls)) { |
|
1741 |
// An uplevel member was found, and was nested more deeply than |
|
1742 |
// any enclosing local of the same name. |
|
1743 |
return f.getInnerClass().getName(); |
|
1744 |
} |
|
1745 |
} catch (ClassNotFound ee) { |
|
1746 |
// a missing superclass, or something catastrophic |
|
1747 |
} |
|
1748 |
} |
|
1749 |
||
1750 |
// No uplevel member found, so use the enclosing local if one was found. |
|
1751 |
if (lf != null) { |
|
1752 |
return lf.getInnerClass().getName(); |
|
1753 |
} |
|
1754 |
||
1755 |
// look in imports, etc. |
|
1756 |
return env.resolveName(name); |
|
1757 |
} |
|
1758 |
||
1759 |
/** |
|
1760 |
* Interpret a qualified class name, which may have further subcomponents.. |
|
1761 |
* Follow inheritance links, as in: |
|
1762 |
* class C { class N { } } class D extends C { } ... new D.N() ... |
|
1763 |
* Ignore outer scopes and packages. |
|
1764 |
* @see resolveName |
|
1765 |
*/ |
|
1766 |
public Identifier resolveInnerClass(Environment env, Identifier nm) { |
|
1767 |
if (nm.isInner()) throw new CompilerError("inner"); |
|
1768 |
if (nm.isQualified()) { |
|
1769 |
Identifier rhead = resolveInnerClass(env, nm.getHead()); |
|
1770 |
try { |
|
1771 |
return env.getClassDefinition(rhead). |
|
1772 |
resolveInnerClass(env, nm.getTail()); |
|
1773 |
} catch (ClassNotFound ee) { |
|
1774 |
// return partially-resolved name someone else can fail on |
|
1775 |
return Identifier.lookupInner(rhead, nm.getTail()); |
|
1776 |
} |
|
1777 |
} else { |
|
1778 |
try { |
|
1779 |
MemberDefinition f = getInnerClass(env, nm); |
|
1780 |
if (f != null) { |
|
1781 |
return f.getInnerClass().getName(); |
|
1782 |
} |
|
1783 |
} catch (ClassNotFound ee) { |
|
1784 |
// a missing superclass, or something catastrophic |
|
1785 |
} |
|
1786 |
// Fake a good name for a diagnostic. |
|
1787 |
return Identifier.lookupInner(this.getName(), nm); |
|
1788 |
} |
|
1789 |
} |
|
1790 |
||
1791 |
/** |
|
1792 |
* While resolving import directives, the question has arisen: |
|
1793 |
* does a given inner class exist? If the top-level class exists, |
|
1794 |
* we ask it about an inner class via this method. |
|
1795 |
* This method looks only at the literal name of the class, |
|
1796 |
* and does not attempt to follow inheritance links. |
|
1797 |
* This is necessary, since at the time imports are being |
|
1798 |
* processed, inheritance links have not been resolved yet. |
|
1799 |
* (Thus, an import directive must always spell a class |
|
1800 |
* name exactly.) |
|
1801 |
*/ |
|
1802 |
public boolean innerClassExists(Identifier nm) { |
|
1803 |
for (MemberDefinition field = getFirstMatch(nm.getHead()) ; field != null ; field = field.getNextMatch()) { |
|
1804 |
if (field.isInnerClass()) { |
|
1805 |
if (field.getInnerClass().isLocal()) { |
|
1806 |
continue; // ignore this name; it is internally generated |
|
1807 |
} |
|
1808 |
return !nm.isQualified() || |
|
1809 |
field.getInnerClass().innerClassExists(nm.getTail()); |
|
1810 |
} |
|
1811 |
} |
|
1812 |
return false; |
|
1813 |
} |
|
1814 |
||
1815 |
/** |
|
1816 |
* Find any method with a given name. |
|
1817 |
*/ |
|
1818 |
public MemberDefinition findAnyMethod(Environment env, Identifier nm) throws ClassNotFound { |
|
1819 |
MemberDefinition f; |
|
1820 |
for (f = getFirstMatch(nm) ; f != null ; f = f.getNextMatch()) { |
|
1821 |
if (f.isMethod()) { |
|
1822 |
return f; |
|
1823 |
} |
|
1824 |
} |
|
1825 |
||
1826 |
// look in the super class |
|
1827 |
ClassDeclaration sup = getSuperClass(); |
|
1828 |
if (sup == null) |
|
1829 |
return null; |
|
1830 |
return sup.getClassDefinition(env).findAnyMethod(env, nm); |
|
1831 |
} |
|
1832 |
||
1833 |
/** |
|
1834 |
* Given the fact that this class has no method "nm" matching "argTypes", |
|
1835 |
* find out if the mismatch can be blamed on a particular actual argument |
|
1836 |
* which disagrees with all of the overloadings. |
|
1837 |
* If so, return the code (i<<2)+(castOK<<1)+ambig, where |
|
1838 |
* "i" is the number of the offending argument, and |
|
1839 |
* "castOK" is 1 if a cast could fix the problem. |
|
1840 |
* The target type for the argument is returned in margTypeResult[0]. |
|
1841 |
* If not all methods agree on this type, "ambig" is 1. |
|
1842 |
* If there is more than one method, the choice of target type is |
|
1843 |
* arbitrary.<p> |
|
1844 |
* Return -1 if every argument is acceptable to at least one method. |
|
1845 |
* Return -2 if there are no methods of the required arity. |
|
1846 |
* The value "start" gives the index of the first argument to begin |
|
1847 |
* checking. |
|
1848 |
*/ |
|
1849 |
public int diagnoseMismatch(Environment env, Identifier nm, Type argTypes[], |
|
1850 |
int start, Type margTypeResult[]) throws ClassNotFound { |
|
1851 |
int haveMatch[] = new int[argTypes.length]; |
|
1852 |
Type margType[] = new Type[argTypes.length]; |
|
1853 |
if (!diagnoseMismatch(env, nm, argTypes, start, haveMatch, margType)) |
|
1854 |
return -2; |
|
1855 |
for (int i = start; i < argTypes.length; i++) { |
|
1856 |
if (haveMatch[i] < 4) { |
|
1857 |
margTypeResult[0] = margType[i]; |
|
1858 |
return (i<<2) | haveMatch[i]; |
|
1859 |
} |
|
1860 |
} |
|
1861 |
return -1; |
|
1862 |
} |
|
1863 |
||
1864 |
private boolean diagnoseMismatch(Environment env, Identifier nm, Type argTypes[], int start, |
|
1865 |
int haveMatch[], Type margType[]) throws ClassNotFound { |
|
1866 |
// look in the current class |
|
1867 |
boolean haveOne = false; |
|
1868 |
MemberDefinition f; |
|
1869 |
for (f = getFirstMatch(nm) ; f != null ; f = f.getNextMatch()) { |
|
1870 |
if (!f.isMethod()) { |
|
1871 |
continue; |
|
1872 |
} |
|
1873 |
Type fArgTypes[] = f.getType().getArgumentTypes(); |
|
1874 |
if (fArgTypes.length == argTypes.length) { |
|
1875 |
haveOne = true; |
|
1876 |
for (int i = start; i < argTypes.length; i++) { |
|
1877 |
Type at = argTypes[i]; |
|
1878 |
Type ft = fArgTypes[i]; |
|
1879 |
if (env.implicitCast(at, ft)) { |
|
1880 |
haveMatch[i] = 4; |
|
1881 |
continue; |
|
1882 |
} else if (haveMatch[i] <= 2 && env.explicitCast(at, ft)) { |
|
1883 |
if (haveMatch[i] < 2) margType[i] = null; |
|
1884 |
haveMatch[i] = 2; |
|
1885 |
} else if (haveMatch[i] > 0) { |
|
1886 |
continue; |
|
1887 |
} |
|
1888 |
if (margType[i] == null) |
|
1889 |
margType[i] = ft; |
|
1890 |
else if (margType[i] != ft) |
|
1891 |
haveMatch[i] |= 1; |
|
1892 |
} |
|
1893 |
} |
|
1894 |
} |
|
1895 |
||
1896 |
// constructors are not inherited |
|
1897 |
if (nm.equals(idInit)) { |
|
1898 |
return haveOne; |
|
1899 |
} |
|
1900 |
||
1901 |
// look in the super class |
|
1902 |
ClassDeclaration sup = getSuperClass(); |
|
1903 |
if (sup != null) { |
|
1904 |
if (sup.getClassDefinition(env).diagnoseMismatch(env, nm, argTypes, start, |
|
1905 |
haveMatch, margType)) |
|
1906 |
haveOne = true; |
|
1907 |
} |
|
1908 |
return haveOne; |
|
1909 |
} |
|
1910 |
||
1911 |
/** |
|
1912 |
* Add a field (no checks) |
|
1913 |
*/ |
|
1914 |
public void addMember(MemberDefinition field) { |
|
1915 |
//System.out.println("ADD = " + field); |
|
1916 |
if (firstMember == null) { |
|
1917 |
firstMember = lastMember = field; |
|
1918 |
} else if (field.isSynthetic() && field.isFinal() |
|
1919 |
&& field.isVariable()) { |
|
1920 |
// insert this at the front, because of initialization order |
|
1921 |
field.nextMember = firstMember; |
|
1922 |
firstMember = field; |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1923 |
field.nextMatch = fieldHash.get(field.name); |
2 | 1924 |
} else { |
1925 |
lastMember.nextMember = field; |
|
1926 |
lastMember = field; |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
1927 |
field.nextMatch = fieldHash.get(field.name); |
2 | 1928 |
} |
1929 |
fieldHash.put(field.name, field); |
|
1930 |
} |
|
1931 |
||
1932 |
/** |
|
1933 |
* Add a field (subclasses make checks) |
|
1934 |
*/ |
|
1935 |
public void addMember(Environment env, MemberDefinition field) { |
|
1936 |
addMember(field); |
|
1937 |
if (resolved) { |
|
1938 |
// a late addition |
|
1939 |
field.resolveTypeStructure(env); |
|
1940 |
} |
|
1941 |
} |
|
1942 |
||
1943 |
/** |
|
1944 |
* Find or create an uplevel reference for the given target. |
|
1945 |
*/ |
|
1946 |
public UplevelReference getReference(LocalMember target) { |
|
1947 |
for (UplevelReference r = references; r != null; r = r.getNext()) { |
|
1948 |
if (r.getTarget() == target) { |
|
1949 |
return r; |
|
1950 |
} |
|
1951 |
} |
|
1952 |
return addReference(target); |
|
1953 |
} |
|
1954 |
||
1955 |
protected UplevelReference addReference(LocalMember target) { |
|
1956 |
if (target.getClassDefinition() == this) { |
|
1957 |
throw new CompilerError("addReference "+target); |
|
1958 |
} |
|
1959 |
referencesMustNotBeFrozen(); |
|
1960 |
UplevelReference r = new UplevelReference(this, target); |
|
1961 |
references = r.insertInto(references); |
|
1962 |
return r; |
|
1963 |
} |
|
1964 |
||
1965 |
/** |
|
1966 |
* Return the list of all uplevel references. |
|
1967 |
*/ |
|
1968 |
public UplevelReference getReferences() { |
|
1969 |
return references; |
|
1970 |
} |
|
1971 |
||
1972 |
/** |
|
1973 |
* Return the same value as getReferences. |
|
1974 |
* Also, mark the set of references frozen. |
|
1975 |
* After that, it is an error to add new references. |
|
1976 |
*/ |
|
1977 |
public UplevelReference getReferencesFrozen() { |
|
1978 |
referencesFrozen = true; |
|
1979 |
return references; |
|
1980 |
} |
|
1981 |
||
1982 |
/** |
|
1983 |
* assertion check |
|
1984 |
*/ |
|
1985 |
public final void referencesMustNotBeFrozen() { |
|
1986 |
if (referencesFrozen) { |
|
1987 |
throw new CompilerError("referencesMustNotBeFrozen "+this); |
|
1988 |
} |
|
1989 |
} |
|
1990 |
||
1991 |
/** |
|
1992 |
* Get helper method for class literal lookup. |
|
1993 |
*/ |
|
1994 |
public MemberDefinition getClassLiteralLookup(long fwhere) { |
|
1995 |
throw new CompilerError("binary class"); |
|
1996 |
} |
|
1997 |
||
1998 |
/** |
|
1999 |
* Add a dependency |
|
2000 |
*/ |
|
2001 |
public void addDependency(ClassDeclaration c) { |
|
2002 |
throw new CompilerError("addDependency"); |
|
2003 |
} |
|
2004 |
||
2005 |
/** |
|
2006 |
* Maintain a hash table of local and anonymous classes |
|
2007 |
* whose internal names are prefixed by the current class. |
|
2008 |
* The key is the simple internal name, less the prefix. |
|
2009 |
*/ |
|
2010 |
||
2011 |
public ClassDefinition getLocalClass(String name) { |
|
2012 |
if (localClasses == null) { |
|
2013 |
return null; |
|
2014 |
} else { |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
2015 |
return localClasses.get(name); |
2 | 2016 |
} |
2017 |
} |
|
2018 |
||
2019 |
public void addLocalClass(ClassDefinition c, String name) { |
|
2020 |
if (localClasses == null) { |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
5506
diff
changeset
|
2021 |
localClasses = new Hashtable<>(LOCAL_CLASSES_SIZE); |
2 | 2022 |
} |
2023 |
localClasses.put(name, c); |
|
2024 |
} |
|
2025 |
||
2026 |
||
2027 |
/** |
|
2028 |
* Print for debugging |
|
2029 |
*/ |
|
2030 |
public void print(PrintStream out) { |
|
2031 |
if (isPublic()) { |
|
2032 |
out.print("public "); |
|
2033 |
} |
|
2034 |
if (isInterface()) { |
|
2035 |
out.print("interface "); |
|
2036 |
} else { |
|
2037 |
out.print("class "); |
|
2038 |
} |
|
2039 |
out.print(getName() + " "); |
|
2040 |
if (getSuperClass() != null) { |
|
2041 |
out.print("extends " + getSuperClass().getName() + " "); |
|
2042 |
} |
|
2043 |
if (interfaces.length > 0) { |
|
2044 |
out.print("implements "); |
|
2045 |
for (int i = 0 ; i < interfaces.length ; i++) { |
|
2046 |
if (i > 0) { |
|
2047 |
out.print(", "); |
|
2048 |
} |
|
2049 |
out.print(interfaces[i].getName()); |
|
2050 |
out.print(" "); |
|
2051 |
} |
|
2052 |
} |
|
2053 |
out.println("{"); |
|
2054 |
||
2055 |
for (MemberDefinition f = getFirstMember() ; f != null ; f = f.getNextMember()) { |
|
2056 |
out.print(" "); |
|
2057 |
f.print(out); |
|
2058 |
} |
|
2059 |
||
2060 |
out.println("}"); |
|
2061 |
} |
|
2062 |
||
2063 |
/** |
|
2064 |
* Convert to String |
|
2065 |
*/ |
|
2066 |
public String toString() { |
|
2067 |
return getClassDeclaration().toString(); |
|
2068 |
} |
|
2069 |
||
2070 |
/** |
|
2071 |
* After the class has been written to disk, try to free up |
|
2072 |
* some storage. |
|
2073 |
*/ |
|
2074 |
public void cleanup(Environment env) { |
|
2075 |
if (env.dump()) { |
|
2076 |
env.output("[cleanup " + getName() + "]"); |
|
2077 |
} |
|
2078 |
for (MemberDefinition f = getFirstMember() ; f != null ; f = f.getNextMember()) { |
|
2079 |
f.cleanup(env); |
|
2080 |
} |
|
2081 |
// keep "references" around, for the sake of local subclasses |
|
2082 |
documentation = null; |
|
2083 |
} |
|
2084 |
} |