Wed, 02 Mar 2011 21:13:55 -0800
6639645: Modeling type implementing missing interfaces
Reviewed-by: darcy, mcimadamore
1 /*
2 * Copyright (c) 1999, 2011, 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() && owner.kind != PCK && owner.kind != TYP)) {
153 return null;
154 }
155 return owner;
156 }
158 public Symbol location(Type site, Types types) {
159 if (owner.name == null || owner.name.isEmpty()) {
160 return location();
161 }
162 if (owner.type.tag == CLASS) {
163 Type ownertype = types.asOuterSuper(site, owner);
164 if (ownertype != null) return ownertype.tsym;
165 }
166 return owner;
167 }
169 /** The symbol's erased type.
170 */
171 public Type erasure(Types types) {
172 if (erasure_field == null)
173 erasure_field = types.erasure(type);
174 return erasure_field;
175 }
177 /** The external type of a symbol. This is the symbol's erased type
178 * except for constructors of inner classes which get the enclosing
179 * instance class added as first argument.
180 */
181 public Type externalType(Types types) {
182 Type t = erasure(types);
183 if (name == name.table.names.init && owner.hasOuterInstance()) {
184 Type outerThisType = types.erasure(owner.type.getEnclosingType());
185 return new MethodType(t.getParameterTypes().prepend(outerThisType),
186 t.getReturnType(),
187 t.getThrownTypes(),
188 t.tsym);
189 } else {
190 return t;
191 }
192 }
194 public boolean isStatic() {
195 return
196 (flags() & STATIC) != 0 ||
197 (owner.flags() & INTERFACE) != 0 && kind != MTH;
198 }
200 public boolean isInterface() {
201 return (flags() & INTERFACE) != 0;
202 }
204 /** Recognize if this symbol was marked @PolymorphicSignature in the source. */
205 public boolean isPolymorphicSignatureGeneric() {
206 return (flags() & (POLYMORPHIC_SIGNATURE | HYPOTHETICAL)) == POLYMORPHIC_SIGNATURE;
207 }
209 /** Recognize if this symbol was split from a @PolymorphicSignature symbol in the source. */
210 public boolean isPolymorphicSignatureInstance() {
211 return (flags() & (POLYMORPHIC_SIGNATURE | HYPOTHETICAL)) == (POLYMORPHIC_SIGNATURE | HYPOTHETICAL);
212 }
214 /** Is this symbol declared (directly or indirectly) local
215 * to a method or variable initializer?
216 * Also includes fields of inner classes which are in
217 * turn local to a method or variable initializer.
218 */
219 public boolean isLocal() {
220 return
221 (owner.kind & (VAR | MTH)) != 0 ||
222 (owner.kind == TYP && owner.isLocal());
223 }
225 /** Has this symbol an empty name? This includes anonymous
226 * inner classses.
227 */
228 public boolean isAnonymous() {
229 return name.isEmpty();
230 }
232 /** Is this symbol a constructor?
233 */
234 public boolean isConstructor() {
235 return name == name.table.names.init;
236 }
238 /** The fully qualified name of this symbol.
239 * This is the same as the symbol's name except for class symbols,
240 * which are handled separately.
241 */
242 public Name getQualifiedName() {
243 return name;
244 }
246 /** The fully qualified name of this symbol after converting to flat
247 * representation. This is the same as the symbol's name except for
248 * class symbols, which are handled separately.
249 */
250 public Name flatName() {
251 return getQualifiedName();
252 }
254 /** If this is a class or package, its members, otherwise null.
255 */
256 public Scope members() {
257 return null;
258 }
260 /** A class is an inner class if it it has an enclosing instance class.
261 */
262 public boolean isInner() {
263 return type.getEnclosingType().tag == CLASS;
264 }
266 /** An inner class has an outer instance if it is not an interface
267 * it has an enclosing instance class which might be referenced from the class.
268 * Nested classes can see instance members of their enclosing class.
269 * Their constructors carry an additional this$n parameter, inserted
270 * implicitly by the compiler.
271 *
272 * @see #isInner
273 */
274 public boolean hasOuterInstance() {
275 return
276 type.getEnclosingType().tag == CLASS && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
277 }
279 /** The closest enclosing class of this symbol's declaration.
280 */
281 public ClassSymbol enclClass() {
282 Symbol c = this;
283 while (c != null &&
284 ((c.kind & TYP) == 0 || c.type.tag != CLASS)) {
285 c = c.owner;
286 }
287 return (ClassSymbol)c;
288 }
290 /** The outermost class which indirectly owns this symbol.
291 */
292 public ClassSymbol outermostClass() {
293 Symbol sym = this;
294 Symbol prev = null;
295 while (sym.kind != PCK) {
296 prev = sym;
297 sym = sym.owner;
298 }
299 return (ClassSymbol) prev;
300 }
302 /** The package which indirectly owns this symbol.
303 */
304 public PackageSymbol packge() {
305 Symbol sym = this;
306 while (sym.kind != PCK) {
307 sym = sym.owner;
308 }
309 return (PackageSymbol) sym;
310 }
312 /** Is this symbol a subclass of `base'? Only defined for ClassSymbols.
313 */
314 public boolean isSubClass(Symbol base, Types types) {
315 throw new AssertionError("isSubClass " + this);
316 }
318 /** Fully check membership: hierarchy, protection, and hiding.
319 * Does not exclude methods not inherited due to overriding.
320 */
321 public boolean isMemberOf(TypeSymbol clazz, Types types) {
322 return
323 owner == clazz ||
324 clazz.isSubClass(owner, types) &&
325 isInheritedIn(clazz, types) &&
326 !hiddenIn((ClassSymbol)clazz, types);
327 }
329 /** Is this symbol the same as or enclosed by the given class? */
330 public boolean isEnclosedBy(ClassSymbol clazz) {
331 for (Symbol sym = this; sym.kind != PCK; sym = sym.owner)
332 if (sym == clazz) return true;
333 return false;
334 }
336 /** Check for hiding. Note that this doesn't handle multiple
337 * (interface) inheritance. */
338 private boolean hiddenIn(ClassSymbol clazz, Types types) {
339 if (kind == MTH && (flags() & STATIC) == 0) return false;
340 while (true) {
341 if (owner == clazz) return false;
342 Scope.Entry e = clazz.members().lookup(name);
343 while (e.scope != null) {
344 if (e.sym == this) return false;
345 if (e.sym.kind == kind &&
346 (kind != MTH ||
347 (e.sym.flags() & STATIC) != 0 &&
348 types.isSubSignature(e.sym.type, type)))
349 return true;
350 e = e.next();
351 }
352 Type superType = types.supertype(clazz.type);
353 if (superType.tag != TypeTags.CLASS) return false;
354 clazz = (ClassSymbol)superType.tsym;
355 }
356 }
358 /** Is this symbol inherited into a given class?
359 * PRE: If symbol's owner is a interface,
360 * it is already assumed that the interface is a superinterface
361 * of given class.
362 * @param clazz The class for which we want to establish membership.
363 * This must be a subclass of the member's owner.
364 */
365 public boolean isInheritedIn(Symbol clazz, Types types) {
366 switch ((int)(flags_field & Flags.AccessFlags)) {
367 default: // error recovery
368 case PUBLIC:
369 return true;
370 case PRIVATE:
371 return this.owner == clazz;
372 case PROTECTED:
373 // we model interfaces as extending Object
374 return (clazz.flags() & INTERFACE) == 0;
375 case 0:
376 PackageSymbol thisPackage = this.packge();
377 for (Symbol sup = clazz;
378 sup != null && sup != this.owner;
379 sup = types.supertype(sup.type).tsym) {
380 while (sup.type.tag == TYPEVAR)
381 sup = sup.type.getUpperBound().tsym;
382 if (sup.type.isErroneous())
383 return true; // error recovery
384 if ((sup.flags() & COMPOUND) != 0)
385 continue;
386 if (sup.packge() != thisPackage)
387 return false;
388 }
389 return (clazz.flags() & INTERFACE) == 0;
390 }
391 }
393 /** The (variable or method) symbol seen as a member of given
394 * class type`site' (this might change the symbol's type).
395 * This is used exclusively for producing diagnostics.
396 */
397 public Symbol asMemberOf(Type site, Types types) {
398 throw new AssertionError();
399 }
401 /** Does this method symbol override `other' symbol, when both are seen as
402 * members of class `origin'? It is assumed that _other is a member
403 * of origin.
404 *
405 * It is assumed that both symbols have the same name. The static
406 * modifier is ignored for this test.
407 *
408 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
409 */
410 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
411 return false;
412 }
414 /** Complete the elaboration of this symbol's definition.
415 */
416 public void complete() throws CompletionFailure {
417 if (completer != null) {
418 Completer c = completer;
419 completer = null;
420 c.complete(this);
421 }
422 }
424 /** True if the symbol represents an entity that exists.
425 */
426 public boolean exists() {
427 return true;
428 }
430 public Type asType() {
431 return type;
432 }
434 public Symbol getEnclosingElement() {
435 return owner;
436 }
438 public ElementKind getKind() {
439 return ElementKind.OTHER; // most unkind
440 }
442 public Set<Modifier> getModifiers() {
443 return Flags.asModifierSet(flags());
444 }
446 public Name getSimpleName() {
447 return name;
448 }
450 /**
451 * @deprecated this method should never be used by javac internally.
452 */
453 @Deprecated
454 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
455 return JavacElements.getAnnotation(this, annoType);
456 }
458 // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList
459 public java.util.List<Symbol> getEnclosedElements() {
460 return List.nil();
461 }
463 public List<TypeSymbol> getTypeParameters() {
464 ListBuffer<TypeSymbol> l = ListBuffer.lb();
465 for (Type t : type.getTypeArguments()) {
466 l.append(t.tsym);
467 }
468 return l.toList();
469 }
471 public static class DelegatedSymbol extends Symbol {
472 protected Symbol other;
473 public DelegatedSymbol(Symbol other) {
474 super(other.kind, other.flags_field, other.name, other.type, other.owner);
475 this.other = other;
476 }
477 public String toString() { return other.toString(); }
478 public Symbol location() { return other.location(); }
479 public Symbol location(Type site, Types types) { return other.location(site, types); }
480 public Type erasure(Types types) { return other.erasure(types); }
481 public Type externalType(Types types) { return other.externalType(types); }
482 public boolean isLocal() { return other.isLocal(); }
483 public boolean isConstructor() { return other.isConstructor(); }
484 public Name getQualifiedName() { return other.getQualifiedName(); }
485 public Name flatName() { return other.flatName(); }
486 public Scope members() { return other.members(); }
487 public boolean isInner() { return other.isInner(); }
488 public boolean hasOuterInstance() { return other.hasOuterInstance(); }
489 public ClassSymbol enclClass() { return other.enclClass(); }
490 public ClassSymbol outermostClass() { return other.outermostClass(); }
491 public PackageSymbol packge() { return other.packge(); }
492 public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); }
493 public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); }
494 public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); }
495 public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); }
496 public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); }
497 public void complete() throws CompletionFailure { other.complete(); }
499 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
500 return other.accept(v, p);
501 }
503 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
504 return v.visitSymbol(other, p);
505 }
506 }
508 /** A class for type symbols. Type variables are represented by instances
509 * of this class, classes and packages by instances of subclasses.
510 */
511 public static class TypeSymbol
512 extends Symbol implements TypeParameterElement {
513 // Implements TypeParameterElement because type parameters don't
514 // have their own TypeSymbol subclass.
515 // TODO: type parameters should have their own TypeSymbol subclass
517 public TypeSymbol(long flags, Name name, Type type, Symbol owner) {
518 super(TYP, flags, name, type, owner);
519 }
521 /** form a fully qualified name from a name and an owner
522 */
523 static public Name formFullName(Name name, Symbol owner) {
524 if (owner == null) return name;
525 if (((owner.kind != ERR)) &&
526 ((owner.kind & (VAR | MTH)) != 0
527 || (owner.kind == TYP && owner.type.tag == TYPEVAR)
528 )) return name;
529 Name prefix = owner.getQualifiedName();
530 if (prefix == null || prefix == prefix.table.names.empty)
531 return name;
532 else return prefix.append('.', name);
533 }
535 /** form a fully qualified name from a name and an owner, after
536 * converting to flat representation
537 */
538 static public Name formFlatName(Name name, Symbol owner) {
539 if (owner == null ||
540 (owner.kind & (VAR | MTH)) != 0
541 || (owner.kind == TYP && owner.type.tag == TYPEVAR)
542 ) return name;
543 char sep = owner.kind == TYP ? '$' : '.';
544 Name prefix = owner.flatName();
545 if (prefix == null || prefix == prefix.table.names.empty)
546 return name;
547 else return prefix.append(sep, name);
548 }
550 /**
551 * A total ordering between type symbols that refines the
552 * class inheritance graph.
553 *
554 * Typevariables always precede other kinds of symbols.
555 */
556 public final boolean precedes(TypeSymbol that, Types types) {
557 if (this == that)
558 return false;
559 if (this.type.tag == that.type.tag) {
560 if (this.type.tag == CLASS) {
561 return
562 types.rank(that.type) < types.rank(this.type) ||
563 types.rank(that.type) == types.rank(this.type) &&
564 that.getQualifiedName().compareTo(this.getQualifiedName()) < 0;
565 } else if (this.type.tag == TYPEVAR) {
566 return types.isSubtype(this.type, that.type);
567 }
568 }
569 return this.type.tag == TYPEVAR;
570 }
572 // For type params; overridden in subclasses.
573 public ElementKind getKind() {
574 return ElementKind.TYPE_PARAMETER;
575 }
577 public java.util.List<Symbol> getEnclosedElements() {
578 List<Symbol> list = List.nil();
579 if (kind == TYP && type.tag == TYPEVAR) {
580 return list;
581 }
582 for (Scope.Entry e = members().elems; e != null; e = e.sibling) {
583 if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this)
584 list = list.prepend(e.sym);
585 }
586 return list;
587 }
589 // For type params.
590 // Perhaps not needed if getEnclosingElement can be spec'ed
591 // to do the same thing.
592 // TODO: getGenericElement() might not be needed
593 public Symbol getGenericElement() {
594 return owner;
595 }
597 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
598 Assert.check(type.tag == TYPEVAR); // else override will be invoked
599 return v.visitTypeParameter(this, p);
600 }
602 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
603 return v.visitTypeSymbol(this, p);
604 }
606 public List<Type> getBounds() {
607 TypeVar t = (TypeVar)type;
608 Type bound = t.getUpperBound();
609 if (!bound.isCompound())
610 return List.of(bound);
611 ClassType ct = (ClassType)bound;
612 if (!ct.tsym.erasure_field.isInterface()) {
613 return ct.interfaces_field.prepend(ct.supertype_field);
614 } else {
615 // No superclass was given in bounds.
616 // In this case, supertype is Object, erasure is first interface.
617 return ct.interfaces_field;
618 }
619 }
620 }
622 /** A class for package symbols
623 */
624 public static class PackageSymbol extends TypeSymbol
625 implements PackageElement {
627 public Scope members_field;
628 public Name fullname;
629 public ClassSymbol package_info; // see bug 6443073
631 public PackageSymbol(Name name, Type type, Symbol owner) {
632 super(0, name, type, owner);
633 this.kind = PCK;
634 this.members_field = null;
635 this.fullname = formFullName(name, owner);
636 }
638 public PackageSymbol(Name name, Symbol owner) {
639 this(name, null, owner);
640 this.type = new PackageType(this);
641 }
643 public String toString() {
644 return fullname.toString();
645 }
647 public Name getQualifiedName() {
648 return fullname;
649 }
651 public boolean isUnnamed() {
652 return name.isEmpty() && owner != null;
653 }
655 public Scope members() {
656 if (completer != null) complete();
657 return members_field;
658 }
660 public long flags() {
661 if (completer != null) complete();
662 return flags_field;
663 }
665 public List<Attribute.Compound> getAnnotationMirrors() {
666 if (completer != null) complete();
667 if (package_info != null && package_info.completer != null) {
668 package_info.complete();
669 if (attributes_field.isEmpty())
670 attributes_field = package_info.attributes_field;
671 }
672 return Assert.checkNonNull(attributes_field);
673 }
675 /** A package "exists" if a type or package that exists has
676 * been seen within it.
677 */
678 public boolean exists() {
679 return (flags_field & EXISTS) != 0;
680 }
682 public ElementKind getKind() {
683 return ElementKind.PACKAGE;
684 }
686 public Symbol getEnclosingElement() {
687 return null;
688 }
690 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
691 return v.visitPackage(this, p);
692 }
694 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
695 return v.visitPackageSymbol(this, p);
696 }
697 }
699 /** A class for class symbols
700 */
701 public static class ClassSymbol extends TypeSymbol implements TypeElement {
703 /** a scope for all class members; variables, methods and inner classes
704 * type parameters are not part of this scope
705 */
706 public Scope members_field;
708 /** the fully qualified name of the class, i.e. pck.outer.inner.
709 * null for anonymous classes
710 */
711 public Name fullname;
713 /** the fully qualified name of the class after converting to flat
714 * representation, i.e. pck.outer$inner,
715 * set externally for local and anonymous classes
716 */
717 public Name flatname;
719 /** the sourcefile where the class came from
720 */
721 public JavaFileObject sourcefile;
723 /** the classfile from where to load this class
724 * this will have extension .class or .java
725 */
726 public JavaFileObject classfile;
728 /** the constant pool of the class
729 */
730 public Pool pool;
732 /** members closure cache (set by Types.membersClosure)
733 */
734 Scope.CompoundScope membersClosure;
736 public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
737 super(flags, name, type, owner);
738 this.members_field = null;
739 this.fullname = formFullName(name, owner);
740 this.flatname = formFlatName(name, owner);
741 this.sourcefile = null;
742 this.classfile = null;
743 this.pool = null;
744 }
746 public ClassSymbol(long flags, Name name, Symbol owner) {
747 this(
748 flags,
749 name,
750 new ClassType(Type.noType, null, null),
751 owner);
752 this.type.tsym = this;
753 }
755 /** The Java source which this symbol represents.
756 */
757 public String toString() {
758 return className();
759 }
761 public long flags() {
762 if (completer != null) complete();
763 return flags_field;
764 }
766 public Scope members() {
767 if (completer != null) complete();
768 return members_field;
769 }
771 public List<Attribute.Compound> getAnnotationMirrors() {
772 if (completer != null) complete();
773 return Assert.checkNonNull(attributes_field);
774 }
776 public Type erasure(Types types) {
777 if (erasure_field == null)
778 erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
779 List.<Type>nil(), this);
780 return erasure_field;
781 }
783 public String className() {
784 if (name.isEmpty())
785 return
786 Log.getLocalizedString("anonymous.class", flatname);
787 else
788 return fullname.toString();
789 }
791 public Name getQualifiedName() {
792 return fullname;
793 }
795 public Name flatName() {
796 return flatname;
797 }
799 public boolean isSubClass(Symbol base, Types types) {
800 if (this == base) {
801 return true;
802 } else if ((base.flags() & INTERFACE) != 0) {
803 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
804 for (List<Type> is = types.interfaces(t);
805 is.nonEmpty();
806 is = is.tail)
807 if (is.head.tsym.isSubClass(base, types)) return true;
808 } else {
809 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
810 if (t.tsym == base) return true;
811 }
812 return false;
813 }
815 /** Complete the elaboration of this symbol's definition.
816 */
817 public void complete() throws CompletionFailure {
818 try {
819 super.complete();
820 } catch (CompletionFailure ex) {
821 // quiet error recovery
822 flags_field |= (PUBLIC|STATIC);
823 this.type = new ErrorType(this, Type.noType);
824 throw ex;
825 }
826 }
828 public List<Type> getInterfaces() {
829 complete();
830 if (type instanceof ClassType) {
831 ClassType t = (ClassType)type;
832 if (t.interfaces_field == null) // FIXME: shouldn't be null
833 t.interfaces_field = List.nil();
834 if (t.all_interfaces_field != null)
835 return Type.getModelTypes(t.all_interfaces_field);
836 return t.interfaces_field;
837 } else {
838 return List.nil();
839 }
840 }
842 public Type getSuperclass() {
843 complete();
844 if (type instanceof ClassType) {
845 ClassType t = (ClassType)type;
846 if (t.supertype_field == null) // FIXME: shouldn't be null
847 t.supertype_field = Type.noType;
848 // An interface has no superclass; its supertype is Object.
849 return t.isInterface()
850 ? Type.noType
851 : t.supertype_field.getModelType();
852 } else {
853 return Type.noType;
854 }
855 }
857 public ElementKind getKind() {
858 long flags = flags();
859 if ((flags & ANNOTATION) != 0)
860 return ElementKind.ANNOTATION_TYPE;
861 else if ((flags & INTERFACE) != 0)
862 return ElementKind.INTERFACE;
863 else if ((flags & ENUM) != 0)
864 return ElementKind.ENUM;
865 else
866 return ElementKind.CLASS;
867 }
869 public NestingKind getNestingKind() {
870 complete();
871 if (owner.kind == PCK)
872 return NestingKind.TOP_LEVEL;
873 else if (name.isEmpty())
874 return NestingKind.ANONYMOUS;
875 else if (owner.kind == MTH)
876 return NestingKind.LOCAL;
877 else
878 return NestingKind.MEMBER;
879 }
881 /**
882 * @deprecated this method should never be used by javac internally.
883 */
884 @Override @Deprecated
885 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
886 return JavacElements.getAnnotation(this, annoType);
887 }
889 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
890 return v.visitType(this, p);
891 }
893 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
894 return v.visitClassSymbol(this, p);
895 }
896 }
899 /** A class for variable symbols
900 */
901 public static class VarSymbol extends Symbol implements VariableElement {
903 /** The variable's declaration position.
904 */
905 public int pos = Position.NOPOS;
907 /** The variable's address. Used for different purposes during
908 * flow analysis, translation and code generation.
909 * Flow analysis:
910 * If this is a blank final or local variable, its sequence number.
911 * Translation:
912 * If this is a private field, its access number.
913 * Code generation:
914 * If this is a local variable, its logical slot number.
915 */
916 public int adr = -1;
918 /** Construct a variable symbol, given its flags, name, type and owner.
919 */
920 public VarSymbol(long flags, Name name, Type type, Symbol owner) {
921 super(VAR, flags, name, type, owner);
922 }
924 /** Clone this symbol with new owner.
925 */
926 public VarSymbol clone(Symbol newOwner) {
927 VarSymbol v = new VarSymbol(flags_field, name, type, newOwner);
928 v.pos = pos;
929 v.adr = adr;
930 v.data = data;
931 // System.out.println("clone " + v + " in " + newOwner);//DEBUG
932 return v;
933 }
935 public String toString() {
936 return name.toString();
937 }
939 public Symbol asMemberOf(Type site, Types types) {
940 return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
941 }
943 public ElementKind getKind() {
944 long flags = flags();
945 if ((flags & PARAMETER) != 0) {
946 if (isExceptionParameter())
947 return ElementKind.EXCEPTION_PARAMETER;
948 else
949 return ElementKind.PARAMETER;
950 } else if ((flags & ENUM) != 0) {
951 return ElementKind.ENUM_CONSTANT;
952 } else if (owner.kind == TYP || owner.kind == ERR) {
953 return ElementKind.FIELD;
954 } else if (isResourceVariable()) {
955 return ElementKind.RESOURCE_VARIABLE;
956 } else {
957 return ElementKind.LOCAL_VARIABLE;
958 }
959 }
961 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
962 return v.visitVariable(this, p);
963 }
965 public Object getConstantValue() { // Mirror API
966 return Constants.decode(getConstValue(), type);
967 }
969 public void setLazyConstValue(final Env<AttrContext> env,
970 final Attr attr,
971 final JCTree.JCExpression initializer)
972 {
973 setData(new Callable<Object>() {
974 public Object call() {
975 return attr.attribLazyConstantValue(env, initializer, type);
976 }
977 });
978 }
980 /**
981 * The variable's constant value, if this is a constant.
982 * Before the constant value is evaluated, it points to an
983 * initalizer environment. If this is not a constant, it can
984 * be used for other stuff.
985 */
986 private Object data;
988 public boolean isExceptionParameter() {
989 return data == ElementKind.EXCEPTION_PARAMETER;
990 }
992 public boolean isResourceVariable() {
993 return data == ElementKind.RESOURCE_VARIABLE;
994 }
996 public Object getConstValue() {
997 // TODO: Consider if getConstValue and getConstantValue can be collapsed
998 if (data == ElementKind.EXCEPTION_PARAMETER ||
999 data == ElementKind.RESOURCE_VARIABLE) {
1000 return null;
1001 } else if (data instanceof Callable<?>) {
1002 // In this case, this is a final variable, with an as
1003 // yet unevaluated initializer.
1004 Callable<?> eval = (Callable<?>)data;
1005 data = null; // to make sure we don't evaluate this twice.
1006 try {
1007 data = eval.call();
1008 } catch (Exception ex) {
1009 ex.printStackTrace();
1010 throw new AssertionError(ex);
1011 }
1012 }
1013 return data;
1014 }
1016 public void setData(Object data) {
1017 Assert.check(!(data instanceof Env<?>), this);
1018 this.data = data;
1019 }
1021 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1022 return v.visitVarSymbol(this, p);
1023 }
1024 }
1026 /** A class for method symbols.
1027 */
1028 public static class MethodSymbol extends Symbol implements ExecutableElement {
1030 /** The code of the method. */
1031 public Code code = null;
1033 /** The parameters of the method. */
1034 public List<VarSymbol> params = null;
1036 /** The names of the parameters */
1037 public List<Name> savedParameterNames;
1039 /** For an attribute field accessor, its default value if any.
1040 * The value is null if none appeared in the method
1041 * declaration.
1042 */
1043 public Attribute defaultValue = null;
1045 /** Construct a method symbol, given its flags, name, type and owner.
1046 */
1047 public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
1048 super(MTH, flags, name, type, owner);
1049 if (owner.type.tag == TYPEVAR) Assert.error(owner + "." + name);
1050 }
1052 /** Clone this symbol with new owner.
1053 */
1054 public MethodSymbol clone(Symbol newOwner) {
1055 MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner);
1056 m.code = code;
1057 return m;
1058 }
1060 /** The Java source which this symbol represents.
1061 */
1062 public String toString() {
1063 if ((flags() & BLOCK) != 0) {
1064 return owner.name.toString();
1065 } else {
1066 String s = (name == name.table.names.init)
1067 ? owner.name.toString()
1068 : name.toString();
1069 if (type != null) {
1070 if (type.tag == FORALL)
1071 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
1072 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
1073 }
1074 return s;
1075 }
1076 }
1078 /** find a symbol that this (proxy method) symbol implements.
1079 * @param c The class whose members are searched for
1080 * implementations
1081 */
1082 public Symbol implemented(TypeSymbol c, Types types) {
1083 Symbol impl = null;
1084 for (List<Type> is = types.interfaces(c.type);
1085 impl == null && is.nonEmpty();
1086 is = is.tail) {
1087 TypeSymbol i = is.head.tsym;
1088 impl = implementedIn(i, types);
1089 if (impl == null)
1090 impl = implemented(i, types);
1091 }
1092 return impl;
1093 }
1095 public Symbol implementedIn(TypeSymbol c, Types types) {
1096 Symbol impl = null;
1097 for (Scope.Entry e = c.members().lookup(name);
1098 impl == null && e.scope != null;
1099 e = e.next()) {
1100 if (this.overrides(e.sym, (TypeSymbol)owner, types, true) &&
1101 // FIXME: I suspect the following requires a
1102 // subst() for a parametric return type.
1103 types.isSameType(type.getReturnType(),
1104 types.memberType(owner.type, e.sym).getReturnType())) {
1105 impl = e.sym;
1106 }
1107 }
1108 return impl;
1109 }
1111 /** Will the erasure of this method be considered by the VM to
1112 * override the erasure of the other when seen from class `origin'?
1113 */
1114 public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
1115 if (isConstructor() || _other.kind != MTH) return false;
1117 if (this == _other) return true;
1118 MethodSymbol other = (MethodSymbol)_other;
1120 // check for a direct implementation
1121 if (other.isOverridableIn((TypeSymbol)owner) &&
1122 types.asSuper(owner.type, other.owner) != null &&
1123 types.isSameType(erasure(types), other.erasure(types)))
1124 return true;
1126 // check for an inherited implementation
1127 return
1128 (flags() & ABSTRACT) == 0 &&
1129 other.isOverridableIn(origin) &&
1130 this.isMemberOf(origin, types) &&
1131 types.isSameType(erasure(types), other.erasure(types));
1132 }
1134 /** The implementation of this (abstract) symbol in class origin,
1135 * from the VM's point of view, null if method does not have an
1136 * implementation in class.
1137 * @param origin The class of which the implementation is a member.
1138 */
1139 public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
1140 for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
1141 for (Scope.Entry e = c.members().lookup(name);
1142 e.scope != null;
1143 e = e.next()) {
1144 if (e.sym.kind == MTH &&
1145 ((MethodSymbol)e.sym).binaryOverrides(this, origin, types))
1146 return (MethodSymbol)e.sym;
1147 }
1148 }
1149 return null;
1150 }
1152 /** Does this symbol override `other' symbol, when both are seen as
1153 * members of class `origin'? It is assumed that _other is a member
1154 * of origin.
1155 *
1156 * It is assumed that both symbols have the same name. The static
1157 * modifier is ignored for this test.
1158 *
1159 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
1160 */
1161 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
1162 if (isConstructor() || _other.kind != MTH) return false;
1164 if (this == _other) return true;
1165 MethodSymbol other = (MethodSymbol)_other;
1167 // check for a direct implementation
1168 if (other.isOverridableIn((TypeSymbol)owner) &&
1169 types.asSuper(owner.type, other.owner) != null) {
1170 Type mt = types.memberType(owner.type, this);
1171 Type ot = types.memberType(owner.type, other);
1172 if (types.isSubSignature(mt, ot)) {
1173 if (!checkResult)
1174 return true;
1175 if (types.returnTypeSubstitutable(mt, ot))
1176 return true;
1177 }
1178 }
1180 // check for an inherited implementation
1181 if ((flags() & ABSTRACT) != 0 ||
1182 (other.flags() & ABSTRACT) == 0 ||
1183 !other.isOverridableIn(origin) ||
1184 !this.isMemberOf(origin, types))
1185 return false;
1187 // assert types.asSuper(origin.type, other.owner) != null;
1188 Type mt = types.memberType(origin.type, this);
1189 Type ot = types.memberType(origin.type, other);
1190 return
1191 types.isSubSignature(mt, ot) &&
1192 (!checkResult || types.resultSubtype(mt, ot, Warner.noWarnings));
1193 }
1195 private boolean isOverridableIn(TypeSymbol origin) {
1196 // JLS3 8.4.6.1
1197 switch ((int)(flags_field & Flags.AccessFlags)) {
1198 case Flags.PRIVATE:
1199 return false;
1200 case Flags.PUBLIC:
1201 return true;
1202 case Flags.PROTECTED:
1203 return (origin.flags() & INTERFACE) == 0;
1204 case 0:
1205 // for package private: can only override in the same
1206 // package
1207 return
1208 this.packge() == origin.packge() &&
1209 (origin.flags() & INTERFACE) == 0;
1210 default:
1211 return false;
1212 }
1213 }
1215 /** The implementation of this (abstract) symbol in class origin;
1216 * null if none exists. Synthetic methods are not considered
1217 * as possible implementations.
1218 */
1219 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
1220 return implementation(origin, types, checkResult, implementation_filter);
1221 }
1222 // where
1223 private static final Filter<Symbol> implementation_filter = new Filter<Symbol>() {
1224 public boolean accepts(Symbol s) {
1225 return s.kind == Kinds.MTH &&
1226 (s.flags() & SYNTHETIC) == 0;
1227 }
1228 };
1230 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) {
1231 MethodSymbol res = types.implementation(this, origin, checkResult, implFilter);
1232 if (res != null)
1233 return res;
1234 // if origin is derived from a raw type, we might have missed
1235 // an implementation because we do not know enough about instantiations.
1236 // in this case continue with the supertype as origin.
1237 if (types.isDerivedRaw(origin.type))
1238 return implementation(types.supertype(origin.type).tsym, types, checkResult);
1239 else
1240 return null;
1241 }
1243 public List<VarSymbol> params() {
1244 owner.complete();
1245 if (params == null) {
1246 // If ClassReader.saveParameterNames has been set true, then
1247 // savedParameterNames will be set to a list of names that
1248 // matches the types in type.getParameterTypes(). If any names
1249 // were not found in the class file, those names in the list will
1250 // be set to the empty name.
1251 // If ClassReader.saveParameterNames has been set false, then
1252 // savedParameterNames will be null.
1253 List<Name> paramNames = savedParameterNames;
1254 savedParameterNames = null;
1255 // discard the provided names if the list of names is the wrong size.
1256 if (paramNames == null || paramNames.size() != type.getParameterTypes().size())
1257 paramNames = List.nil();
1258 ListBuffer<VarSymbol> buf = new ListBuffer<VarSymbol>();
1259 List<Name> remaining = paramNames;
1260 // assert: remaining and paramNames are both empty or both
1261 // have same cardinality as type.getParameterTypes()
1262 int i = 0;
1263 for (Type t : type.getParameterTypes()) {
1264 Name paramName;
1265 if (remaining.isEmpty()) {
1266 // no names for any parameters available
1267 paramName = createArgName(i, paramNames);
1268 } else {
1269 paramName = remaining.head;
1270 remaining = remaining.tail;
1271 if (paramName.isEmpty()) {
1272 // no name for this specific parameter
1273 paramName = createArgName(i, paramNames);
1274 }
1275 }
1276 buf.append(new VarSymbol(PARAMETER, paramName, t, this));
1277 i++;
1278 }
1279 params = buf.toList();
1280 }
1281 return params;
1282 }
1284 // Create a name for the argument at position 'index' that is not in
1285 // the exclude list. In normal use, either no names will have been
1286 // provided, in which case the exclude list is empty, or all the names
1287 // will have been provided, in which case this method will not be called.
1288 private Name createArgName(int index, List<Name> exclude) {
1289 String prefix = "arg";
1290 while (true) {
1291 Name argName = name.table.fromString(prefix + index);
1292 if (!exclude.contains(argName))
1293 return argName;
1294 prefix += "$";
1295 }
1296 }
1298 public Symbol asMemberOf(Type site, Types types) {
1299 return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
1300 }
1302 public ElementKind getKind() {
1303 if (name == name.table.names.init)
1304 return ElementKind.CONSTRUCTOR;
1305 else if (name == name.table.names.clinit)
1306 return ElementKind.STATIC_INIT;
1307 else
1308 return ElementKind.METHOD;
1309 }
1311 public Attribute getDefaultValue() {
1312 return defaultValue;
1313 }
1315 public List<VarSymbol> getParameters() {
1316 return params();
1317 }
1319 public boolean isVarArgs() {
1320 return (flags() & VARARGS) != 0;
1321 }
1323 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1324 return v.visitExecutable(this, p);
1325 }
1327 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1328 return v.visitMethodSymbol(this, p);
1329 }
1331 public Type getReturnType() {
1332 return asType().getReturnType();
1333 }
1335 public List<Type> getThrownTypes() {
1336 return asType().getThrownTypes();
1337 }
1338 }
1340 /** A class for predefined operators.
1341 */
1342 public static class OperatorSymbol extends MethodSymbol {
1344 public int opcode;
1346 public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
1347 super(PUBLIC | STATIC, name, type, owner);
1348 this.opcode = opcode;
1349 }
1351 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1352 return v.visitOperatorSymbol(this, p);
1353 }
1354 }
1356 /** Symbol completer interface.
1357 */
1358 public static interface Completer {
1359 void complete(Symbol sym) throws CompletionFailure;
1360 }
1362 public static class CompletionFailure extends RuntimeException {
1363 private static final long serialVersionUID = 0;
1364 public Symbol sym;
1366 /** A diagnostic object describing the failure
1367 */
1368 public JCDiagnostic diag;
1370 /** A localized string describing the failure.
1371 * @deprecated Use {@code getDetail()} or {@code getMessage()}
1372 */
1373 @Deprecated
1374 public String errmsg;
1376 public CompletionFailure(Symbol sym, String errmsg) {
1377 this.sym = sym;
1378 this.errmsg = errmsg;
1379 // this.printStackTrace();//DEBUG
1380 }
1382 public CompletionFailure(Symbol sym, JCDiagnostic diag) {
1383 this.sym = sym;
1384 this.diag = diag;
1385 // this.printStackTrace();//DEBUG
1386 }
1388 public JCDiagnostic getDiagnostic() {
1389 return diag;
1390 }
1392 @Override
1393 public String getMessage() {
1394 if (diag != null)
1395 return diag.getMessage(null);
1396 else
1397 return errmsg;
1398 }
1400 public Object getDetailValue() {
1401 return (diag != null ? diag : errmsg);
1402 }
1404 @Override
1405 public CompletionFailure initCause(Throwable cause) {
1406 super.initCause(cause);
1407 return this;
1408 }
1410 }
1412 /**
1413 * A visitor for symbols. A visitor is used to implement operations
1414 * (or relations) on symbols. Most common operations on types are
1415 * binary relations and this interface is designed for binary
1416 * relations, that is, operations on the form
1417 * Symbol × P → R.
1418 * <!-- In plain text: Type x P -> R -->
1419 *
1420 * @param <R> the return type of the operation implemented by this
1421 * visitor; use Void if no return type is needed.
1422 * @param <P> the type of the second argument (the first being the
1423 * symbol itself) of the operation implemented by this visitor; use
1424 * Void if a second argument is not needed.
1425 */
1426 public interface Visitor<R,P> {
1427 R visitClassSymbol(ClassSymbol s, P arg);
1428 R visitMethodSymbol(MethodSymbol s, P arg);
1429 R visitPackageSymbol(PackageSymbol s, P arg);
1430 R visitOperatorSymbol(OperatorSymbol s, P arg);
1431 R visitVarSymbol(VarSymbol s, P arg);
1432 R visitTypeSymbol(TypeSymbol s, P arg);
1433 R visitSymbol(Symbol s, P arg);
1434 }
1435 }