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