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