Mon, 26 Mar 2012 15:28:49 +0100
7133185: Update 292 overload resolution logic to match JLS
Summary: Re-implement special overload resolution support for method handles according to the JLS SE 7 definition
Reviewed-by: jjg, dlsmith, jrose
1 /*
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
26 package com.sun.tools.javac.code;
28 import java.util.Set;
29 import java.util.concurrent.Callable;
30 import javax.lang.model.element.*;
31 import javax.tools.JavaFileObject;
33 import com.sun.tools.javac.util.*;
34 import com.sun.tools.javac.util.Name;
35 import com.sun.tools.javac.code.Type.*;
36 import com.sun.tools.javac.comp.Attr;
37 import com.sun.tools.javac.comp.AttrContext;
38 import com.sun.tools.javac.comp.Env;
39 import com.sun.tools.javac.jvm.*;
40 import com.sun.tools.javac.model.*;
41 import com.sun.tools.javac.tree.JCTree;
43 import static com.sun.tools.javac.code.Flags.*;
44 import static com.sun.tools.javac.code.Kinds.*;
45 import static com.sun.tools.javac.code.TypeTags.*;
47 /** Root class for Java symbols. It contains subclasses
48 * for specific sorts of symbols, such as variables, methods and operators,
49 * types, packages. Each subclass is represented as a static inner class
50 * inside Symbol.
51 *
52 * <p><b>This is NOT part of any supported API.
53 * If you write code that depends on this, you do so at your own risk.
54 * This code and its internal interfaces are subject to change or
55 * deletion without notice.</b>
56 */
57 public abstract class Symbol implements Element {
58 // public Throwable debug = new Throwable();
60 /** The kind of this symbol.
61 * @see Kinds
62 */
63 public int kind;
65 /** The flags of this symbol.
66 */
67 public long flags_field;
69 /** An accessor method for the flags of this symbol.
70 * Flags of class symbols should be accessed through the accessor
71 * method to make sure that the class symbol is loaded.
72 */
73 public long flags() { return flags_field; }
75 /** The attributes of this symbol.
76 */
77 public List<Attribute.Compound> attributes_field;
79 /** An accessor method for the attributes of this symbol.
80 * Attributes of class symbols should be accessed through the accessor
81 * method to make sure that the class symbol is loaded.
82 */
83 public List<Attribute.Compound> getAnnotationMirrors() {
84 return Assert.checkNonNull(attributes_field);
85 }
87 /** Fetch a particular annotation from a symbol. */
88 public Attribute.Compound attribute(Symbol anno) {
89 for (Attribute.Compound a : getAnnotationMirrors())
90 if (a.type.tsym == anno) return a;
91 return null;
92 }
94 /** The name of this symbol in Utf8 representation.
95 */
96 public Name name;
98 /** The type of this symbol.
99 */
100 public Type type;
102 /** The owner of this symbol.
103 */
104 public Symbol owner;
106 /** The completer of this symbol.
107 */
108 public Completer completer;
110 /** A cache for the type erasure of this symbol.
111 */
112 public Type erasure_field;
114 /** Construct a symbol with given kind, flags, name, type and owner.
115 */
116 public Symbol(int kind, long flags, Name name, Type type, Symbol owner) {
117 this.kind = kind;
118 this.flags_field = flags;
119 this.type = type;
120 this.owner = owner;
121 this.completer = null;
122 this.erasure_field = null;
123 this.attributes_field = List.nil();
124 this.name = name;
125 }
127 /** Clone this symbol with new owner.
128 * Legal only for fields and methods.
129 */
130 public Symbol clone(Symbol newOwner) {
131 throw new AssertionError();
132 }
134 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
135 return v.visitSymbol(this, p);
136 }
138 /** The Java source which this symbol represents.
139 * A description of this symbol; overrides Object.
140 */
141 public String toString() {
142 return name.toString();
143 }
145 /** A Java source description of the location of this symbol; used for
146 * error reporting.
147 *
148 * @return null if the symbol is a package or a toplevel class defined in
149 * the default package; otherwise, the owner symbol is returned
150 */
151 public Symbol location() {
152 if (owner.name == null || (owner.name.isEmpty() &&
153 (owner.flags() & BLOCK) == 0 && owner.kind != PCK && owner.kind != TYP)) {
154 return null;
155 }
156 return owner;
157 }
159 public Symbol location(Type site, Types types) {
160 if (owner.name == null || owner.name.isEmpty()) {
161 return location();
162 }
163 if (owner.type.tag == CLASS) {
164 Type ownertype = types.asOuterSuper(site, owner);
165 if (ownertype != null) return ownertype.tsym;
166 }
167 return owner;
168 }
170 /** The symbol's erased type.
171 */
172 public Type erasure(Types types) {
173 if (erasure_field == null)
174 erasure_field = types.erasure(type);
175 return erasure_field;
176 }
178 /** The external type of a symbol. This is the symbol's erased type
179 * except for constructors of inner classes which get the enclosing
180 * instance class added as first argument.
181 */
182 public Type externalType(Types types) {
183 Type t = erasure(types);
184 if (name == name.table.names.init && owner.hasOuterInstance()) {
185 Type outerThisType = types.erasure(owner.type.getEnclosingType());
186 return new MethodType(t.getParameterTypes().prepend(outerThisType),
187 t.getReturnType(),
188 t.getThrownTypes(),
189 t.tsym);
190 } else {
191 return t;
192 }
193 }
195 public boolean isStatic() {
196 return
197 (flags() & STATIC) != 0 ||
198 (owner.flags() & INTERFACE) != 0 && kind != MTH;
199 }
201 public boolean isInterface() {
202 return (flags() & INTERFACE) != 0;
203 }
205 /** Is this symbol declared (directly or indirectly) local
206 * to a method or variable initializer?
207 * Also includes fields of inner classes which are in
208 * turn local to a method or variable initializer.
209 */
210 public boolean isLocal() {
211 return
212 (owner.kind & (VAR | MTH)) != 0 ||
213 (owner.kind == TYP && owner.isLocal());
214 }
216 /** Has this symbol an empty name? This includes anonymous
217 * inner classses.
218 */
219 public boolean isAnonymous() {
220 return name.isEmpty();
221 }
223 /** Is this symbol a constructor?
224 */
225 public boolean isConstructor() {
226 return name == name.table.names.init;
227 }
229 /** The fully qualified name of this symbol.
230 * This is the same as the symbol's name except for class symbols,
231 * which are handled separately.
232 */
233 public Name getQualifiedName() {
234 return name;
235 }
237 /** The fully qualified name of this symbol after converting to flat
238 * representation. This is the same as the symbol's name except for
239 * class symbols, which are handled separately.
240 */
241 public Name flatName() {
242 return getQualifiedName();
243 }
245 /** If this is a class or package, its members, otherwise null.
246 */
247 public Scope members() {
248 return null;
249 }
251 /** A class is an inner class if it it has an enclosing instance class.
252 */
253 public boolean isInner() {
254 return type.getEnclosingType().tag == CLASS;
255 }
257 /** An inner class has an outer instance if it is not an interface
258 * it has an enclosing instance class which might be referenced from the class.
259 * Nested classes can see instance members of their enclosing class.
260 * Their constructors carry an additional this$n parameter, inserted
261 * implicitly by the compiler.
262 *
263 * @see #isInner
264 */
265 public boolean hasOuterInstance() {
266 return
267 type.getEnclosingType().tag == CLASS && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
268 }
270 /** The closest enclosing class of this symbol's declaration.
271 */
272 public ClassSymbol enclClass() {
273 Symbol c = this;
274 while (c != null &&
275 ((c.kind & TYP) == 0 || c.type.tag != CLASS)) {
276 c = c.owner;
277 }
278 return (ClassSymbol)c;
279 }
281 /** The outermost class which indirectly owns this symbol.
282 */
283 public ClassSymbol outermostClass() {
284 Symbol sym = this;
285 Symbol prev = null;
286 while (sym.kind != PCK) {
287 prev = sym;
288 sym = sym.owner;
289 }
290 return (ClassSymbol) prev;
291 }
293 /** The package which indirectly owns this symbol.
294 */
295 public PackageSymbol packge() {
296 Symbol sym = this;
297 while (sym.kind != PCK) {
298 sym = sym.owner;
299 }
300 return (PackageSymbol) sym;
301 }
303 /** Is this symbol a subclass of `base'? Only defined for ClassSymbols.
304 */
305 public boolean isSubClass(Symbol base, Types types) {
306 throw new AssertionError("isSubClass " + this);
307 }
309 /** Fully check membership: hierarchy, protection, and hiding.
310 * Does not exclude methods not inherited due to overriding.
311 */
312 public boolean isMemberOf(TypeSymbol clazz, Types types) {
313 return
314 owner == clazz ||
315 clazz.isSubClass(owner, types) &&
316 isInheritedIn(clazz, types) &&
317 !hiddenIn((ClassSymbol)clazz, types);
318 }
320 /** Is this symbol the same as or enclosed by the given class? */
321 public boolean isEnclosedBy(ClassSymbol clazz) {
322 for (Symbol sym = this; sym.kind != PCK; sym = sym.owner)
323 if (sym == clazz) return true;
324 return false;
325 }
327 /** Check for hiding. Note that this doesn't handle multiple
328 * (interface) inheritance. */
329 private boolean hiddenIn(ClassSymbol clazz, Types types) {
330 if (kind == MTH && (flags() & STATIC) == 0) return false;
331 while (true) {
332 if (owner == clazz) return false;
333 Scope.Entry e = clazz.members().lookup(name);
334 while (e.scope != null) {
335 if (e.sym == this) return false;
336 if (e.sym.kind == kind &&
337 (kind != MTH ||
338 (e.sym.flags() & STATIC) != 0 &&
339 types.isSubSignature(e.sym.type, type)))
340 return true;
341 e = e.next();
342 }
343 Type superType = types.supertype(clazz.type);
344 if (superType.tag != TypeTags.CLASS) return false;
345 clazz = (ClassSymbol)superType.tsym;
346 }
347 }
349 /** Is this symbol inherited into a given class?
350 * PRE: If symbol's owner is a interface,
351 * it is already assumed that the interface is a superinterface
352 * of given class.
353 * @param clazz The class for which we want to establish membership.
354 * This must be a subclass of the member's owner.
355 */
356 public boolean isInheritedIn(Symbol clazz, Types types) {
357 switch ((int)(flags_field & Flags.AccessFlags)) {
358 default: // error recovery
359 case PUBLIC:
360 return true;
361 case PRIVATE:
362 return this.owner == clazz;
363 case PROTECTED:
364 // we model interfaces as extending Object
365 return (clazz.flags() & INTERFACE) == 0;
366 case 0:
367 PackageSymbol thisPackage = this.packge();
368 for (Symbol sup = clazz;
369 sup != null && sup != this.owner;
370 sup = types.supertype(sup.type).tsym) {
371 while (sup.type.tag == TYPEVAR)
372 sup = sup.type.getUpperBound().tsym;
373 if (sup.type.isErroneous())
374 return true; // error recovery
375 if ((sup.flags() & COMPOUND) != 0)
376 continue;
377 if (sup.packge() != thisPackage)
378 return false;
379 }
380 return (clazz.flags() & INTERFACE) == 0;
381 }
382 }
384 /** The (variable or method) symbol seen as a member of given
385 * class type`site' (this might change the symbol's type).
386 * This is used exclusively for producing diagnostics.
387 */
388 public Symbol asMemberOf(Type site, Types types) {
389 throw new AssertionError();
390 }
392 /** Does this method symbol override `other' symbol, when both are seen as
393 * members of class `origin'? It is assumed that _other is a member
394 * of origin.
395 *
396 * It is assumed that both symbols have the same name. The static
397 * modifier is ignored for this test.
398 *
399 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
400 */
401 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
402 return false;
403 }
405 /** Complete the elaboration of this symbol's definition.
406 */
407 public void complete() throws CompletionFailure {
408 if (completer != null) {
409 Completer c = completer;
410 completer = null;
411 c.complete(this);
412 }
413 }
415 /** True if the symbol represents an entity that exists.
416 */
417 public boolean exists() {
418 return true;
419 }
421 public Type asType() {
422 return type;
423 }
425 public Symbol getEnclosingElement() {
426 return owner;
427 }
429 public ElementKind getKind() {
430 return ElementKind.OTHER; // most unkind
431 }
433 public Set<Modifier> getModifiers() {
434 return Flags.asModifierSet(flags());
435 }
437 public Name getSimpleName() {
438 return name;
439 }
441 /**
442 * @deprecated this method should never be used by javac internally.
443 */
444 @Deprecated
445 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
446 return JavacElements.getAnnotation(this, annoType);
447 }
449 // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList
450 public java.util.List<Symbol> getEnclosedElements() {
451 return List.nil();
452 }
454 public List<TypeSymbol> getTypeParameters() {
455 ListBuffer<TypeSymbol> l = ListBuffer.lb();
456 for (Type t : type.getTypeArguments()) {
457 l.append(t.tsym);
458 }
459 return l.toList();
460 }
462 public static class DelegatedSymbol extends Symbol {
463 protected Symbol other;
464 public DelegatedSymbol(Symbol other) {
465 super(other.kind, other.flags_field, other.name, other.type, other.owner);
466 this.other = other;
467 }
468 public String toString() { return other.toString(); }
469 public Symbol location() { return other.location(); }
470 public Symbol location(Type site, Types types) { return other.location(site, types); }
471 public Type erasure(Types types) { return other.erasure(types); }
472 public Type externalType(Types types) { return other.externalType(types); }
473 public boolean isLocal() { return other.isLocal(); }
474 public boolean isConstructor() { return other.isConstructor(); }
475 public Name getQualifiedName() { return other.getQualifiedName(); }
476 public Name flatName() { return other.flatName(); }
477 public Scope members() { return other.members(); }
478 public boolean isInner() { return other.isInner(); }
479 public boolean hasOuterInstance() { return other.hasOuterInstance(); }
480 public ClassSymbol enclClass() { return other.enclClass(); }
481 public ClassSymbol outermostClass() { return other.outermostClass(); }
482 public PackageSymbol packge() { return other.packge(); }
483 public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); }
484 public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); }
485 public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); }
486 public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); }
487 public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); }
488 public void complete() throws CompletionFailure { other.complete(); }
490 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
491 return other.accept(v, p);
492 }
494 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
495 return v.visitSymbol(other, p);
496 }
497 }
499 /** A class for type symbols. Type variables are represented by instances
500 * of this class, classes and packages by instances of subclasses.
501 */
502 public static class TypeSymbol
503 extends Symbol implements TypeParameterElement {
504 // Implements TypeParameterElement because type parameters don't
505 // have their own TypeSymbol subclass.
506 // TODO: type parameters should have their own TypeSymbol subclass
508 public TypeSymbol(long flags, Name name, Type type, Symbol owner) {
509 super(TYP, flags, name, type, owner);
510 }
512 /** form a fully qualified name from a name and an owner
513 */
514 static public Name formFullName(Name name, Symbol owner) {
515 if (owner == null) return name;
516 if (((owner.kind != ERR)) &&
517 ((owner.kind & (VAR | MTH)) != 0
518 || (owner.kind == TYP && owner.type.tag == TYPEVAR)
519 )) return name;
520 Name prefix = owner.getQualifiedName();
521 if (prefix == null || prefix == prefix.table.names.empty)
522 return name;
523 else return prefix.append('.', name);
524 }
526 /** form a fully qualified name from a name and an owner, after
527 * converting to flat representation
528 */
529 static public Name formFlatName(Name name, Symbol owner) {
530 if (owner == null ||
531 (owner.kind & (VAR | MTH)) != 0
532 || (owner.kind == TYP && owner.type.tag == TYPEVAR)
533 ) return name;
534 char sep = owner.kind == TYP ? '$' : '.';
535 Name prefix = owner.flatName();
536 if (prefix == null || prefix == prefix.table.names.empty)
537 return name;
538 else return prefix.append(sep, name);
539 }
541 /**
542 * A total ordering between type symbols that refines the
543 * class inheritance graph.
544 *
545 * Typevariables always precede other kinds of symbols.
546 */
547 public final boolean precedes(TypeSymbol that, Types types) {
548 if (this == that)
549 return false;
550 if (this.type.tag == that.type.tag) {
551 if (this.type.tag == CLASS) {
552 return
553 types.rank(that.type) < types.rank(this.type) ||
554 types.rank(that.type) == types.rank(this.type) &&
555 that.getQualifiedName().compareTo(this.getQualifiedName()) < 0;
556 } else if (this.type.tag == TYPEVAR) {
557 return types.isSubtype(this.type, that.type);
558 }
559 }
560 return this.type.tag == TYPEVAR;
561 }
563 // For type params; overridden in subclasses.
564 public ElementKind getKind() {
565 return ElementKind.TYPE_PARAMETER;
566 }
568 public java.util.List<Symbol> getEnclosedElements() {
569 List<Symbol> list = List.nil();
570 if (kind == TYP && type.tag == TYPEVAR) {
571 return list;
572 }
573 for (Scope.Entry e = members().elems; e != null; e = e.sibling) {
574 if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this)
575 list = list.prepend(e.sym);
576 }
577 return list;
578 }
580 // For type params.
581 // Perhaps not needed if getEnclosingElement can be spec'ed
582 // to do the same thing.
583 // TODO: getGenericElement() might not be needed
584 public Symbol getGenericElement() {
585 return owner;
586 }
588 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
589 Assert.check(type.tag == TYPEVAR); // else override will be invoked
590 return v.visitTypeParameter(this, p);
591 }
593 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
594 return v.visitTypeSymbol(this, p);
595 }
597 public List<Type> getBounds() {
598 TypeVar t = (TypeVar)type;
599 Type bound = t.getUpperBound();
600 if (!bound.isCompound())
601 return List.of(bound);
602 ClassType ct = (ClassType)bound;
603 if (!ct.tsym.erasure_field.isInterface()) {
604 return ct.interfaces_field.prepend(ct.supertype_field);
605 } else {
606 // No superclass was given in bounds.
607 // In this case, supertype is Object, erasure is first interface.
608 return ct.interfaces_field;
609 }
610 }
611 }
613 /** A class for package symbols
614 */
615 public static class PackageSymbol extends TypeSymbol
616 implements PackageElement {
618 public Scope members_field;
619 public Name fullname;
620 public ClassSymbol package_info; // see bug 6443073
622 public PackageSymbol(Name name, Type type, Symbol owner) {
623 super(0, name, type, owner);
624 this.kind = PCK;
625 this.members_field = null;
626 this.fullname = formFullName(name, owner);
627 }
629 public PackageSymbol(Name name, Symbol owner) {
630 this(name, null, owner);
631 this.type = new PackageType(this);
632 }
634 public String toString() {
635 return fullname.toString();
636 }
638 public Name getQualifiedName() {
639 return fullname;
640 }
642 public boolean isUnnamed() {
643 return name.isEmpty() && owner != null;
644 }
646 public Scope members() {
647 if (completer != null) complete();
648 return members_field;
649 }
651 public long flags() {
652 if (completer != null) complete();
653 return flags_field;
654 }
656 public List<Attribute.Compound> getAnnotationMirrors() {
657 if (completer != null) complete();
658 if (package_info != null && package_info.completer != null) {
659 package_info.complete();
660 if (attributes_field.isEmpty())
661 attributes_field = package_info.attributes_field;
662 }
663 return Assert.checkNonNull(attributes_field);
664 }
666 /** A package "exists" if a type or package that exists has
667 * been seen within it.
668 */
669 public boolean exists() {
670 return (flags_field & EXISTS) != 0;
671 }
673 public ElementKind getKind() {
674 return ElementKind.PACKAGE;
675 }
677 public Symbol getEnclosingElement() {
678 return null;
679 }
681 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
682 return v.visitPackage(this, p);
683 }
685 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
686 return v.visitPackageSymbol(this, p);
687 }
688 }
690 /** A class for class symbols
691 */
692 public static class ClassSymbol extends TypeSymbol implements TypeElement {
694 /** a scope for all class members; variables, methods and inner classes
695 * type parameters are not part of this scope
696 */
697 public Scope members_field;
699 /** the fully qualified name of the class, i.e. pck.outer.inner.
700 * null for anonymous classes
701 */
702 public Name fullname;
704 /** the fully qualified name of the class after converting to flat
705 * representation, i.e. pck.outer$inner,
706 * set externally for local and anonymous classes
707 */
708 public Name flatname;
710 /** the sourcefile where the class came from
711 */
712 public JavaFileObject sourcefile;
714 /** the classfile from where to load this class
715 * this will have extension .class or .java
716 */
717 public JavaFileObject classfile;
719 /** the list of translated local classes (used for generating
720 * InnerClasses attribute)
721 */
722 public List<ClassSymbol> trans_local;
724 /** the constant pool of the class
725 */
726 public Pool pool;
728 public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
729 super(flags, name, type, owner);
730 this.members_field = null;
731 this.fullname = formFullName(name, owner);
732 this.flatname = formFlatName(name, owner);
733 this.sourcefile = null;
734 this.classfile = null;
735 this.pool = null;
736 }
738 public ClassSymbol(long flags, Name name, Symbol owner) {
739 this(
740 flags,
741 name,
742 new ClassType(Type.noType, null, null),
743 owner);
744 this.type.tsym = this;
745 }
747 /** The Java source which this symbol represents.
748 */
749 public String toString() {
750 return className();
751 }
753 public long flags() {
754 if (completer != null) complete();
755 return flags_field;
756 }
758 public Scope members() {
759 if (completer != null) complete();
760 return members_field;
761 }
763 public List<Attribute.Compound> getAnnotationMirrors() {
764 if (completer != null) complete();
765 return Assert.checkNonNull(attributes_field);
766 }
768 public Type erasure(Types types) {
769 if (erasure_field == null)
770 erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
771 List.<Type>nil(), this);
772 return erasure_field;
773 }
775 public String className() {
776 if (name.isEmpty())
777 return
778 Log.getLocalizedString("anonymous.class", flatname);
779 else
780 return fullname.toString();
781 }
783 public Name getQualifiedName() {
784 return fullname;
785 }
787 public Name flatName() {
788 return flatname;
789 }
791 public boolean isSubClass(Symbol base, Types types) {
792 if (this == base) {
793 return true;
794 } else if ((base.flags() & INTERFACE) != 0) {
795 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
796 for (List<Type> is = types.interfaces(t);
797 is.nonEmpty();
798 is = is.tail)
799 if (is.head.tsym.isSubClass(base, types)) return true;
800 } else {
801 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
802 if (t.tsym == base) return true;
803 }
804 return false;
805 }
807 /** Complete the elaboration of this symbol's definition.
808 */
809 public void complete() throws CompletionFailure {
810 try {
811 super.complete();
812 } catch (CompletionFailure ex) {
813 // quiet error recovery
814 flags_field |= (PUBLIC|STATIC);
815 this.type = new ErrorType(this, Type.noType);
816 throw ex;
817 }
818 }
820 public List<Type> getInterfaces() {
821 complete();
822 if (type instanceof ClassType) {
823 ClassType t = (ClassType)type;
824 if (t.interfaces_field == null) // FIXME: shouldn't be null
825 t.interfaces_field = List.nil();
826 if (t.all_interfaces_field != null)
827 return Type.getModelTypes(t.all_interfaces_field);
828 return t.interfaces_field;
829 } else {
830 return List.nil();
831 }
832 }
834 public Type getSuperclass() {
835 complete();
836 if (type instanceof ClassType) {
837 ClassType t = (ClassType)type;
838 if (t.supertype_field == null) // FIXME: shouldn't be null
839 t.supertype_field = Type.noType;
840 // An interface has no superclass; its supertype is Object.
841 return t.isInterface()
842 ? Type.noType
843 : t.supertype_field.getModelType();
844 } else {
845 return Type.noType;
846 }
847 }
849 public ElementKind getKind() {
850 long flags = flags();
851 if ((flags & ANNOTATION) != 0)
852 return ElementKind.ANNOTATION_TYPE;
853 else if ((flags & INTERFACE) != 0)
854 return ElementKind.INTERFACE;
855 else if ((flags & ENUM) != 0)
856 return ElementKind.ENUM;
857 else
858 return ElementKind.CLASS;
859 }
861 public NestingKind getNestingKind() {
862 complete();
863 if (owner.kind == PCK)
864 return NestingKind.TOP_LEVEL;
865 else if (name.isEmpty())
866 return NestingKind.ANONYMOUS;
867 else if (owner.kind == MTH)
868 return NestingKind.LOCAL;
869 else
870 return NestingKind.MEMBER;
871 }
873 /**
874 * @deprecated this method should never be used by javac internally.
875 */
876 @Override @Deprecated
877 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
878 return JavacElements.getAnnotation(this, annoType);
879 }
881 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
882 return v.visitType(this, p);
883 }
885 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
886 return v.visitClassSymbol(this, p);
887 }
888 }
891 /** A class for variable symbols
892 */
893 public static class VarSymbol extends Symbol implements VariableElement {
895 /** The variable's declaration position.
896 */
897 public int pos = Position.NOPOS;
899 /** The variable's address. Used for different purposes during
900 * flow analysis, translation and code generation.
901 * Flow analysis:
902 * If this is a blank final or local variable, its sequence number.
903 * Translation:
904 * If this is a private field, its access number.
905 * Code generation:
906 * If this is a local variable, its logical slot number.
907 */
908 public int adr = -1;
910 /** Construct a variable symbol, given its flags, name, type and owner.
911 */
912 public VarSymbol(long flags, Name name, Type type, Symbol owner) {
913 super(VAR, flags, name, type, owner);
914 }
916 /** Clone this symbol with new owner.
917 */
918 public VarSymbol clone(Symbol newOwner) {
919 VarSymbol v = new VarSymbol(flags_field, name, type, newOwner);
920 v.pos = pos;
921 v.adr = adr;
922 v.data = data;
923 // System.out.println("clone " + v + " in " + newOwner);//DEBUG
924 return v;
925 }
927 public String toString() {
928 return name.toString();
929 }
931 public Symbol asMemberOf(Type site, Types types) {
932 return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
933 }
935 public ElementKind getKind() {
936 long flags = flags();
937 if ((flags & PARAMETER) != 0) {
938 if (isExceptionParameter())
939 return ElementKind.EXCEPTION_PARAMETER;
940 else
941 return ElementKind.PARAMETER;
942 } else if ((flags & ENUM) != 0) {
943 return ElementKind.ENUM_CONSTANT;
944 } else if (owner.kind == TYP || owner.kind == ERR) {
945 return ElementKind.FIELD;
946 } else if (isResourceVariable()) {
947 return ElementKind.RESOURCE_VARIABLE;
948 } else {
949 return ElementKind.LOCAL_VARIABLE;
950 }
951 }
953 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
954 return v.visitVariable(this, p);
955 }
957 public Object getConstantValue() { // Mirror API
958 return Constants.decode(getConstValue(), type);
959 }
961 public void setLazyConstValue(final Env<AttrContext> env,
962 final Attr attr,
963 final JCTree.JCExpression initializer)
964 {
965 setData(new Callable<Object>() {
966 public Object call() {
967 return attr.attribLazyConstantValue(env, initializer, type);
968 }
969 });
970 }
972 /**
973 * The variable's constant value, if this is a constant.
974 * Before the constant value is evaluated, it points to an
975 * initalizer environment. If this is not a constant, it can
976 * be used for other stuff.
977 */
978 private Object data;
980 public boolean isExceptionParameter() {
981 return data == ElementKind.EXCEPTION_PARAMETER;
982 }
984 public boolean isResourceVariable() {
985 return data == ElementKind.RESOURCE_VARIABLE;
986 }
988 public Object getConstValue() {
989 // TODO: Consider if getConstValue and getConstantValue can be collapsed
990 if (data == ElementKind.EXCEPTION_PARAMETER ||
991 data == ElementKind.RESOURCE_VARIABLE) {
992 return null;
993 } else if (data instanceof Callable<?>) {
994 // In this case, this is a final variable, with an as
995 // yet unevaluated initializer.
996 Callable<?> eval = (Callable<?>)data;
997 data = null; // to make sure we don't evaluate this twice.
998 try {
999 data = eval.call();
1000 } catch (Exception ex) {
1001 throw new AssertionError(ex);
1002 }
1003 }
1004 return data;
1005 }
1007 public void setData(Object data) {
1008 Assert.check(!(data instanceof Env<?>), this);
1009 this.data = data;
1010 }
1012 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1013 return v.visitVarSymbol(this, p);
1014 }
1015 }
1017 /** A class for method symbols.
1018 */
1019 public static class MethodSymbol extends Symbol implements ExecutableElement {
1021 /** The code of the method. */
1022 public Code code = null;
1024 /** The parameters of the method. */
1025 public List<VarSymbol> params = null;
1027 /** The names of the parameters */
1028 public List<Name> savedParameterNames;
1030 /** For an attribute field accessor, its default value if any.
1031 * The value is null if none appeared in the method
1032 * declaration.
1033 */
1034 public Attribute defaultValue = null;
1036 /** Construct a method symbol, given its flags, name, type and owner.
1037 */
1038 public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
1039 super(MTH, flags, name, type, owner);
1040 if (owner.type.tag == TYPEVAR) Assert.error(owner + "." + name);
1041 }
1043 /** Clone this symbol with new owner.
1044 */
1045 public MethodSymbol clone(Symbol newOwner) {
1046 MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner);
1047 m.code = code;
1048 return m;
1049 }
1051 /** The Java source which this symbol represents.
1052 */
1053 public String toString() {
1054 if ((flags() & BLOCK) != 0) {
1055 return owner.name.toString();
1056 } else {
1057 String s = (name == name.table.names.init)
1058 ? owner.name.toString()
1059 : name.toString();
1060 if (type != null) {
1061 if (type.tag == FORALL)
1062 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
1063 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
1064 }
1065 return s;
1066 }
1067 }
1069 /** find a symbol that this (proxy method) symbol implements.
1070 * @param c The class whose members are searched for
1071 * implementations
1072 */
1073 public Symbol implemented(TypeSymbol c, Types types) {
1074 Symbol impl = null;
1075 for (List<Type> is = types.interfaces(c.type);
1076 impl == null && is.nonEmpty();
1077 is = is.tail) {
1078 TypeSymbol i = is.head.tsym;
1079 impl = implementedIn(i, types);
1080 if (impl == null)
1081 impl = implemented(i, types);
1082 }
1083 return impl;
1084 }
1086 public Symbol implementedIn(TypeSymbol c, Types types) {
1087 Symbol impl = null;
1088 for (Scope.Entry e = c.members().lookup(name);
1089 impl == null && e.scope != null;
1090 e = e.next()) {
1091 if (this.overrides(e.sym, (TypeSymbol)owner, types, true) &&
1092 // FIXME: I suspect the following requires a
1093 // subst() for a parametric return type.
1094 types.isSameType(type.getReturnType(),
1095 types.memberType(owner.type, e.sym).getReturnType())) {
1096 impl = e.sym;
1097 }
1098 }
1099 return impl;
1100 }
1102 /** Will the erasure of this method be considered by the VM to
1103 * override the erasure of the other when seen from class `origin'?
1104 */
1105 public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
1106 if (isConstructor() || _other.kind != MTH) return false;
1108 if (this == _other) return true;
1109 MethodSymbol other = (MethodSymbol)_other;
1111 // check for a direct implementation
1112 if (other.isOverridableIn((TypeSymbol)owner) &&
1113 types.asSuper(owner.type, other.owner) != null &&
1114 types.isSameType(erasure(types), other.erasure(types)))
1115 return true;
1117 // check for an inherited implementation
1118 return
1119 (flags() & ABSTRACT) == 0 &&
1120 other.isOverridableIn(origin) &&
1121 this.isMemberOf(origin, types) &&
1122 types.isSameType(erasure(types), other.erasure(types));
1123 }
1125 /** The implementation of this (abstract) symbol in class origin,
1126 * from the VM's point of view, null if method does not have an
1127 * implementation in class.
1128 * @param origin The class of which the implementation is a member.
1129 */
1130 public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
1131 for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
1132 for (Scope.Entry e = c.members().lookup(name);
1133 e.scope != null;
1134 e = e.next()) {
1135 if (e.sym.kind == MTH &&
1136 ((MethodSymbol)e.sym).binaryOverrides(this, origin, types))
1137 return (MethodSymbol)e.sym;
1138 }
1139 }
1140 return null;
1141 }
1143 /** Does this symbol override `other' symbol, when both are seen as
1144 * members of class `origin'? It is assumed that _other is a member
1145 * of origin.
1146 *
1147 * It is assumed that both symbols have the same name. The static
1148 * modifier is ignored for this test.
1149 *
1150 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
1151 */
1152 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
1153 if (isConstructor() || _other.kind != MTH) return false;
1155 if (this == _other) return true;
1156 MethodSymbol other = (MethodSymbol)_other;
1158 // check for a direct implementation
1159 if (other.isOverridableIn((TypeSymbol)owner) &&
1160 types.asSuper(owner.type, other.owner) != null) {
1161 Type mt = types.memberType(owner.type, this);
1162 Type ot = types.memberType(owner.type, other);
1163 if (types.isSubSignature(mt, ot)) {
1164 if (!checkResult)
1165 return true;
1166 if (types.returnTypeSubstitutable(mt, ot))
1167 return true;
1168 }
1169 }
1171 // check for an inherited implementation
1172 if ((flags() & ABSTRACT) != 0 ||
1173 (other.flags() & ABSTRACT) == 0 ||
1174 !other.isOverridableIn(origin) ||
1175 !this.isMemberOf(origin, types))
1176 return false;
1178 // assert types.asSuper(origin.type, other.owner) != null;
1179 Type mt = types.memberType(origin.type, this);
1180 Type ot = types.memberType(origin.type, other);
1181 return
1182 types.isSubSignature(mt, ot) &&
1183 (!checkResult || types.resultSubtype(mt, ot, Warner.noWarnings));
1184 }
1186 private boolean isOverridableIn(TypeSymbol origin) {
1187 // JLS 8.4.6.1
1188 switch ((int)(flags_field & Flags.AccessFlags)) {
1189 case Flags.PRIVATE:
1190 return false;
1191 case Flags.PUBLIC:
1192 return true;
1193 case Flags.PROTECTED:
1194 return (origin.flags() & INTERFACE) == 0;
1195 case 0:
1196 // for package private: can only override in the same
1197 // package
1198 return
1199 this.packge() == origin.packge() &&
1200 (origin.flags() & INTERFACE) == 0;
1201 default:
1202 return false;
1203 }
1204 }
1206 /** The implementation of this (abstract) symbol in class origin;
1207 * null if none exists. Synthetic methods are not considered
1208 * as possible implementations.
1209 */
1210 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
1211 return implementation(origin, types, checkResult, implementation_filter);
1212 }
1213 // where
1214 private static final Filter<Symbol> implementation_filter = new Filter<Symbol>() {
1215 public boolean accepts(Symbol s) {
1216 return s.kind == Kinds.MTH &&
1217 (s.flags() & SYNTHETIC) == 0;
1218 }
1219 };
1221 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) {
1222 MethodSymbol res = types.implementation(this, origin, checkResult, implFilter);
1223 if (res != null)
1224 return res;
1225 // if origin is derived from a raw type, we might have missed
1226 // an implementation because we do not know enough about instantiations.
1227 // in this case continue with the supertype as origin.
1228 if (types.isDerivedRaw(origin.type) && !origin.isInterface())
1229 return implementation(types.supertype(origin.type).tsym, types, checkResult);
1230 else
1231 return null;
1232 }
1234 public List<VarSymbol> params() {
1235 owner.complete();
1236 if (params == null) {
1237 // If ClassReader.saveParameterNames has been set true, then
1238 // savedParameterNames will be set to a list of names that
1239 // matches the types in type.getParameterTypes(). If any names
1240 // were not found in the class file, those names in the list will
1241 // be set to the empty name.
1242 // If ClassReader.saveParameterNames has been set false, then
1243 // savedParameterNames will be null.
1244 List<Name> paramNames = savedParameterNames;
1245 savedParameterNames = null;
1246 // discard the provided names if the list of names is the wrong size.
1247 if (paramNames == null || paramNames.size() != type.getParameterTypes().size())
1248 paramNames = List.nil();
1249 ListBuffer<VarSymbol> buf = new ListBuffer<VarSymbol>();
1250 List<Name> remaining = paramNames;
1251 // assert: remaining and paramNames are both empty or both
1252 // have same cardinality as type.getParameterTypes()
1253 int i = 0;
1254 for (Type t : type.getParameterTypes()) {
1255 Name paramName;
1256 if (remaining.isEmpty()) {
1257 // no names for any parameters available
1258 paramName = createArgName(i, paramNames);
1259 } else {
1260 paramName = remaining.head;
1261 remaining = remaining.tail;
1262 if (paramName.isEmpty()) {
1263 // no name for this specific parameter
1264 paramName = createArgName(i, paramNames);
1265 }
1266 }
1267 buf.append(new VarSymbol(PARAMETER, paramName, t, this));
1268 i++;
1269 }
1270 params = buf.toList();
1271 }
1272 return params;
1273 }
1275 // Create a name for the argument at position 'index' that is not in
1276 // the exclude list. In normal use, either no names will have been
1277 // provided, in which case the exclude list is empty, or all the names
1278 // will have been provided, in which case this method will not be called.
1279 private Name createArgName(int index, List<Name> exclude) {
1280 String prefix = "arg";
1281 while (true) {
1282 Name argName = name.table.fromString(prefix + index);
1283 if (!exclude.contains(argName))
1284 return argName;
1285 prefix += "$";
1286 }
1287 }
1289 public Symbol asMemberOf(Type site, Types types) {
1290 return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
1291 }
1293 public ElementKind getKind() {
1294 if (name == name.table.names.init)
1295 return ElementKind.CONSTRUCTOR;
1296 else if (name == name.table.names.clinit)
1297 return ElementKind.STATIC_INIT;
1298 else if ((flags() & BLOCK) != 0)
1299 return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT;
1300 else
1301 return ElementKind.METHOD;
1302 }
1304 public boolean isStaticOrInstanceInit() {
1305 return getKind() == ElementKind.STATIC_INIT ||
1306 getKind() == ElementKind.INSTANCE_INIT;
1307 }
1309 /**
1310 * A polymorphic signature method (JLS SE 7, 8.4.1) is a method that
1311 * (i) is declared in the java.lang.invoke.MethodHandle class, (ii) takes
1312 * a single variable arity parameter (iii) whose declared type is Object[],
1313 * (iv) has a return type of Object and (v) is native.
1314 */
1315 public boolean isSignaturePolymorphic(Types types) {
1316 List<Type> argtypes = type.getParameterTypes();
1317 Type firstElemType = argtypes.nonEmpty() ?
1318 types.elemtype(argtypes.head) :
1319 null;
1320 return owner == types.syms.methodHandleType.tsym &&
1321 argtypes.length() == 1 &&
1322 firstElemType != null &&
1323 types.isSameType(firstElemType, types.syms.objectType) &&
1324 types.isSameType(type.getReturnType(), types.syms.objectType) &&
1325 (flags() & NATIVE) != 0;
1326 }
1328 public Attribute getDefaultValue() {
1329 return defaultValue;
1330 }
1332 public List<VarSymbol> getParameters() {
1333 return params();
1334 }
1336 public boolean isVarArgs() {
1337 return (flags() & VARARGS) != 0;
1338 }
1340 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1341 return v.visitExecutable(this, p);
1342 }
1344 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1345 return v.visitMethodSymbol(this, p);
1346 }
1348 public Type getReturnType() {
1349 return asType().getReturnType();
1350 }
1352 public List<Type> getThrownTypes() {
1353 return asType().getThrownTypes();
1354 }
1355 }
1357 /** A class for predefined operators.
1358 */
1359 public static class OperatorSymbol extends MethodSymbol {
1361 public int opcode;
1363 public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
1364 super(PUBLIC | STATIC, name, type, owner);
1365 this.opcode = opcode;
1366 }
1368 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1369 return v.visitOperatorSymbol(this, p);
1370 }
1371 }
1373 /** Symbol completer interface.
1374 */
1375 public static interface Completer {
1376 void complete(Symbol sym) throws CompletionFailure;
1377 }
1379 public static class CompletionFailure extends RuntimeException {
1380 private static final long serialVersionUID = 0;
1381 public Symbol sym;
1383 /** A diagnostic object describing the failure
1384 */
1385 public JCDiagnostic diag;
1387 /** A localized string describing the failure.
1388 * @deprecated Use {@code getDetail()} or {@code getMessage()}
1389 */
1390 @Deprecated
1391 public String errmsg;
1393 public CompletionFailure(Symbol sym, String errmsg) {
1394 this.sym = sym;
1395 this.errmsg = errmsg;
1396 // this.printStackTrace();//DEBUG
1397 }
1399 public CompletionFailure(Symbol sym, JCDiagnostic diag) {
1400 this.sym = sym;
1401 this.diag = diag;
1402 // this.printStackTrace();//DEBUG
1403 }
1405 public JCDiagnostic getDiagnostic() {
1406 return diag;
1407 }
1409 @Override
1410 public String getMessage() {
1411 if (diag != null)
1412 return diag.getMessage(null);
1413 else
1414 return errmsg;
1415 }
1417 public Object getDetailValue() {
1418 return (diag != null ? diag : errmsg);
1419 }
1421 @Override
1422 public CompletionFailure initCause(Throwable cause) {
1423 super.initCause(cause);
1424 return this;
1425 }
1427 }
1429 /**
1430 * A visitor for symbols. A visitor is used to implement operations
1431 * (or relations) on symbols. Most common operations on types are
1432 * binary relations and this interface is designed for binary
1433 * relations, that is, operations on the form
1434 * Symbol × P → R.
1435 * <!-- In plain text: Type x P -> R -->
1436 *
1437 * @param <R> the return type of the operation implemented by this
1438 * visitor; use Void if no return type is needed.
1439 * @param <P> the type of the second argument (the first being the
1440 * symbol itself) of the operation implemented by this visitor; use
1441 * Void if a second argument is not needed.
1442 */
1443 public interface Visitor<R,P> {
1444 R visitClassSymbol(ClassSymbol s, P arg);
1445 R visitMethodSymbol(MethodSymbol s, P arg);
1446 R visitPackageSymbol(PackageSymbol s, P arg);
1447 R visitOperatorSymbol(OperatorSymbol s, P arg);
1448 R visitVarSymbol(VarSymbol s, P arg);
1449 R visitTypeSymbol(TypeSymbol s, P arg);
1450 R visitSymbol(Symbol s, P arg);
1451 }
1452 }