src/share/classes/com/sun/tools/javac/code/Symbol.java

Tue, 18 Mar 2014 22:12:46 +0000

author
vromero
date
Tue, 18 Mar 2014 22:12:46 +0000
changeset 2434
7de1481c6cd8
parent 2427
a3ad6e2ede44
child 2525
2eb010b6cb22
child 2717
11743872bfc9
permissions
-rw-r--r--

8036007: javac crashes when encountering an unresolvable interface
Reviewed-by: vromero, jlahoda
Contributed-by: paul.govereau@oracle.com

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

mercurial