Thu, 01 Nov 2012 10:48:36 +0100
7153951: Add new lint option -Xlint:auxiliaryclass
Reviewed-by: jjg, mcimadamore, forax
1 /*
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.javac.code;
28 import java.util.Set;
29 import java.util.concurrent.Callable;
31 import javax.lang.model.element.*;
32 import javax.tools.JavaFileObject;
34 import com.sun.tools.javac.code.Type.*;
35 import com.sun.tools.javac.comp.Attr;
36 import com.sun.tools.javac.comp.AttrContext;
37 import com.sun.tools.javac.comp.Env;
38 import com.sun.tools.javac.jvm.*;
39 import com.sun.tools.javac.model.*;
40 import com.sun.tools.javac.tree.JCTree;
41 import com.sun.tools.javac.util.*;
42 import com.sun.tools.javac.util.Name;
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.TypeTag.CLASS;
46 import static com.sun.tools.javac.code.TypeTag.FORALL;
47 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
49 /** Root class for Java symbols. It contains subclasses
50 * for specific sorts of symbols, such as variables, methods and operators,
51 * types, packages. Each subclass is represented as a static inner class
52 * inside Symbol.
53 *
54 * <p><b>This is NOT part of any supported API.
55 * If you write code that depends on this, you do so at your own risk.
56 * This code and its internal interfaces are subject to change or
57 * deletion without notice.</b>
58 */
59 public abstract class Symbol implements Element {
60 // public Throwable debug = new Throwable();
62 /** The kind of this symbol.
63 * @see Kinds
64 */
65 public int kind;
67 /** The flags of this symbol.
68 */
69 public long flags_field;
71 /** An accessor method for the flags of this symbol.
72 * Flags of class symbols should be accessed through the accessor
73 * method to make sure that the class symbol is loaded.
74 */
75 public long flags() { return flags_field; }
77 /** The attributes of this symbol are contained in this
78 * Annotations. The Annotations instance is NOT immutable.
79 */
80 public final Annotations annotations = new Annotations(this);
82 /** An accessor method for the attributes of this symbol.
83 * Attributes of class symbols should be accessed through the accessor
84 * method to make sure that the class symbol is loaded.
85 */
86 public List<Attribute.Compound> getAnnotationMirrors() {
87 return Assert.checkNonNull(annotations.getAttributes());
88 }
90 /** Fetch a particular annotation from a symbol. */
91 public Attribute.Compound attribute(Symbol anno) {
92 for (Attribute.Compound a : getAnnotationMirrors()) {
93 if (a.type.tsym == anno) return a;
94 }
95 return null;
96 }
98 /** The name of this symbol in Utf8 representation.
99 */
100 public Name name;
102 /** The type of this symbol.
103 */
104 public Type type;
106 /** The owner of this symbol.
107 */
108 public Symbol owner;
110 /** The completer of this symbol.
111 */
112 public Completer completer;
114 /** A cache for the type erasure of this symbol.
115 */
116 public Type erasure_field;
118 /** Construct a symbol with given kind, flags, name, type and owner.
119 */
120 public Symbol(int kind, long flags, Name name, Type type, Symbol owner) {
121 this.kind = kind;
122 this.flags_field = flags;
123 this.type = type;
124 this.owner = owner;
125 this.completer = null;
126 this.erasure_field = null;
127 this.name = name;
128 }
130 /** Clone this symbol with new owner.
131 * Legal only for fields and methods.
132 */
133 public Symbol clone(Symbol newOwner) {
134 throw new AssertionError();
135 }
137 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
138 return v.visitSymbol(this, p);
139 }
141 /** The Java source which this symbol represents.
142 * A description of this symbol; overrides Object.
143 */
144 public String toString() {
145 return name.toString();
146 }
148 /** A Java source description of the location of this symbol; used for
149 * error reporting.
150 *
151 * @return null if the symbol is a package or a toplevel class defined in
152 * the default package; otherwise, the owner symbol is returned
153 */
154 public Symbol location() {
155 if (owner.name == null || (owner.name.isEmpty() &&
156 (owner.flags() & BLOCK) == 0 && owner.kind != PCK && owner.kind != TYP)) {
157 return null;
158 }
159 return owner;
160 }
162 public Symbol location(Type site, Types types) {
163 if (owner.name == null || owner.name.isEmpty()) {
164 return location();
165 }
166 if (owner.type.hasTag(CLASS)) {
167 Type ownertype = types.asOuterSuper(site, owner);
168 if (ownertype != null) return ownertype.tsym;
169 }
170 return owner;
171 }
173 public Symbol baseSymbol() {
174 return this;
175 }
177 /** The symbol's erased type.
178 */
179 public Type erasure(Types types) {
180 if (erasure_field == null)
181 erasure_field = types.erasure(type);
182 return erasure_field;
183 }
185 /** The external type of a symbol. This is the symbol's erased type
186 * except for constructors of inner classes which get the enclosing
187 * instance class added as first argument.
188 */
189 public Type externalType(Types types) {
190 Type t = erasure(types);
191 if (name == name.table.names.init && owner.hasOuterInstance()) {
192 Type outerThisType = types.erasure(owner.type.getEnclosingType());
193 return new MethodType(t.getParameterTypes().prepend(outerThisType),
194 t.getReturnType(),
195 t.getThrownTypes(),
196 t.tsym);
197 } else {
198 return t;
199 }
200 }
202 public boolean isStatic() {
203 return
204 (flags() & STATIC) != 0 ||
205 (owner.flags() & INTERFACE) != 0 && kind != MTH;
206 }
208 public boolean isInterface() {
209 return (flags() & INTERFACE) != 0;
210 }
212 /** Is this symbol declared (directly or indirectly) local
213 * to a method or variable initializer?
214 * Also includes fields of inner classes which are in
215 * turn local to a method or variable initializer.
216 */
217 public boolean isLocal() {
218 return
219 (owner.kind & (VAR | MTH)) != 0 ||
220 (owner.kind == TYP && owner.isLocal());
221 }
223 /** Has this symbol an empty name? This includes anonymous
224 * inner classses.
225 */
226 public boolean isAnonymous() {
227 return name.isEmpty();
228 }
230 /** Is this symbol a constructor?
231 */
232 public boolean isConstructor() {
233 return name == name.table.names.init;
234 }
236 /** The fully qualified name of this symbol.
237 * This is the same as the symbol's name except for class symbols,
238 * which are handled separately.
239 */
240 public Name getQualifiedName() {
241 return name;
242 }
244 /** The fully qualified name of this symbol after converting to flat
245 * representation. This is the same as the symbol's name except for
246 * class symbols, which are handled separately.
247 */
248 public Name flatName() {
249 return getQualifiedName();
250 }
252 /** If this is a class or package, its members, otherwise null.
253 */
254 public Scope members() {
255 return null;
256 }
258 /** A class is an inner class if it it has an enclosing instance class.
259 */
260 public boolean isInner() {
261 return type.getEnclosingType().hasTag(CLASS);
262 }
264 /** An inner class has an outer instance if it is not an interface
265 * it has an enclosing instance class which might be referenced from the class.
266 * Nested classes can see instance members of their enclosing class.
267 * Their constructors carry an additional this$n parameter, inserted
268 * implicitly by the compiler.
269 *
270 * @see #isInner
271 */
272 public boolean hasOuterInstance() {
273 return
274 type.getEnclosingType().hasTag(CLASS) && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
275 }
277 /** The closest enclosing class of this symbol's declaration.
278 */
279 public ClassSymbol enclClass() {
280 Symbol c = this;
281 while (c != null &&
282 ((c.kind & TYP) == 0 || !c.type.hasTag(CLASS))) {
283 c = c.owner;
284 }
285 return (ClassSymbol)c;
286 }
288 /** The outermost class which indirectly owns this symbol.
289 */
290 public ClassSymbol outermostClass() {
291 Symbol sym = this;
292 Symbol prev = null;
293 while (sym.kind != PCK) {
294 prev = sym;
295 sym = sym.owner;
296 }
297 return (ClassSymbol) prev;
298 }
300 /** The package which indirectly owns this symbol.
301 */
302 public PackageSymbol packge() {
303 Symbol sym = this;
304 while (sym.kind != PCK) {
305 sym = sym.owner;
306 }
307 return (PackageSymbol) sym;
308 }
310 /** Is this symbol a subclass of `base'? Only defined for ClassSymbols.
311 */
312 public boolean isSubClass(Symbol base, Types types) {
313 throw new AssertionError("isSubClass " + this);
314 }
316 /** Fully check membership: hierarchy, protection, and hiding.
317 * Does not exclude methods not inherited due to overriding.
318 */
319 public boolean isMemberOf(TypeSymbol clazz, Types types) {
320 return
321 owner == clazz ||
322 clazz.isSubClass(owner, types) &&
323 isInheritedIn(clazz, types) &&
324 !hiddenIn((ClassSymbol)clazz, types);
325 }
327 /** Is this symbol the same as or enclosed by the given class? */
328 public boolean isEnclosedBy(ClassSymbol clazz) {
329 for (Symbol sym = this; sym.kind != PCK; sym = sym.owner)
330 if (sym == clazz) return true;
331 return false;
332 }
334 /** Check for hiding. Note that this doesn't handle multiple
335 * (interface) inheritance. */
336 private boolean hiddenIn(ClassSymbol clazz, Types types) {
337 if (kind == MTH && (flags() & STATIC) == 0) return false;
338 while (true) {
339 if (owner == clazz) return false;
340 Scope.Entry e = clazz.members().lookup(name);
341 while (e.scope != null) {
342 if (e.sym == this) return false;
343 if (e.sym.kind == kind &&
344 (kind != MTH ||
345 (e.sym.flags() & STATIC) != 0 &&
346 types.isSubSignature(e.sym.type, type)))
347 return true;
348 e = e.next();
349 }
350 Type superType = types.supertype(clazz.type);
351 if (!superType.hasTag(CLASS)) return false;
352 clazz = (ClassSymbol)superType.tsym;
353 }
354 }
356 /** Is this symbol inherited into a given class?
357 * PRE: If symbol's owner is a interface,
358 * it is already assumed that the interface is a superinterface
359 * of given class.
360 * @param clazz The class for which we want to establish membership.
361 * This must be a subclass of the member's owner.
362 */
363 public boolean isInheritedIn(Symbol clazz, Types types) {
364 switch ((int)(flags_field & Flags.AccessFlags)) {
365 default: // error recovery
366 case PUBLIC:
367 return true;
368 case PRIVATE:
369 return this.owner == clazz;
370 case PROTECTED:
371 // we model interfaces as extending Object
372 return (clazz.flags() & INTERFACE) == 0;
373 case 0:
374 PackageSymbol thisPackage = this.packge();
375 for (Symbol sup = clazz;
376 sup != null && sup != this.owner;
377 sup = types.supertype(sup.type).tsym) {
378 while (sup.type.hasTag(TYPEVAR))
379 sup = sup.type.getUpperBound().tsym;
380 if (sup.type.isErroneous())
381 return true; // error recovery
382 if ((sup.flags() & COMPOUND) != 0)
383 continue;
384 if (sup.packge() != thisPackage)
385 return false;
386 }
387 return (clazz.flags() & INTERFACE) == 0;
388 }
389 }
391 /** The (variable or method) symbol seen as a member of given
392 * class type`site' (this might change the symbol's type).
393 * This is used exclusively for producing diagnostics.
394 */
395 public Symbol asMemberOf(Type site, Types types) {
396 throw new AssertionError();
397 }
399 /** Does this method symbol override `other' symbol, when both are seen as
400 * members of class `origin'? It is assumed that _other is a member
401 * of origin.
402 *
403 * It is assumed that both symbols have the same name. The static
404 * modifier is ignored for this test.
405 *
406 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
407 */
408 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
409 return false;
410 }
412 /** Complete the elaboration of this symbol's definition.
413 */
414 public void complete() throws CompletionFailure {
415 if (completer != null) {
416 Completer c = completer;
417 completer = null;
418 c.complete(this);
419 }
420 }
422 /** True if the symbol represents an entity that exists.
423 */
424 public boolean exists() {
425 return true;
426 }
428 public Type asType() {
429 return type;
430 }
432 public Symbol getEnclosingElement() {
433 return owner;
434 }
436 public ElementKind getKind() {
437 return ElementKind.OTHER; // most unkind
438 }
440 public Set<Modifier> getModifiers() {
441 return Flags.asModifierSet(flags());
442 }
444 public Name getSimpleName() {
445 return name;
446 }
448 /**
449 * @deprecated this method should never be used by javac internally.
450 */
451 @Deprecated
452 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
453 return JavacElements.getAnnotation(this, annoType);
454 }
456 // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList
457 public java.util.List<Symbol> getEnclosedElements() {
458 return List.nil();
459 }
461 public List<TypeSymbol> getTypeParameters() {
462 ListBuffer<TypeSymbol> l = ListBuffer.lb();
463 for (Type t : type.getTypeArguments()) {
464 l.append(t.tsym);
465 }
466 return l.toList();
467 }
469 public static class DelegatedSymbol extends Symbol {
470 protected Symbol other;
471 public DelegatedSymbol(Symbol other) {
472 super(other.kind, other.flags_field, other.name, other.type, other.owner);
473 this.other = other;
474 }
475 public String toString() { return other.toString(); }
476 public Symbol location() { return other.location(); }
477 public Symbol location(Type site, Types types) { return other.location(site, types); }
478 public Type erasure(Types types) { return other.erasure(types); }
479 public Type externalType(Types types) { return other.externalType(types); }
480 public boolean isLocal() { return other.isLocal(); }
481 public boolean isConstructor() { return other.isConstructor(); }
482 public Name getQualifiedName() { return other.getQualifiedName(); }
483 public Name flatName() { return other.flatName(); }
484 public Scope members() { return other.members(); }
485 public boolean isInner() { return other.isInner(); }
486 public boolean hasOuterInstance() { return other.hasOuterInstance(); }
487 public ClassSymbol enclClass() { return other.enclClass(); }
488 public ClassSymbol outermostClass() { return other.outermostClass(); }
489 public PackageSymbol packge() { return other.packge(); }
490 public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); }
491 public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); }
492 public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); }
493 public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); }
494 public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); }
495 public void complete() throws CompletionFailure { other.complete(); }
497 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
498 return other.accept(v, p);
499 }
501 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
502 return v.visitSymbol(other, p);
503 }
504 }
506 /** A class for type symbols. Type variables are represented by instances
507 * of this class, classes and packages by instances of subclasses.
508 */
509 public static class TypeSymbol
510 extends Symbol implements TypeParameterElement {
511 // Implements TypeParameterElement because type parameters don't
512 // have their own TypeSymbol subclass.
513 // TODO: type parameters should have their own TypeSymbol subclass
515 public TypeSymbol(long flags, Name name, Type type, Symbol owner) {
516 super(TYP, flags, name, type, owner);
517 }
519 /** form a fully qualified name from a name and an owner
520 */
521 static public Name formFullName(Name name, Symbol owner) {
522 if (owner == null) return name;
523 if (((owner.kind != ERR)) &&
524 ((owner.kind & (VAR | MTH)) != 0
525 || (owner.kind == TYP && owner.type.hasTag(TYPEVAR))
526 )) return name;
527 Name prefix = owner.getQualifiedName();
528 if (prefix == null || prefix == prefix.table.names.empty)
529 return name;
530 else return prefix.append('.', name);
531 }
533 /** form a fully qualified name from a name and an owner, after
534 * converting to flat representation
535 */
536 static public Name formFlatName(Name name, Symbol owner) {
537 if (owner == null ||
538 (owner.kind & (VAR | MTH)) != 0
539 || (owner.kind == TYP && owner.type.hasTag(TYPEVAR))
540 ) return name;
541 char sep = owner.kind == TYP ? '$' : '.';
542 Name prefix = owner.flatName();
543 if (prefix == null || prefix == prefix.table.names.empty)
544 return name;
545 else return prefix.append(sep, name);
546 }
548 /**
549 * A total ordering between type symbols that refines the
550 * class inheritance graph.
551 *
552 * Typevariables always precede other kinds of symbols.
553 */
554 public final boolean precedes(TypeSymbol that, Types types) {
555 if (this == that)
556 return false;
557 if (this.type.tag == that.type.tag) {
558 if (this.type.hasTag(CLASS)) {
559 return
560 types.rank(that.type) < types.rank(this.type) ||
561 types.rank(that.type) == types.rank(this.type) &&
562 that.getQualifiedName().compareTo(this.getQualifiedName()) < 0;
563 } else if (this.type.hasTag(TYPEVAR)) {
564 return types.isSubtype(this.type, that.type);
565 }
566 }
567 return this.type.hasTag(TYPEVAR);
568 }
570 // For type params; overridden in subclasses.
571 public ElementKind getKind() {
572 return ElementKind.TYPE_PARAMETER;
573 }
575 public java.util.List<Symbol> getEnclosedElements() {
576 List<Symbol> list = List.nil();
577 if (kind == TYP && type.hasTag(TYPEVAR)) {
578 return list;
579 }
580 for (Scope.Entry e = members().elems; e != null; e = e.sibling) {
581 if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this)
582 list = list.prepend(e.sym);
583 }
584 return list;
585 }
587 // For type params.
588 // Perhaps not needed if getEnclosingElement can be spec'ed
589 // to do the same thing.
590 // TODO: getGenericElement() might not be needed
591 public Symbol getGenericElement() {
592 return owner;
593 }
595 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
596 Assert.check(type.hasTag(TYPEVAR)); // else override will be invoked
597 return v.visitTypeParameter(this, p);
598 }
600 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
601 return v.visitTypeSymbol(this, p);
602 }
604 public List<Type> getBounds() {
605 TypeVar t = (TypeVar)type;
606 Type bound = t.getUpperBound();
607 if (!bound.isCompound())
608 return List.of(bound);
609 ClassType ct = (ClassType)bound;
610 if (!ct.tsym.erasure_field.isInterface()) {
611 return ct.interfaces_field.prepend(ct.supertype_field);
612 } else {
613 // No superclass was given in bounds.
614 // In this case, supertype is Object, erasure is first interface.
615 return ct.interfaces_field;
616 }
617 }
618 }
620 /** A class for package symbols
621 */
622 public static class PackageSymbol extends TypeSymbol
623 implements PackageElement {
625 public Scope members_field;
626 public Name fullname;
627 public ClassSymbol package_info; // see bug 6443073
629 public PackageSymbol(Name name, Type type, Symbol owner) {
630 super(0, name, type, owner);
631 this.kind = PCK;
632 this.members_field = null;
633 this.fullname = formFullName(name, owner);
634 }
636 public PackageSymbol(Name name, Symbol owner) {
637 this(name, null, owner);
638 this.type = new PackageType(this);
639 }
641 public String toString() {
642 return fullname.toString();
643 }
645 public Name getQualifiedName() {
646 return fullname;
647 }
649 public boolean isUnnamed() {
650 return name.isEmpty() && owner != null;
651 }
653 public Scope members() {
654 if (completer != null) complete();
655 return members_field;
656 }
658 public long flags() {
659 if (completer != null) complete();
660 return flags_field;
661 }
663 public List<Attribute.Compound> getAnnotationMirrors() {
664 if (completer != null) complete();
665 if (package_info != null && package_info.completer != null) {
666 package_info.complete();
667 if (annotations.isEmpty()) {
668 annotations.setAttributes(package_info.annotations);
669 }
670 }
671 return Assert.checkNonNull(annotations.getAttributes());
672 }
674 /** A package "exists" if a type or package that exists has
675 * been seen within it.
676 */
677 public boolean exists() {
678 return (flags_field & EXISTS) != 0;
679 }
681 public ElementKind getKind() {
682 return ElementKind.PACKAGE;
683 }
685 public Symbol getEnclosingElement() {
686 return null;
687 }
689 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
690 return v.visitPackage(this, p);
691 }
693 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
694 return v.visitPackageSymbol(this, p);
695 }
696 }
698 /** A class for class symbols
699 */
700 public static class ClassSymbol extends TypeSymbol implements TypeElement {
702 /** a scope for all class members; variables, methods and inner classes
703 * type parameters are not part of this scope
704 */
705 public Scope members_field;
707 /** the fully qualified name of the class, i.e. pck.outer.inner.
708 * null for anonymous classes
709 */
710 public Name fullname;
712 /** the fully qualified name of the class after converting to flat
713 * representation, i.e. pck.outer$inner,
714 * set externally for local and anonymous classes
715 */
716 public Name flatname;
718 /** the sourcefile where the class came from
719 */
720 public JavaFileObject sourcefile;
722 /** the classfile from where to load this class
723 * this will have extension .class or .java
724 */
725 public JavaFileObject classfile;
727 /** the list of translated local classes (used for generating
728 * InnerClasses attribute)
729 */
730 public List<ClassSymbol> trans_local;
732 /** the constant pool of the class
733 */
734 public Pool pool;
736 public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
737 super(flags, name, type, owner);
738 this.members_field = null;
739 this.fullname = formFullName(name, owner);
740 this.flatname = formFlatName(name, owner);
741 this.sourcefile = null;
742 this.classfile = null;
743 this.pool = null;
744 }
746 public ClassSymbol(long flags, Name name, Symbol owner) {
747 this(
748 flags,
749 name,
750 new ClassType(Type.noType, null, null),
751 owner);
752 this.type.tsym = this;
753 }
755 /** The Java source which this symbol represents.
756 */
757 public String toString() {
758 return className();
759 }
761 public long flags() {
762 if (completer != null) complete();
763 return flags_field;
764 }
766 public Scope members() {
767 if (completer != null) complete();
768 return members_field;
769 }
771 public List<Attribute.Compound> getAnnotationMirrors() {
772 if (completer != null) complete();
773 return Assert.checkNonNull(annotations.getAttributes());
774 }
776 public Type erasure(Types types) {
777 if (erasure_field == null)
778 erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
779 List.<Type>nil(), this);
780 return erasure_field;
781 }
783 public String className() {
784 if (name.isEmpty())
785 return
786 Log.getLocalizedString("anonymous.class", flatname);
787 else
788 return fullname.toString();
789 }
791 public Name getQualifiedName() {
792 return fullname;
793 }
795 public Name flatName() {
796 return flatname;
797 }
799 public boolean isSubClass(Symbol base, Types types) {
800 if (this == base) {
801 return true;
802 } else if ((base.flags() & INTERFACE) != 0) {
803 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t))
804 for (List<Type> is = types.interfaces(t);
805 is.nonEmpty();
806 is = is.tail)
807 if (is.head.tsym.isSubClass(base, types)) return true;
808 } else {
809 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t))
810 if (t.tsym == base) return true;
811 }
812 return false;
813 }
815 /** Complete the elaboration of this symbol's definition.
816 */
817 public void complete() throws CompletionFailure {
818 try {
819 super.complete();
820 } catch (CompletionFailure ex) {
821 // quiet error recovery
822 flags_field |= (PUBLIC|STATIC);
823 this.type = new ErrorType(this, Type.noType);
824 throw ex;
825 }
826 }
828 public List<Type> getInterfaces() {
829 complete();
830 if (type instanceof ClassType) {
831 ClassType t = (ClassType)type;
832 if (t.interfaces_field == null) // FIXME: shouldn't be null
833 t.interfaces_field = List.nil();
834 if (t.all_interfaces_field != null)
835 return Type.getModelTypes(t.all_interfaces_field);
836 return t.interfaces_field;
837 } else {
838 return List.nil();
839 }
840 }
842 public Type getSuperclass() {
843 complete();
844 if (type instanceof ClassType) {
845 ClassType t = (ClassType)type;
846 if (t.supertype_field == null) // FIXME: shouldn't be null
847 t.supertype_field = Type.noType;
848 // An interface has no superclass; its supertype is Object.
849 return t.isInterface()
850 ? Type.noType
851 : t.supertype_field.getModelType();
852 } else {
853 return Type.noType;
854 }
855 }
857 public ElementKind getKind() {
858 long flags = flags();
859 if ((flags & ANNOTATION) != 0)
860 return ElementKind.ANNOTATION_TYPE;
861 else if ((flags & INTERFACE) != 0)
862 return ElementKind.INTERFACE;
863 else if ((flags & ENUM) != 0)
864 return ElementKind.ENUM;
865 else
866 return ElementKind.CLASS;
867 }
869 public NestingKind getNestingKind() {
870 complete();
871 if (owner.kind == PCK)
872 return NestingKind.TOP_LEVEL;
873 else if (name.isEmpty())
874 return NestingKind.ANONYMOUS;
875 else if (owner.kind == MTH)
876 return NestingKind.LOCAL;
877 else
878 return NestingKind.MEMBER;
879 }
881 /**
882 * @deprecated this method should never be used by javac internally.
883 */
884 @Override @Deprecated
885 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
886 return JavacElements.getAnnotation(this, annoType);
887 }
889 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
890 return v.visitType(this, p);
891 }
893 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
894 return v.visitClassSymbol(this, p);
895 }
896 }
899 /** A class for variable symbols
900 */
901 public static class VarSymbol extends Symbol implements VariableElement {
903 /** The variable's declaration position.
904 */
905 public int pos = Position.NOPOS;
907 /** The variable's address. Used for different purposes during
908 * flow analysis, translation and code generation.
909 * Flow analysis:
910 * If this is a blank final or local variable, its sequence number.
911 * Translation:
912 * If this is a private field, its access number.
913 * Code generation:
914 * If this is a local variable, its logical slot number.
915 */
916 public int adr = -1;
918 /** Construct a variable symbol, given its flags, name, type and owner.
919 */
920 public VarSymbol(long flags, Name name, Type type, Symbol owner) {
921 super(VAR, flags, name, type, owner);
922 }
924 /** Clone this symbol with new owner.
925 */
926 public VarSymbol clone(Symbol newOwner) {
927 VarSymbol v = new VarSymbol(flags_field, name, type, newOwner) {
928 @Override
929 public Symbol baseSymbol() {
930 return VarSymbol.this;
931 }
932 };
933 v.pos = pos;
934 v.adr = adr;
935 v.data = data;
936 // System.out.println("clone " + v + " in " + newOwner);//DEBUG
937 return v;
938 }
940 public String toString() {
941 return name.toString();
942 }
944 public Symbol asMemberOf(Type site, Types types) {
945 return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
946 }
948 public ElementKind getKind() {
949 long flags = flags();
950 if ((flags & PARAMETER) != 0) {
951 if (isExceptionParameter())
952 return ElementKind.EXCEPTION_PARAMETER;
953 else
954 return ElementKind.PARAMETER;
955 } else if ((flags & ENUM) != 0) {
956 return ElementKind.ENUM_CONSTANT;
957 } else if (owner.kind == TYP || owner.kind == ERR) {
958 return ElementKind.FIELD;
959 } else if (isResourceVariable()) {
960 return ElementKind.RESOURCE_VARIABLE;
961 } else {
962 return ElementKind.LOCAL_VARIABLE;
963 }
964 }
966 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
967 return v.visitVariable(this, p);
968 }
970 public Object getConstantValue() { // Mirror API
971 return Constants.decode(getConstValue(), type);
972 }
974 public void setLazyConstValue(final Env<AttrContext> env,
975 final Attr attr,
976 final JCTree.JCExpression initializer)
977 {
978 setData(new Callable<Object>() {
979 public Object call() {
980 return attr.attribLazyConstantValue(env, initializer, type);
981 }
982 });
983 }
985 /**
986 * The variable's constant value, if this is a constant.
987 * Before the constant value is evaluated, it points to an
988 * initalizer environment. If this is not a constant, it can
989 * be used for other stuff.
990 */
991 private Object data;
993 public boolean isExceptionParameter() {
994 return data == ElementKind.EXCEPTION_PARAMETER;
995 }
997 public boolean isResourceVariable() {
998 return data == ElementKind.RESOURCE_VARIABLE;
999 }
1001 public Object getConstValue() {
1002 // TODO: Consider if getConstValue and getConstantValue can be collapsed
1003 if (data == ElementKind.EXCEPTION_PARAMETER ||
1004 data == ElementKind.RESOURCE_VARIABLE) {
1005 return null;
1006 } else if (data instanceof Callable<?>) {
1007 // In this case, this is a final variable, with an as
1008 // yet unevaluated initializer.
1009 Callable<?> eval = (Callable<?>)data;
1010 data = null; // to make sure we don't evaluate this twice.
1011 try {
1012 data = eval.call();
1013 } catch (Exception ex) {
1014 throw new AssertionError(ex);
1015 }
1016 }
1017 return data;
1018 }
1020 public void setData(Object data) {
1021 Assert.check(!(data instanceof Env<?>), this);
1022 this.data = data;
1023 }
1025 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1026 return v.visitVarSymbol(this, p);
1027 }
1028 }
1030 /** A class for method symbols.
1031 */
1032 public static class MethodSymbol extends Symbol implements ExecutableElement {
1034 /** The code of the method. */
1035 public Code code = null;
1037 /** The parameters of the method. */
1038 public List<VarSymbol> params = null;
1040 /** The names of the parameters */
1041 public List<Name> savedParameterNames;
1043 /** For an attribute field accessor, its default value if any.
1044 * The value is null if none appeared in the method
1045 * declaration.
1046 */
1047 public Attribute defaultValue = null;
1049 /** Construct a method symbol, given its flags, name, type and owner.
1050 */
1051 public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
1052 super(MTH, flags, name, type, owner);
1053 if (owner.type.hasTag(TYPEVAR)) Assert.error(owner + "." + name);
1054 }
1056 /** Clone this symbol with new owner.
1057 */
1058 public MethodSymbol clone(Symbol newOwner) {
1059 MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner) {
1060 @Override
1061 public Symbol baseSymbol() {
1062 return MethodSymbol.this;
1063 }
1064 };
1065 m.code = code;
1066 return m;
1067 }
1069 /** The Java source which this symbol represents.
1070 */
1071 public String toString() {
1072 if ((flags() & BLOCK) != 0) {
1073 return owner.name.toString();
1074 } else {
1075 String s = (name == name.table.names.init)
1076 ? owner.name.toString()
1077 : name.toString();
1078 if (type != null) {
1079 if (type.hasTag(FORALL))
1080 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
1081 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
1082 }
1083 return s;
1084 }
1085 }
1087 public boolean isDynamic() {
1088 return false;
1089 }
1091 /** find a symbol that this (proxy method) symbol implements.
1092 * @param c The class whose members are searched for
1093 * implementations
1094 */
1095 public Symbol implemented(TypeSymbol c, Types types) {
1096 Symbol impl = null;
1097 for (List<Type> is = types.interfaces(c.type);
1098 impl == null && is.nonEmpty();
1099 is = is.tail) {
1100 TypeSymbol i = is.head.tsym;
1101 impl = implementedIn(i, types);
1102 if (impl == null)
1103 impl = implemented(i, types);
1104 }
1105 return impl;
1106 }
1108 public Symbol implementedIn(TypeSymbol c, Types types) {
1109 Symbol impl = null;
1110 for (Scope.Entry e = c.members().lookup(name);
1111 impl == null && e.scope != null;
1112 e = e.next()) {
1113 if (this.overrides(e.sym, (TypeSymbol)owner, types, true) &&
1114 // FIXME: I suspect the following requires a
1115 // subst() for a parametric return type.
1116 types.isSameType(type.getReturnType(),
1117 types.memberType(owner.type, e.sym).getReturnType())) {
1118 impl = e.sym;
1119 }
1120 }
1121 return impl;
1122 }
1124 /** Will the erasure of this method be considered by the VM to
1125 * override the erasure of the other when seen from class `origin'?
1126 */
1127 public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
1128 if (isConstructor() || _other.kind != MTH) return false;
1130 if (this == _other) return true;
1131 MethodSymbol other = (MethodSymbol)_other;
1133 // check for a direct implementation
1134 if (other.isOverridableIn((TypeSymbol)owner) &&
1135 types.asSuper(owner.type, other.owner) != null &&
1136 types.isSameType(erasure(types), other.erasure(types)))
1137 return true;
1139 // check for an inherited implementation
1140 return
1141 (flags() & ABSTRACT) == 0 &&
1142 other.isOverridableIn(origin) &&
1143 this.isMemberOf(origin, types) &&
1144 types.isSameType(erasure(types), other.erasure(types));
1145 }
1147 /** The implementation of this (abstract) symbol in class origin,
1148 * from the VM's point of view, null if method does not have an
1149 * implementation in class.
1150 * @param origin The class of which the implementation is a member.
1151 */
1152 public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
1153 for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
1154 for (Scope.Entry e = c.members().lookup(name);
1155 e.scope != null;
1156 e = e.next()) {
1157 if (e.sym.kind == MTH &&
1158 ((MethodSymbol)e.sym).binaryOverrides(this, origin, types))
1159 return (MethodSymbol)e.sym;
1160 }
1161 }
1162 return null;
1163 }
1165 /** Does this symbol override `other' symbol, when both are seen as
1166 * members of class `origin'? It is assumed that _other is a member
1167 * of origin.
1168 *
1169 * It is assumed that both symbols have the same name. The static
1170 * modifier is ignored for this test.
1171 *
1172 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
1173 */
1174 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
1175 if (isConstructor() || _other.kind != MTH) return false;
1177 if (this == _other) return true;
1178 MethodSymbol other = (MethodSymbol)_other;
1180 // check for a direct implementation
1181 if (other.isOverridableIn((TypeSymbol)owner) &&
1182 types.asSuper(owner.type, other.owner) != null) {
1183 Type mt = types.memberType(owner.type, this);
1184 Type ot = types.memberType(owner.type, other);
1185 if (types.isSubSignature(mt, ot)) {
1186 if (!checkResult)
1187 return true;
1188 if (types.returnTypeSubstitutable(mt, ot))
1189 return true;
1190 }
1191 }
1193 // check for an inherited implementation
1194 if ((flags() & ABSTRACT) != 0 ||
1195 (other.flags() & ABSTRACT) == 0 ||
1196 !other.isOverridableIn(origin) ||
1197 !this.isMemberOf(origin, types))
1198 return false;
1200 // assert types.asSuper(origin.type, other.owner) != null;
1201 Type mt = types.memberType(origin.type, this);
1202 Type ot = types.memberType(origin.type, other);
1203 return
1204 types.isSubSignature(mt, ot) &&
1205 (!checkResult || types.resultSubtype(mt, ot, Warner.noWarnings));
1206 }
1208 private boolean isOverridableIn(TypeSymbol origin) {
1209 // JLS 8.4.6.1
1210 switch ((int)(flags_field & Flags.AccessFlags)) {
1211 case Flags.PRIVATE:
1212 return false;
1213 case Flags.PUBLIC:
1214 return true;
1215 case Flags.PROTECTED:
1216 return (origin.flags() & INTERFACE) == 0;
1217 case 0:
1218 // for package private: can only override in the same
1219 // package
1220 return
1221 this.packge() == origin.packge() &&
1222 (origin.flags() & INTERFACE) == 0;
1223 default:
1224 return false;
1225 }
1226 }
1228 /** The implementation of this (abstract) symbol in class origin;
1229 * null if none exists. Synthetic methods are not considered
1230 * as possible implementations.
1231 */
1232 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
1233 return implementation(origin, types, checkResult, implementation_filter);
1234 }
1235 // where
1236 private static final Filter<Symbol> implementation_filter = new Filter<Symbol>() {
1237 public boolean accepts(Symbol s) {
1238 return s.kind == Kinds.MTH &&
1239 (s.flags() & SYNTHETIC) == 0;
1240 }
1241 };
1243 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) {
1244 MethodSymbol res = types.implementation(this, origin, checkResult, implFilter);
1245 if (res != null)
1246 return res;
1247 // if origin is derived from a raw type, we might have missed
1248 // an implementation because we do not know enough about instantiations.
1249 // in this case continue with the supertype as origin.
1250 if (types.isDerivedRaw(origin.type) && !origin.isInterface())
1251 return implementation(types.supertype(origin.type).tsym, types, checkResult);
1252 else
1253 return null;
1254 }
1256 public List<VarSymbol> params() {
1257 owner.complete();
1258 if (params == null) {
1259 // If ClassReader.saveParameterNames has been set true, then
1260 // savedParameterNames will be set to a list of names that
1261 // matches the types in type.getParameterTypes(). If any names
1262 // were not found in the class file, those names in the list will
1263 // be set to the empty name.
1264 // If ClassReader.saveParameterNames has been set false, then
1265 // savedParameterNames will be null.
1266 List<Name> paramNames = savedParameterNames;
1267 savedParameterNames = null;
1268 // discard the provided names if the list of names is the wrong size.
1269 if (paramNames == null || paramNames.size() != type.getParameterTypes().size())
1270 paramNames = List.nil();
1271 ListBuffer<VarSymbol> buf = new ListBuffer<VarSymbol>();
1272 List<Name> remaining = paramNames;
1273 // assert: remaining and paramNames are both empty or both
1274 // have same cardinality as type.getParameterTypes()
1275 int i = 0;
1276 for (Type t : type.getParameterTypes()) {
1277 Name paramName;
1278 if (remaining.isEmpty()) {
1279 // no names for any parameters available
1280 paramName = createArgName(i, paramNames);
1281 } else {
1282 paramName = remaining.head;
1283 remaining = remaining.tail;
1284 if (paramName.isEmpty()) {
1285 // no name for this specific parameter
1286 paramName = createArgName(i, paramNames);
1287 }
1288 }
1289 buf.append(new VarSymbol(PARAMETER, paramName, t, this));
1290 i++;
1291 }
1292 params = buf.toList();
1293 }
1294 return params;
1295 }
1297 // Create a name for the argument at position 'index' that is not in
1298 // the exclude list. In normal use, either no names will have been
1299 // provided, in which case the exclude list is empty, or all the names
1300 // will have been provided, in which case this method will not be called.
1301 private Name createArgName(int index, List<Name> exclude) {
1302 String prefix = "arg";
1303 while (true) {
1304 Name argName = name.table.fromString(prefix + index);
1305 if (!exclude.contains(argName))
1306 return argName;
1307 prefix += "$";
1308 }
1309 }
1311 public Symbol asMemberOf(Type site, Types types) {
1312 return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
1313 }
1315 public ElementKind getKind() {
1316 if (name == name.table.names.init)
1317 return ElementKind.CONSTRUCTOR;
1318 else if (name == name.table.names.clinit)
1319 return ElementKind.STATIC_INIT;
1320 else if ((flags() & BLOCK) != 0)
1321 return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT;
1322 else
1323 return ElementKind.METHOD;
1324 }
1326 public boolean isStaticOrInstanceInit() {
1327 return getKind() == ElementKind.STATIC_INIT ||
1328 getKind() == ElementKind.INSTANCE_INIT;
1329 }
1331 /**
1332 * A polymorphic signature method (JLS SE 7, 8.4.1) is a method that
1333 * (i) is declared in the java.lang.invoke.MethodHandle class, (ii) takes
1334 * a single variable arity parameter (iii) whose declared type is Object[],
1335 * (iv) has a return type of Object and (v) is native.
1336 */
1337 public boolean isSignaturePolymorphic(Types types) {
1338 List<Type> argtypes = type.getParameterTypes();
1339 Type firstElemType = argtypes.nonEmpty() ?
1340 types.elemtype(argtypes.head) :
1341 null;
1342 return owner == types.syms.methodHandleType.tsym &&
1343 argtypes.length() == 1 &&
1344 firstElemType != null &&
1345 types.isSameType(firstElemType, types.syms.objectType) &&
1346 types.isSameType(type.getReturnType(), types.syms.objectType) &&
1347 (flags() & NATIVE) != 0;
1348 }
1350 public Attribute getDefaultValue() {
1351 return defaultValue;
1352 }
1354 public List<VarSymbol> getParameters() {
1355 return params();
1356 }
1358 public boolean isVarArgs() {
1359 return (flags() & VARARGS) != 0;
1360 }
1362 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1363 return v.visitExecutable(this, p);
1364 }
1366 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1367 return v.visitMethodSymbol(this, p);
1368 }
1370 public Type getReturnType() {
1371 return asType().getReturnType();
1372 }
1374 public List<Type> getThrownTypes() {
1375 return asType().getThrownTypes();
1376 }
1377 }
1379 /** A class for invokedynamic method calls.
1380 */
1381 public static class DynamicMethodSymbol extends MethodSymbol {
1383 public Object[] staticArgs;
1384 public Symbol bsm;
1385 public int bsmKind;
1387 public DynamicMethodSymbol(Name name, Symbol owner, int bsmKind, MethodSymbol bsm, Type type, Object[] staticArgs) {
1388 super(0, name, type, owner);
1389 this.bsm = bsm;
1390 this.bsmKind = bsmKind;
1391 this.staticArgs = staticArgs;
1392 }
1394 @Override
1395 public boolean isDynamic() {
1396 return true;
1397 }
1398 }
1400 /** A class for predefined operators.
1401 */
1402 public static class OperatorSymbol extends MethodSymbol {
1404 public int opcode;
1406 public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
1407 super(PUBLIC | STATIC, name, type, owner);
1408 this.opcode = opcode;
1409 }
1411 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1412 return v.visitOperatorSymbol(this, p);
1413 }
1414 }
1416 /** Symbol completer interface.
1417 */
1418 public static interface Completer {
1419 void complete(Symbol sym) throws CompletionFailure;
1420 }
1422 public static class CompletionFailure extends RuntimeException {
1423 private static final long serialVersionUID = 0;
1424 public Symbol sym;
1426 /** A diagnostic object describing the failure
1427 */
1428 public JCDiagnostic diag;
1430 /** A localized string describing the failure.
1431 * @deprecated Use {@code getDetail()} or {@code getMessage()}
1432 */
1433 @Deprecated
1434 public String errmsg;
1436 public CompletionFailure(Symbol sym, String errmsg) {
1437 this.sym = sym;
1438 this.errmsg = errmsg;
1439 // this.printStackTrace();//DEBUG
1440 }
1442 public CompletionFailure(Symbol sym, JCDiagnostic diag) {
1443 this.sym = sym;
1444 this.diag = diag;
1445 // this.printStackTrace();//DEBUG
1446 }
1448 public JCDiagnostic getDiagnostic() {
1449 return diag;
1450 }
1452 @Override
1453 public String getMessage() {
1454 if (diag != null)
1455 return diag.getMessage(null);
1456 else
1457 return errmsg;
1458 }
1460 public Object getDetailValue() {
1461 return (diag != null ? diag : errmsg);
1462 }
1464 @Override
1465 public CompletionFailure initCause(Throwable cause) {
1466 super.initCause(cause);
1467 return this;
1468 }
1470 }
1472 /**
1473 * A visitor for symbols. A visitor is used to implement operations
1474 * (or relations) on symbols. Most common operations on types are
1475 * binary relations and this interface is designed for binary
1476 * relations, that is, operations on the form
1477 * Symbol × P → R.
1478 * <!-- In plain text: Type x P -> R -->
1479 *
1480 * @param <R> the return type of the operation implemented by this
1481 * visitor; use Void if no return type is needed.
1482 * @param <P> the type of the second argument (the first being the
1483 * symbol itself) of the operation implemented by this visitor; use
1484 * Void if a second argument is not needed.
1485 */
1486 public interface Visitor<R,P> {
1487 R visitClassSymbol(ClassSymbol s, P arg);
1488 R visitMethodSymbol(MethodSymbol s, P arg);
1489 R visitPackageSymbol(PackageSymbol s, P arg);
1490 R visitOperatorSymbol(OperatorSymbol s, P arg);
1491 R visitVarSymbol(VarSymbol s, P arg);
1492 R visitTypeSymbol(TypeSymbol s, P arg);
1493 R visitSymbol(Symbol s, P arg);
1494 }
1495 }