Fri, 23 May 2014 16:27:22 +0100
8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
Reviewed-by: jjg
Contributed-by: vicente.romero@oracle.com, paul.govereau@oracle.com
1 /*
2 * Copyright (c) 1999, 2014, 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.lang.annotation.Annotation;
29 import java.lang.annotation.Inherited;
30 import java.util.Set;
31 import java.util.concurrent.Callable;
33 import javax.lang.model.element.*;
34 import javax.tools.JavaFileObject;
36 import com.sun.tools.javac.code.Type.*;
37 import com.sun.tools.javac.comp.Annotate;
38 import com.sun.tools.javac.comp.Attr;
39 import com.sun.tools.javac.comp.AttrContext;
40 import com.sun.tools.javac.comp.Env;
41 import com.sun.tools.javac.jvm.*;
42 import com.sun.tools.javac.util.*;
43 import com.sun.tools.javac.util.Name;
44 import static com.sun.tools.javac.code.Flags.*;
45 import static com.sun.tools.javac.code.Kinds.*;
46 import static com.sun.tools.javac.code.TypeTag.CLASS;
47 import static com.sun.tools.javac.code.TypeTag.FORALL;
48 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
49 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
51 /** Root class for Java symbols. It contains subclasses
52 * for specific sorts of symbols, such as variables, methods and operators,
53 * types, packages. Each subclass is represented as a static inner class
54 * inside Symbol.
55 *
56 * <p><b>This is NOT part of any supported API.
57 * If you write code that depends on this, you do so at your own risk.
58 * This code and its internal interfaces are subject to change or
59 * deletion without notice.</b>
60 */
61 public abstract class Symbol extends AnnoConstruct implements Element {
63 /** The kind of this symbol.
64 * @see Kinds
65 */
66 public int kind;
68 /** The flags of this symbol.
69 */
70 public long flags_field;
72 /** An accessor method for the flags of this symbol.
73 * Flags of class symbols should be accessed through the accessor
74 * method to make sure that the class symbol is loaded.
75 */
76 public long flags() { return flags_field; }
78 /** The name of this symbol in Utf8 representation.
79 */
80 public Name name;
82 /** The type of this symbol.
83 */
84 public Type type;
86 /** The owner of this symbol.
87 */
88 public Symbol owner;
90 /** The completer of this symbol.
91 */
92 public Completer completer;
94 /** A cache for the type erasure of this symbol.
95 */
96 public Type erasure_field;
98 // <editor-fold defaultstate="collapsed" desc="annotations">
100 /** The attributes of this symbol are contained in this
101 * SymbolMetadata. The SymbolMetadata instance is NOT immutable.
102 */
103 protected SymbolMetadata metadata;
106 /** An accessor method for the attributes of this symbol.
107 * Attributes of class symbols should be accessed through the accessor
108 * method to make sure that the class symbol is loaded.
109 */
110 public List<Attribute.Compound> getRawAttributes() {
111 return (metadata == null)
112 ? List.<Attribute.Compound>nil()
113 : metadata.getDeclarationAttributes();
114 }
116 /** An accessor method for the type attributes of this symbol.
117 * Attributes of class symbols should be accessed through the accessor
118 * method to make sure that the class symbol is loaded.
119 */
120 public List<Attribute.TypeCompound> getRawTypeAttributes() {
121 return (metadata == null)
122 ? List.<Attribute.TypeCompound>nil()
123 : metadata.getTypeAttributes();
124 }
126 /** Fetch a particular annotation from a symbol. */
127 public Attribute.Compound attribute(Symbol anno) {
128 for (Attribute.Compound a : getRawAttributes()) {
129 if (a.type.tsym == anno) return a;
130 }
131 return null;
132 }
134 public boolean annotationsPendingCompletion() {
135 return metadata == null ? false : metadata.pendingCompletion();
136 }
138 public void appendAttributes(List<Attribute.Compound> l) {
139 if (l.nonEmpty()) {
140 initedMetadata().append(l);
141 }
142 }
144 public void appendClassInitTypeAttributes(List<Attribute.TypeCompound> l) {
145 if (l.nonEmpty()) {
146 initedMetadata().appendClassInitTypeAttributes(l);
147 }
148 }
150 public void appendInitTypeAttributes(List<Attribute.TypeCompound> l) {
151 if (l.nonEmpty()) {
152 initedMetadata().appendInitTypeAttributes(l);
153 }
154 }
156 public void appendTypeAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.TypeCompound> ctx) {
157 initedMetadata().appendTypeAttributesWithCompletion(ctx);
158 }
160 public void appendUniqueTypeAttributes(List<Attribute.TypeCompound> l) {
161 if (l.nonEmpty()) {
162 initedMetadata().appendUniqueTypes(l);
163 }
164 }
166 public List<Attribute.TypeCompound> getClassInitTypeAttributes() {
167 return (metadata == null)
168 ? List.<Attribute.TypeCompound>nil()
169 : metadata.getClassInitTypeAttributes();
170 }
172 public List<Attribute.TypeCompound> getInitTypeAttributes() {
173 return (metadata == null)
174 ? List.<Attribute.TypeCompound>nil()
175 : metadata.getInitTypeAttributes();
176 }
178 public List<Attribute.Compound> getDeclarationAttributes() {
179 return (metadata == null)
180 ? List.<Attribute.Compound>nil()
181 : metadata.getDeclarationAttributes();
182 }
184 public boolean hasAnnotations() {
185 return (metadata != null && !metadata.isEmpty());
186 }
188 public boolean hasTypeAnnotations() {
189 return (metadata != null && !metadata.isTypesEmpty());
190 }
192 public void prependAttributes(List<Attribute.Compound> l) {
193 if (l.nonEmpty()) {
194 initedMetadata().prepend(l);
195 }
196 }
198 public void resetAnnotations() {
199 initedMetadata().reset();
200 }
202 public void setAttributes(Symbol other) {
203 if (metadata != null || other.metadata != null) {
204 initedMetadata().setAttributes(other.metadata);
205 }
206 }
208 public void setDeclarationAttributes(List<Attribute.Compound> a) {
209 if (metadata != null || a.nonEmpty()) {
210 initedMetadata().setDeclarationAttributes(a);
211 }
212 }
214 public void setDeclarationAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.Compound> ctx) {
215 initedMetadata().setDeclarationAttributesWithCompletion(ctx);
216 }
218 public void setTypeAttributes(List<Attribute.TypeCompound> a) {
219 if (metadata != null || a.nonEmpty()) {
220 if (metadata == null)
221 metadata = new SymbolMetadata(this);
222 metadata.setTypeAttributes(a);
223 }
224 }
226 private SymbolMetadata initedMetadata() {
227 if (metadata == null)
228 metadata = new SymbolMetadata(this);
229 return metadata;
230 }
232 /** This method is intended for debugging only. */
233 public SymbolMetadata getMetadata() {
234 return metadata;
235 }
237 // </editor-fold>
239 /** Construct a symbol with given kind, flags, name, type and owner.
240 */
241 public Symbol(int kind, long flags, Name name, Type type, Symbol owner) {
242 this.kind = kind;
243 this.flags_field = flags;
244 this.type = type;
245 this.owner = owner;
246 this.completer = null;
247 this.erasure_field = null;
248 this.name = name;
249 }
251 /** Clone this symbol with new owner.
252 * Legal only for fields and methods.
253 */
254 public Symbol clone(Symbol newOwner) {
255 throw new AssertionError();
256 }
258 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
259 return v.visitSymbol(this, p);
260 }
262 /** The Java source which this symbol represents.
263 * A description of this symbol; overrides Object.
264 */
265 public String toString() {
266 return name.toString();
267 }
269 /** A Java source description of the location of this symbol; used for
270 * error reporting.
271 *
272 * @return null if the symbol is a package or a toplevel class defined in
273 * the default package; otherwise, the owner symbol is returned
274 */
275 public Symbol location() {
276 if (owner.name == null || (owner.name.isEmpty() &&
277 (owner.flags() & BLOCK) == 0 && owner.kind != PCK && owner.kind != TYP)) {
278 return null;
279 }
280 return owner;
281 }
283 public Symbol location(Type site, Types types) {
284 if (owner.name == null || owner.name.isEmpty()) {
285 return location();
286 }
287 if (owner.type.hasTag(CLASS)) {
288 Type ownertype = types.asOuterSuper(site, owner);
289 if (ownertype != null) return ownertype.tsym;
290 }
291 return owner;
292 }
294 public Symbol baseSymbol() {
295 return this;
296 }
298 /** The symbol's erased type.
299 */
300 public Type erasure(Types types) {
301 if (erasure_field == null)
302 erasure_field = types.erasure(type);
303 return erasure_field;
304 }
306 /** The external type of a symbol. This is the symbol's erased type
307 * except for constructors of inner classes which get the enclosing
308 * instance class added as first argument.
309 */
310 public Type externalType(Types types) {
311 Type t = erasure(types);
312 if (name == name.table.names.init && owner.hasOuterInstance()) {
313 Type outerThisType = types.erasure(owner.type.getEnclosingType());
314 return new MethodType(t.getParameterTypes().prepend(outerThisType),
315 t.getReturnType(),
316 t.getThrownTypes(),
317 t.tsym);
318 } else {
319 return t;
320 }
321 }
323 public boolean isDeprecated() {
324 return (flags_field & DEPRECATED) != 0;
325 }
327 public boolean isStatic() {
328 return
329 (flags() & STATIC) != 0 ||
330 (owner.flags() & INTERFACE) != 0 && kind != MTH &&
331 name != name.table.names._this;
332 }
334 public boolean isInterface() {
335 return (flags() & INTERFACE) != 0;
336 }
338 public boolean isPrivate() {
339 return (flags_field & Flags.AccessFlags) == PRIVATE;
340 }
342 public boolean isEnum() {
343 return (flags() & ENUM) != 0;
344 }
346 /** Is this symbol declared (directly or indirectly) local
347 * to a method or variable initializer?
348 * Also includes fields of inner classes which are in
349 * turn local to a method or variable initializer.
350 */
351 public boolean isLocal() {
352 return
353 (owner.kind & (VAR | MTH)) != 0 ||
354 (owner.kind == TYP && owner.isLocal());
355 }
357 /** Has this symbol an empty name? This includes anonymous
358 * inner classes.
359 */
360 public boolean isAnonymous() {
361 return name.isEmpty();
362 }
364 /** Is this symbol a constructor?
365 */
366 public boolean isConstructor() {
367 return name == name.table.names.init;
368 }
370 /** The fully qualified name of this symbol.
371 * This is the same as the symbol's name except for class symbols,
372 * which are handled separately.
373 */
374 public Name getQualifiedName() {
375 return name;
376 }
378 /** The fully qualified name of this symbol after converting to flat
379 * representation. This is the same as the symbol's name except for
380 * class symbols, which are handled separately.
381 */
382 public Name flatName() {
383 return getQualifiedName();
384 }
386 /** If this is a class or package, its members, otherwise null.
387 */
388 public Scope members() {
389 return null;
390 }
392 /** A class is an inner class if it it has an enclosing instance class.
393 */
394 public boolean isInner() {
395 return kind == TYP && type.getEnclosingType().hasTag(CLASS);
396 }
398 /** An inner class has an outer instance if it is not an interface
399 * it has an enclosing instance class which might be referenced from the class.
400 * Nested classes can see instance members of their enclosing class.
401 * Their constructors carry an additional this$n parameter, inserted
402 * implicitly by the compiler.
403 *
404 * @see #isInner
405 */
406 public boolean hasOuterInstance() {
407 return
408 type.getEnclosingType().hasTag(CLASS) && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
409 }
411 /** The closest enclosing class of this symbol's declaration.
412 */
413 public ClassSymbol enclClass() {
414 Symbol c = this;
415 while (c != null &&
416 ((c.kind & TYP) == 0 || !c.type.hasTag(CLASS))) {
417 c = c.owner;
418 }
419 return (ClassSymbol)c;
420 }
422 /** The outermost class which indirectly owns this symbol.
423 */
424 public ClassSymbol outermostClass() {
425 Symbol sym = this;
426 Symbol prev = null;
427 while (sym.kind != PCK) {
428 prev = sym;
429 sym = sym.owner;
430 }
431 return (ClassSymbol) prev;
432 }
434 /** The package which indirectly owns this symbol.
435 */
436 public PackageSymbol packge() {
437 Symbol sym = this;
438 while (sym.kind != PCK) {
439 sym = sym.owner;
440 }
441 return (PackageSymbol) sym;
442 }
444 /** Is this symbol a subclass of `base'? Only defined for ClassSymbols.
445 */
446 public boolean isSubClass(Symbol base, Types types) {
447 throw new AssertionError("isSubClass " + this);
448 }
450 /** Fully check membership: hierarchy, protection, and hiding.
451 * Does not exclude methods not inherited due to overriding.
452 */
453 public boolean isMemberOf(TypeSymbol clazz, Types types) {
454 return
455 owner == clazz ||
456 clazz.isSubClass(owner, types) &&
457 isInheritedIn(clazz, types) &&
458 !hiddenIn((ClassSymbol)clazz, types);
459 }
461 /** Is this symbol the same as or enclosed by the given class? */
462 public boolean isEnclosedBy(ClassSymbol clazz) {
463 for (Symbol sym = this; sym.kind != PCK; sym = sym.owner)
464 if (sym == clazz) return true;
465 return false;
466 }
468 private boolean hiddenIn(ClassSymbol clazz, Types types) {
469 Symbol sym = hiddenInInternal(clazz, types);
470 Assert.check(sym != null, "the result of hiddenInInternal() can't be null");
471 /* If we find the current symbol then there is no symbol hiding it
472 */
473 return sym != this;
474 }
476 /** This method looks in the supertypes graph that has the current class as the
477 * initial node, till it finds the current symbol or another symbol that hides it.
478 * If the current class has more than one supertype (extends one class and
479 * implements one or more interfaces) then null can be returned, meaning that
480 * a wrong path in the supertypes graph was selected. Null can only be returned
481 * as a temporary value, as a result of the recursive call.
482 */
483 private Symbol hiddenInInternal(ClassSymbol currentClass, Types types) {
484 if (currentClass == owner) {
485 return this;
486 }
487 Scope.Entry e = currentClass.members().lookup(name);
488 while (e.scope != null) {
489 if (e.sym.kind == kind &&
490 (kind != MTH ||
491 (e.sym.flags() & STATIC) != 0 &&
492 types.isSubSignature(e.sym.type, type))) {
493 return e.sym;
494 }
495 e = e.next();
496 }
497 Symbol hiddenSym = null;
498 for (Type st : types.interfaces(currentClass.type)
499 .prepend(types.supertype(currentClass.type))) {
500 if (st != null && (st.hasTag(CLASS))) {
501 Symbol sym = hiddenInInternal((ClassSymbol)st.tsym, types);
502 if (sym == this) {
503 return this;
504 } else if (sym != null) {
505 hiddenSym = sym;
506 }
507 }
508 }
509 return hiddenSym;
510 }
512 /** Is this symbol inherited into a given class?
513 * PRE: If symbol's owner is a interface,
514 * it is already assumed that the interface is a superinterface
515 * of given class.
516 * @param clazz The class for which we want to establish membership.
517 * This must be a subclass of the member's owner.
518 */
519 public boolean isInheritedIn(Symbol clazz, Types types) {
520 switch ((int)(flags_field & Flags.AccessFlags)) {
521 default: // error recovery
522 case PUBLIC:
523 return true;
524 case PRIVATE:
525 return this.owner == clazz;
526 case PROTECTED:
527 // we model interfaces as extending Object
528 return (clazz.flags() & INTERFACE) == 0;
529 case 0:
530 PackageSymbol thisPackage = this.packge();
531 for (Symbol sup = clazz;
532 sup != null && sup != this.owner;
533 sup = types.supertype(sup.type).tsym) {
534 while (sup.type.hasTag(TYPEVAR))
535 sup = sup.type.getUpperBound().tsym;
536 if (sup.type.isErroneous())
537 return true; // error recovery
538 if ((sup.flags() & COMPOUND) != 0)
539 continue;
540 if (sup.packge() != thisPackage)
541 return false;
542 }
543 return (clazz.flags() & INTERFACE) == 0;
544 }
545 }
547 /** The (variable or method) symbol seen as a member of given
548 * class type`site' (this might change the symbol's type).
549 * This is used exclusively for producing diagnostics.
550 */
551 public Symbol asMemberOf(Type site, Types types) {
552 throw new AssertionError();
553 }
555 /** Does this method symbol override `other' symbol, when both are seen as
556 * members of class `origin'? It is assumed that _other is a member
557 * of origin.
558 *
559 * It is assumed that both symbols have the same name. The static
560 * modifier is ignored for this test.
561 *
562 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
563 */
564 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
565 return false;
566 }
568 /** Complete the elaboration of this symbol's definition.
569 */
570 public void complete() throws CompletionFailure {
571 if (completer != null) {
572 Completer c = completer;
573 completer = null;
574 c.complete(this);
575 }
576 }
578 /** True if the symbol represents an entity that exists.
579 */
580 public boolean exists() {
581 return true;
582 }
584 public Type asType() {
585 return type;
586 }
588 public Symbol getEnclosingElement() {
589 return owner;
590 }
592 public ElementKind getKind() {
593 return ElementKind.OTHER; // most unkind
594 }
596 public Set<Modifier> getModifiers() {
597 return Flags.asModifierSet(flags());
598 }
600 public Name getSimpleName() {
601 return name;
602 }
604 /**
605 * This is the implementation for {@code
606 * javax.lang.model.element.Element.getAnnotationMirrors()}.
607 */
608 @Override
609 public List<Attribute.Compound> getAnnotationMirrors() {
610 return getRawAttributes();
611 }
614 // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList
615 public java.util.List<Symbol> getEnclosedElements() {
616 return List.nil();
617 }
619 public List<TypeVariableSymbol> getTypeParameters() {
620 ListBuffer<TypeVariableSymbol> l = new ListBuffer<>();
621 for (Type t : type.getTypeArguments()) {
622 Assert.check(t.tsym.getKind() == ElementKind.TYPE_PARAMETER);
623 l.append((TypeVariableSymbol)t.tsym);
624 }
625 return l.toList();
626 }
628 public static class DelegatedSymbol<T extends Symbol> extends Symbol {
629 protected T other;
630 public DelegatedSymbol(T other) {
631 super(other.kind, other.flags_field, other.name, other.type, other.owner);
632 this.other = other;
633 }
634 public String toString() { return other.toString(); }
635 public Symbol location() { return other.location(); }
636 public Symbol location(Type site, Types types) { return other.location(site, types); }
637 public Symbol baseSymbol() { return other; }
638 public Type erasure(Types types) { return other.erasure(types); }
639 public Type externalType(Types types) { return other.externalType(types); }
640 public boolean isLocal() { return other.isLocal(); }
641 public boolean isConstructor() { return other.isConstructor(); }
642 public Name getQualifiedName() { return other.getQualifiedName(); }
643 public Name flatName() { return other.flatName(); }
644 public Scope members() { return other.members(); }
645 public boolean isInner() { return other.isInner(); }
646 public boolean hasOuterInstance() { return other.hasOuterInstance(); }
647 public ClassSymbol enclClass() { return other.enclClass(); }
648 public ClassSymbol outermostClass() { return other.outermostClass(); }
649 public PackageSymbol packge() { return other.packge(); }
650 public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); }
651 public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); }
652 public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); }
653 public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); }
654 public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); }
655 public void complete() throws CompletionFailure { other.complete(); }
657 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
658 return other.accept(v, p);
659 }
661 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
662 return v.visitSymbol(other, p);
663 }
665 public T getUnderlyingSymbol() {
666 return other;
667 }
668 }
670 /** A base class for Symbols representing types.
671 */
672 public static abstract class TypeSymbol extends Symbol {
673 public TypeSymbol(int kind, long flags, Name name, Type type, Symbol owner) {
674 super(kind, flags, name, type, owner);
675 }
676 /** form a fully qualified name from a name and an owner
677 */
678 static public Name formFullName(Name name, Symbol owner) {
679 if (owner == null) return name;
680 if (((owner.kind != ERR)) &&
681 ((owner.kind & (VAR | MTH)) != 0
682 || (owner.kind == TYP && owner.type.hasTag(TYPEVAR))
683 )) return name;
684 Name prefix = owner.getQualifiedName();
685 if (prefix == null || prefix == prefix.table.names.empty)
686 return name;
687 else return prefix.append('.', name);
688 }
690 /** form a fully qualified name from a name and an owner, after
691 * converting to flat representation
692 */
693 static public Name formFlatName(Name name, Symbol owner) {
694 if (owner == null ||
695 (owner.kind & (VAR | MTH)) != 0
696 || (owner.kind == TYP && owner.type.hasTag(TYPEVAR))
697 ) return name;
698 char sep = owner.kind == TYP ? '$' : '.';
699 Name prefix = owner.flatName();
700 if (prefix == null || prefix == prefix.table.names.empty)
701 return name;
702 else return prefix.append(sep, name);
703 }
705 /**
706 * A partial ordering between type symbols that refines the
707 * class inheritance graph.
708 *
709 * Type variables always precede other kinds of symbols.
710 */
711 public final boolean precedes(TypeSymbol that, Types types) {
712 if (this == that)
713 return false;
714 if (type.hasTag(that.type.getTag())) {
715 if (type.hasTag(CLASS)) {
716 return
717 types.rank(that.type) < types.rank(this.type) ||
718 types.rank(that.type) == types.rank(this.type) &&
719 that.getQualifiedName().compareTo(this.getQualifiedName()) < 0;
720 } else if (type.hasTag(TYPEVAR)) {
721 return types.isSubtype(this.type, that.type);
722 }
723 }
724 return type.hasTag(TYPEVAR);
725 }
727 @Override
728 public java.util.List<Symbol> getEnclosedElements() {
729 List<Symbol> list = List.nil();
730 if (kind == TYP && type.hasTag(TYPEVAR)) {
731 return list;
732 }
733 for (Scope.Entry e = members().elems; e != null; e = e.sibling) {
734 if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this)
735 list = list.prepend(e.sym);
736 }
737 return list;
738 }
740 @Override
741 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
742 return v.visitTypeSymbol(this, p);
743 }
744 }
746 /**
747 * Type variables are represented by instances of this class.
748 */
749 public static class TypeVariableSymbol
750 extends TypeSymbol implements TypeParameterElement {
752 public TypeVariableSymbol(long flags, Name name, Type type, Symbol owner) {
753 super(TYP, flags, name, type, owner);
754 }
756 public ElementKind getKind() {
757 return ElementKind.TYPE_PARAMETER;
758 }
760 @Override
761 public Symbol getGenericElement() {
762 return owner;
763 }
765 public List<Type> getBounds() {
766 TypeVar t = (TypeVar)type;
767 Type bound = t.getUpperBound();
768 if (!bound.isCompound())
769 return List.of(bound);
770 ClassType ct = (ClassType)bound;
771 if (!ct.tsym.erasure_field.isInterface()) {
772 return ct.interfaces_field.prepend(ct.supertype_field);
773 } else {
774 // No superclass was given in bounds.
775 // In this case, supertype is Object, erasure is first interface.
776 return ct.interfaces_field;
777 }
778 }
780 @Override
781 public List<Attribute.Compound> getAnnotationMirrors() {
782 return onlyTypeVariableAnnotations(owner.getRawTypeAttributes());
783 }
785 private List<Attribute.Compound> onlyTypeVariableAnnotations(
786 List<Attribute.TypeCompound> candidates) {
787 // Declaration annotations on TypeParameters are stored in type attributes
788 List<Attribute.Compound> res = List.nil();
789 for (Attribute.TypeCompound a : candidates) {
790 if (a.position.type == TargetType.CLASS_TYPE_PARAMETER ||
791 a.position.type == TargetType.METHOD_TYPE_PARAMETER)
792 res = res.prepend(a);
793 }
795 return res = res.reverse();
796 }
800 // Helper to getAnnotation[s]
801 @Override
802 public <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
804 String name = annoType.getName();
806 // Declaration annotations on type variables are stored in type attributes
807 // on the owner of the TypeVariableSymbol
808 List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
809 for (Attribute.TypeCompound anno : candidates)
810 if (anno.position.type == TargetType.CLASS_TYPE_PARAMETER ||
811 anno.position.type == TargetType.METHOD_TYPE_PARAMETER)
812 if (name.contentEquals(anno.type.tsym.flatName()))
813 return anno;
815 return null;
816 }
820 @Override
821 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
822 return v.visitTypeParameter(this, p);
823 }
824 }
826 /** A class for package symbols
827 */
828 public static class PackageSymbol extends TypeSymbol
829 implements PackageElement {
831 public Scope members_field;
832 public Name fullname;
833 public ClassSymbol package_info; // see bug 6443073
835 public PackageSymbol(Name name, Type type, Symbol owner) {
836 super(PCK, 0, name, type, owner);
837 this.members_field = null;
838 this.fullname = formFullName(name, owner);
839 }
841 public PackageSymbol(Name name, Symbol owner) {
842 this(name, null, owner);
843 this.type = new PackageType(this);
844 }
846 public String toString() {
847 return fullname.toString();
848 }
850 public Name getQualifiedName() {
851 return fullname;
852 }
854 public boolean isUnnamed() {
855 return name.isEmpty() && owner != null;
856 }
858 public Scope members() {
859 if (completer != null) complete();
860 return members_field;
861 }
863 public long flags() {
864 if (completer != null) complete();
865 return flags_field;
866 }
868 @Override
869 public List<Attribute.Compound> getRawAttributes() {
870 if (completer != null) complete();
871 if (package_info != null && package_info.completer != null) {
872 package_info.complete();
873 mergeAttributes();
874 }
875 return super.getRawAttributes();
876 }
878 private void mergeAttributes() {
879 if (metadata == null &&
880 package_info.metadata != null) {
881 metadata = new SymbolMetadata(this);
882 metadata.setAttributes(package_info.metadata);
883 }
884 }
886 /** A package "exists" if a type or package that exists has
887 * been seen within it.
888 */
889 public boolean exists() {
890 return (flags_field & EXISTS) != 0;
891 }
893 public ElementKind getKind() {
894 return ElementKind.PACKAGE;
895 }
897 public Symbol getEnclosingElement() {
898 return null;
899 }
901 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
902 return v.visitPackage(this, p);
903 }
905 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
906 return v.visitPackageSymbol(this, p);
907 }
908 }
910 /** A class for class symbols
911 */
912 public static class ClassSymbol extends TypeSymbol implements TypeElement {
914 /** a scope for all class members; variables, methods and inner classes
915 * type parameters are not part of this scope
916 */
917 public Scope members_field;
919 /** the fully qualified name of the class, i.e. pck.outer.inner.
920 * null for anonymous classes
921 */
922 public Name fullname;
924 /** the fully qualified name of the class after converting to flat
925 * representation, i.e. pck.outer$inner,
926 * set externally for local and anonymous classes
927 */
928 public Name flatname;
930 /** the sourcefile where the class came from
931 */
932 public JavaFileObject sourcefile;
934 /** the classfile from where to load this class
935 * this will have extension .class or .java
936 */
937 public JavaFileObject classfile;
939 /** the list of translated local classes (used for generating
940 * InnerClasses attribute)
941 */
942 public List<ClassSymbol> trans_local;
944 /** the constant pool of the class
945 */
946 public Pool pool;
948 public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
949 super(TYP, flags, name, type, owner);
950 this.members_field = null;
951 this.fullname = formFullName(name, owner);
952 this.flatname = formFlatName(name, owner);
953 this.sourcefile = null;
954 this.classfile = null;
955 this.pool = null;
956 }
958 public ClassSymbol(long flags, Name name, Symbol owner) {
959 this(
960 flags,
961 name,
962 new ClassType(Type.noType, null, null),
963 owner);
964 this.type.tsym = this;
965 }
967 /** The Java source which this symbol represents.
968 */
969 public String toString() {
970 return className();
971 }
973 public long flags() {
974 if (completer != null) complete();
975 return flags_field;
976 }
978 public Scope members() {
979 if (completer != null) complete();
980 return members_field;
981 }
983 @Override
984 public List<Attribute.Compound> getRawAttributes() {
985 if (completer != null) complete();
986 return super.getRawAttributes();
987 }
989 @Override
990 public List<Attribute.TypeCompound> getRawTypeAttributes() {
991 if (completer != null) complete();
992 return super.getRawTypeAttributes();
993 }
995 public Type erasure(Types types) {
996 if (erasure_field == null)
997 erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
998 List.<Type>nil(), this);
999 return erasure_field;
1000 }
1002 public String className() {
1003 if (name.isEmpty())
1004 return
1005 Log.getLocalizedString("anonymous.class", flatname);
1006 else
1007 return fullname.toString();
1008 }
1010 public Name getQualifiedName() {
1011 return fullname;
1012 }
1014 public Name flatName() {
1015 return flatname;
1016 }
1018 public boolean isSubClass(Symbol base, Types types) {
1019 if (this == base) {
1020 return true;
1021 } else if ((base.flags() & INTERFACE) != 0) {
1022 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t))
1023 for (List<Type> is = types.interfaces(t);
1024 is.nonEmpty();
1025 is = is.tail)
1026 if (is.head.tsym.isSubClass(base, types)) return true;
1027 } else {
1028 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t))
1029 if (t.tsym == base) return true;
1030 }
1031 return false;
1032 }
1034 /** Complete the elaboration of this symbol's definition.
1035 */
1036 public void complete() throws CompletionFailure {
1037 try {
1038 super.complete();
1039 } catch (CompletionFailure ex) {
1040 // quiet error recovery
1041 flags_field |= (PUBLIC|STATIC);
1042 this.type = new ErrorType(this, Type.noType);
1043 throw ex;
1044 }
1045 }
1047 public List<Type> getInterfaces() {
1048 complete();
1049 if (type instanceof ClassType) {
1050 ClassType t = (ClassType)type;
1051 if (t.interfaces_field == null) // FIXME: shouldn't be null
1052 t.interfaces_field = List.nil();
1053 if (t.all_interfaces_field != null)
1054 return Type.getModelTypes(t.all_interfaces_field);
1055 return t.interfaces_field;
1056 } else {
1057 return List.nil();
1058 }
1059 }
1061 public Type getSuperclass() {
1062 complete();
1063 if (type instanceof ClassType) {
1064 ClassType t = (ClassType)type;
1065 if (t.supertype_field == null) // FIXME: shouldn't be null
1066 t.supertype_field = Type.noType;
1067 // An interface has no superclass; its supertype is Object.
1068 return t.isInterface()
1069 ? Type.noType
1070 : t.supertype_field.getModelType();
1071 } else {
1072 return Type.noType;
1073 }
1074 }
1076 /**
1077 * Returns the next class to search for inherited annotations or {@code null}
1078 * if the next class can't be found.
1079 */
1080 private ClassSymbol getSuperClassToSearchForAnnotations() {
1082 Type sup = getSuperclass();
1084 if (!sup.hasTag(CLASS) || sup.isErroneous())
1085 return null;
1087 return (ClassSymbol) sup.tsym;
1088 }
1091 @Override
1092 protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
1094 ClassSymbol sup = getSuperClassToSearchForAnnotations();
1096 return sup == null ? super.getInheritedAnnotations(annoType)
1097 : sup.getAnnotationsByType(annoType);
1098 }
1101 public ElementKind getKind() {
1102 long flags = flags();
1103 if ((flags & ANNOTATION) != 0)
1104 return ElementKind.ANNOTATION_TYPE;
1105 else if ((flags & INTERFACE) != 0)
1106 return ElementKind.INTERFACE;
1107 else if ((flags & ENUM) != 0)
1108 return ElementKind.ENUM;
1109 else
1110 return ElementKind.CLASS;
1111 }
1113 @Override
1114 public Set<Modifier> getModifiers() {
1115 long flags = flags();
1116 return Flags.asModifierSet(flags & ~DEFAULT);
1117 }
1119 public NestingKind getNestingKind() {
1120 complete();
1121 if (owner.kind == PCK)
1122 return NestingKind.TOP_LEVEL;
1123 else if (name.isEmpty())
1124 return NestingKind.ANONYMOUS;
1125 else if (owner.kind == MTH)
1126 return NestingKind.LOCAL;
1127 else
1128 return NestingKind.MEMBER;
1129 }
1132 @Override
1133 protected <A extends Annotation> Attribute.Compound getAttribute(final Class<A> annoType) {
1135 Attribute.Compound attrib = super.getAttribute(annoType);
1137 boolean inherited = annoType.isAnnotationPresent(Inherited.class);
1138 if (attrib != null || !inherited)
1139 return attrib;
1141 // Search supertypes
1142 ClassSymbol superType = getSuperClassToSearchForAnnotations();
1143 return superType == null ? null
1144 : superType.getAttribute(annoType);
1145 }
1150 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1151 return v.visitType(this, p);
1152 }
1154 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1155 return v.visitClassSymbol(this, p);
1156 }
1157 }
1160 /** A class for variable symbols
1161 */
1162 public static class VarSymbol extends Symbol implements VariableElement {
1164 /** The variable's declaration position.
1165 */
1166 public int pos = Position.NOPOS;
1168 /** The variable's address. Used for different purposes during
1169 * flow analysis, translation and code generation.
1170 * Flow analysis:
1171 * If this is a blank final or local variable, its sequence number.
1172 * Translation:
1173 * If this is a private field, its access number.
1174 * Code generation:
1175 * If this is a local variable, its logical slot number.
1176 */
1177 public int adr = -1;
1179 /** Construct a variable symbol, given its flags, name, type and owner.
1180 */
1181 public VarSymbol(long flags, Name name, Type type, Symbol owner) {
1182 super(VAR, flags, name, type, owner);
1183 }
1185 /** Clone this symbol with new owner.
1186 */
1187 public VarSymbol clone(Symbol newOwner) {
1188 VarSymbol v = new VarSymbol(flags_field, name, type, newOwner) {
1189 @Override
1190 public Symbol baseSymbol() {
1191 return VarSymbol.this;
1192 }
1193 };
1194 v.pos = pos;
1195 v.adr = adr;
1196 v.data = data;
1197 // System.out.println("clone " + v + " in " + newOwner);//DEBUG
1198 return v;
1199 }
1201 public String toString() {
1202 return name.toString();
1203 }
1205 public Symbol asMemberOf(Type site, Types types) {
1206 return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
1207 }
1209 public ElementKind getKind() {
1210 long flags = flags();
1211 if ((flags & PARAMETER) != 0) {
1212 if (isExceptionParameter())
1213 return ElementKind.EXCEPTION_PARAMETER;
1214 else
1215 return ElementKind.PARAMETER;
1216 } else if ((flags & ENUM) != 0) {
1217 return ElementKind.ENUM_CONSTANT;
1218 } else if (owner.kind == TYP || owner.kind == ERR) {
1219 return ElementKind.FIELD;
1220 } else if (isResourceVariable()) {
1221 return ElementKind.RESOURCE_VARIABLE;
1222 } else {
1223 return ElementKind.LOCAL_VARIABLE;
1224 }
1225 }
1227 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1228 return v.visitVariable(this, p);
1229 }
1231 public Object getConstantValue() { // Mirror API
1232 return Constants.decode(getConstValue(), type);
1233 }
1235 public void setLazyConstValue(final Env<AttrContext> env,
1236 final Attr attr,
1237 final JCVariableDecl variable)
1238 {
1239 setData(new Callable<Object>() {
1240 public Object call() {
1241 return attr.attribLazyConstantValue(env, variable, type);
1242 }
1243 });
1244 }
1246 /**
1247 * The variable's constant value, if this is a constant.
1248 * Before the constant value is evaluated, it points to an
1249 * initializer environment. If this is not a constant, it can
1250 * be used for other stuff.
1251 */
1252 private Object data;
1254 public boolean isExceptionParameter() {
1255 return data == ElementKind.EXCEPTION_PARAMETER;
1256 }
1258 public boolean isResourceVariable() {
1259 return data == ElementKind.RESOURCE_VARIABLE;
1260 }
1262 public Object getConstValue() {
1263 // TODO: Consider if getConstValue and getConstantValue can be collapsed
1264 if (data == ElementKind.EXCEPTION_PARAMETER ||
1265 data == ElementKind.RESOURCE_VARIABLE) {
1266 return null;
1267 } else if (data instanceof Callable<?>) {
1268 // In this case, this is a final variable, with an as
1269 // yet unevaluated initializer.
1270 Callable<?> eval = (Callable<?>)data;
1271 data = null; // to make sure we don't evaluate this twice.
1272 try {
1273 data = eval.call();
1274 } catch (Exception ex) {
1275 throw new AssertionError(ex);
1276 }
1277 }
1278 return data;
1279 }
1281 public void setData(Object data) {
1282 Assert.check(!(data instanceof Env<?>), this);
1283 this.data = data;
1284 }
1286 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1287 return v.visitVarSymbol(this, p);
1288 }
1289 }
1291 /** A class for method symbols.
1292 */
1293 public static class MethodSymbol extends Symbol implements ExecutableElement {
1295 /** The code of the method. */
1296 public Code code = null;
1298 /** The extra (synthetic/mandated) parameters of the method. */
1299 public List<VarSymbol> extraParams = List.nil();
1301 /** The captured local variables in an anonymous class */
1302 public List<VarSymbol> capturedLocals = List.nil();
1304 /** The parameters of the method. */
1305 public List<VarSymbol> params = null;
1307 /** The names of the parameters */
1308 public List<Name> savedParameterNames;
1310 /** For an attribute field accessor, its default value if any.
1311 * The value is null if none appeared in the method
1312 * declaration.
1313 */
1314 public Attribute defaultValue = null;
1316 /** Construct a method symbol, given its flags, name, type and owner.
1317 */
1318 public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
1319 super(MTH, flags, name, type, owner);
1320 if (owner.type.hasTag(TYPEVAR)) Assert.error(owner + "." + name);
1321 }
1323 /** Clone this symbol with new owner.
1324 */
1325 public MethodSymbol clone(Symbol newOwner) {
1326 MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner) {
1327 @Override
1328 public Symbol baseSymbol() {
1329 return MethodSymbol.this;
1330 }
1331 };
1332 m.code = code;
1333 return m;
1334 }
1336 @Override
1337 public Set<Modifier> getModifiers() {
1338 long flags = flags();
1339 return Flags.asModifierSet((flags & DEFAULT) != 0 ? flags & ~ABSTRACT : flags);
1340 }
1342 /** The Java source which this symbol represents.
1343 */
1344 public String toString() {
1345 if ((flags() & BLOCK) != 0) {
1346 return owner.name.toString();
1347 } else {
1348 String s = (name == name.table.names.init)
1349 ? owner.name.toString()
1350 : name.toString();
1351 if (type != null) {
1352 if (type.hasTag(FORALL))
1353 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
1354 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
1355 }
1356 return s;
1357 }
1358 }
1360 public boolean isDynamic() {
1361 return false;
1362 }
1364 /** find a symbol that this (proxy method) symbol implements.
1365 * @param c The class whose members are searched for
1366 * implementations
1367 */
1368 public Symbol implemented(TypeSymbol c, Types types) {
1369 Symbol impl = null;
1370 for (List<Type> is = types.interfaces(c.type);
1371 impl == null && is.nonEmpty();
1372 is = is.tail) {
1373 TypeSymbol i = is.head.tsym;
1374 impl = implementedIn(i, types);
1375 if (impl == null)
1376 impl = implemented(i, types);
1377 }
1378 return impl;
1379 }
1381 public Symbol implementedIn(TypeSymbol c, Types types) {
1382 Symbol impl = null;
1383 for (Scope.Entry e = c.members().lookup(name);
1384 impl == null && e.scope != null;
1385 e = e.next()) {
1386 if (this.overrides(e.sym, (TypeSymbol)owner, types, true) &&
1387 // FIXME: I suspect the following requires a
1388 // subst() for a parametric return type.
1389 types.isSameType(type.getReturnType(),
1390 types.memberType(owner.type, e.sym).getReturnType())) {
1391 impl = e.sym;
1392 }
1393 }
1394 return impl;
1395 }
1397 /** Will the erasure of this method be considered by the VM to
1398 * override the erasure of the other when seen from class `origin'?
1399 */
1400 public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
1401 if (isConstructor() || _other.kind != MTH) return false;
1403 if (this == _other) return true;
1404 MethodSymbol other = (MethodSymbol)_other;
1406 // check for a direct implementation
1407 if (other.isOverridableIn((TypeSymbol)owner) &&
1408 types.asSuper(owner.type, other.owner) != null &&
1409 types.isSameType(erasure(types), other.erasure(types)))
1410 return true;
1412 // check for an inherited implementation
1413 return
1414 (flags() & ABSTRACT) == 0 &&
1415 other.isOverridableIn(origin) &&
1416 this.isMemberOf(origin, types) &&
1417 types.isSameType(erasure(types), other.erasure(types));
1418 }
1420 /** The implementation of this (abstract) symbol in class origin,
1421 * from the VM's point of view, null if method does not have an
1422 * implementation in class.
1423 * @param origin The class of which the implementation is a member.
1424 */
1425 public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
1426 for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
1427 for (Scope.Entry e = c.members().lookup(name);
1428 e.scope != null;
1429 e = e.next()) {
1430 if (e.sym.kind == MTH &&
1431 ((MethodSymbol)e.sym).binaryOverrides(this, origin, types))
1432 return (MethodSymbol)e.sym;
1433 }
1434 }
1435 return null;
1436 }
1438 /** Does this symbol override `other' symbol, when both are seen as
1439 * members of class `origin'? It is assumed that _other is a member
1440 * of origin.
1441 *
1442 * It is assumed that both symbols have the same name. The static
1443 * modifier is ignored for this test.
1444 *
1445 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
1446 */
1447 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
1448 if (isConstructor() || _other.kind != MTH) return false;
1450 if (this == _other) return true;
1451 MethodSymbol other = (MethodSymbol)_other;
1453 // check for a direct implementation
1454 if (other.isOverridableIn((TypeSymbol)owner) &&
1455 types.asSuper(owner.type, other.owner) != null) {
1456 Type mt = types.memberType(owner.type, this);
1457 Type ot = types.memberType(owner.type, other);
1458 if (types.isSubSignature(mt, ot)) {
1459 if (!checkResult)
1460 return true;
1461 if (types.returnTypeSubstitutable(mt, ot))
1462 return true;
1463 }
1464 }
1466 // check for an inherited implementation
1467 if ((flags() & ABSTRACT) != 0 ||
1468 ((other.flags() & ABSTRACT) == 0 && (other.flags() & DEFAULT) == 0) ||
1469 !other.isOverridableIn(origin) ||
1470 !this.isMemberOf(origin, types))
1471 return false;
1473 // assert types.asSuper(origin.type, other.owner) != null;
1474 Type mt = types.memberType(origin.type, this);
1475 Type ot = types.memberType(origin.type, other);
1476 return
1477 types.isSubSignature(mt, ot) &&
1478 (!checkResult || types.resultSubtype(mt, ot, types.noWarnings));
1479 }
1481 private boolean isOverridableIn(TypeSymbol origin) {
1482 // JLS 8.4.6.1
1483 switch ((int)(flags_field & Flags.AccessFlags)) {
1484 case Flags.PRIVATE:
1485 return false;
1486 case Flags.PUBLIC:
1487 return !this.owner.isInterface() ||
1488 (flags_field & STATIC) == 0;
1489 case Flags.PROTECTED:
1490 return (origin.flags() & INTERFACE) == 0;
1491 case 0:
1492 // for package private: can only override in the same
1493 // package
1494 return
1495 this.packge() == origin.packge() &&
1496 (origin.flags() & INTERFACE) == 0;
1497 default:
1498 return false;
1499 }
1500 }
1502 @Override
1503 public boolean isInheritedIn(Symbol clazz, Types types) {
1504 switch ((int)(flags_field & Flags.AccessFlags)) {
1505 case PUBLIC:
1506 return !this.owner.isInterface() ||
1507 clazz == owner ||
1508 (flags_field & STATIC) == 0;
1509 default:
1510 return super.isInheritedIn(clazz, types);
1511 }
1512 }
1514 /** The implementation of this (abstract) symbol in class origin;
1515 * null if none exists. Synthetic methods are not considered
1516 * as possible implementations.
1517 */
1518 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
1519 return implementation(origin, types, checkResult, implementation_filter);
1520 }
1521 // where
1522 public static final Filter<Symbol> implementation_filter = new Filter<Symbol>() {
1523 public boolean accepts(Symbol s) {
1524 return s.kind == Kinds.MTH &&
1525 (s.flags() & SYNTHETIC) == 0;
1526 }
1527 };
1529 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) {
1530 MethodSymbol res = types.implementation(this, origin, checkResult, implFilter);
1531 if (res != null)
1532 return res;
1533 // if origin is derived from a raw type, we might have missed
1534 // an implementation because we do not know enough about instantiations.
1535 // in this case continue with the supertype as origin.
1536 if (types.isDerivedRaw(origin.type) && !origin.isInterface())
1537 return implementation(types.supertype(origin.type).tsym, types, checkResult);
1538 else
1539 return null;
1540 }
1542 public List<VarSymbol> params() {
1543 owner.complete();
1544 if (params == null) {
1545 // If ClassReader.saveParameterNames has been set true, then
1546 // savedParameterNames will be set to a list of names that
1547 // matches the types in type.getParameterTypes(). If any names
1548 // were not found in the class file, those names in the list will
1549 // be set to the empty name.
1550 // If ClassReader.saveParameterNames has been set false, then
1551 // savedParameterNames will be null.
1552 List<Name> paramNames = savedParameterNames;
1553 savedParameterNames = null;
1554 // discard the provided names if the list of names is the wrong size.
1555 if (paramNames == null || paramNames.size() != type.getParameterTypes().size()) {
1556 paramNames = List.nil();
1557 }
1558 ListBuffer<VarSymbol> buf = new ListBuffer<VarSymbol>();
1559 List<Name> remaining = paramNames;
1560 // assert: remaining and paramNames are both empty or both
1561 // have same cardinality as type.getParameterTypes()
1562 int i = 0;
1563 for (Type t : type.getParameterTypes()) {
1564 Name paramName;
1565 if (remaining.isEmpty()) {
1566 // no names for any parameters available
1567 paramName = createArgName(i, paramNames);
1568 } else {
1569 paramName = remaining.head;
1570 remaining = remaining.tail;
1571 if (paramName.isEmpty()) {
1572 // no name for this specific parameter
1573 paramName = createArgName(i, paramNames);
1574 }
1575 }
1576 buf.append(new VarSymbol(PARAMETER, paramName, t, this));
1577 i++;
1578 }
1579 params = buf.toList();
1580 }
1581 return params;
1582 }
1584 // Create a name for the argument at position 'index' that is not in
1585 // the exclude list. In normal use, either no names will have been
1586 // provided, in which case the exclude list is empty, or all the names
1587 // will have been provided, in which case this method will not be called.
1588 private Name createArgName(int index, List<Name> exclude) {
1589 String prefix = "arg";
1590 while (true) {
1591 Name argName = name.table.fromString(prefix + index);
1592 if (!exclude.contains(argName))
1593 return argName;
1594 prefix += "$";
1595 }
1596 }
1598 public Symbol asMemberOf(Type site, Types types) {
1599 return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
1600 }
1602 public ElementKind getKind() {
1603 if (name == name.table.names.init)
1604 return ElementKind.CONSTRUCTOR;
1605 else if (name == name.table.names.clinit)
1606 return ElementKind.STATIC_INIT;
1607 else if ((flags() & BLOCK) != 0)
1608 return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT;
1609 else
1610 return ElementKind.METHOD;
1611 }
1613 public boolean isStaticOrInstanceInit() {
1614 return getKind() == ElementKind.STATIC_INIT ||
1615 getKind() == ElementKind.INSTANCE_INIT;
1616 }
1618 public Attribute getDefaultValue() {
1619 return defaultValue;
1620 }
1622 public List<VarSymbol> getParameters() {
1623 return params();
1624 }
1626 public boolean isVarArgs() {
1627 return (flags() & VARARGS) != 0;
1628 }
1630 public boolean isDefault() {
1631 return (flags() & DEFAULT) != 0;
1632 }
1634 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1635 return v.visitExecutable(this, p);
1636 }
1638 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1639 return v.visitMethodSymbol(this, p);
1640 }
1642 public Type getReceiverType() {
1643 return asType().getReceiverType();
1644 }
1646 public Type getReturnType() {
1647 return asType().getReturnType();
1648 }
1650 public List<Type> getThrownTypes() {
1651 return asType().getThrownTypes();
1652 }
1653 }
1655 /** A class for invokedynamic method calls.
1656 */
1657 public static class DynamicMethodSymbol extends MethodSymbol {
1659 public Object[] staticArgs;
1660 public Symbol bsm;
1661 public int bsmKind;
1663 public DynamicMethodSymbol(Name name, Symbol owner, int bsmKind, MethodSymbol bsm, Type type, Object[] staticArgs) {
1664 super(0, name, type, owner);
1665 this.bsm = bsm;
1666 this.bsmKind = bsmKind;
1667 this.staticArgs = staticArgs;
1668 }
1670 @Override
1671 public boolean isDynamic() {
1672 return true;
1673 }
1674 }
1676 /** A class for predefined operators.
1677 */
1678 public static class OperatorSymbol extends MethodSymbol {
1680 public int opcode;
1682 public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
1683 super(PUBLIC | STATIC, name, type, owner);
1684 this.opcode = opcode;
1685 }
1687 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1688 return v.visitOperatorSymbol(this, p);
1689 }
1690 }
1692 /** Symbol completer interface.
1693 */
1694 public static interface Completer {
1695 void complete(Symbol sym) throws CompletionFailure;
1696 }
1698 public static class CompletionFailure extends RuntimeException {
1699 private static final long serialVersionUID = 0;
1700 public Symbol sym;
1702 /** A diagnostic object describing the failure
1703 */
1704 public JCDiagnostic diag;
1706 /** A localized string describing the failure.
1707 * @deprecated Use {@code getDetail()} or {@code getMessage()}
1708 */
1709 @Deprecated
1710 public String errmsg;
1712 public CompletionFailure(Symbol sym, String errmsg) {
1713 this.sym = sym;
1714 this.errmsg = errmsg;
1715 // this.printStackTrace();//DEBUG
1716 }
1718 public CompletionFailure(Symbol sym, JCDiagnostic diag) {
1719 this.sym = sym;
1720 this.diag = diag;
1721 // this.printStackTrace();//DEBUG
1722 }
1724 public JCDiagnostic getDiagnostic() {
1725 return diag;
1726 }
1728 @Override
1729 public String getMessage() {
1730 if (diag != null)
1731 return diag.getMessage(null);
1732 else
1733 return errmsg;
1734 }
1736 public Object getDetailValue() {
1737 return (diag != null ? diag : errmsg);
1738 }
1740 @Override
1741 public CompletionFailure initCause(Throwable cause) {
1742 super.initCause(cause);
1743 return this;
1744 }
1746 }
1748 /**
1749 * A visitor for symbols. A visitor is used to implement operations
1750 * (or relations) on symbols. Most common operations on types are
1751 * binary relations and this interface is designed for binary
1752 * relations, that is, operations on the form
1753 * Symbol × P → R.
1754 * <!-- In plain text: Type x P -> R -->
1755 *
1756 * @param <R> the return type of the operation implemented by this
1757 * visitor; use Void if no return type is needed.
1758 * @param <P> the type of the second argument (the first being the
1759 * symbol itself) of the operation implemented by this visitor; use
1760 * Void if a second argument is not needed.
1761 */
1762 public interface Visitor<R,P> {
1763 R visitClassSymbol(ClassSymbol s, P arg);
1764 R visitMethodSymbol(MethodSymbol s, P arg);
1765 R visitPackageSymbol(PackageSymbol s, P arg);
1766 R visitOperatorSymbol(OperatorSymbol s, P arg);
1767 R visitVarSymbol(VarSymbol s, P arg);
1768 R visitTypeSymbol(TypeSymbol s, P arg);
1769 R visitSymbol(Symbol s, P arg);
1770 }
1771 }