Fri, 08 Aug 2008 17:43:24 +0100
6676362: Spurious forward reference error with final var + instance variable initializer
Summary: Some javac forward reference errors aren't compliant with the JLS
Reviewed-by: jjg
1 /*
2 * Copyright 1999-2008 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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 API supported by Sun Microsystems. If
53 * 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 assert attributes_field != null;
85 return attributes_field;
86 }
88 /** Fetch a particular annotation from a symbol. */
89 public Attribute.Compound attribute(Symbol anno) {
90 for (Attribute.Compound a : getAnnotationMirrors())
91 if (a.type.tsym == anno) return a;
92 return null;
93 }
95 /** The name of this symbol in Utf8 representation.
96 */
97 public Name name;
99 /** The type of this symbol.
100 */
101 public Type type;
103 /** The owner of this symbol.
104 */
105 public Symbol owner;
107 /** The completer of this symbol.
108 */
109 public Completer completer;
111 /** A cache for the type erasure of this symbol.
112 */
113 public Type erasure_field;
115 /** Construct a symbol with given kind, flags, name, type and owner.
116 */
117 public Symbol(int kind, long flags, Name name, Type type, Symbol owner) {
118 this.kind = kind;
119 this.flags_field = flags;
120 this.type = type;
121 this.owner = owner;
122 this.completer = null;
123 this.erasure_field = null;
124 this.attributes_field = List.nil();
125 this.name = name;
126 }
128 /** Clone this symbol with new owner.
129 * Legal only for fields and methods.
130 */
131 public Symbol clone(Symbol newOwner) {
132 throw new AssertionError();
133 }
135 /** The Java source which this symbol represents.
136 * A description of this symbol; overrides Object.
137 */
138 public String toString() {
139 return name.toString();
140 }
142 /** A Java source description of the location of this symbol; used for
143 * error reporting.
144 *
145 * @return null if the symbol is a package or a toplevel class defined in
146 * the default package; otherwise, the owner symbol is returned
147 */
148 public Symbol location() {
149 if (owner.name == null || (owner.name.len == 0 && owner.kind != PCK)) {
150 return null;
151 }
152 return owner;
153 }
155 public Symbol location(Type site, Types types) {
156 if (owner.name == null || owner.name.len == 0) {
157 return location();
158 }
159 if (owner.type.tag == CLASS) {
160 Type ownertype = types.asOuterSuper(site, owner);
161 if (ownertype != null) return ownertype.tsym;
162 }
163 return owner;
164 }
166 /** The symbol's erased type.
167 */
168 public Type erasure(Types types) {
169 if (erasure_field == null)
170 erasure_field = types.erasure(type);
171 return erasure_field;
172 }
174 /** The external type of a symbol. This is the symbol's erased type
175 * except for constructors of inner classes which get the enclosing
176 * instance class added as first argument.
177 */
178 public Type externalType(Types types) {
179 Type t = erasure(types);
180 if (name == name.table.init && owner.hasOuterInstance()) {
181 Type outerThisType = types.erasure(owner.type.getEnclosingType());
182 return new MethodType(t.getParameterTypes().prepend(outerThisType),
183 t.getReturnType(),
184 t.getThrownTypes(),
185 t.tsym);
186 } else {
187 return t;
188 }
189 }
191 public boolean isStatic() {
192 return
193 (flags() & STATIC) != 0 ||
194 (owner.flags() & INTERFACE) != 0 && kind != MTH;
195 }
197 public boolean isInterface() {
198 return (flags() & INTERFACE) != 0;
199 }
201 /** Is this symbol declared (directly or indirectly) local
202 * to a method or variable initializer?
203 * Also includes fields of inner classes which are in
204 * turn local to a method or variable initializer.
205 */
206 public boolean isLocal() {
207 return
208 (owner.kind & (VAR | MTH)) != 0 ||
209 (owner.kind == TYP && owner.isLocal());
210 }
212 /** Is this symbol a constructor?
213 */
214 public boolean isConstructor() {
215 return name == name.table.init;
216 }
218 /** The fully qualified name of this symbol.
219 * This is the same as the symbol's name except for class symbols,
220 * which are handled separately.
221 */
222 public Name getQualifiedName() {
223 return name;
224 }
226 /** The fully qualified name of this symbol after converting to flat
227 * representation. This is the same as the symbol's name except for
228 * class symbols, which are handled separately.
229 */
230 public Name flatName() {
231 return getQualifiedName();
232 }
234 /** If this is a class or package, its members, otherwise null.
235 */
236 public Scope members() {
237 return null;
238 }
240 /** A class is an inner class if it it has an enclosing instance class.
241 */
242 public boolean isInner() {
243 return type.getEnclosingType().tag == CLASS;
244 }
246 /** An inner class has an outer instance if it is not an interface
247 * it has an enclosing instance class which might be referenced from the class.
248 * Nested classes can see instance members of their enclosing class.
249 * Their constructors carry an additional this$n parameter, inserted
250 * implicitly by the compiler.
251 *
252 * @see #isInner
253 */
254 public boolean hasOuterInstance() {
255 return
256 type.getEnclosingType().tag == CLASS && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
257 }
259 /** The closest enclosing class of this symbol's declaration.
260 */
261 public ClassSymbol enclClass() {
262 Symbol c = this;
263 while (c != null &&
264 ((c.kind & TYP) == 0 || c.type.tag != CLASS)) {
265 c = c.owner;
266 }
267 return (ClassSymbol)c;
268 }
270 /** The outermost class which indirectly owns this symbol.
271 */
272 public ClassSymbol outermostClass() {
273 Symbol sym = this;
274 Symbol prev = null;
275 while (sym.kind != PCK) {
276 prev = sym;
277 sym = sym.owner;
278 }
279 return (ClassSymbol) prev;
280 }
282 /** The package which indirectly owns this symbol.
283 */
284 public PackageSymbol packge() {
285 Symbol sym = this;
286 while (sym.kind != PCK) {
287 sym = sym.owner;
288 }
289 return (PackageSymbol) sym;
290 }
292 /** Is this symbol a subclass of `base'? Only defined for ClassSymbols.
293 */
294 public boolean isSubClass(Symbol base, Types types) {
295 throw new AssertionError("isSubClass " + this);
296 }
298 /** Fully check membership: hierarchy, protection, and hiding.
299 * Does not exclude methods not inherited due to overriding.
300 */
301 public boolean isMemberOf(TypeSymbol clazz, Types types) {
302 return
303 owner == clazz ||
304 clazz.isSubClass(owner, types) &&
305 isInheritedIn(clazz, types) &&
306 !hiddenIn((ClassSymbol)clazz, types);
307 }
309 /** Is this symbol the same as or enclosed by the given class? */
310 public boolean isEnclosedBy(ClassSymbol clazz) {
311 for (Symbol sym = this; sym.kind != PCK; sym = sym.owner)
312 if (sym == clazz) return true;
313 return false;
314 }
316 /** Check for hiding. Note that this doesn't handle multiple
317 * (interface) inheritance. */
318 private boolean hiddenIn(ClassSymbol clazz, Types types) {
319 if (kind == MTH && (flags() & STATIC) == 0) return false;
320 while (true) {
321 if (owner == clazz) return false;
322 Scope.Entry e = clazz.members().lookup(name);
323 while (e.scope != null) {
324 if (e.sym == this) return false;
325 if (e.sym.kind == kind &&
326 (kind != MTH ||
327 (e.sym.flags() & STATIC) != 0 &&
328 types.isSubSignature(e.sym.type, type)))
329 return true;
330 e = e.next();
331 }
332 Type superType = types.supertype(clazz.type);
333 if (superType.tag != TypeTags.CLASS) return false;
334 clazz = (ClassSymbol)superType.tsym;
335 }
336 }
338 /** Is this symbol inherited into a given class?
339 * PRE: If symbol's owner is a interface,
340 * it is already assumed that the interface is a superinterface
341 * of given class.
342 * @param clazz The class for which we want to establish membership.
343 * This must be a subclass of the member's owner.
344 */
345 public boolean isInheritedIn(Symbol clazz, Types types) {
346 switch ((int)(flags_field & Flags.AccessFlags)) {
347 default: // error recovery
348 case PUBLIC:
349 return true;
350 case PRIVATE:
351 return this.owner == clazz;
352 case PROTECTED:
353 // we model interfaces as extending Object
354 return (clazz.flags() & INTERFACE) == 0;
355 case 0:
356 PackageSymbol thisPackage = this.packge();
357 for (Symbol sup = clazz;
358 sup != null && sup != this.owner;
359 sup = types.supertype(sup.type).tsym) {
360 if (sup.type.isErroneous())
361 return true; // error recovery
362 if ((sup.flags() & COMPOUND) != 0)
363 continue;
364 if (sup.packge() != thisPackage)
365 return false;
366 }
367 return (clazz.flags() & INTERFACE) == 0;
368 }
369 }
371 /** The (variable or method) symbol seen as a member of given
372 * class type`site' (this might change the symbol's type).
373 * This is used exclusively for producing diagnostics.
374 */
375 public Symbol asMemberOf(Type site, Types types) {
376 throw new AssertionError();
377 }
379 /** Does this method symbol override `other' symbol, when both are seen as
380 * members of class `origin'? It is assumed that _other is a member
381 * of origin.
382 *
383 * It is assumed that both symbols have the same name. The static
384 * modifier is ignored for this test.
385 *
386 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
387 */
388 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
389 return false;
390 }
392 /** Complete the elaboration of this symbol's definition.
393 */
394 public void complete() throws CompletionFailure {
395 if (completer != null) {
396 Completer c = completer;
397 completer = null;
398 c.complete(this);
399 }
400 }
402 /** True if the symbol represents an entity that exists.
403 */
404 public boolean exists() {
405 return true;
406 }
408 public Type asType() {
409 return type;
410 }
412 public Symbol getEnclosingElement() {
413 return owner;
414 }
416 public ElementKind getKind() {
417 return ElementKind.OTHER; // most unkind
418 }
420 public Set<Modifier> getModifiers() {
421 return Flags.asModifierSet(flags());
422 }
424 public Name getSimpleName() {
425 return name;
426 }
428 /**
429 * @deprecated this method should never be used by javac internally.
430 */
431 @Deprecated
432 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
433 return JavacElements.getAnnotation(this, annoType);
434 }
436 // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList
437 public java.util.List<Symbol> getEnclosedElements() {
438 return List.nil();
439 }
441 public List<TypeSymbol> getTypeParameters() {
442 ListBuffer<TypeSymbol> l = ListBuffer.lb();
443 for (Type t : type.getTypeArguments()) {
444 l.append(t.tsym);
445 }
446 return l.toList();
447 }
449 public static class DelegatedSymbol extends Symbol {
450 protected Symbol other;
451 public DelegatedSymbol(Symbol other) {
452 super(other.kind, other.flags_field, other.name, other.type, other.owner);
453 this.other = other;
454 }
455 public String toString() { return other.toString(); }
456 public Symbol location() { return other.location(); }
457 public Symbol location(Type site, Types types) { return other.location(site, types); }
458 public Type erasure(Types types) { return other.erasure(types); }
459 public Type externalType(Types types) { return other.externalType(types); }
460 public boolean isLocal() { return other.isLocal(); }
461 public boolean isConstructor() { return other.isConstructor(); }
462 public Name getQualifiedName() { return other.getQualifiedName(); }
463 public Name flatName() { return other.flatName(); }
464 public Scope members() { return other.members(); }
465 public boolean isInner() { return other.isInner(); }
466 public boolean hasOuterInstance() { return other.hasOuterInstance(); }
467 public ClassSymbol enclClass() { return other.enclClass(); }
468 public ClassSymbol outermostClass() { return other.outermostClass(); }
469 public PackageSymbol packge() { return other.packge(); }
470 public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); }
471 public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); }
472 public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); }
473 public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); }
474 public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); }
475 public void complete() throws CompletionFailure { other.complete(); }
477 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
478 return other.accept(v, p);
479 }
480 }
482 /** A class for type symbols. Type variables are represented by instances
483 * of this class, classes and packages by instances of subclasses.
484 */
485 public static class TypeSymbol
486 extends Symbol implements TypeParameterElement {
487 // Implements TypeParameterElement because type parameters don't
488 // have their own TypeSymbol subclass.
489 // TODO: type parameters should have their own TypeSymbol subclass
491 public TypeSymbol(long flags, Name name, Type type, Symbol owner) {
492 super(TYP, flags, name, type, owner);
493 }
495 /** form a fully qualified name from a name and an owner
496 */
497 static public Name formFullName(Name name, Symbol owner) {
498 if (owner == null) return name;
499 if (((owner.kind != ERR)) &&
500 ((owner.kind & (VAR | MTH)) != 0
501 || (owner.kind == TYP && owner.type.tag == TYPEVAR)
502 )) return name;
503 Name prefix = owner.getQualifiedName();
504 if (prefix == null || prefix == prefix.table.empty)
505 return name;
506 else return prefix.append('.', name);
507 }
509 /** form a fully qualified name from a name and an owner, after
510 * converting to flat representation
511 */
512 static public Name formFlatName(Name name, Symbol owner) {
513 if (owner == null ||
514 (owner.kind & (VAR | MTH)) != 0
515 || (owner.kind == TYP && owner.type.tag == TYPEVAR)
516 ) return name;
517 char sep = owner.kind == TYP ? '$' : '.';
518 Name prefix = owner.flatName();
519 if (prefix == null || prefix == prefix.table.empty)
520 return name;
521 else return prefix.append(sep, name);
522 }
524 /**
525 * A total ordering between type symbols that refines the
526 * class inheritance graph.
527 *
528 * Typevariables always precede other kinds of symbols.
529 */
530 public final boolean precedes(TypeSymbol that, Types types) {
531 if (this == that)
532 return false;
533 if (this.type.tag == that.type.tag) {
534 if (this.type.tag == CLASS) {
535 return
536 types.rank(that.type) < types.rank(this.type) ||
537 types.rank(that.type) == types.rank(this.type) &&
538 that.getQualifiedName().compareTo(this.getQualifiedName()) < 0;
539 } else if (this.type.tag == TYPEVAR) {
540 return types.isSubtype(this.type, that.type);
541 }
542 }
543 return this.type.tag == TYPEVAR;
544 }
546 // For type params; overridden in subclasses.
547 public ElementKind getKind() {
548 return ElementKind.TYPE_PARAMETER;
549 }
551 public java.util.List<Symbol> getEnclosedElements() {
552 List<Symbol> list = List.nil();
553 for (Scope.Entry e = members().elems; e != null; e = e.sibling) {
554 if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this)
555 list = list.prepend(e.sym);
556 }
557 return list;
558 }
560 // For type params.
561 // Perhaps not needed if getEnclosingElement can be spec'ed
562 // to do the same thing.
563 // TODO: getGenericElement() might not be needed
564 public Symbol getGenericElement() {
565 return owner;
566 }
568 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
569 assert type.tag == TYPEVAR; // else override will be invoked
570 return v.visitTypeParameter(this, p);
571 }
573 public List<Type> getBounds() {
574 TypeVar t = (TypeVar)type;
575 Type bound = t.getUpperBound();
576 if (!bound.isCompound())
577 return List.of(bound);
578 ClassType ct = (ClassType)bound;
579 if (!ct.tsym.erasure_field.isInterface()) {
580 return ct.interfaces_field.prepend(ct.supertype_field);
581 } else {
582 // No superclass was given in bounds.
583 // In this case, supertype is Object, erasure is first interface.
584 return ct.interfaces_field;
585 }
586 }
587 }
589 /** A class for package symbols
590 */
591 public static class PackageSymbol extends TypeSymbol
592 implements PackageElement {
594 public Scope members_field;
595 public Name fullname;
596 public ClassSymbol package_info; // see bug 6443073
598 public PackageSymbol(Name name, Type type, Symbol owner) {
599 super(0, name, type, owner);
600 this.kind = PCK;
601 this.members_field = null;
602 this.fullname = formFullName(name, owner);
603 }
605 public PackageSymbol(Name name, Symbol owner) {
606 this(name, null, owner);
607 this.type = new PackageType(this);
608 }
610 public String toString() {
611 return fullname.toString();
612 }
614 public Name getQualifiedName() {
615 return fullname;
616 }
618 public boolean isUnnamed() {
619 return name.isEmpty() && owner != null;
620 }
622 public Scope members() {
623 if (completer != null) complete();
624 return members_field;
625 }
627 public long flags() {
628 if (completer != null) complete();
629 return flags_field;
630 }
632 public List<Attribute.Compound> getAnnotationMirrors() {
633 if (completer != null) complete();
634 assert attributes_field != null;
635 return attributes_field;
636 }
638 /** A package "exists" if a type or package that exists has
639 * been seen within it.
640 */
641 public boolean exists() {
642 return (flags_field & EXISTS) != 0;
643 }
645 public ElementKind getKind() {
646 return ElementKind.PACKAGE;
647 }
649 public Symbol getEnclosingElement() {
650 return null;
651 }
653 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
654 return v.visitPackage(this, p);
655 }
656 }
658 /** A class for class symbols
659 */
660 public static class ClassSymbol extends TypeSymbol implements TypeElement {
662 /** a scope for all class members; variables, methods and inner classes
663 * type parameters are not part of this scope
664 */
665 public Scope members_field;
667 /** the fully qualified name of the class, i.e. pck.outer.inner.
668 * null for anonymous classes
669 */
670 public Name fullname;
672 /** the fully qualified name of the class after converting to flat
673 * representation, i.e. pck.outer$inner,
674 * set externally for local and anonymous classes
675 */
676 public Name flatname;
678 /** the sourcefile where the class came from
679 */
680 public JavaFileObject sourcefile;
682 /** the classfile from where to load this class
683 * this will have extension .class or .java
684 */
685 public JavaFileObject classfile;
687 /** the constant pool of the class
688 */
689 public Pool pool;
691 public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
692 super(flags, name, type, owner);
693 this.members_field = null;
694 this.fullname = formFullName(name, owner);
695 this.flatname = formFlatName(name, owner);
696 this.sourcefile = null;
697 this.classfile = null;
698 this.pool = null;
699 }
701 public ClassSymbol(long flags, Name name, Symbol owner) {
702 this(
703 flags,
704 name,
705 new ClassType(Type.noType, null, null),
706 owner);
707 this.type.tsym = this;
708 }
710 /** The Java source which this symbol represents.
711 */
712 public String toString() {
713 return className();
714 }
716 public long flags() {
717 if (completer != null) complete();
718 return flags_field;
719 }
721 public Scope members() {
722 if (completer != null) complete();
723 return members_field;
724 }
726 public List<Attribute.Compound> getAnnotationMirrors() {
727 if (completer != null) complete();
728 assert attributes_field != null;
729 return attributes_field;
730 }
732 public Type erasure(Types types) {
733 if (erasure_field == null)
734 erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
735 List.<Type>nil(), this);
736 return erasure_field;
737 }
739 public String className() {
740 if (name.len == 0)
741 return
742 Log.getLocalizedString("anonymous.class", flatname);
743 else
744 return fullname.toString();
745 }
747 public Name getQualifiedName() {
748 return fullname;
749 }
751 public Name flatName() {
752 return flatname;
753 }
755 public boolean isSubClass(Symbol base, Types types) {
756 if (this == base) {
757 return true;
758 } else if ((base.flags() & INTERFACE) != 0) {
759 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
760 for (List<Type> is = types.interfaces(t);
761 is.nonEmpty();
762 is = is.tail)
763 if (is.head.tsym.isSubClass(base, types)) return true;
764 } else {
765 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
766 if (t.tsym == base) return true;
767 }
768 return false;
769 }
771 /** Complete the elaboration of this symbol's definition.
772 */
773 public void complete() throws CompletionFailure {
774 try {
775 super.complete();
776 } catch (CompletionFailure ex) {
777 // quiet error recovery
778 flags_field |= (PUBLIC|STATIC);
779 this.type = new ErrorType(this);
780 throw ex;
781 }
782 }
784 public List<Type> getInterfaces() {
785 complete();
786 if (type instanceof ClassType) {
787 ClassType t = (ClassType)type;
788 if (t.interfaces_field == null) // FIXME: shouldn't be null
789 t.interfaces_field = List.nil();
790 return t.interfaces_field;
791 } else {
792 return List.nil();
793 }
794 }
796 public Type getSuperclass() {
797 complete();
798 if (type instanceof ClassType) {
799 ClassType t = (ClassType)type;
800 if (t.supertype_field == null) // FIXME: shouldn't be null
801 t.supertype_field = Type.noType;
802 // An interface has no superclass; its supertype is Object.
803 return t.isInterface()
804 ? Type.noType
805 : t.supertype_field;
806 } else {
807 return Type.noType;
808 }
809 }
811 public ElementKind getKind() {
812 long flags = flags();
813 if ((flags & ANNOTATION) != 0)
814 return ElementKind.ANNOTATION_TYPE;
815 else if ((flags & INTERFACE) != 0)
816 return ElementKind.INTERFACE;
817 else if ((flags & ENUM) != 0)
818 return ElementKind.ENUM;
819 else
820 return ElementKind.CLASS;
821 }
823 public NestingKind getNestingKind() {
824 complete();
825 if (owner.kind == PCK)
826 return NestingKind.TOP_LEVEL;
827 else if (name.isEmpty())
828 return NestingKind.ANONYMOUS;
829 else if (owner.kind == MTH)
830 return NestingKind.LOCAL;
831 else
832 return NestingKind.MEMBER;
833 }
835 /**
836 * @deprecated this method should never be used by javac internally.
837 */
838 @Override @Deprecated
839 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
840 return JavacElements.getAnnotation(this, annoType);
841 }
843 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
844 return v.visitType(this, p);
845 }
846 }
849 /** A class for variable symbols
850 */
851 public static class VarSymbol extends Symbol implements VariableElement {
853 /** The variable's declaration position.
854 */
855 public int pos = Position.NOPOS;
857 /** The variable's address. Used for different purposes during
858 * flow analysis, translation and code generation.
859 * Flow analysis:
860 * If this is a blank final or local variable, its sequence number.
861 * Translation:
862 * If this is a private field, its access number.
863 * Code generation:
864 * If this is a local variable, its logical slot number.
865 */
866 public int adr = -1;
868 /** Construct a variable symbol, given its flags, name, type and owner.
869 */
870 public VarSymbol(long flags, Name name, Type type, Symbol owner) {
871 super(VAR, flags, name, type, owner);
872 }
874 /** Clone this symbol with new owner.
875 */
876 public VarSymbol clone(Symbol newOwner) {
877 VarSymbol v = new VarSymbol(flags_field, name, type, newOwner);
878 v.pos = pos;
879 v.adr = adr;
880 v.data = data;
881 // System.out.println("clone " + v + " in " + newOwner);//DEBUG
882 return v;
883 }
885 public String toString() {
886 return name.toString();
887 }
889 public Symbol asMemberOf(Type site, Types types) {
890 return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
891 }
893 public ElementKind getKind() {
894 long flags = flags();
895 if ((flags & PARAMETER) != 0) {
896 if (isExceptionParameter())
897 return ElementKind.EXCEPTION_PARAMETER;
898 else
899 return ElementKind.PARAMETER;
900 } else if ((flags & ENUM) != 0) {
901 return ElementKind.ENUM_CONSTANT;
902 } else if (owner.kind == TYP || owner.kind == ERR) {
903 return ElementKind.FIELD;
904 } else {
905 return ElementKind.LOCAL_VARIABLE;
906 }
907 }
909 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
910 return v.visitVariable(this, p);
911 }
913 public Object getConstantValue() { // Mirror API
914 return Constants.decode(getConstValue(), type);
915 }
917 public void setLazyConstValue(final Env<AttrContext> env,
918 final Log log,
919 final Attr attr,
920 final JCTree.JCExpression initializer)
921 {
922 setData(new Callable<Object>() {
923 public Object call() {
924 JavaFileObject source = log.useSource(env.toplevel.sourcefile);
925 try {
926 Type itype = attr.attribExpr(initializer, env, type);
927 if (itype.constValue() != null)
928 return attr.coerce(itype, type).constValue();
929 else
930 return null;
931 } finally {
932 log.useSource(source);
933 }
934 }
935 });
936 }
938 /**
939 * The variable's constant value, if this is a constant.
940 * Before the constant value is evaluated, it points to an
941 * initalizer environment. If this is not a constant, it can
942 * be used for other stuff.
943 */
944 private Object data;
946 public boolean isExceptionParameter() {
947 return data == ElementKind.EXCEPTION_PARAMETER;
948 }
950 public Object getConstValue() {
951 // TODO: Consider if getConstValue and getConstantValue can be collapsed
952 if (data == ElementKind.EXCEPTION_PARAMETER) {
953 return null;
954 } else if (data instanceof Callable<?>) {
955 // In this case, this is final a variable, with an as
956 // yet unevaluated initializer.
957 Callable<?> eval = (Callable<?>)data;
958 data = null; // to make sure we don't evaluate this twice.
959 try {
960 data = eval.call();
961 } catch (Exception ex) {
962 throw new AssertionError(ex);
963 }
964 }
965 return data;
966 }
968 public void setData(Object data) {
969 assert !(data instanceof Env<?>) : this;
970 this.data = data;
971 }
972 }
974 /** A class for method symbols.
975 */
976 public static class MethodSymbol extends Symbol implements ExecutableElement {
978 /** The code of the method. */
979 public Code code = null;
981 /** The parameters of the method. */
982 public List<VarSymbol> params = null;
984 /** The names of the parameters */
985 public List<Name> savedParameterNames;
987 /** For an attribute field accessor, its default value if any.
988 * The value is null if none appeared in the method
989 * declaration.
990 */
991 public Attribute defaultValue = null;
993 /** Construct a method symbol, given its flags, name, type and owner.
994 */
995 public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
996 super(MTH, flags, name, type, owner);
997 assert owner.type.tag != TYPEVAR : owner + "." + name;
998 }
1000 /** Clone this symbol with new owner.
1001 */
1002 public MethodSymbol clone(Symbol newOwner) {
1003 MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner);
1004 m.code = code;
1005 return m;
1006 }
1008 /** The Java source which this symbol represents.
1009 */
1010 public String toString() {
1011 if ((flags() & BLOCK) != 0) {
1012 return owner.name.toString();
1013 } else {
1014 String s = (name == name.table.init)
1015 ? owner.name.toString()
1016 : name.toString();
1017 if (type != null) {
1018 if (type.tag == FORALL)
1019 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
1020 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
1021 }
1022 return s;
1023 }
1024 }
1026 /** find a symbol that this (proxy method) symbol implements.
1027 * @param c The class whose members are searched for
1028 * implementations
1029 */
1030 public Symbol implemented(TypeSymbol c, Types types) {
1031 Symbol impl = null;
1032 for (List<Type> is = types.interfaces(c.type);
1033 impl == null && is.nonEmpty();
1034 is = is.tail) {
1035 TypeSymbol i = is.head.tsym;
1036 for (Scope.Entry e = i.members().lookup(name);
1037 impl == null && e.scope != null;
1038 e = e.next()) {
1039 if (this.overrides(e.sym, (TypeSymbol)owner, types, true) &&
1040 // FIXME: I suspect the following requires a
1041 // subst() for a parametric return type.
1042 types.isSameType(type.getReturnType(),
1043 types.memberType(owner.type, e.sym).getReturnType())) {
1044 impl = e.sym;
1045 }
1046 if (impl == null)
1047 impl = implemented(i, types);
1048 }
1049 }
1050 return impl;
1051 }
1053 /** Will the erasure of this method be considered by the VM to
1054 * override the erasure of the other when seen from class `origin'?
1055 */
1056 public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
1057 if (isConstructor() || _other.kind != MTH) return false;
1059 if (this == _other) return true;
1060 MethodSymbol other = (MethodSymbol)_other;
1062 // check for a direct implementation
1063 if (other.isOverridableIn((TypeSymbol)owner) &&
1064 types.asSuper(owner.type, other.owner) != null &&
1065 types.isSameType(erasure(types), other.erasure(types)))
1066 return true;
1068 // check for an inherited implementation
1069 return
1070 (flags() & ABSTRACT) == 0 &&
1071 other.isOverridableIn(origin) &&
1072 this.isMemberOf(origin, types) &&
1073 types.isSameType(erasure(types), other.erasure(types));
1074 }
1076 /** The implementation of this (abstract) symbol in class origin,
1077 * from the VM's point of view, null if method does not have an
1078 * implementation in class.
1079 * @param origin The class of which the implementation is a member.
1080 */
1081 public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
1082 for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
1083 for (Scope.Entry e = c.members().lookup(name);
1084 e.scope != null;
1085 e = e.next()) {
1086 if (e.sym.kind == MTH &&
1087 ((MethodSymbol)e.sym).binaryOverrides(this, origin, types))
1088 return (MethodSymbol)e.sym;
1089 }
1090 }
1091 return null;
1092 }
1094 /** Does this symbol override `other' symbol, when both are seen as
1095 * members of class `origin'? It is assumed that _other is a member
1096 * of origin.
1097 *
1098 * It is assumed that both symbols have the same name. The static
1099 * modifier is ignored for this test.
1100 *
1101 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
1102 */
1103 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
1104 if (isConstructor() || _other.kind != MTH) return false;
1106 if (this == _other) return true;
1107 MethodSymbol other = (MethodSymbol)_other;
1109 // check for a direct implementation
1110 if (other.isOverridableIn((TypeSymbol)owner) &&
1111 types.asSuper(owner.type, other.owner) != null) {
1112 Type mt = types.memberType(owner.type, this);
1113 Type ot = types.memberType(owner.type, other);
1114 if (types.isSubSignature(mt, ot)) {
1115 if (!checkResult)
1116 return true;
1117 if (types.returnTypeSubstitutable(mt, ot))
1118 return true;
1119 }
1120 }
1122 // check for an inherited implementation
1123 if ((flags() & ABSTRACT) != 0 ||
1124 (other.flags() & ABSTRACT) == 0 ||
1125 !other.isOverridableIn(origin) ||
1126 !this.isMemberOf(origin, types))
1127 return false;
1129 // assert types.asSuper(origin.type, other.owner) != null;
1130 Type mt = types.memberType(origin.type, this);
1131 Type ot = types.memberType(origin.type, other);
1132 return
1133 types.isSubSignature(mt, ot) &&
1134 (!checkResult || types.resultSubtype(mt, ot, Warner.noWarnings));
1135 }
1137 private boolean isOverridableIn(TypeSymbol origin) {
1138 // JLS3 8.4.6.1
1139 switch ((int)(flags_field & Flags.AccessFlags)) {
1140 case Flags.PRIVATE:
1141 return false;
1142 case Flags.PUBLIC:
1143 return true;
1144 case Flags.PROTECTED:
1145 return (origin.flags() & INTERFACE) == 0;
1146 case 0:
1147 // for package private: can only override in the same
1148 // package
1149 return
1150 this.packge() == origin.packge() &&
1151 (origin.flags() & INTERFACE) == 0;
1152 default:
1153 return false;
1154 }
1155 }
1157 /** The implementation of this (abstract) symbol in class origin;
1158 * null if none exists. Synthetic methods are not considered
1159 * as possible implementations.
1160 */
1161 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
1162 for (Type t = origin.type; t.tag == CLASS; t = types.supertype(t)) {
1163 TypeSymbol c = t.tsym;
1164 for (Scope.Entry e = c.members().lookup(name);
1165 e.scope != null;
1166 e = e.next()) {
1167 if (e.sym.kind == MTH) {
1168 MethodSymbol m = (MethodSymbol) e.sym;
1169 if (m.overrides(this, origin, types, checkResult) &&
1170 (m.flags() & SYNTHETIC) == 0)
1171 return m;
1172 }
1173 }
1174 }
1175 // if origin is derived from a raw type, we might have missed
1176 // an implementation because we do not know enough about instantiations.
1177 // in this case continue with the supertype as origin.
1178 if (types.isDerivedRaw(origin.type))
1179 return implementation(types.supertype(origin.type).tsym, types, checkResult);
1180 else
1181 return null;
1182 }
1184 public List<VarSymbol> params() {
1185 owner.complete();
1186 if (params == null) {
1187 List<Name> names = savedParameterNames;
1188 savedParameterNames = null;
1189 if (names == null) {
1190 names = List.nil();
1191 int i = 0;
1192 for (Type t : type.getParameterTypes())
1193 names = names.prepend(name.table.fromString("arg" + i++));
1194 names = names.reverse();
1195 }
1196 ListBuffer<VarSymbol> buf = new ListBuffer<VarSymbol>();
1197 for (Type t : type.getParameterTypes()) {
1198 buf.append(new VarSymbol(PARAMETER, names.head, t, this));
1199 names = names.tail;
1200 }
1201 params = buf.toList();
1202 }
1203 return params;
1204 }
1206 public Symbol asMemberOf(Type site, Types types) {
1207 return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
1208 }
1210 public ElementKind getKind() {
1211 if (name == name.table.init)
1212 return ElementKind.CONSTRUCTOR;
1213 else if (name == name.table.clinit)
1214 return ElementKind.STATIC_INIT;
1215 else
1216 return ElementKind.METHOD;
1217 }
1219 public Attribute getDefaultValue() {
1220 return defaultValue;
1221 }
1223 public List<VarSymbol> getParameters() {
1224 return params();
1225 }
1227 public boolean isVarArgs() {
1228 return (flags() & VARARGS) != 0;
1229 }
1231 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1232 return v.visitExecutable(this, p);
1233 }
1235 public Type getReturnType() {
1236 return asType().getReturnType();
1237 }
1239 public List<Type> getThrownTypes() {
1240 return asType().getThrownTypes();
1241 }
1242 }
1244 /** A class for predefined operators.
1245 */
1246 public static class OperatorSymbol extends MethodSymbol {
1248 public int opcode;
1250 public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
1251 super(PUBLIC | STATIC, name, type, owner);
1252 this.opcode = opcode;
1253 }
1254 }
1256 /** Symbol completer interface.
1257 */
1258 public static interface Completer {
1259 void complete(Symbol sym) throws CompletionFailure;
1260 }
1262 public static class CompletionFailure extends RuntimeException {
1263 private static final long serialVersionUID = 0;
1264 public Symbol sym;
1266 /** A diagnostic object describing the failure
1267 */
1268 public JCDiagnostic diag;
1270 /** A localized string describing the failure.
1271 * @deprecated Use {@code getDetail()} or {@code getMessage()}
1272 */
1273 @Deprecated
1274 public String errmsg;
1276 public CompletionFailure(Symbol sym, String errmsg) {
1277 this.sym = sym;
1278 this.errmsg = errmsg;
1279 // this.printStackTrace();//DEBUG
1280 }
1282 public CompletionFailure(Symbol sym, JCDiagnostic diag) {
1283 this.sym = sym;
1284 this.diag = diag;
1285 // this.printStackTrace();//DEBUG
1286 }
1288 public JCDiagnostic getDiagnostic() {
1289 return diag;
1290 }
1292 @Override
1293 public String getMessage() {
1294 if (diag != null)
1295 return diag.getMessage(null);
1296 else
1297 return errmsg;
1298 }
1300 public Object getDetailValue() {
1301 return (diag != null ? diag : errmsg);
1302 }
1304 @Override
1305 public CompletionFailure initCause(Throwable cause) {
1306 super.initCause(cause);
1307 return this;
1308 }
1310 }
1311 }