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