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

Sun, 20 Oct 2013 12:01:43 -0700

author
jjg
date
Sun, 20 Oct 2013 12:01:43 -0700
changeset 2149
e5d3cd43c85e
parent 2100
1e7ad879f15e
child 2301
27a3026256cd
permissions
-rw-r--r--

8025109: Better encapsulation for AnnotatedType
Reviewed-by: jjg
Contributed-by: wdietl@gmail.com

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

mercurial