Wed, 21 Sep 2011 21:56:53 -0700
7092965: javac should not close processorClassLoader before end of compilation
Reviewed-by: darcy
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 list of translated local classes (used for generating
730 * InnerClasses attribute)
731 */
732 public List<ClassSymbol> trans_local;
734 /** the constant pool of the class
735 */
736 public Pool pool;
738 public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
739 super(flags, name, type, owner);
740 this.members_field = null;
741 this.fullname = formFullName(name, owner);
742 this.flatname = formFlatName(name, owner);
743 this.sourcefile = null;
744 this.classfile = null;
745 this.pool = null;
746 }
748 public ClassSymbol(long flags, Name name, Symbol owner) {
749 this(
750 flags,
751 name,
752 new ClassType(Type.noType, null, null),
753 owner);
754 this.type.tsym = this;
755 }
757 /** The Java source which this symbol represents.
758 */
759 public String toString() {
760 return className();
761 }
763 public long flags() {
764 if (completer != null) complete();
765 return flags_field;
766 }
768 public Scope members() {
769 if (completer != null) complete();
770 return members_field;
771 }
773 public List<Attribute.Compound> getAnnotationMirrors() {
774 if (completer != null) complete();
775 return Assert.checkNonNull(attributes_field);
776 }
778 public Type erasure(Types types) {
779 if (erasure_field == null)
780 erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
781 List.<Type>nil(), this);
782 return erasure_field;
783 }
785 public String className() {
786 if (name.isEmpty())
787 return
788 Log.getLocalizedString("anonymous.class", flatname);
789 else
790 return fullname.toString();
791 }
793 public Name getQualifiedName() {
794 return fullname;
795 }
797 public Name flatName() {
798 return flatname;
799 }
801 public boolean isSubClass(Symbol base, Types types) {
802 if (this == base) {
803 return true;
804 } else if ((base.flags() & INTERFACE) != 0) {
805 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
806 for (List<Type> is = types.interfaces(t);
807 is.nonEmpty();
808 is = is.tail)
809 if (is.head.tsym.isSubClass(base, types)) return true;
810 } else {
811 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
812 if (t.tsym == base) return true;
813 }
814 return false;
815 }
817 /** Complete the elaboration of this symbol's definition.
818 */
819 public void complete() throws CompletionFailure {
820 try {
821 super.complete();
822 } catch (CompletionFailure ex) {
823 // quiet error recovery
824 flags_field |= (PUBLIC|STATIC);
825 this.type = new ErrorType(this, Type.noType);
826 throw ex;
827 }
828 }
830 public List<Type> getInterfaces() {
831 complete();
832 if (type instanceof ClassType) {
833 ClassType t = (ClassType)type;
834 if (t.interfaces_field == null) // FIXME: shouldn't be null
835 t.interfaces_field = List.nil();
836 if (t.all_interfaces_field != null)
837 return Type.getModelTypes(t.all_interfaces_field);
838 return t.interfaces_field;
839 } else {
840 return List.nil();
841 }
842 }
844 public Type getSuperclass() {
845 complete();
846 if (type instanceof ClassType) {
847 ClassType t = (ClassType)type;
848 if (t.supertype_field == null) // FIXME: shouldn't be null
849 t.supertype_field = Type.noType;
850 // An interface has no superclass; its supertype is Object.
851 return t.isInterface()
852 ? Type.noType
853 : t.supertype_field.getModelType();
854 } else {
855 return Type.noType;
856 }
857 }
859 public ElementKind getKind() {
860 long flags = flags();
861 if ((flags & ANNOTATION) != 0)
862 return ElementKind.ANNOTATION_TYPE;
863 else if ((flags & INTERFACE) != 0)
864 return ElementKind.INTERFACE;
865 else if ((flags & ENUM) != 0)
866 return ElementKind.ENUM;
867 else
868 return ElementKind.CLASS;
869 }
871 public NestingKind getNestingKind() {
872 complete();
873 if (owner.kind == PCK)
874 return NestingKind.TOP_LEVEL;
875 else if (name.isEmpty())
876 return NestingKind.ANONYMOUS;
877 else if (owner.kind == MTH)
878 return NestingKind.LOCAL;
879 else
880 return NestingKind.MEMBER;
881 }
883 /**
884 * @deprecated this method should never be used by javac internally.
885 */
886 @Override @Deprecated
887 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
888 return JavacElements.getAnnotation(this, annoType);
889 }
891 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
892 return v.visitType(this, p);
893 }
895 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
896 return v.visitClassSymbol(this, p);
897 }
898 }
901 /** A class for variable symbols
902 */
903 public static class VarSymbol extends Symbol implements VariableElement {
905 /** The variable's declaration position.
906 */
907 public int pos = Position.NOPOS;
909 /** The variable's address. Used for different purposes during
910 * flow analysis, translation and code generation.
911 * Flow analysis:
912 * If this is a blank final or local variable, its sequence number.
913 * Translation:
914 * If this is a private field, its access number.
915 * Code generation:
916 * If this is a local variable, its logical slot number.
917 */
918 public int adr = -1;
920 /** Construct a variable symbol, given its flags, name, type and owner.
921 */
922 public VarSymbol(long flags, Name name, Type type, Symbol owner) {
923 super(VAR, flags, name, type, owner);
924 }
926 /** Clone this symbol with new owner.
927 */
928 public VarSymbol clone(Symbol newOwner) {
929 VarSymbol v = new VarSymbol(flags_field, name, type, newOwner);
930 v.pos = pos;
931 v.adr = adr;
932 v.data = data;
933 // System.out.println("clone " + v + " in " + newOwner);//DEBUG
934 return v;
935 }
937 public String toString() {
938 return name.toString();
939 }
941 public Symbol asMemberOf(Type site, Types types) {
942 return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
943 }
945 public ElementKind getKind() {
946 long flags = flags();
947 if ((flags & PARAMETER) != 0) {
948 if (isExceptionParameter())
949 return ElementKind.EXCEPTION_PARAMETER;
950 else
951 return ElementKind.PARAMETER;
952 } else if ((flags & ENUM) != 0) {
953 return ElementKind.ENUM_CONSTANT;
954 } else if (owner.kind == TYP || owner.kind == ERR) {
955 return ElementKind.FIELD;
956 } else if (isResourceVariable()) {
957 return ElementKind.RESOURCE_VARIABLE;
958 } else {
959 return ElementKind.LOCAL_VARIABLE;
960 }
961 }
963 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
964 return v.visitVariable(this, p);
965 }
967 public Object getConstantValue() { // Mirror API
968 return Constants.decode(getConstValue(), type);
969 }
971 public void setLazyConstValue(final Env<AttrContext> env,
972 final Attr attr,
973 final JCTree.JCExpression initializer)
974 {
975 setData(new Callable<Object>() {
976 public Object call() {
977 return attr.attribLazyConstantValue(env, initializer, type);
978 }
979 });
980 }
982 /**
983 * The variable's constant value, if this is a constant.
984 * Before the constant value is evaluated, it points to an
985 * initalizer environment. If this is not a constant, it can
986 * be used for other stuff.
987 */
988 private Object data;
990 public boolean isExceptionParameter() {
991 return data == ElementKind.EXCEPTION_PARAMETER;
992 }
994 public boolean isResourceVariable() {
995 return data == ElementKind.RESOURCE_VARIABLE;
996 }
998 public Object getConstValue() {
999 // TODO: Consider if getConstValue and getConstantValue can be collapsed
1000 if (data == ElementKind.EXCEPTION_PARAMETER ||
1001 data == ElementKind.RESOURCE_VARIABLE) {
1002 return null;
1003 } else if (data instanceof Callable<?>) {
1004 // In this case, this is a final variable, with an as
1005 // yet unevaluated initializer.
1006 Callable<?> eval = (Callable<?>)data;
1007 data = null; // to make sure we don't evaluate this twice.
1008 try {
1009 data = eval.call();
1010 } catch (Exception ex) {
1011 throw new AssertionError(ex);
1012 }
1013 }
1014 return data;
1015 }
1017 public void setData(Object data) {
1018 Assert.check(!(data instanceof Env<?>), this);
1019 this.data = data;
1020 }
1022 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1023 return v.visitVarSymbol(this, p);
1024 }
1025 }
1027 /** A class for method symbols.
1028 */
1029 public static class MethodSymbol extends Symbol implements ExecutableElement {
1031 /** The code of the method. */
1032 public Code code = null;
1034 /** The parameters of the method. */
1035 public List<VarSymbol> params = null;
1037 /** The names of the parameters */
1038 public List<Name> savedParameterNames;
1040 /** For an attribute field accessor, its default value if any.
1041 * The value is null if none appeared in the method
1042 * declaration.
1043 */
1044 public Attribute defaultValue = null;
1046 /** Construct a method symbol, given its flags, name, type and owner.
1047 */
1048 public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
1049 super(MTH, flags, name, type, owner);
1050 if (owner.type.tag == TYPEVAR) Assert.error(owner + "." + name);
1051 }
1053 /** Clone this symbol with new owner.
1054 */
1055 public MethodSymbol clone(Symbol newOwner) {
1056 MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner);
1057 m.code = code;
1058 return m;
1059 }
1061 /** The Java source which this symbol represents.
1062 */
1063 public String toString() {
1064 if ((flags() & BLOCK) != 0) {
1065 return owner.name.toString();
1066 } else {
1067 String s = (name == name.table.names.init)
1068 ? owner.name.toString()
1069 : name.toString();
1070 if (type != null) {
1071 if (type.tag == FORALL)
1072 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
1073 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
1074 }
1075 return s;
1076 }
1077 }
1079 /** find a symbol that this (proxy method) symbol implements.
1080 * @param c The class whose members are searched for
1081 * implementations
1082 */
1083 public Symbol implemented(TypeSymbol c, Types types) {
1084 Symbol impl = null;
1085 for (List<Type> is = types.interfaces(c.type);
1086 impl == null && is.nonEmpty();
1087 is = is.tail) {
1088 TypeSymbol i = is.head.tsym;
1089 impl = implementedIn(i, types);
1090 if (impl == null)
1091 impl = implemented(i, types);
1092 }
1093 return impl;
1094 }
1096 public Symbol implementedIn(TypeSymbol c, Types types) {
1097 Symbol impl = null;
1098 for (Scope.Entry e = c.members().lookup(name);
1099 impl == null && e.scope != null;
1100 e = e.next()) {
1101 if (this.overrides(e.sym, (TypeSymbol)owner, types, true) &&
1102 // FIXME: I suspect the following requires a
1103 // subst() for a parametric return type.
1104 types.isSameType(type.getReturnType(),
1105 types.memberType(owner.type, e.sym).getReturnType())) {
1106 impl = e.sym;
1107 }
1108 }
1109 return impl;
1110 }
1112 /** Will the erasure of this method be considered by the VM to
1113 * override the erasure of the other when seen from class `origin'?
1114 */
1115 public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
1116 if (isConstructor() || _other.kind != MTH) return false;
1118 if (this == _other) return true;
1119 MethodSymbol other = (MethodSymbol)_other;
1121 // check for a direct implementation
1122 if (other.isOverridableIn((TypeSymbol)owner) &&
1123 types.asSuper(owner.type, other.owner) != null &&
1124 types.isSameType(erasure(types), other.erasure(types)))
1125 return true;
1127 // check for an inherited implementation
1128 return
1129 (flags() & ABSTRACT) == 0 &&
1130 other.isOverridableIn(origin) &&
1131 this.isMemberOf(origin, types) &&
1132 types.isSameType(erasure(types), other.erasure(types));
1133 }
1135 /** The implementation of this (abstract) symbol in class origin,
1136 * from the VM's point of view, null if method does not have an
1137 * implementation in class.
1138 * @param origin The class of which the implementation is a member.
1139 */
1140 public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
1141 for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
1142 for (Scope.Entry e = c.members().lookup(name);
1143 e.scope != null;
1144 e = e.next()) {
1145 if (e.sym.kind == MTH &&
1146 ((MethodSymbol)e.sym).binaryOverrides(this, origin, types))
1147 return (MethodSymbol)e.sym;
1148 }
1149 }
1150 return null;
1151 }
1153 /** Does this symbol override `other' symbol, when both are seen as
1154 * members of class `origin'? It is assumed that _other is a member
1155 * of origin.
1156 *
1157 * It is assumed that both symbols have the same name. The static
1158 * modifier is ignored for this test.
1159 *
1160 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
1161 */
1162 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
1163 if (isConstructor() || _other.kind != MTH) return false;
1165 if (this == _other) return true;
1166 MethodSymbol other = (MethodSymbol)_other;
1168 // check for a direct implementation
1169 if (other.isOverridableIn((TypeSymbol)owner) &&
1170 types.asSuper(owner.type, other.owner) != null) {
1171 Type mt = types.memberType(owner.type, this);
1172 Type ot = types.memberType(owner.type, other);
1173 if (types.isSubSignature(mt, ot)) {
1174 if (!checkResult)
1175 return true;
1176 if (types.returnTypeSubstitutable(mt, ot))
1177 return true;
1178 }
1179 }
1181 // check for an inherited implementation
1182 if ((flags() & ABSTRACT) != 0 ||
1183 (other.flags() & ABSTRACT) == 0 ||
1184 !other.isOverridableIn(origin) ||
1185 !this.isMemberOf(origin, types))
1186 return false;
1188 // assert types.asSuper(origin.type, other.owner) != null;
1189 Type mt = types.memberType(origin.type, this);
1190 Type ot = types.memberType(origin.type, other);
1191 return
1192 types.isSubSignature(mt, ot) &&
1193 (!checkResult || types.resultSubtype(mt, ot, Warner.noWarnings));
1194 }
1196 private boolean isOverridableIn(TypeSymbol origin) {
1197 // JLS 8.4.6.1
1198 switch ((int)(flags_field & Flags.AccessFlags)) {
1199 case Flags.PRIVATE:
1200 return false;
1201 case Flags.PUBLIC:
1202 return true;
1203 case Flags.PROTECTED:
1204 return (origin.flags() & INTERFACE) == 0;
1205 case 0:
1206 // for package private: can only override in the same
1207 // package
1208 return
1209 this.packge() == origin.packge() &&
1210 (origin.flags() & INTERFACE) == 0;
1211 default:
1212 return false;
1213 }
1214 }
1216 /** The implementation of this (abstract) symbol in class origin;
1217 * null if none exists. Synthetic methods are not considered
1218 * as possible implementations.
1219 */
1220 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
1221 return implementation(origin, types, checkResult, implementation_filter);
1222 }
1223 // where
1224 private static final Filter<Symbol> implementation_filter = new Filter<Symbol>() {
1225 public boolean accepts(Symbol s) {
1226 return s.kind == Kinds.MTH &&
1227 (s.flags() & SYNTHETIC) == 0;
1228 }
1229 };
1231 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) {
1232 MethodSymbol res = types.implementation(this, origin, checkResult, implFilter);
1233 if (res != null)
1234 return res;
1235 // if origin is derived from a raw type, we might have missed
1236 // an implementation because we do not know enough about instantiations.
1237 // in this case continue with the supertype as origin.
1238 if (types.isDerivedRaw(origin.type))
1239 return implementation(types.supertype(origin.type).tsym, types, checkResult);
1240 else
1241 return null;
1242 }
1244 public List<VarSymbol> params() {
1245 owner.complete();
1246 if (params == null) {
1247 // If ClassReader.saveParameterNames has been set true, then
1248 // savedParameterNames will be set to a list of names that
1249 // matches the types in type.getParameterTypes(). If any names
1250 // were not found in the class file, those names in the list will
1251 // be set to the empty name.
1252 // If ClassReader.saveParameterNames has been set false, then
1253 // savedParameterNames will be null.
1254 List<Name> paramNames = savedParameterNames;
1255 savedParameterNames = null;
1256 // discard the provided names if the list of names is the wrong size.
1257 if (paramNames == null || paramNames.size() != type.getParameterTypes().size())
1258 paramNames = List.nil();
1259 ListBuffer<VarSymbol> buf = new ListBuffer<VarSymbol>();
1260 List<Name> remaining = paramNames;
1261 // assert: remaining and paramNames are both empty or both
1262 // have same cardinality as type.getParameterTypes()
1263 int i = 0;
1264 for (Type t : type.getParameterTypes()) {
1265 Name paramName;
1266 if (remaining.isEmpty()) {
1267 // no names for any parameters available
1268 paramName = createArgName(i, paramNames);
1269 } else {
1270 paramName = remaining.head;
1271 remaining = remaining.tail;
1272 if (paramName.isEmpty()) {
1273 // no name for this specific parameter
1274 paramName = createArgName(i, paramNames);
1275 }
1276 }
1277 buf.append(new VarSymbol(PARAMETER, paramName, t, this));
1278 i++;
1279 }
1280 params = buf.toList();
1281 }
1282 return params;
1283 }
1285 // Create a name for the argument at position 'index' that is not in
1286 // the exclude list. In normal use, either no names will have been
1287 // provided, in which case the exclude list is empty, or all the names
1288 // will have been provided, in which case this method will not be called.
1289 private Name createArgName(int index, List<Name> exclude) {
1290 String prefix = "arg";
1291 while (true) {
1292 Name argName = name.table.fromString(prefix + index);
1293 if (!exclude.contains(argName))
1294 return argName;
1295 prefix += "$";
1296 }
1297 }
1299 public Symbol asMemberOf(Type site, Types types) {
1300 return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
1301 }
1303 public ElementKind getKind() {
1304 if (name == name.table.names.init)
1305 return ElementKind.CONSTRUCTOR;
1306 else if (name == name.table.names.clinit)
1307 return ElementKind.STATIC_INIT;
1308 else if ((flags() & BLOCK) != 0)
1309 return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT;
1310 else
1311 return ElementKind.METHOD;
1312 }
1314 public boolean isStaticOrInstanceInit() {
1315 return getKind() == ElementKind.STATIC_INIT ||
1316 getKind() == ElementKind.INSTANCE_INIT;
1317 }
1319 public Attribute getDefaultValue() {
1320 return defaultValue;
1321 }
1323 public List<VarSymbol> getParameters() {
1324 return params();
1325 }
1327 public boolean isVarArgs() {
1328 return (flags() & VARARGS) != 0;
1329 }
1331 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1332 return v.visitExecutable(this, p);
1333 }
1335 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1336 return v.visitMethodSymbol(this, p);
1337 }
1339 public Type getReturnType() {
1340 return asType().getReturnType();
1341 }
1343 public List<Type> getThrownTypes() {
1344 return asType().getThrownTypes();
1345 }
1346 }
1348 /** A class for predefined operators.
1349 */
1350 public static class OperatorSymbol extends MethodSymbol {
1352 public int opcode;
1354 public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
1355 super(PUBLIC | STATIC, name, type, owner);
1356 this.opcode = opcode;
1357 }
1359 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1360 return v.visitOperatorSymbol(this, p);
1361 }
1362 }
1364 /** Symbol completer interface.
1365 */
1366 public static interface Completer {
1367 void complete(Symbol sym) throws CompletionFailure;
1368 }
1370 public static class CompletionFailure extends RuntimeException {
1371 private static final long serialVersionUID = 0;
1372 public Symbol sym;
1374 /** A diagnostic object describing the failure
1375 */
1376 public JCDiagnostic diag;
1378 /** A localized string describing the failure.
1379 * @deprecated Use {@code getDetail()} or {@code getMessage()}
1380 */
1381 @Deprecated
1382 public String errmsg;
1384 public CompletionFailure(Symbol sym, String errmsg) {
1385 this.sym = sym;
1386 this.errmsg = errmsg;
1387 // this.printStackTrace();//DEBUG
1388 }
1390 public CompletionFailure(Symbol sym, JCDiagnostic diag) {
1391 this.sym = sym;
1392 this.diag = diag;
1393 // this.printStackTrace();//DEBUG
1394 }
1396 public JCDiagnostic getDiagnostic() {
1397 return diag;
1398 }
1400 @Override
1401 public String getMessage() {
1402 if (diag != null)
1403 return diag.getMessage(null);
1404 else
1405 return errmsg;
1406 }
1408 public Object getDetailValue() {
1409 return (diag != null ? diag : errmsg);
1410 }
1412 @Override
1413 public CompletionFailure initCause(Throwable cause) {
1414 super.initCause(cause);
1415 return this;
1416 }
1418 }
1420 /**
1421 * A visitor for symbols. A visitor is used to implement operations
1422 * (or relations) on symbols. Most common operations on types are
1423 * binary relations and this interface is designed for binary
1424 * relations, that is, operations on the form
1425 * Symbol × P → R.
1426 * <!-- In plain text: Type x P -> R -->
1427 *
1428 * @param <R> the return type of the operation implemented by this
1429 * visitor; use Void if no return type is needed.
1430 * @param <P> the type of the second argument (the first being the
1431 * symbol itself) of the operation implemented by this visitor; use
1432 * Void if a second argument is not needed.
1433 */
1434 public interface Visitor<R,P> {
1435 R visitClassSymbol(ClassSymbol s, P arg);
1436 R visitMethodSymbol(MethodSymbol s, P arg);
1437 R visitPackageSymbol(PackageSymbol s, P arg);
1438 R visitOperatorSymbol(OperatorSymbol s, P arg);
1439 R visitVarSymbol(VarSymbol s, P arg);
1440 R visitTypeSymbol(TypeSymbol s, P arg);
1441 R visitSymbol(Symbol s, P arg);
1442 }
1443 }