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

Mon, 16 Sep 2013 14:13:44 +0200

author
jlahoda
date
Mon, 16 Sep 2013 14:13:44 +0200
changeset 2028
4ce8148ffc4f
parent 2014
6cffcd15a17e
child 2047
5f915a0c9615
permissions
-rw-r--r--

8021112: Spurious unchecked warning reported by javac
6480588: No way to suppress deprecation warnings when implementing deprecated interface
Summary: Fixing DeferredLintHandler configuration, so lint warnings are reported with correct @SuppressWarnings settings
Reviewed-by: jjg, vromero

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

mercurial