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

Wed, 19 Mar 2014 16:44:49 +0000

author
vromero
date
Wed, 19 Mar 2014 16:44:49 +0000
changeset 2301
27a3026256cd
parent 2149
e5d3cd43c85e
child 2384
327122b01a9e
permissions
-rw-r--r--

8034924: Incorrect inheritance of inaccessible static method
Reviewed-by: jjg, jlahoda

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() {
jjg@1374 395 return 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 /**
duke@1 706 * A total ordering between type symbols that refines the
duke@1 707 * class inheritance graph.
duke@1 708 *
duke@1 709 * Typevariables 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() {
jfranck@1709 782 return onlyTypeVariableAnnotations(owner.getRawTypeAttributes());
jfranck@1709 783 }
jfranck@1709 784
jfranck@1709 785 private List<Attribute.Compound> onlyTypeVariableAnnotations(
jfranck@1709 786 List<Attribute.TypeCompound> candidates) {
jfranck@1709 787 // Declaration annotations on TypeParameters are stored in type attributes
jfranck@1709 788 List<Attribute.Compound> res = List.nil();
jfranck@1709 789 for (Attribute.TypeCompound a : candidates) {
jfranck@1709 790 if (a.position.type == TargetType.CLASS_TYPE_PARAMETER ||
jfranck@1709 791 a.position.type == TargetType.METHOD_TYPE_PARAMETER)
jfranck@1709 792 res = res.prepend(a);
jfranck@1709 793 }
jfranck@1709 794
jfranck@1709 795 return res = res.reverse();
jfranck@1709 796 }
jfranck@1709 797
alundblad@2100 798
alundblad@2100 799
alundblad@2100 800 // Helper to getAnnotation[s]
alundblad@2100 801 @Override
alundblad@2100 802 public <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
alundblad@2100 803
alundblad@2100 804 String name = annoType.getName();
alundblad@2100 805
alundblad@2100 806 // Declaration annotations on type variables are stored in type attributes
alundblad@2100 807 // on the owner of the TypeVariableSymbol
alundblad@2100 808 List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
alundblad@2100 809 for (Attribute.TypeCompound anno : candidates)
alundblad@2100 810 if (anno.position.type == TargetType.CLASS_TYPE_PARAMETER ||
alundblad@2100 811 anno.position.type == TargetType.METHOD_TYPE_PARAMETER)
alundblad@2100 812 if (name.contentEquals(anno.type.tsym.flatName()))
alundblad@2100 813 return anno;
alundblad@2100 814
alundblad@2100 815 return null;
alundblad@2100 816 }
alundblad@2100 817
alundblad@2100 818
alundblad@2100 819
jfranck@1709 820 @Override
jfranck@1689 821 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
jfranck@1689 822 return v.visitTypeParameter(this, p);
jfranck@1689 823 }
duke@1 824 }
duke@1 825
duke@1 826 /** A class for package symbols
duke@1 827 */
duke@1 828 public static class PackageSymbol extends TypeSymbol
duke@1 829 implements PackageElement {
duke@1 830
duke@1 831 public Scope members_field;
duke@1 832 public Name fullname;
duke@1 833 public ClassSymbol package_info; // see bug 6443073
duke@1 834
duke@1 835 public PackageSymbol(Name name, Type type, Symbol owner) {
jfranck@1689 836 super(PCK, 0, name, type, owner);
duke@1 837 this.members_field = null;
duke@1 838 this.fullname = formFullName(name, owner);
duke@1 839 }
duke@1 840
duke@1 841 public PackageSymbol(Name name, Symbol owner) {
duke@1 842 this(name, null, owner);
duke@1 843 this.type = new PackageType(this);
duke@1 844 }
duke@1 845
duke@1 846 public String toString() {
duke@1 847 return fullname.toString();
duke@1 848 }
duke@1 849
duke@1 850 public Name getQualifiedName() {
duke@1 851 return fullname;
duke@1 852 }
duke@1 853
duke@1 854 public boolean isUnnamed() {
duke@1 855 return name.isEmpty() && owner != null;
duke@1 856 }
duke@1 857
duke@1 858 public Scope members() {
duke@1 859 if (completer != null) complete();
duke@1 860 return members_field;
duke@1 861 }
duke@1 862
duke@1 863 public long flags() {
duke@1 864 if (completer != null) complete();
duke@1 865 return flags_field;
duke@1 866 }
duke@1 867
jfranck@1464 868 @Override
jfranck@1464 869 public List<Attribute.Compound> getRawAttributes() {
duke@1 870 if (completer != null) complete();
jjg@483 871 if (package_info != null && package_info.completer != null) {
jjg@483 872 package_info.complete();
jfranck@1464 873 mergeAttributes();
jjg@483 874 }
jfranck@1464 875 return super.getRawAttributes();
jfranck@1464 876 }
jfranck@1464 877
jfranck@1464 878 private void mergeAttributes() {
jjg@2149 879 if (metadata == null &&
jjg@2149 880 package_info.metadata != null) {
jjg@2149 881 metadata = new SymbolMetadata(this);
jjg@2149 882 metadata.setAttributes(package_info.metadata);
jfranck@1313 883 }
duke@1 884 }
duke@1 885
duke@1 886 /** A package "exists" if a type or package that exists has
duke@1 887 * been seen within it.
duke@1 888 */
duke@1 889 public boolean exists() {
duke@1 890 return (flags_field & EXISTS) != 0;
duke@1 891 }
duke@1 892
duke@1 893 public ElementKind getKind() {
duke@1 894 return ElementKind.PACKAGE;
duke@1 895 }
duke@1 896
duke@1 897 public Symbol getEnclosingElement() {
duke@1 898 return null;
duke@1 899 }
duke@1 900
duke@1 901 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 902 return v.visitPackage(this, p);
duke@1 903 }
mcimadamore@121 904
mcimadamore@121 905 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 906 return v.visitPackageSymbol(this, p);
mcimadamore@121 907 }
duke@1 908 }
duke@1 909
duke@1 910 /** A class for class symbols
duke@1 911 */
duke@1 912 public static class ClassSymbol extends TypeSymbol implements TypeElement {
duke@1 913
duke@1 914 /** a scope for all class members; variables, methods and inner classes
duke@1 915 * type parameters are not part of this scope
duke@1 916 */
duke@1 917 public Scope members_field;
duke@1 918
duke@1 919 /** the fully qualified name of the class, i.e. pck.outer.inner.
duke@1 920 * null for anonymous classes
duke@1 921 */
duke@1 922 public Name fullname;
duke@1 923
duke@1 924 /** the fully qualified name of the class after converting to flat
duke@1 925 * representation, i.e. pck.outer$inner,
duke@1 926 * set externally for local and anonymous classes
duke@1 927 */
duke@1 928 public Name flatname;
duke@1 929
duke@1 930 /** the sourcefile where the class came from
duke@1 931 */
duke@1 932 public JavaFileObject sourcefile;
duke@1 933
duke@1 934 /** the classfile from where to load this class
duke@1 935 * this will have extension .class or .java
duke@1 936 */
duke@1 937 public JavaFileObject classfile;
duke@1 938
mcimadamore@1086 939 /** the list of translated local classes (used for generating
mcimadamore@1086 940 * InnerClasses attribute)
mcimadamore@1086 941 */
mcimadamore@1086 942 public List<ClassSymbol> trans_local;
mcimadamore@1086 943
duke@1 944 /** the constant pool of the class
duke@1 945 */
duke@1 946 public Pool pool;
duke@1 947
duke@1 948 public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
jfranck@1689 949 super(TYP, flags, name, type, owner);
duke@1 950 this.members_field = null;
duke@1 951 this.fullname = formFullName(name, owner);
duke@1 952 this.flatname = formFlatName(name, owner);
duke@1 953 this.sourcefile = null;
duke@1 954 this.classfile = null;
duke@1 955 this.pool = null;
duke@1 956 }
duke@1 957
duke@1 958 public ClassSymbol(long flags, Name name, Symbol owner) {
duke@1 959 this(
duke@1 960 flags,
duke@1 961 name,
duke@1 962 new ClassType(Type.noType, null, null),
duke@1 963 owner);
duke@1 964 this.type.tsym = this;
duke@1 965 }
duke@1 966
duke@1 967 /** The Java source which this symbol represents.
duke@1 968 */
duke@1 969 public String toString() {
duke@1 970 return className();
duke@1 971 }
duke@1 972
duke@1 973 public long flags() {
duke@1 974 if (completer != null) complete();
duke@1 975 return flags_field;
duke@1 976 }
duke@1 977
duke@1 978 public Scope members() {
duke@1 979 if (completer != null) complete();
duke@1 980 return members_field;
duke@1 981 }
duke@1 982
jfranck@1464 983 @Override
jfranck@1464 984 public List<Attribute.Compound> getRawAttributes() {
duke@1 985 if (completer != null) complete();
jfranck@1464 986 return super.getRawAttributes();
duke@1 987 }
duke@1 988
jjg@1521 989 @Override
jjg@1521 990 public List<Attribute.TypeCompound> getRawTypeAttributes() {
jjg@1521 991 if (completer != null) complete();
jjg@1521 992 return super.getRawTypeAttributes();
jjg@1521 993 }
jjg@1521 994
duke@1 995 public Type erasure(Types types) {
duke@1 996 if (erasure_field == null)
duke@1 997 erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
duke@1 998 List.<Type>nil(), this);
duke@1 999 return erasure_field;
duke@1 1000 }
duke@1 1001
duke@1 1002 public String className() {
jjg@113 1003 if (name.isEmpty())
duke@1 1004 return
duke@1 1005 Log.getLocalizedString("anonymous.class", flatname);
duke@1 1006 else
duke@1 1007 return fullname.toString();
duke@1 1008 }
duke@1 1009
duke@1 1010 public Name getQualifiedName() {
duke@1 1011 return fullname;
duke@1 1012 }
duke@1 1013
duke@1 1014 public Name flatName() {
duke@1 1015 return flatname;
duke@1 1016 }
duke@1 1017
duke@1 1018 public boolean isSubClass(Symbol base, Types types) {
duke@1 1019 if (this == base) {
duke@1 1020 return true;
duke@1 1021 } else if ((base.flags() & INTERFACE) != 0) {
jjg@1374 1022 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t))
duke@1 1023 for (List<Type> is = types.interfaces(t);
duke@1 1024 is.nonEmpty();
duke@1 1025 is = is.tail)
duke@1 1026 if (is.head.tsym.isSubClass(base, types)) return true;
duke@1 1027 } else {
jjg@1374 1028 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t))
duke@1 1029 if (t.tsym == base) return true;
duke@1 1030 }
duke@1 1031 return false;
duke@1 1032 }
duke@1 1033
duke@1 1034 /** Complete the elaboration of this symbol's definition.
duke@1 1035 */
duke@1 1036 public void complete() throws CompletionFailure {
duke@1 1037 try {
duke@1 1038 super.complete();
duke@1 1039 } catch (CompletionFailure ex) {
duke@1 1040 // quiet error recovery
duke@1 1041 flags_field |= (PUBLIC|STATIC);
jjg@110 1042 this.type = new ErrorType(this, Type.noType);
duke@1 1043 throw ex;
duke@1 1044 }
duke@1 1045 }
duke@1 1046
duke@1 1047 public List<Type> getInterfaces() {
duke@1 1048 complete();
duke@1 1049 if (type instanceof ClassType) {
duke@1 1050 ClassType t = (ClassType)type;
duke@1 1051 if (t.interfaces_field == null) // FIXME: shouldn't be null
duke@1 1052 t.interfaces_field = List.nil();
jjg@904 1053 if (t.all_interfaces_field != null)
jjg@904 1054 return Type.getModelTypes(t.all_interfaces_field);
duke@1 1055 return t.interfaces_field;
duke@1 1056 } else {
duke@1 1057 return List.nil();
duke@1 1058 }
duke@1 1059 }
duke@1 1060
duke@1 1061 public Type getSuperclass() {
duke@1 1062 complete();
duke@1 1063 if (type instanceof ClassType) {
duke@1 1064 ClassType t = (ClassType)type;
duke@1 1065 if (t.supertype_field == null) // FIXME: shouldn't be null
duke@1 1066 t.supertype_field = Type.noType;
duke@1 1067 // An interface has no superclass; its supertype is Object.
duke@1 1068 return t.isInterface()
duke@1 1069 ? Type.noType
jjg@904 1070 : t.supertype_field.getModelType();
duke@1 1071 } else {
duke@1 1072 return Type.noType;
duke@1 1073 }
duke@1 1074 }
duke@1 1075
alundblad@2100 1076 /**
alundblad@2100 1077 * Returns the next class to search for inherited annotations or {@code null}
alundblad@2100 1078 * if the next class can't be found.
alundblad@2100 1079 */
alundblad@2100 1080 private ClassSymbol getSuperClassToSearchForAnnotations() {
alundblad@2100 1081
alundblad@2100 1082 Type sup = getSuperclass();
alundblad@2100 1083
alundblad@2100 1084 if (!sup.hasTag(CLASS) || sup.isErroneous())
alundblad@2100 1085 return null;
alundblad@2100 1086
alundblad@2100 1087 return (ClassSymbol) sup.tsym;
alundblad@2100 1088 }
alundblad@2100 1089
alundblad@2100 1090
alundblad@2100 1091 @Override
alundblad@2100 1092 protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
alundblad@2100 1093
alundblad@2100 1094 ClassSymbol sup = getSuperClassToSearchForAnnotations();
alundblad@2100 1095
alundblad@2100 1096 return sup == null ? super.getInheritedAnnotations(annoType)
alundblad@2100 1097 : sup.getAnnotationsByType(annoType);
alundblad@2100 1098 }
alundblad@2100 1099
alundblad@2100 1100
duke@1 1101 public ElementKind getKind() {
duke@1 1102 long flags = flags();
duke@1 1103 if ((flags & ANNOTATION) != 0)
duke@1 1104 return ElementKind.ANNOTATION_TYPE;
duke@1 1105 else if ((flags & INTERFACE) != 0)
duke@1 1106 return ElementKind.INTERFACE;
duke@1 1107 else if ((flags & ENUM) != 0)
duke@1 1108 return ElementKind.ENUM;
duke@1 1109 else
duke@1 1110 return ElementKind.CLASS;
duke@1 1111 }
duke@1 1112
jlahoda@2081 1113 @Override
jlahoda@2081 1114 public Set<Modifier> getModifiers() {
jlahoda@2081 1115 long flags = flags();
jlahoda@2081 1116 return Flags.asModifierSet(flags & ~DEFAULT);
jlahoda@2081 1117 }
jlahoda@2081 1118
duke@1 1119 public NestingKind getNestingKind() {
duke@1 1120 complete();
duke@1 1121 if (owner.kind == PCK)
duke@1 1122 return NestingKind.TOP_LEVEL;
duke@1 1123 else if (name.isEmpty())
duke@1 1124 return NestingKind.ANONYMOUS;
duke@1 1125 else if (owner.kind == MTH)
duke@1 1126 return NestingKind.LOCAL;
duke@1 1127 else
duke@1 1128 return NestingKind.MEMBER;
duke@1 1129 }
duke@1 1130
alundblad@2100 1131
jjg@1645 1132 @Override
alundblad@2100 1133 protected <A extends Annotation> Attribute.Compound getAttribute(final Class<A> annoType) {
alundblad@2100 1134
alundblad@2100 1135 Attribute.Compound attrib = super.getAttribute(annoType);
alundblad@2100 1136
alundblad@2100 1137 boolean inherited = annoType.isAnnotationPresent(Inherited.class);
alundblad@2100 1138 if (attrib != null || !inherited)
alundblad@2100 1139 return attrib;
alundblad@2100 1140
alundblad@2100 1141 // Search supertypes
alundblad@2100 1142 ClassSymbol superType = getSuperClassToSearchForAnnotations();
alundblad@2100 1143 return superType == null ? null
alundblad@2100 1144 : superType.getAttribute(annoType);
duke@1 1145 }
duke@1 1146
alundblad@2100 1147
alundblad@2100 1148
alundblad@2100 1149
duke@1 1150 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 1151 return v.visitType(this, p);
duke@1 1152 }
mcimadamore@121 1153
mcimadamore@121 1154 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 1155 return v.visitClassSymbol(this, p);
mcimadamore@121 1156 }
duke@1 1157 }
duke@1 1158
duke@1 1159
duke@1 1160 /** A class for variable symbols
duke@1 1161 */
duke@1 1162 public static class VarSymbol extends Symbol implements VariableElement {
duke@1 1163
duke@1 1164 /** The variable's declaration position.
duke@1 1165 */
duke@1 1166 public int pos = Position.NOPOS;
duke@1 1167
duke@1 1168 /** The variable's address. Used for different purposes during
duke@1 1169 * flow analysis, translation and code generation.
duke@1 1170 * Flow analysis:
duke@1 1171 * If this is a blank final or local variable, its sequence number.
duke@1 1172 * Translation:
duke@1 1173 * If this is a private field, its access number.
duke@1 1174 * Code generation:
duke@1 1175 * If this is a local variable, its logical slot number.
duke@1 1176 */
duke@1 1177 public int adr = -1;
duke@1 1178
duke@1 1179 /** Construct a variable symbol, given its flags, name, type and owner.
duke@1 1180 */
duke@1 1181 public VarSymbol(long flags, Name name, Type type, Symbol owner) {
duke@1 1182 super(VAR, flags, name, type, owner);
duke@1 1183 }
duke@1 1184
duke@1 1185 /** Clone this symbol with new owner.
duke@1 1186 */
duke@1 1187 public VarSymbol clone(Symbol newOwner) {
mcimadamore@1352 1188 VarSymbol v = new VarSymbol(flags_field, name, type, newOwner) {
mcimadamore@1352 1189 @Override
mcimadamore@1352 1190 public Symbol baseSymbol() {
mcimadamore@1352 1191 return VarSymbol.this;
mcimadamore@1352 1192 }
mcimadamore@1352 1193 };
duke@1 1194 v.pos = pos;
duke@1 1195 v.adr = adr;
duke@1 1196 v.data = data;
duke@1 1197 // System.out.println("clone " + v + " in " + newOwner);//DEBUG
duke@1 1198 return v;
duke@1 1199 }
duke@1 1200
duke@1 1201 public String toString() {
duke@1 1202 return name.toString();
duke@1 1203 }
duke@1 1204
duke@1 1205 public Symbol asMemberOf(Type site, Types types) {
duke@1 1206 return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
duke@1 1207 }
duke@1 1208
duke@1 1209 public ElementKind getKind() {
duke@1 1210 long flags = flags();
duke@1 1211 if ((flags & PARAMETER) != 0) {
duke@1 1212 if (isExceptionParameter())
duke@1 1213 return ElementKind.EXCEPTION_PARAMETER;
duke@1 1214 else
duke@1 1215 return ElementKind.PARAMETER;
duke@1 1216 } else if ((flags & ENUM) != 0) {
duke@1 1217 return ElementKind.ENUM_CONSTANT;
duke@1 1218 } else if (owner.kind == TYP || owner.kind == ERR) {
duke@1 1219 return ElementKind.FIELD;
sundar@697 1220 } else if (isResourceVariable()) {
sundar@697 1221 return ElementKind.RESOURCE_VARIABLE;
duke@1 1222 } else {
duke@1 1223 return ElementKind.LOCAL_VARIABLE;
duke@1 1224 }
duke@1 1225 }
duke@1 1226
duke@1 1227 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 1228 return v.visitVariable(this, p);
duke@1 1229 }
duke@1 1230
duke@1 1231 public Object getConstantValue() { // Mirror API
duke@1 1232 return Constants.decode(getConstValue(), type);
duke@1 1233 }
duke@1 1234
duke@1 1235 public void setLazyConstValue(final Env<AttrContext> env,
duke@1 1236 final Attr attr,
jlahoda@2028 1237 final JCVariableDecl variable)
duke@1 1238 {
duke@1 1239 setData(new Callable<Object>() {
duke@1 1240 public Object call() {
jlahoda@2028 1241 return attr.attribLazyConstantValue(env, variable, type);
duke@1 1242 }
duke@1 1243 });
duke@1 1244 }
duke@1 1245
duke@1 1246 /**
duke@1 1247 * The variable's constant value, if this is a constant.
duke@1 1248 * Before the constant value is evaluated, it points to an
jjg@1984 1249 * initializer environment. If this is not a constant, it can
duke@1 1250 * be used for other stuff.
duke@1 1251 */
duke@1 1252 private Object data;
duke@1 1253
duke@1 1254 public boolean isExceptionParameter() {
duke@1 1255 return data == ElementKind.EXCEPTION_PARAMETER;
duke@1 1256 }
duke@1 1257
darcy@609 1258 public boolean isResourceVariable() {
darcy@609 1259 return data == ElementKind.RESOURCE_VARIABLE;
darcy@609 1260 }
darcy@609 1261
duke@1 1262 public Object getConstValue() {
duke@1 1263 // TODO: Consider if getConstValue and getConstantValue can be collapsed
darcy@609 1264 if (data == ElementKind.EXCEPTION_PARAMETER ||
darcy@609 1265 data == ElementKind.RESOURCE_VARIABLE) {
duke@1 1266 return null;
duke@1 1267 } else if (data instanceof Callable<?>) {
darcy@609 1268 // In this case, this is a final variable, with an as
duke@1 1269 // yet unevaluated initializer.
duke@1 1270 Callable<?> eval = (Callable<?>)data;
duke@1 1271 data = null; // to make sure we don't evaluate this twice.
duke@1 1272 try {
duke@1 1273 data = eval.call();
duke@1 1274 } catch (Exception ex) {
duke@1 1275 throw new AssertionError(ex);
duke@1 1276 }
duke@1 1277 }
duke@1 1278 return data;
duke@1 1279 }
duke@1 1280
duke@1 1281 public void setData(Object data) {
jjg@816 1282 Assert.check(!(data instanceof Env<?>), this);
duke@1 1283 this.data = data;
duke@1 1284 }
mcimadamore@121 1285
mcimadamore@121 1286 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 1287 return v.visitVarSymbol(this, p);
mcimadamore@121 1288 }
duke@1 1289 }
duke@1 1290
duke@1 1291 /** A class for method symbols.
duke@1 1292 */
duke@1 1293 public static class MethodSymbol extends Symbol implements ExecutableElement {
duke@1 1294
duke@1 1295 /** The code of the method. */
duke@1 1296 public Code code = null;
duke@1 1297
mcimadamore@1565 1298 /** The extra (synthetic/mandated) parameters of the method. */
mcimadamore@1565 1299 public List<VarSymbol> extraParams = List.nil();
mcimadamore@1565 1300
emc@1975 1301 /** The captured local variables in an anonymous class */
emc@1975 1302 public List<VarSymbol> capturedLocals = List.nil();
emc@1975 1303
duke@1 1304 /** The parameters of the method. */
duke@1 1305 public List<VarSymbol> params = null;
duke@1 1306
duke@1 1307 /** The names of the parameters */
duke@1 1308 public List<Name> savedParameterNames;
duke@1 1309
duke@1 1310 /** For an attribute field accessor, its default value if any.
duke@1 1311 * The value is null if none appeared in the method
duke@1 1312 * declaration.
duke@1 1313 */
duke@1 1314 public Attribute defaultValue = null;
duke@1 1315
duke@1 1316 /** Construct a method symbol, given its flags, name, type and owner.
duke@1 1317 */
duke@1 1318 public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
duke@1 1319 super(MTH, flags, name, type, owner);
jjg@1374 1320 if (owner.type.hasTag(TYPEVAR)) Assert.error(owner + "." + name);
duke@1 1321 }
duke@1 1322
duke@1 1323 /** Clone this symbol with new owner.
duke@1 1324 */
duke@1 1325 public MethodSymbol clone(Symbol newOwner) {
mcimadamore@1352 1326 MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner) {
mcimadamore@1352 1327 @Override
mcimadamore@1352 1328 public Symbol baseSymbol() {
mcimadamore@1352 1329 return MethodSymbol.this;
mcimadamore@1352 1330 }
mcimadamore@1352 1331 };
duke@1 1332 m.code = code;
duke@1 1333 return m;
duke@1 1334 }
duke@1 1335
mcimadamore@1692 1336 @Override
mcimadamore@1692 1337 public Set<Modifier> getModifiers() {
mcimadamore@1692 1338 long flags = flags();
mcimadamore@1692 1339 return Flags.asModifierSet((flags & DEFAULT) != 0 ? flags & ~ABSTRACT : flags);
mcimadamore@1692 1340 }
mcimadamore@1692 1341
duke@1 1342 /** The Java source which this symbol represents.
duke@1 1343 */
duke@1 1344 public String toString() {
duke@1 1345 if ((flags() & BLOCK) != 0) {
duke@1 1346 return owner.name.toString();
duke@1 1347 } else {
jjg@113 1348 String s = (name == name.table.names.init)
duke@1 1349 ? owner.name.toString()
duke@1 1350 : name.toString();
duke@1 1351 if (type != null) {
jjg@1374 1352 if (type.hasTag(FORALL))
duke@1 1353 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
duke@1 1354 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
duke@1 1355 }
duke@1 1356 return s;
duke@1 1357 }
duke@1 1358 }
duke@1 1359
mcimadamore@1336 1360 public boolean isDynamic() {
mcimadamore@1336 1361 return false;
mcimadamore@1336 1362 }
mcimadamore@1336 1363
duke@1 1364 /** find a symbol that this (proxy method) symbol implements.
duke@1 1365 * @param c The class whose members are searched for
duke@1 1366 * implementations
duke@1 1367 */
duke@1 1368 public Symbol implemented(TypeSymbol c, Types types) {
duke@1 1369 Symbol impl = null;
duke@1 1370 for (List<Type> is = types.interfaces(c.type);
duke@1 1371 impl == null && is.nonEmpty();
duke@1 1372 is = is.tail) {
duke@1 1373 TypeSymbol i = is.head.tsym;
mcimadamore@780 1374 impl = implementedIn(i, types);
mcimadamore@780 1375 if (impl == null)
mcimadamore@780 1376 impl = implemented(i, types);
mcimadamore@780 1377 }
mcimadamore@780 1378 return impl;
mcimadamore@780 1379 }
mcimadamore@780 1380
mcimadamore@780 1381 public Symbol implementedIn(TypeSymbol c, Types types) {
mcimadamore@780 1382 Symbol impl = null;
mcimadamore@780 1383 for (Scope.Entry e = c.members().lookup(name);
mcimadamore@780 1384 impl == null && e.scope != null;
mcimadamore@780 1385 e = e.next()) {
mcimadamore@780 1386 if (this.overrides(e.sym, (TypeSymbol)owner, types, true) &&
mcimadamore@780 1387 // FIXME: I suspect the following requires a
mcimadamore@780 1388 // subst() for a parametric return type.
mcimadamore@780 1389 types.isSameType(type.getReturnType(),
mcimadamore@780 1390 types.memberType(owner.type, e.sym).getReturnType())) {
mcimadamore@780 1391 impl = e.sym;
duke@1 1392 }
duke@1 1393 }
duke@1 1394 return impl;
duke@1 1395 }
duke@1 1396
duke@1 1397 /** Will the erasure of this method be considered by the VM to
duke@1 1398 * override the erasure of the other when seen from class `origin'?
duke@1 1399 */
duke@1 1400 public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
duke@1 1401 if (isConstructor() || _other.kind != MTH) return false;
duke@1 1402
duke@1 1403 if (this == _other) return true;
duke@1 1404 MethodSymbol other = (MethodSymbol)_other;
duke@1 1405
duke@1 1406 // check for a direct implementation
duke@1 1407 if (other.isOverridableIn((TypeSymbol)owner) &&
duke@1 1408 types.asSuper(owner.type, other.owner) != null &&
duke@1 1409 types.isSameType(erasure(types), other.erasure(types)))
duke@1 1410 return true;
duke@1 1411
duke@1 1412 // check for an inherited implementation
duke@1 1413 return
duke@1 1414 (flags() & ABSTRACT) == 0 &&
duke@1 1415 other.isOverridableIn(origin) &&
duke@1 1416 this.isMemberOf(origin, types) &&
duke@1 1417 types.isSameType(erasure(types), other.erasure(types));
duke@1 1418 }
duke@1 1419
duke@1 1420 /** The implementation of this (abstract) symbol in class origin,
duke@1 1421 * from the VM's point of view, null if method does not have an
duke@1 1422 * implementation in class.
duke@1 1423 * @param origin The class of which the implementation is a member.
duke@1 1424 */
duke@1 1425 public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
duke@1 1426 for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
duke@1 1427 for (Scope.Entry e = c.members().lookup(name);
duke@1 1428 e.scope != null;
duke@1 1429 e = e.next()) {
duke@1 1430 if (e.sym.kind == MTH &&
duke@1 1431 ((MethodSymbol)e.sym).binaryOverrides(this, origin, types))
duke@1 1432 return (MethodSymbol)e.sym;
duke@1 1433 }
duke@1 1434 }
duke@1 1435 return null;
duke@1 1436 }
duke@1 1437
duke@1 1438 /** Does this symbol override `other' symbol, when both are seen as
duke@1 1439 * members of class `origin'? It is assumed that _other is a member
duke@1 1440 * of origin.
duke@1 1441 *
duke@1 1442 * It is assumed that both symbols have the same name. The static
duke@1 1443 * modifier is ignored for this test.
duke@1 1444 *
duke@1 1445 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
duke@1 1446 */
duke@1 1447 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
duke@1 1448 if (isConstructor() || _other.kind != MTH) return false;
duke@1 1449
duke@1 1450 if (this == _other) return true;
duke@1 1451 MethodSymbol other = (MethodSymbol)_other;
duke@1 1452
duke@1 1453 // check for a direct implementation
duke@1 1454 if (other.isOverridableIn((TypeSymbol)owner) &&
duke@1 1455 types.asSuper(owner.type, other.owner) != null) {
duke@1 1456 Type mt = types.memberType(owner.type, this);
duke@1 1457 Type ot = types.memberType(owner.type, other);
duke@1 1458 if (types.isSubSignature(mt, ot)) {
duke@1 1459 if (!checkResult)
duke@1 1460 return true;
duke@1 1461 if (types.returnTypeSubstitutable(mt, ot))
duke@1 1462 return true;
duke@1 1463 }
duke@1 1464 }
duke@1 1465
duke@1 1466 // check for an inherited implementation
duke@1 1467 if ((flags() & ABSTRACT) != 0 ||
mcimadamore@1415 1468 ((other.flags() & ABSTRACT) == 0 && (other.flags() & DEFAULT) == 0) ||
mcimadamore@1393 1469 !other.isOverridableIn(origin) ||
mcimadamore@1393 1470 !this.isMemberOf(origin, types))
duke@1 1471 return false;
duke@1 1472
duke@1 1473 // assert types.asSuper(origin.type, other.owner) != null;
duke@1 1474 Type mt = types.memberType(origin.type, this);
duke@1 1475 Type ot = types.memberType(origin.type, other);
duke@1 1476 return
duke@1 1477 types.isSubSignature(mt, ot) &&
mcimadamore@1415 1478 (!checkResult || types.resultSubtype(mt, ot, types.noWarnings));
duke@1 1479 }
duke@1 1480
duke@1 1481 private boolean isOverridableIn(TypeSymbol origin) {
jjh@972 1482 // JLS 8.4.6.1
duke@1 1483 switch ((int)(flags_field & Flags.AccessFlags)) {
duke@1 1484 case Flags.PRIVATE:
duke@1 1485 return false;
duke@1 1486 case Flags.PUBLIC:
mcimadamore@1513 1487 return !this.owner.isInterface() ||
mcimadamore@1513 1488 (flags_field & STATIC) == 0;
duke@1 1489 case Flags.PROTECTED:
duke@1 1490 return (origin.flags() & INTERFACE) == 0;
duke@1 1491 case 0:
duke@1 1492 // for package private: can only override in the same
duke@1 1493 // package
duke@1 1494 return
duke@1 1495 this.packge() == origin.packge() &&
duke@1 1496 (origin.flags() & INTERFACE) == 0;
duke@1 1497 default:
duke@1 1498 return false;
duke@1 1499 }
duke@1 1500 }
duke@1 1501
mcimadamore@1513 1502 @Override
mcimadamore@1513 1503 public boolean isInheritedIn(Symbol clazz, Types types) {
mcimadamore@1513 1504 switch ((int)(flags_field & Flags.AccessFlags)) {
mcimadamore@1513 1505 case PUBLIC:
mcimadamore@1513 1506 return !this.owner.isInterface() ||
mcimadamore@1513 1507 clazz == owner ||
mcimadamore@1513 1508 (flags_field & STATIC) == 0;
mcimadamore@1513 1509 default:
mcimadamore@1513 1510 return super.isInheritedIn(clazz, types);
mcimadamore@1513 1511 }
mcimadamore@1513 1512 }
mcimadamore@1513 1513
duke@1 1514 /** The implementation of this (abstract) symbol in class origin;
duke@1 1515 * null if none exists. Synthetic methods are not considered
duke@1 1516 * as possible implementations.
duke@1 1517 */
duke@1 1518 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
mcimadamore@673 1519 return implementation(origin, types, checkResult, implementation_filter);
mcimadamore@673 1520 }
mcimadamore@673 1521 // where
vromero@1607 1522 public static final Filter<Symbol> implementation_filter = new Filter<Symbol>() {
mcimadamore@673 1523 public boolean accepts(Symbol s) {
mcimadamore@673 1524 return s.kind == Kinds.MTH &&
mcimadamore@673 1525 (s.flags() & SYNTHETIC) == 0;
mcimadamore@673 1526 }
mcimadamore@673 1527 };
mcimadamore@673 1528
mcimadamore@673 1529 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) {
mcimadamore@858 1530 MethodSymbol res = types.implementation(this, origin, checkResult, implFilter);
mcimadamore@341 1531 if (res != null)
mcimadamore@341 1532 return res;
duke@1 1533 // if origin is derived from a raw type, we might have missed
duke@1 1534 // an implementation because we do not know enough about instantiations.
duke@1 1535 // in this case continue with the supertype as origin.
mcimadamore@1222 1536 if (types.isDerivedRaw(origin.type) && !origin.isInterface())
duke@1 1537 return implementation(types.supertype(origin.type).tsym, types, checkResult);
duke@1 1538 else
duke@1 1539 return null;
duke@1 1540 }
duke@1 1541
duke@1 1542 public List<VarSymbol> params() {
duke@1 1543 owner.complete();
duke@1 1544 if (params == null) {
jjg@428 1545 // If ClassReader.saveParameterNames has been set true, then
jjg@428 1546 // savedParameterNames will be set to a list of names that
jjg@428 1547 // matches the types in type.getParameterTypes(). If any names
jjg@428 1548 // were not found in the class file, those names in the list will
jjg@428 1549 // be set to the empty name.
jjg@428 1550 // If ClassReader.saveParameterNames has been set false, then
jjg@428 1551 // savedParameterNames will be null.
jjg@428 1552 List<Name> paramNames = savedParameterNames;
duke@1 1553 savedParameterNames = null;
jjg@428 1554 // discard the provided names if the list of names is the wrong size.
jjg@1473 1555 if (paramNames == null || paramNames.size() != type.getParameterTypes().size()) {
jjg@428 1556 paramNames = List.nil();
jjg@1473 1557 }
duke@1 1558 ListBuffer<VarSymbol> buf = new ListBuffer<VarSymbol>();
jjg@428 1559 List<Name> remaining = paramNames;
jjg@428 1560 // assert: remaining and paramNames are both empty or both
jjg@428 1561 // have same cardinality as type.getParameterTypes()
jjg@428 1562 int i = 0;
duke@1 1563 for (Type t : type.getParameterTypes()) {
jjg@428 1564 Name paramName;
jjg@428 1565 if (remaining.isEmpty()) {
jjg@428 1566 // no names for any parameters available
jjg@428 1567 paramName = createArgName(i, paramNames);
jjg@428 1568 } else {
jjg@428 1569 paramName = remaining.head;
jjg@428 1570 remaining = remaining.tail;
jjg@428 1571 if (paramName.isEmpty()) {
jjg@428 1572 // no name for this specific parameter
jjg@428 1573 paramName = createArgName(i, paramNames);
jjg@428 1574 }
jjg@428 1575 }
jjg@428 1576 buf.append(new VarSymbol(PARAMETER, paramName, t, this));
jjg@428 1577 i++;
duke@1 1578 }
duke@1 1579 params = buf.toList();
duke@1 1580 }
duke@1 1581 return params;
duke@1 1582 }
duke@1 1583
jjg@428 1584 // Create a name for the argument at position 'index' that is not in
jjg@428 1585 // the exclude list. In normal use, either no names will have been
jjg@428 1586 // provided, in which case the exclude list is empty, or all the names
jjg@428 1587 // will have been provided, in which case this method will not be called.
jjg@428 1588 private Name createArgName(int index, List<Name> exclude) {
jjg@428 1589 String prefix = "arg";
jjg@428 1590 while (true) {
jjg@428 1591 Name argName = name.table.fromString(prefix + index);
jjg@428 1592 if (!exclude.contains(argName))
jjg@428 1593 return argName;
jjg@428 1594 prefix += "$";
jjg@428 1595 }
jjg@428 1596 }
jjg@428 1597
duke@1 1598 public Symbol asMemberOf(Type site, Types types) {
duke@1 1599 return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
duke@1 1600 }
duke@1 1601
duke@1 1602 public ElementKind getKind() {
jjg@113 1603 if (name == name.table.names.init)
duke@1 1604 return ElementKind.CONSTRUCTOR;
jjg@113 1605 else if (name == name.table.names.clinit)
duke@1 1606 return ElementKind.STATIC_INIT;
mcimadamore@1085 1607 else if ((flags() & BLOCK) != 0)
mcimadamore@1085 1608 return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT;
duke@1 1609 else
duke@1 1610 return ElementKind.METHOD;
duke@1 1611 }
duke@1 1612
mcimadamore@1085 1613 public boolean isStaticOrInstanceInit() {
mcimadamore@1085 1614 return getKind() == ElementKind.STATIC_INIT ||
mcimadamore@1085 1615 getKind() == ElementKind.INSTANCE_INIT;
mcimadamore@1085 1616 }
mcimadamore@1085 1617
duke@1 1618 public Attribute getDefaultValue() {
duke@1 1619 return defaultValue;
duke@1 1620 }
duke@1 1621
jjg@1521 1622 public List<VarSymbol> getParameters() {
duke@1 1623 return params();
duke@1 1624 }
duke@1 1625
duke@1 1626 public boolean isVarArgs() {
duke@1 1627 return (flags() & VARARGS) != 0;
duke@1 1628 }
duke@1 1629
darcy@1459 1630 public boolean isDefault() {
darcy@1459 1631 return (flags() & DEFAULT) != 0;
darcy@1459 1632 }
darcy@1459 1633
duke@1 1634 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 1635 return v.visitExecutable(this, p);
duke@1 1636 }
duke@1 1637
mcimadamore@121 1638 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 1639 return v.visitMethodSymbol(this, p);
mcimadamore@121 1640 }
mcimadamore@121 1641
jjg@1645 1642 public Type getReceiverType() {
jjg@1645 1643 return asType().getReceiverType();
jjg@1645 1644 }
jjg@1645 1645
duke@1 1646 public Type getReturnType() {
duke@1 1647 return asType().getReturnType();
duke@1 1648 }
duke@1 1649
duke@1 1650 public List<Type> getThrownTypes() {
duke@1 1651 return asType().getThrownTypes();
duke@1 1652 }
duke@1 1653 }
duke@1 1654
mcimadamore@1336 1655 /** A class for invokedynamic method calls.
mcimadamore@1336 1656 */
mcimadamore@1336 1657 public static class DynamicMethodSymbol extends MethodSymbol {
mcimadamore@1336 1658
mcimadamore@1336 1659 public Object[] staticArgs;
mcimadamore@1336 1660 public Symbol bsm;
mcimadamore@1336 1661 public int bsmKind;
mcimadamore@1336 1662
mcimadamore@1336 1663 public DynamicMethodSymbol(Name name, Symbol owner, int bsmKind, MethodSymbol bsm, Type type, Object[] staticArgs) {
mcimadamore@1336 1664 super(0, name, type, owner);
mcimadamore@1336 1665 this.bsm = bsm;
mcimadamore@1336 1666 this.bsmKind = bsmKind;
mcimadamore@1336 1667 this.staticArgs = staticArgs;
mcimadamore@1336 1668 }
mcimadamore@1336 1669
mcimadamore@1336 1670 @Override
mcimadamore@1336 1671 public boolean isDynamic() {
mcimadamore@1336 1672 return true;
mcimadamore@1336 1673 }
mcimadamore@1336 1674 }
mcimadamore@1336 1675
duke@1 1676 /** A class for predefined operators.
duke@1 1677 */
duke@1 1678 public static class OperatorSymbol extends MethodSymbol {
duke@1 1679
duke@1 1680 public int opcode;
duke@1 1681
duke@1 1682 public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
duke@1 1683 super(PUBLIC | STATIC, name, type, owner);
duke@1 1684 this.opcode = opcode;
duke@1 1685 }
mcimadamore@121 1686
mcimadamore@121 1687 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 1688 return v.visitOperatorSymbol(this, p);
mcimadamore@121 1689 }
duke@1 1690 }
duke@1 1691
duke@1 1692 /** Symbol completer interface.
duke@1 1693 */
duke@1 1694 public static interface Completer {
duke@1 1695 void complete(Symbol sym) throws CompletionFailure;
duke@1 1696 }
duke@1 1697
duke@1 1698 public static class CompletionFailure extends RuntimeException {
duke@1 1699 private static final long serialVersionUID = 0;
duke@1 1700 public Symbol sym;
duke@1 1701
jjg@12 1702 /** A diagnostic object describing the failure
jjg@12 1703 */
jjg@12 1704 public JCDiagnostic diag;
jjg@12 1705
duke@1 1706 /** A localized string describing the failure.
jjg@12 1707 * @deprecated Use {@code getDetail()} or {@code getMessage()}
duke@1 1708 */
jjg@12 1709 @Deprecated
duke@1 1710 public String errmsg;
duke@1 1711
duke@1 1712 public CompletionFailure(Symbol sym, String errmsg) {
duke@1 1713 this.sym = sym;
duke@1 1714 this.errmsg = errmsg;
duke@1 1715 // this.printStackTrace();//DEBUG
duke@1 1716 }
duke@1 1717
jjg@12 1718 public CompletionFailure(Symbol sym, JCDiagnostic diag) {
jjg@12 1719 this.sym = sym;
jjg@12 1720 this.diag = diag;
jjg@12 1721 // this.printStackTrace();//DEBUG
jjg@12 1722 }
jjg@12 1723
jjg@12 1724 public JCDiagnostic getDiagnostic() {
jjg@12 1725 return diag;
jjg@12 1726 }
jjg@12 1727
jjg@12 1728 @Override
duke@1 1729 public String getMessage() {
jjg@12 1730 if (diag != null)
jjg@12 1731 return diag.getMessage(null);
jjg@12 1732 else
jjg@12 1733 return errmsg;
jjg@12 1734 }
jjg@12 1735
jjg@12 1736 public Object getDetailValue() {
jjg@12 1737 return (diag != null ? diag : errmsg);
duke@1 1738 }
duke@1 1739
duke@1 1740 @Override
duke@1 1741 public CompletionFailure initCause(Throwable cause) {
duke@1 1742 super.initCause(cause);
duke@1 1743 return this;
duke@1 1744 }
duke@1 1745
duke@1 1746 }
mcimadamore@121 1747
mcimadamore@121 1748 /**
mcimadamore@121 1749 * A visitor for symbols. A visitor is used to implement operations
mcimadamore@121 1750 * (or relations) on symbols. Most common operations on types are
mcimadamore@121 1751 * binary relations and this interface is designed for binary
mcimadamore@121 1752 * relations, that is, operations on the form
mcimadamore@121 1753 * Symbol&nbsp;&times;&nbsp;P&nbsp;&rarr;&nbsp;R.
mcimadamore@121 1754 * <!-- In plain text: Type x P -> R -->
mcimadamore@121 1755 *
mcimadamore@121 1756 * @param <R> the return type of the operation implemented by this
mcimadamore@121 1757 * visitor; use Void if no return type is needed.
mcimadamore@121 1758 * @param <P> the type of the second argument (the first being the
mcimadamore@121 1759 * symbol itself) of the operation implemented by this visitor; use
mcimadamore@121 1760 * Void if a second argument is not needed.
mcimadamore@121 1761 */
mcimadamore@121 1762 public interface Visitor<R,P> {
mcimadamore@121 1763 R visitClassSymbol(ClassSymbol s, P arg);
mcimadamore@121 1764 R visitMethodSymbol(MethodSymbol s, P arg);
mcimadamore@121 1765 R visitPackageSymbol(PackageSymbol s, P arg);
mcimadamore@121 1766 R visitOperatorSymbol(OperatorSymbol s, P arg);
mcimadamore@121 1767 R visitVarSymbol(VarSymbol s, P arg);
mcimadamore@121 1768 R visitTypeSymbol(TypeSymbol s, P arg);
mcimadamore@121 1769 R visitSymbol(Symbol s, P arg);
mcimadamore@121 1770 }
duke@1 1771 }

mercurial