1 /* 2 * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.javac.code; 27 28 import java.lang.annotation.Annotation; 29 import java.lang.annotation.Inherited; 30 import java.util.Collections; 31 import java.util.EnumSet; 32 import java.util.HashMap; 33 import java.util.Map; 34 import java.util.Set; 35 import java.util.concurrent.Callable; 36 import java.util.function.Supplier; 37 38 import javax.lang.model.element.Element; 39 import javax.lang.model.element.ElementKind; 40 import javax.lang.model.element.ElementVisitor; 41 import javax.lang.model.element.ExecutableElement; 42 import javax.lang.model.element.Modifier; 43 import javax.lang.model.element.ModuleElement; 44 import javax.lang.model.element.NestingKind; 45 import javax.lang.model.element.PackageElement; 46 import javax.lang.model.element.RecordComponentElement; 47 import javax.lang.model.element.TypeElement; 48 import javax.lang.model.element.TypeParameterElement; 49 import javax.lang.model.element.VariableElement; 50 import javax.tools.JavaFileManager; 51 import javax.tools.JavaFileObject; 52 53 import com.sun.tools.javac.code.Kinds.Kind; 54 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata; 55 import com.sun.tools.javac.code.Type.*; 56 import com.sun.tools.javac.comp.Attr; 57 import com.sun.tools.javac.comp.AttrContext; 58 import com.sun.tools.javac.comp.Env; 59 import com.sun.tools.javac.jvm.*; 60 import com.sun.tools.javac.jvm.PoolConstant; 61 import com.sun.tools.javac.tree.JCTree; 62 import com.sun.tools.javac.tree.JCTree.JCAnnotation; 63 import com.sun.tools.javac.tree.JCTree.JCFieldAccess; 64 import com.sun.tools.javac.tree.JCTree.JCVariableDecl; 65 import com.sun.tools.javac.tree.JCTree.Tag; 66 import com.sun.tools.javac.util.*; 67 import com.sun.tools.javac.util.DefinedBy.Api; 68 import com.sun.tools.javac.util.List; 69 import com.sun.tools.javac.util.Name; 70 71 import static com.sun.tools.javac.code.Flags.*; 72 import static com.sun.tools.javac.code.Kinds.*; 73 import static com.sun.tools.javac.code.Kinds.Kind.*; 74 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE; 75 import com.sun.tools.javac.code.Scope.WriteableScope; 76 import static com.sun.tools.javac.code.TypeTag.CLASS; 77 import static com.sun.tools.javac.code.TypeTag.FORALL; 78 import static com.sun.tools.javac.code.TypeTag.TYPEVAR; 79 import static com.sun.tools.javac.jvm.ByteCodes.iadd; 80 import static com.sun.tools.javac.jvm.ByteCodes.ishll; 81 import static com.sun.tools.javac.jvm.ByteCodes.lushrl; 82 import static com.sun.tools.javac.jvm.ByteCodes.lxor; 83 import static com.sun.tools.javac.jvm.ByteCodes.string_add; 84 85 /** Root class for Java symbols. It contains subclasses 86 * for specific sorts of symbols, such as variables, methods and operators, 87 * types, packages. Each subclass is represented as a static inner class 88 * inside Symbol. 89 * 90 * <p><b>This is NOT part of any supported API. 91 * If you write code that depends on this, you do so at your own risk. 92 * This code and its internal interfaces are subject to change or 93 * deletion without notice.</b> 94 */ 95 public abstract class Symbol extends AnnoConstruct implements PoolConstant, Element { 96 97 /** The kind of this symbol. 98 * @see Kinds 99 */ 100 public Kind kind; 101 102 /** The flags of this symbol. 103 */ 104 public long flags_field; 105 106 /** An accessor method for the flags of this symbol. 107 * Flags of class symbols should be accessed through the accessor 108 * method to make sure that the class symbol is loaded. 109 */ 110 public long flags() { return flags_field; } 111 112 /** The name of this symbol in Utf8 representation. 113 */ 114 public Name name; 115 116 /** The type of this symbol. 117 */ 118 public Type type; 119 120 /** The owner of this symbol. 121 */ 122 public Symbol owner; 123 124 /** The completer of this symbol. 125 * This should never equal null (NULL_COMPLETER should be used instead). 126 */ 127 public Completer completer; 128 129 /** A cache for the type erasure of this symbol. 130 */ 131 public Type erasure_field; 132 133 // <editor-fold defaultstate="collapsed" desc="annotations"> 134 135 /** The attributes of this symbol are contained in this 136 * SymbolMetadata. The SymbolMetadata instance is NOT immutable. 137 */ 138 protected SymbolMetadata metadata; 139 140 141 /** An accessor method for the attributes of this symbol. 142 * Attributes of class symbols should be accessed through the accessor 143 * method to make sure that the class symbol is loaded. 144 */ 145 public List<Attribute.Compound> getRawAttributes() { 146 return (metadata == null) 147 ? List.nil() 148 : metadata.getDeclarationAttributes(); 149 } 150 151 /** An accessor method for the type attributes of this symbol. 152 * Attributes of class symbols should be accessed through the accessor 153 * method to make sure that the class symbol is loaded. 154 */ 155 public List<Attribute.TypeCompound> getRawTypeAttributes() { 156 return (metadata == null) 157 ? List.nil() 158 : metadata.getTypeAttributes(); 159 } 160 161 /** Fetch a particular annotation from a symbol. */ 162 public Attribute.Compound attribute(Symbol anno) { 163 for (Attribute.Compound a : getRawAttributes()) { 164 if (a.type.tsym == anno) return a; 165 } 166 return null; 167 } 168 169 public boolean annotationsPendingCompletion() { 170 return metadata == null ? false : metadata.pendingCompletion(); 171 } 172 173 public void appendAttributes(List<Attribute.Compound> l) { 174 if (l.nonEmpty()) { 175 initedMetadata().append(l); 176 } 177 } 178 179 public void appendClassInitTypeAttributes(List<Attribute.TypeCompound> l) { 180 if (l.nonEmpty()) { 181 initedMetadata().appendClassInitTypeAttributes(l); 182 } 183 } 184 185 public void appendInitTypeAttributes(List<Attribute.TypeCompound> l) { 186 if (l.nonEmpty()) { 187 initedMetadata().appendInitTypeAttributes(l); 188 } 189 } 190 191 public void appendUniqueTypeAttributes(List<Attribute.TypeCompound> l) { 192 if (l.nonEmpty()) { 193 initedMetadata().appendUniqueTypes(l); 194 } 195 } 196 197 public List<Attribute.TypeCompound> getClassInitTypeAttributes() { 198 return (metadata == null) 199 ? List.nil() 200 : metadata.getClassInitTypeAttributes(); 201 } 202 203 public List<Attribute.TypeCompound> getInitTypeAttributes() { 204 return (metadata == null) 205 ? List.nil() 206 : metadata.getInitTypeAttributes(); 207 } 208 209 public void setInitTypeAttributes(List<Attribute.TypeCompound> l) { 210 initedMetadata().setInitTypeAttributes(l); 211 } 212 213 public void setClassInitTypeAttributes(List<Attribute.TypeCompound> l) { 214 initedMetadata().setClassInitTypeAttributes(l); 215 } 216 217 public List<Attribute.Compound> getDeclarationAttributes() { 218 return (metadata == null) 219 ? List.nil() 220 : metadata.getDeclarationAttributes(); 221 } 222 223 public boolean hasAnnotations() { 224 return (metadata != null && !metadata.isEmpty()); 225 } 226 227 public boolean hasTypeAnnotations() { 228 return (metadata != null && !metadata.isTypesEmpty()); 229 } 230 231 public boolean isCompleted() { 232 return completer.isTerminal(); 233 } 234 235 public void prependAttributes(List<Attribute.Compound> l) { 236 if (l.nonEmpty()) { 237 initedMetadata().prepend(l); 238 } 239 } 240 241 public void resetAnnotations() { 242 initedMetadata().reset(); 243 } 244 245 public void setAttributes(Symbol other) { 246 if (metadata != null || other.metadata != null) { 247 initedMetadata().setAttributes(other.metadata); 248 } 249 } 250 251 public void setDeclarationAttributes(List<Attribute.Compound> a) { 252 if (metadata != null || a.nonEmpty()) { 253 initedMetadata().setDeclarationAttributes(a); 254 } 255 } 256 257 public void setTypeAttributes(List<Attribute.TypeCompound> a) { 258 if (metadata != null || a.nonEmpty()) { 259 if (metadata == null) 260 metadata = new SymbolMetadata(this); 261 metadata.setTypeAttributes(a); 262 } 263 } 264 265 private SymbolMetadata initedMetadata() { 266 if (metadata == null) 267 metadata = new SymbolMetadata(this); 268 return metadata; 269 } 270 271 /** This method is intended for debugging only. */ 272 public SymbolMetadata getMetadata() { 273 return metadata; 274 } 275 276 // </editor-fold> 277 278 /** Construct a symbol with given kind, flags, name, type and owner. 279 */ 280 public Symbol(Kind kind, long flags, Name name, Type type, Symbol owner) { 281 this.kind = kind; 282 this.flags_field = flags; 283 this.type = type; 284 this.owner = owner; 285 this.completer = Completer.NULL_COMPLETER; 286 this.erasure_field = null; 287 this.name = name; 288 } 289 290 @Override 291 public int poolTag() { 292 throw new AssertionError("Invalid pool entry"); 293 } 294 295 /** Clone this symbol with new owner. 296 * Legal only for fields and methods. 297 */ 298 public Symbol clone(Symbol newOwner) { 299 throw new AssertionError(); 300 } 301 302 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 303 return v.visitSymbol(this, p); 304 } 305 306 /** The Java source which this symbol represents. 307 * A description of this symbol; overrides Object. 308 */ 309 public String toString() { 310 return name.toString(); 311 } 312 313 /** A Java source description of the location of this symbol; used for 314 * error reporting. 315 * 316 * @return null if the symbol is a package or a toplevel class defined in 317 * the default package; otherwise, the owner symbol is returned 318 */ 319 public Symbol location() { 320 if (owner.name == null || (owner.name.isEmpty() && 321 (owner.flags() & BLOCK) == 0 && 322 owner.kind != PCK && 323 owner.kind != TYP)) { 324 return null; 325 } 326 return owner; 327 } 328 329 public Symbol location(Type site, Types types) { 330 if (owner.name == null || owner.name.isEmpty()) { 331 return location(); 332 } 333 if (owner.type.hasTag(CLASS)) { 334 Type ownertype = types.asOuterSuper(site, owner); 335 if (ownertype != null) return ownertype.tsym; 336 } 337 return owner; 338 } 339 340 public Symbol baseSymbol() { 341 return this; 342 } 343 344 /** The symbol's erased type. 345 */ 346 public Type erasure(Types types) { 347 if (erasure_field == null) 348 erasure_field = types.erasure(type); 349 return erasure_field; 350 } 351 352 /** The external type of a symbol. This is the symbol's erased type 353 * except for constructors of inner classes which get the enclosing 354 * instance class added as first argument. 355 */ 356 public Type externalType(Types types) { 357 Type t = erasure(types); 358 if (name == name.table.names.init && owner.hasOuterInstance()) { 359 Type outerThisType = types.erasure(owner.type.getEnclosingType()); 360 return new MethodType(t.getParameterTypes().prepend(outerThisType), 361 t.getReturnType(), 362 t.getThrownTypes(), 363 t.tsym); 364 } else { 365 return t; 366 } 367 } 368 369 public boolean isDeprecated() { 370 return (flags_field & DEPRECATED) != 0; 371 } 372 373 public boolean hasDeprecatedAnnotation() { 374 return (flags_field & DEPRECATED_ANNOTATION) != 0; 375 } 376 377 public boolean isDeprecatedForRemoval() { 378 return (flags_field & DEPRECATED_REMOVAL) != 0; 379 } 380 381 public boolean isPreviewApi() { 382 return (flags_field & PREVIEW_API) != 0; 383 } 384 385 public boolean isDeprecatableViaAnnotation() { 386 switch (getKind()) { 387 case LOCAL_VARIABLE: 388 case PACKAGE: 389 case PARAMETER: 390 case RESOURCE_VARIABLE: 391 case EXCEPTION_PARAMETER: 392 return false; 393 default: 394 return true; 395 } 396 } 397 398 public boolean isStatic() { 399 return 400 (flags() & STATIC) != 0 || 401 (owner.flags() & INTERFACE) != 0 && kind != MTH && 402 name != name.table.names._this; 403 } 404 405 public boolean isInterface() { 406 return (flags() & INTERFACE) != 0; 407 } 408 409 public boolean isAbstract() { 410 return (flags_field & ABSTRACT) != 0; 411 } 412 413 public boolean isPrivate() { 414 return (flags_field & Flags.AccessFlags) == PRIVATE; 415 } 416 417 public boolean isPublic() { 418 return (flags_field & Flags.AccessFlags) == PUBLIC; 419 } 420 421 public boolean isEnum() { 422 return (flags() & ENUM) != 0; 423 } 424 425 public boolean isFinal() { 426 return (flags_field & FINAL) != 0; 427 } 428 429 /** Is this symbol declared (directly or indirectly) local 430 * to a method or variable initializer? 431 * Also includes fields of inner classes which are in 432 * turn local to a method or variable initializer. 433 */ 434 public boolean isLocal() { 435 return 436 (owner.kind.matches(KindSelector.VAL_MTH) || 437 (owner.kind == TYP && owner.isLocal())); 438 } 439 440 /** Has this symbol an empty name? This includes anonymous 441 * inner classes. 442 */ 443 public boolean isAnonymous() { 444 return name.isEmpty(); 445 } 446 447 /** Is this symbol a constructor? 448 */ 449 public boolean isConstructor() { 450 return name == name.table.names.init; 451 } 452 453 public boolean isDynamic() { 454 return false; 455 } 456 457 /** The fully qualified name of this symbol. 458 * This is the same as the symbol's name except for class symbols, 459 * which are handled separately. 460 */ 461 public Name getQualifiedName() { 462 return name; 463 } 464 465 /** The fully qualified name of this symbol after converting to flat 466 * representation. This is the same as the symbol's name except for 467 * class symbols, which are handled separately. 468 */ 469 public Name flatName() { 470 return getQualifiedName(); 471 } 472 473 /** If this is a class or package, its members, otherwise null. 474 */ 475 public WriteableScope members() { 476 return null; 477 } 478 479 /** A class is an inner class if it it has an enclosing instance class. 480 */ 481 public boolean isInner() { 482 return kind == TYP && type.getEnclosingType().hasTag(CLASS); 483 } 484 485 /** An inner class has an outer instance if it is not an interface 486 * it has an enclosing instance class which might be referenced from the class. 487 * Nested classes can see instance members of their enclosing class. 488 * Their constructors carry an additional this$n parameter, inserted 489 * implicitly by the compiler. 490 * 491 * @see #isInner 492 */ 493 public boolean hasOuterInstance() { 494 return 495 type.getEnclosingType().hasTag(CLASS) && (flags() & (INTERFACE | NOOUTERTHIS)) == 0; 496 } 497 498 /** The closest enclosing class of this symbol's declaration. 499 * Warning: this (misnamed) method returns the receiver itself 500 * when the receiver is a class (as opposed to its enclosing 501 * class as one may be misled to believe.) 502 */ 503 public ClassSymbol enclClass() { 504 Symbol c = this; 505 while (c != null && 506 (!c.kind.matches(KindSelector.TYP) || !c.type.hasTag(CLASS))) { 507 c = c.owner; 508 } 509 return (ClassSymbol)c; 510 } 511 512 /** The outermost class which indirectly owns this symbol. 513 */ 514 public ClassSymbol outermostClass() { 515 Symbol sym = this; 516 Symbol prev = null; 517 while (sym.kind != PCK) { 518 prev = sym; 519 sym = sym.owner; 520 } 521 return (ClassSymbol) prev; 522 } 523 524 /** The package which indirectly owns this symbol. 525 */ 526 public PackageSymbol packge() { 527 Symbol sym = this; 528 while (sym.kind != PCK) { 529 sym = sym.owner; 530 } 531 return (PackageSymbol) sym; 532 } 533 534 /** Is this symbol a subclass of `base'? Only defined for ClassSymbols. 535 */ 536 public boolean isSubClass(Symbol base, Types types) { 537 throw new AssertionError("isSubClass " + this); 538 } 539 540 /** Fully check membership: hierarchy, protection, and hiding. 541 * Does not exclude methods not inherited due to overriding. 542 */ 543 public boolean isMemberOf(TypeSymbol clazz, Types types) { 544 return 545 owner == clazz || 546 clazz.isSubClass(owner, types) && 547 isInheritedIn(clazz, types) && 548 !hiddenIn((ClassSymbol)clazz, types); 549 } 550 551 /** Is this symbol the same as or enclosed by the given class? */ 552 public boolean isEnclosedBy(ClassSymbol clazz) { 553 for (Symbol sym = this; sym.kind != PCK; sym = sym.owner) 554 if (sym == clazz) return true; 555 return false; 556 } 557 558 private boolean hiddenIn(ClassSymbol clazz, Types types) { 559 Symbol sym = hiddenInInternal(clazz, types); 560 Assert.check(sym != null, "the result of hiddenInInternal() can't be null"); 561 /* If we find the current symbol then there is no symbol hiding it 562 */ 563 return sym != this; 564 } 565 566 /** This method looks in the supertypes graph that has the current class as the 567 * initial node, till it finds the current symbol or another symbol that hides it. 568 * If the current class has more than one supertype (extends one class and 569 * implements one or more interfaces) then null can be returned, meaning that 570 * a wrong path in the supertypes graph was selected. Null can only be returned 571 * as a temporary value, as a result of the recursive call. 572 */ 573 private Symbol hiddenInInternal(ClassSymbol currentClass, Types types) { 574 if (currentClass == owner) { 575 return this; 576 } 577 for (Symbol sym : currentClass.members().getSymbolsByName(name)) { 578 if (sym.kind == kind && 579 (kind != MTH || 580 (sym.flags() & STATIC) != 0 && 581 types.isSubSignature(sym.type, type))) { 582 return sym; 583 } 584 } 585 Symbol hiddenSym = null; 586 for (Type st : types.interfaces(currentClass.type) 587 .prepend(types.supertype(currentClass.type))) { 588 if (st != null && (st.hasTag(CLASS))) { 589 Symbol sym = hiddenInInternal((ClassSymbol)st.tsym, types); 590 if (sym == this) { 591 return this; 592 } else if (sym != null) { 593 hiddenSym = sym; 594 } 595 } 596 } 597 return hiddenSym; 598 } 599 600 /** Is this symbol accessible in a given class? 601 * PRE: If symbol's owner is a interface, 602 * it is already assumed that the interface is a superinterface 603 * the given class. 604 * @param clazz The class for which we want to establish membership. 605 * This must be a subclass of the member's owner. 606 */ 607 public final boolean isAccessibleIn(Symbol clazz, Types types) { 608 switch ((int)(flags_field & Flags.AccessFlags)) { 609 default: // error recovery 610 case PUBLIC: 611 return true; 612 case PRIVATE: 613 return this.owner == clazz; 614 case PROTECTED: 615 // we model interfaces as extending Object 616 return (clazz.flags() & INTERFACE) == 0; 617 case 0: 618 PackageSymbol thisPackage = this.packge(); 619 for (Symbol sup = clazz; 620 sup != null && sup != this.owner; 621 sup = types.supertype(sup.type).tsym) { 622 while (sup.type.hasTag(TYPEVAR)) 623 sup = sup.type.getUpperBound().tsym; 624 if (sup.type.isErroneous()) 625 return true; // error recovery 626 if ((sup.flags() & COMPOUND) != 0) 627 continue; 628 if (sup.packge() != thisPackage) 629 return false; 630 } 631 return (clazz.flags() & INTERFACE) == 0; 632 } 633 } 634 635 /** Is this symbol inherited into a given class? 636 * PRE: If symbol's owner is a interface, 637 * it is already assumed that the interface is a superinterface 638 * of the given class. 639 * @param clazz The class for which we want to establish membership. 640 * This must be a subclass of the member's owner. 641 */ 642 public boolean isInheritedIn(Symbol clazz, Types types) { 643 return isAccessibleIn(clazz, types); 644 } 645 646 /** The (variable or method) symbol seen as a member of given 647 * class type`site' (this might change the symbol's type). 648 * This is used exclusively for producing diagnostics. 649 */ 650 public Symbol asMemberOf(Type site, Types types) { 651 throw new AssertionError(); 652 } 653 654 /** Does this method symbol override `other' symbol, when both are seen as 655 * members of class `origin'? It is assumed that _other is a member 656 * of origin. 657 * 658 * It is assumed that both symbols have the same name. The static 659 * modifier is ignored for this test. 660 * 661 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4 662 */ 663 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) { 664 return false; 665 } 666 667 /** Complete the elaboration of this symbol's definition. 668 */ 669 public void complete() throws CompletionFailure { 670 if (completer != Completer.NULL_COMPLETER) { 671 Completer c = completer; 672 completer = Completer.NULL_COMPLETER; 673 c.complete(this); 674 } 675 } 676 677 public void apiComplete() throws CompletionFailure { 678 try { 679 complete(); 680 } catch (CompletionFailure cf) { 681 cf.dcfh.handleAPICompletionFailure(cf); 682 } 683 } 684 685 /** True if the symbol represents an entity that exists. 686 */ 687 public boolean exists() { 688 return true; 689 } 690 691 @DefinedBy(Api.LANGUAGE_MODEL) 692 public Type asType() { 693 return type; 694 } 695 696 @DefinedBy(Api.LANGUAGE_MODEL) 697 public Symbol getEnclosingElement() { 698 return owner; 699 } 700 701 @DefinedBy(Api.LANGUAGE_MODEL) 702 public ElementKind getKind() { 703 return ElementKind.OTHER; // most unkind 704 } 705 706 @DefinedBy(Api.LANGUAGE_MODEL) 707 public Set<Modifier> getModifiers() { 708 apiComplete(); 709 return Flags.asModifierSet(flags()); 710 } 711 712 @DefinedBy(Api.LANGUAGE_MODEL) 713 public Name getSimpleName() { 714 return name; 715 } 716 717 /** 718 * This is the implementation for {@code 719 * javax.lang.model.element.Element.getAnnotationMirrors()}. 720 */ 721 @Override @DefinedBy(Api.LANGUAGE_MODEL) 722 public List<Attribute.Compound> getAnnotationMirrors() { 723 apiComplete(); 724 return getRawAttributes(); 725 } 726 727 728 // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList 729 @DefinedBy(Api.LANGUAGE_MODEL) 730 public java.util.List<Symbol> getEnclosedElements() { 731 return List.nil(); 732 } 733 734 public List<TypeVariableSymbol> getTypeParameters() { 735 ListBuffer<TypeVariableSymbol> l = new ListBuffer<>(); 736 for (Type t : type.getTypeArguments()) { 737 Assert.check(t.tsym.getKind() == ElementKind.TYPE_PARAMETER); 738 l.append((TypeVariableSymbol)t.tsym); 739 } 740 return l.toList(); 741 } 742 743 public static class DelegatedSymbol<T extends Symbol> extends Symbol { 744 protected T other; 745 public DelegatedSymbol(T other) { 746 super(other.kind, other.flags_field, other.name, other.type, other.owner); 747 this.other = other; 748 } 749 public String toString() { return other.toString(); } 750 public Symbol location() { return other.location(); } 751 public Symbol location(Type site, Types types) { return other.location(site, types); } 752 public Symbol baseSymbol() { return other; } 753 public Type erasure(Types types) { return other.erasure(types); } 754 public Type externalType(Types types) { return other.externalType(types); } 755 public boolean isLocal() { return other.isLocal(); } 756 public boolean isConstructor() { return other.isConstructor(); } 757 public Name getQualifiedName() { return other.getQualifiedName(); } 758 public Name flatName() { return other.flatName(); } 759 public WriteableScope members() { return other.members(); } 760 public boolean isInner() { return other.isInner(); } 761 public boolean hasOuterInstance() { return other.hasOuterInstance(); } 762 public ClassSymbol enclClass() { return other.enclClass(); } 763 public ClassSymbol outermostClass() { return other.outermostClass(); } 764 public PackageSymbol packge() { return other.packge(); } 765 public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); } 766 public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); } 767 public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); } 768 public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); } 769 public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); } 770 public void complete() throws CompletionFailure { other.complete(); } 771 772 @DefinedBy(Api.LANGUAGE_MODEL) 773 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 774 return other.accept(v, p); 775 } 776 777 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 778 return v.visitSymbol(other, p); 779 } 780 781 public T getUnderlyingSymbol() { 782 return other; 783 } 784 } 785 786 /** A base class for Symbols representing types. 787 */ 788 public static abstract class TypeSymbol extends Symbol { 789 public TypeSymbol(Kind kind, long flags, Name name, Type type, Symbol owner) { 790 super(kind, flags, name, type, owner); 791 } 792 /** form a fully qualified name from a name and an owner 793 */ 794 static public Name formFullName(Name name, Symbol owner) { 795 if (owner == null) return name; 796 if ((owner.kind != ERR) && 797 (owner.kind.matches(KindSelector.VAL_MTH) || 798 (owner.kind == TYP && owner.type.hasTag(TYPEVAR)) 799 )) return name; 800 Name prefix = owner.getQualifiedName(); 801 if (prefix == null || prefix == prefix.table.names.empty) 802 return name; 803 else return prefix.append('.', name); 804 } 805 806 /** form a fully qualified name from a name and an owner, after 807 * converting to flat representation 808 */ 809 static public Name formFlatName(Name name, Symbol owner) { 810 if (owner == null || owner.kind.matches(KindSelector.VAL_MTH) || 811 (owner.kind == TYP && owner.type.hasTag(TYPEVAR)) 812 ) return name; 813 char sep = owner.kind == TYP ? '$' : '.'; 814 Name prefix = owner.flatName(); 815 if (prefix == null || prefix == prefix.table.names.empty) 816 return name; 817 else return prefix.append(sep, name); 818 } 819 820 /** 821 * A partial ordering between type symbols that refines the 822 * class inheritance graph. 823 * 824 * Type variables always precede other kinds of symbols. 825 */ 826 public final boolean precedes(TypeSymbol that, Types types) { 827 if (this == that) 828 return false; 829 if (type.hasTag(that.type.getTag())) { 830 if (type.hasTag(CLASS)) { 831 return 832 types.rank(that.type) < types.rank(this.type) || 833 types.rank(that.type) == types.rank(this.type) && 834 that.getQualifiedName().compareTo(this.getQualifiedName()) < 0; 835 } else if (type.hasTag(TYPEVAR)) { 836 return types.isSubtype(this.type, that.type); 837 } 838 } 839 return type.hasTag(TYPEVAR); 840 } 841 842 @Override @DefinedBy(Api.LANGUAGE_MODEL) 843 public List<Symbol> getEnclosedElements() { 844 List<Symbol> list = List.nil(); 845 if (kind == TYP && type.hasTag(TYPEVAR)) { 846 return list; 847 } 848 apiComplete(); 849 for (Symbol sym : members().getSymbols(NON_RECURSIVE)) { 850 sym.apiComplete(); 851 if ((sym.flags() & SYNTHETIC) == 0 && sym.owner == this && sym.kind != ERR) { 852 list = list.prepend(sym); 853 } 854 } 855 return list; 856 } 857 858 public AnnotationTypeMetadata getAnnotationTypeMetadata() { 859 Assert.error("Only on ClassSymbol"); 860 return null; //unreachable 861 } 862 863 public boolean isAnnotationType() { return false; } 864 865 @Override 866 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 867 return v.visitTypeSymbol(this, p); 868 } 869 } 870 871 /** 872 * Type variables are represented by instances of this class. 873 */ 874 public static class TypeVariableSymbol 875 extends TypeSymbol implements TypeParameterElement { 876 877 public TypeVariableSymbol(long flags, Name name, Type type, Symbol owner) { 878 super(TYP, flags, name, type, owner); 879 } 880 881 @DefinedBy(Api.LANGUAGE_MODEL) 882 public ElementKind getKind() { 883 return ElementKind.TYPE_PARAMETER; 884 } 885 886 @Override @DefinedBy(Api.LANGUAGE_MODEL) 887 public Symbol getGenericElement() { 888 return owner; 889 } 890 891 @DefinedBy(Api.LANGUAGE_MODEL) 892 public List<Type> getBounds() { 893 TypeVar t = (TypeVar)type; 894 Type bound = t.getUpperBound(); 895 if (!bound.isCompound()) 896 return List.of(bound); 897 ClassType ct = (ClassType)bound; 898 if (!ct.tsym.erasure_field.isInterface()) { 899 return ct.interfaces_field.prepend(ct.supertype_field); 900 } else { 901 // No superclass was given in bounds. 902 // In this case, supertype is Object, erasure is first interface. 903 return ct.interfaces_field; 904 } 905 } 906 907 @Override @DefinedBy(Api.LANGUAGE_MODEL) 908 public List<Attribute.Compound> getAnnotationMirrors() { 909 // Declaration annotations on type variables are stored in type attributes 910 // on the owner of the TypeVariableSymbol 911 List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes(); 912 int index = owner.getTypeParameters().indexOf(this); 913 List<Attribute.Compound> res = List.nil(); 914 for (Attribute.TypeCompound a : candidates) { 915 if (isCurrentSymbolsAnnotation(a, index)) 916 res = res.prepend(a); 917 } 918 919 return res.reverse(); 920 } 921 922 // Helper to getAnnotation[s] 923 @Override 924 public <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) { 925 String name = annoType.getName(); 926 927 // Declaration annotations on type variables are stored in type attributes 928 // on the owner of the TypeVariableSymbol 929 List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes(); 930 int index = owner.getTypeParameters().indexOf(this); 931 for (Attribute.TypeCompound anno : candidates) 932 if (isCurrentSymbolsAnnotation(anno, index) && 933 name.contentEquals(anno.type.tsym.flatName())) 934 return anno; 935 936 return null; 937 } 938 //where: 939 boolean isCurrentSymbolsAnnotation(Attribute.TypeCompound anno, int index) { 940 return (anno.position.type == TargetType.CLASS_TYPE_PARAMETER || 941 anno.position.type == TargetType.METHOD_TYPE_PARAMETER) && 942 anno.position.parameter_index == index; 943 } 944 945 946 @Override @DefinedBy(Api.LANGUAGE_MODEL) 947 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 948 return v.visitTypeParameter(this, p); 949 } 950 } 951 /** A class for module symbols. 952 */ 953 public static class ModuleSymbol extends TypeSymbol 954 implements ModuleElement { 955 956 public Name version; 957 public JavaFileManager.Location sourceLocation; 958 public JavaFileManager.Location classLocation; 959 public JavaFileManager.Location patchLocation; 960 public JavaFileManager.Location patchOutputLocation; 961 962 /** All directives, in natural order. */ 963 public List<com.sun.tools.javac.code.Directive> directives; 964 public List<com.sun.tools.javac.code.Directive.RequiresDirective> requires; 965 public List<com.sun.tools.javac.code.Directive.ExportsDirective> exports; 966 public List<com.sun.tools.javac.code.Directive.OpensDirective> opens; 967 public List<com.sun.tools.javac.code.Directive.ProvidesDirective> provides; 968 public List<com.sun.tools.javac.code.Directive.UsesDirective> uses; 969 970 public ClassSymbol module_info; 971 972 public PackageSymbol unnamedPackage; 973 public Map<Name, PackageSymbol> visiblePackages; 974 public Set<ModuleSymbol> readModules; 975 public List<Symbol> enclosedPackages = List.nil(); 976 977 public Completer usesProvidesCompleter = Completer.NULL_COMPLETER; 978 public final Set<ModuleFlags> flags = EnumSet.noneOf(ModuleFlags.class); 979 public final Set<ModuleResolutionFlags> resolutionFlags = EnumSet.noneOf(ModuleResolutionFlags.class); 980 981 /** 982 * Create a ModuleSymbol with an associated module-info ClassSymbol. 983 */ 984 public static ModuleSymbol create(Name name, Name module_info) { 985 ModuleSymbol msym = new ModuleSymbol(name, null); 986 ClassSymbol info = new ClassSymbol(Flags.MODULE, module_info, msym); 987 info.fullname = formFullName(module_info, msym); 988 info.flatname = info.fullname; 989 info.members_field = WriteableScope.create(info); 990 msym.module_info = info; 991 return msym; 992 } 993 994 public ModuleSymbol(Name name, Symbol owner) { 995 super(MDL, 0, name, null, owner); 996 Assert.checkNonNull(name); 997 this.type = new ModuleType(this); 998 } 999 1000 @Override 1001 public int poolTag() { 1002 return ClassFile.CONSTANT_Module; 1003 } 1004 1005 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1006 public Name getSimpleName() { 1007 return Convert.shortName(name); 1008 } 1009 1010 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1011 public boolean isOpen() { 1012 return flags.contains(ModuleFlags.OPEN); 1013 } 1014 1015 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1016 public boolean isUnnamed() { 1017 return name.isEmpty() && owner == null; 1018 } 1019 1020 @Override 1021 public boolean isDeprecated() { 1022 return hasDeprecatedAnnotation(); 1023 } 1024 1025 public boolean isNoModule() { 1026 return false; 1027 } 1028 1029 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1030 public ElementKind getKind() { 1031 return ElementKind.MODULE; 1032 } 1033 1034 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1035 public java.util.List<Directive> getDirectives() { 1036 apiComplete(); 1037 completeUsesProvides(); 1038 return Collections.unmodifiableList(directives); 1039 } 1040 1041 public void completeUsesProvides() { 1042 if (usesProvidesCompleter != Completer.NULL_COMPLETER) { 1043 Completer c = usesProvidesCompleter; 1044 usesProvidesCompleter = Completer.NULL_COMPLETER; 1045 c.complete(this); 1046 } 1047 } 1048 1049 @Override 1050 public ClassSymbol outermostClass() { 1051 return null; 1052 } 1053 1054 @Override 1055 public String toString() { 1056 // TODO: the following strings should be localized 1057 // Do this with custom anon subtypes in Symtab 1058 String n = (name == null) ? "<unknown>" 1059 : (name.isEmpty()) ? "<unnamed>" 1060 : String.valueOf(name); 1061 return n; 1062 } 1063 1064 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1065 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 1066 return v.visitModule(this, p); 1067 } 1068 1069 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1070 public List<Symbol> getEnclosedElements() { 1071 List<Symbol> list = List.nil(); 1072 for (Symbol sym : enclosedPackages) { 1073 if (sym.members().anyMatch(m -> m.kind == TYP)) 1074 list = list.prepend(sym); 1075 } 1076 return list; 1077 } 1078 1079 public void reset() { 1080 this.directives = null; 1081 this.requires = null; 1082 this.exports = null; 1083 this.provides = null; 1084 this.uses = null; 1085 this.visiblePackages = null; 1086 } 1087 1088 } 1089 1090 public enum ModuleFlags { 1091 OPEN(0x0020), 1092 SYNTHETIC(0x1000), 1093 MANDATED(0x8000); 1094 1095 public static int value(Set<ModuleFlags> s) { 1096 int v = 0; 1097 for (ModuleFlags f: s) 1098 v |= f.value; 1099 return v; 1100 } 1101 1102 private ModuleFlags(int value) { 1103 this.value = value; 1104 } 1105 1106 public final int value; 1107 } 1108 1109 public enum ModuleResolutionFlags { 1110 DO_NOT_RESOLVE_BY_DEFAULT(0x0001), 1111 WARN_DEPRECATED(0x0002), 1112 WARN_DEPRECATED_REMOVAL(0x0004), 1113 WARN_INCUBATING(0x0008); 1114 1115 public static int value(Set<ModuleResolutionFlags> s) { 1116 int v = 0; 1117 for (ModuleResolutionFlags f: s) 1118 v |= f.value; 1119 return v; 1120 } 1121 1122 private ModuleResolutionFlags(int value) { 1123 this.value = value; 1124 } 1125 1126 public final int value; 1127 } 1128 1129 /** A class for package symbols 1130 */ 1131 public static class PackageSymbol extends TypeSymbol 1132 implements PackageElement { 1133 1134 public WriteableScope members_field; 1135 public Name fullname; 1136 public ClassSymbol package_info; // see bug 6443073 1137 public ModuleSymbol modle; 1138 // the file containing the documentation comments for the package 1139 public JavaFileObject sourcefile; 1140 1141 public PackageSymbol(Name name, Type type, Symbol owner) { 1142 super(PCK, 0, name, type, owner); 1143 this.members_field = null; 1144 this.fullname = formFullName(name, owner); 1145 } 1146 1147 public PackageSymbol(Name name, Symbol owner) { 1148 this(name, null, owner); 1149 this.type = new PackageType(this); 1150 } 1151 1152 public String toString() { 1153 return fullname.toString(); 1154 } 1155 1156 @DefinedBy(Api.LANGUAGE_MODEL) 1157 public Name getQualifiedName() { 1158 return fullname; 1159 } 1160 1161 @DefinedBy(Api.LANGUAGE_MODEL) 1162 public boolean isUnnamed() { 1163 return name.isEmpty() && owner != null; 1164 } 1165 1166 public WriteableScope members() { 1167 complete(); 1168 return members_field; 1169 } 1170 1171 @Override 1172 public int poolTag() { 1173 return ClassFile.CONSTANT_Package; 1174 } 1175 1176 public long flags() { 1177 complete(); 1178 return flags_field; 1179 } 1180 1181 @Override 1182 public List<Attribute.Compound> getRawAttributes() { 1183 complete(); 1184 if (package_info != null) { 1185 package_info.complete(); 1186 mergeAttributes(); 1187 } 1188 return super.getRawAttributes(); 1189 } 1190 1191 private void mergeAttributes() { 1192 if (metadata == null && 1193 package_info.metadata != null) { 1194 metadata = new SymbolMetadata(this); 1195 metadata.setAttributes(package_info.metadata); 1196 } 1197 } 1198 1199 /** A package "exists" if a type or package that exists has 1200 * been seen within it. 1201 */ 1202 public boolean exists() { 1203 return (flags_field & EXISTS) != 0; 1204 } 1205 1206 @DefinedBy(Api.LANGUAGE_MODEL) 1207 public ElementKind getKind() { 1208 return ElementKind.PACKAGE; 1209 } 1210 1211 @DefinedBy(Api.LANGUAGE_MODEL) 1212 public Symbol getEnclosingElement() { 1213 return modle != null && !modle.isNoModule() ? modle : null; 1214 } 1215 1216 @DefinedBy(Api.LANGUAGE_MODEL) 1217 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 1218 return v.visitPackage(this, p); 1219 } 1220 1221 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 1222 return v.visitPackageSymbol(this, p); 1223 } 1224 1225 /**Resets the Symbol into the state good for next round of annotation processing.*/ 1226 public void reset() { 1227 metadata = null; 1228 } 1229 1230 } 1231 1232 public static class RootPackageSymbol extends PackageSymbol { 1233 public final MissingInfoHandler missingInfoHandler; 1234 1235 public RootPackageSymbol(Name name, Symbol owner, MissingInfoHandler missingInfoHandler) { 1236 super(name, owner); 1237 this.missingInfoHandler = missingInfoHandler; 1238 } 1239 1240 } 1241 1242 /** A class for class symbols 1243 */ 1244 public static class ClassSymbol extends TypeSymbol implements TypeElement { 1245 1246 /** a scope for all class members; variables, methods and inner classes 1247 * type parameters are not part of this scope 1248 */ 1249 public WriteableScope members_field; 1250 1251 /** the fully qualified name of the class, i.e. pck.outer.inner. 1252 * null for anonymous classes 1253 */ 1254 public Name fullname; 1255 1256 /** the fully qualified name of the class after converting to flat 1257 * representation, i.e. pck.outer$inner, 1258 * set externally for local and anonymous classes 1259 */ 1260 public Name flatname; 1261 1262 /** the sourcefile where the class came from 1263 */ 1264 public JavaFileObject sourcefile; 1265 1266 /** the classfile from where to load this class 1267 * this will have extension .class or .java 1268 */ 1269 public JavaFileObject classfile; 1270 1271 /** the list of translated local classes (used for generating 1272 * InnerClasses attribute) 1273 */ 1274 public List<ClassSymbol> trans_local; 1275 1276 /** the annotation metadata attached to this class */ 1277 private AnnotationTypeMetadata annotationTypeMetadata; 1278 1279 /* the list of any of record components, only non empty if the class is a record 1280 * and it has at least one record component 1281 */ 1282 private List<RecordComponent> recordComponents = List.nil(); 1283 1284 public ClassSymbol(long flags, Name name, Type type, Symbol owner) { 1285 super(TYP, flags, name, type, owner); 1286 this.members_field = null; 1287 this.fullname = formFullName(name, owner); 1288 this.flatname = formFlatName(name, owner); 1289 this.sourcefile = null; 1290 this.classfile = null; 1291 this.annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType(); 1292 } 1293 1294 public ClassSymbol(long flags, Name name, Symbol owner) { 1295 this( 1296 flags, 1297 name, 1298 new ClassType(Type.noType, null, null), 1299 owner); 1300 this.type.tsym = this; 1301 } 1302 1303 /** The Java source which this symbol represents. 1304 */ 1305 public String toString() { 1306 return className(); 1307 } 1308 1309 public long flags() { 1310 complete(); 1311 return flags_field; 1312 } 1313 1314 public WriteableScope members() { 1315 complete(); 1316 return members_field; 1317 } 1318 1319 @Override 1320 public List<Attribute.Compound> getRawAttributes() { 1321 complete(); 1322 return super.getRawAttributes(); 1323 } 1324 1325 @Override 1326 public List<Attribute.TypeCompound> getRawTypeAttributes() { 1327 complete(); 1328 return super.getRawTypeAttributes(); 1329 } 1330 1331 public Type erasure(Types types) { 1332 if (erasure_field == null) 1333 erasure_field = new ClassType(types.erasure(type.getEnclosingType()), 1334 List.nil(), this, 1335 type.getMetadata()); 1336 return erasure_field; 1337 } 1338 1339 public String className() { 1340 if (name.isEmpty()) 1341 return 1342 Log.getLocalizedString("anonymous.class", flatname); 1343 else 1344 return fullname.toString(); 1345 } 1346 1347 @DefinedBy(Api.LANGUAGE_MODEL) 1348 public Name getQualifiedName() { 1349 return fullname; 1350 } 1351 1352 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1353 public List<Symbol> getEnclosedElements() { 1354 List<Symbol> result = super.getEnclosedElements(); 1355 if (!recordComponents.isEmpty()) { 1356 List<RecordComponent> reversed = recordComponents.reverse(); 1357 for (RecordComponent rc : reversed) { 1358 result = result.prepend(rc); 1359 } 1360 } 1361 return result; 1362 } 1363 1364 public Name flatName() { 1365 return flatname; 1366 } 1367 1368 public boolean isSubClass(Symbol base, Types types) { 1369 if (this == base) { 1370 return true; 1371 } else if ((base.flags() & INTERFACE) != 0) { 1372 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t)) 1373 for (List<Type> is = types.interfaces(t); 1374 is.nonEmpty(); 1375 is = is.tail) 1376 if (is.head.tsym.isSubClass(base, types)) return true; 1377 } else { 1378 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t)) 1379 if (t.tsym == base) return true; 1380 } 1381 return false; 1382 } 1383 1384 /** Complete the elaboration of this symbol's definition. 1385 */ 1386 public void complete() throws CompletionFailure { 1387 Completer origCompleter = completer; 1388 try { 1389 super.complete(); 1390 } catch (CompletionFailure ex) { 1391 ex.dcfh.classSymbolCompleteFailed(this, origCompleter); 1392 // quiet error recovery 1393 flags_field |= (PUBLIC|STATIC); 1394 this.type = new ErrorType(this, Type.noType); 1395 throw ex; 1396 } 1397 } 1398 1399 @DefinedBy(Api.LANGUAGE_MODEL) 1400 public List<Type> getInterfaces() { 1401 apiComplete(); 1402 if (type instanceof ClassType) { 1403 ClassType t = (ClassType)type; 1404 if (t.interfaces_field == null) // FIXME: shouldn't be null 1405 t.interfaces_field = List.nil(); 1406 if (t.all_interfaces_field != null) 1407 return Type.getModelTypes(t.all_interfaces_field); 1408 return t.interfaces_field; 1409 } else { 1410 return List.nil(); 1411 } 1412 } 1413 1414 @DefinedBy(Api.LANGUAGE_MODEL) 1415 public Type getSuperclass() { 1416 apiComplete(); 1417 if (type instanceof ClassType) { 1418 ClassType t = (ClassType)type; 1419 if (t.supertype_field == null) // FIXME: shouldn't be null 1420 t.supertype_field = Type.noType; 1421 // An interface has no superclass; its supertype is Object. 1422 return t.isInterface() 1423 ? Type.noType 1424 : t.supertype_field.getModelType(); 1425 } else { 1426 return Type.noType; 1427 } 1428 } 1429 1430 /** 1431 * Returns the next class to search for inherited annotations or {@code null} 1432 * if the next class can't be found. 1433 */ 1434 private ClassSymbol getSuperClassToSearchForAnnotations() { 1435 1436 Type sup = getSuperclass(); 1437 1438 if (!sup.hasTag(CLASS) || sup.isErroneous()) 1439 return null; 1440 1441 return (ClassSymbol) sup.tsym; 1442 } 1443 1444 1445 @Override 1446 protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) { 1447 1448 ClassSymbol sup = getSuperClassToSearchForAnnotations(); 1449 1450 return sup == null ? super.getInheritedAnnotations(annoType) 1451 : sup.getAnnotationsByType(annoType); 1452 } 1453 1454 1455 @DefinedBy(Api.LANGUAGE_MODEL) 1456 @SuppressWarnings("preview") 1457 public ElementKind getKind() { 1458 apiComplete(); 1459 long flags = flags(); 1460 if ((flags & ANNOTATION) != 0) 1461 return ElementKind.ANNOTATION_TYPE; 1462 else if ((flags & INTERFACE) != 0) 1463 return ElementKind.INTERFACE; 1464 else if ((flags & ENUM) != 0) 1465 return ElementKind.ENUM; 1466 else if ((flags & RECORD) != 0) 1467 return ElementKind.RECORD; 1468 else 1469 return ElementKind.CLASS; 1470 } 1471 1472 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1473 public Set<Modifier> getModifiers() { 1474 apiComplete(); 1475 long flags = flags(); 1476 return Flags.asModifierSet(flags & ~DEFAULT); 1477 } 1478 1479 public RecordComponent getRecordComponent(VarSymbol field) { 1480 for (RecordComponent rc : recordComponents) { 1481 if (rc.name == field.name) { 1482 return rc; 1483 } 1484 } 1485 return null; 1486 } 1487 1488 public RecordComponent getRecordComponent(JCVariableDecl var, boolean addIfMissing, List<JCAnnotation> annotations) { 1489 for (RecordComponent rc : recordComponents) { 1490 if (rc.name == var.name) { 1491 return rc; 1492 } 1493 } 1494 RecordComponent rc = null; 1495 if (addIfMissing) { 1496 recordComponents = recordComponents.append(rc = new RecordComponent(var, annotations)); 1497 } 1498 return rc; 1499 } 1500 1501 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1502 @SuppressWarnings("preview") 1503 public List<? extends RecordComponent> getRecordComponents() { 1504 return recordComponents; 1505 } 1506 1507 @DefinedBy(Api.LANGUAGE_MODEL) 1508 public NestingKind getNestingKind() { 1509 apiComplete(); 1510 if (owner.kind == PCK) 1511 return NestingKind.TOP_LEVEL; 1512 else if (name.isEmpty()) 1513 return NestingKind.ANONYMOUS; 1514 else if (owner.kind == MTH) 1515 return NestingKind.LOCAL; 1516 else 1517 return NestingKind.MEMBER; 1518 } 1519 1520 @Override 1521 protected <A extends Annotation> Attribute.Compound getAttribute(final Class<A> annoType) { 1522 1523 Attribute.Compound attrib = super.getAttribute(annoType); 1524 1525 boolean inherited = annoType.isAnnotationPresent(Inherited.class); 1526 if (attrib != null || !inherited) 1527 return attrib; 1528 1529 // Search supertypes 1530 ClassSymbol superType = getSuperClassToSearchForAnnotations(); 1531 return superType == null ? null 1532 : superType.getAttribute(annoType); 1533 } 1534 1535 @DefinedBy(Api.LANGUAGE_MODEL) 1536 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 1537 return v.visitType(this, p); 1538 } 1539 1540 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 1541 return v.visitClassSymbol(this, p); 1542 } 1543 1544 public void markAbstractIfNeeded(Types types) { 1545 if (types.enter.getEnv(this) != null && 1546 (flags() & ENUM) != 0 && types.supertype(type).tsym == types.syms.enumSym && 1547 (flags() & (FINAL | ABSTRACT)) == 0) { 1548 if (types.firstUnimplementedAbstract(this) != null) 1549 // add the ABSTRACT flag to an enum 1550 flags_field |= ABSTRACT; 1551 } 1552 } 1553 1554 /**Resets the Symbol into the state good for next round of annotation processing.*/ 1555 public void reset() { 1556 kind = TYP; 1557 erasure_field = null; 1558 members_field = null; 1559 flags_field = 0; 1560 if (type instanceof ClassType) { 1561 ClassType t = (ClassType)type; 1562 t.setEnclosingType(Type.noType); 1563 t.rank_field = -1; 1564 t.typarams_field = null; 1565 t.allparams_field = null; 1566 t.supertype_field = null; 1567 t.interfaces_field = null; 1568 t.all_interfaces_field = null; 1569 } 1570 clearAnnotationMetadata(); 1571 } 1572 1573 public void clearAnnotationMetadata() { 1574 metadata = null; 1575 annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType(); 1576 } 1577 1578 @Override 1579 public AnnotationTypeMetadata getAnnotationTypeMetadata() { 1580 return annotationTypeMetadata; 1581 } 1582 1583 @Override 1584 public boolean isAnnotationType() { 1585 return (flags_field & Flags.ANNOTATION) != 0; 1586 } 1587 1588 public void setAnnotationTypeMetadata(AnnotationTypeMetadata a) { 1589 Assert.checkNonNull(a); 1590 Assert.check(!annotationTypeMetadata.isMetadataForAnnotationType()); 1591 this.annotationTypeMetadata = a; 1592 } 1593 1594 public boolean isRecord() { 1595 return (flags_field & RECORD) != 0; 1596 } 1597 } 1598 1599 1600 /** A class for variable symbols 1601 */ 1602 public static class VarSymbol extends Symbol implements VariableElement { 1603 1604 /** The variable's declaration position. 1605 */ 1606 public int pos = Position.NOPOS; 1607 1608 /** The variable's address. Used for different purposes during 1609 * flow analysis, translation and code generation. 1610 * Flow analysis: 1611 * If this is a blank final or local variable, its sequence number. 1612 * Translation: 1613 * If this is a private field, its access number. 1614 * Code generation: 1615 * If this is a local variable, its logical slot number. 1616 */ 1617 public int adr = -1; 1618 1619 /** Construct a variable symbol, given its flags, name, type and owner. 1620 */ 1621 public VarSymbol(long flags, Name name, Type type, Symbol owner) { 1622 super(VAR, flags, name, type, owner); 1623 } 1624 1625 @Override 1626 public int poolTag() { 1627 return ClassFile.CONSTANT_Fieldref; 1628 } 1629 1630 public MethodHandleSymbol asMethodHandle(boolean getter) { 1631 return new MethodHandleSymbol(this, getter); 1632 } 1633 1634 /** Clone this symbol with new owner. 1635 */ 1636 public VarSymbol clone(Symbol newOwner) { 1637 VarSymbol v = new VarSymbol(flags_field, name, type, newOwner) { 1638 @Override 1639 public Symbol baseSymbol() { 1640 return VarSymbol.this; 1641 } 1642 1643 @Override 1644 public Object poolKey(Types types) { 1645 return new Pair<>(newOwner, baseSymbol()); 1646 } 1647 }; 1648 v.pos = pos; 1649 v.adr = adr; 1650 v.data = data; 1651 // System.out.println("clone " + v + " in " + newOwner);//DEBUG 1652 return v; 1653 } 1654 1655 public String toString() { 1656 return name.toString(); 1657 } 1658 1659 public Symbol asMemberOf(Type site, Types types) { 1660 return new VarSymbol(flags_field, name, types.memberType(site, this), owner); 1661 } 1662 1663 @DefinedBy(Api.LANGUAGE_MODEL) 1664 public ElementKind getKind() { 1665 long flags = flags(); 1666 if ((flags & PARAMETER) != 0) { 1667 if (isExceptionParameter()) 1668 return ElementKind.EXCEPTION_PARAMETER; 1669 else 1670 return ElementKind.PARAMETER; 1671 } else if ((flags & ENUM) != 0) { 1672 return ElementKind.ENUM_CONSTANT; 1673 } else if (owner.kind == TYP || owner.kind == ERR) { 1674 return ElementKind.FIELD; 1675 } else if (isResourceVariable()) { 1676 return ElementKind.RESOURCE_VARIABLE; 1677 } else if ((flags & MATCH_BINDING) != 0) { 1678 @SuppressWarnings("preview") 1679 ElementKind kind = ElementKind.BINDING_VARIABLE; 1680 return kind; 1681 } else { 1682 return ElementKind.LOCAL_VARIABLE; 1683 } 1684 } 1685 1686 @DefinedBy(Api.LANGUAGE_MODEL) 1687 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 1688 return v.visitVariable(this, p); 1689 } 1690 1691 @DefinedBy(Api.LANGUAGE_MODEL) 1692 public Object getConstantValue() { // Mirror API 1693 return Constants.decode(getConstValue(), type); 1694 } 1695 1696 public void setLazyConstValue(final Env<AttrContext> env, 1697 final Attr attr, 1698 final JCVariableDecl variable) 1699 { 1700 setData((Callable<Object>)() -> attr.attribLazyConstantValue(env, variable, type)); 1701 } 1702 1703 /** 1704 * The variable's constant value, if this is a constant. 1705 * Before the constant value is evaluated, it points to an 1706 * initializer environment. If this is not a constant, it can 1707 * be used for other stuff. 1708 */ 1709 private Object data; 1710 1711 public boolean isExceptionParameter() { 1712 return data == ElementKind.EXCEPTION_PARAMETER; 1713 } 1714 1715 public boolean isResourceVariable() { 1716 return data == ElementKind.RESOURCE_VARIABLE; 1717 } 1718 1719 public Object getConstValue() { 1720 // TODO: Consider if getConstValue and getConstantValue can be collapsed 1721 if (data == ElementKind.EXCEPTION_PARAMETER || 1722 data == ElementKind.RESOURCE_VARIABLE) { 1723 return null; 1724 } else if (data instanceof Callable<?>) { 1725 // In this case, this is a final variable, with an as 1726 // yet unevaluated initializer. 1727 Callable<?> eval = (Callable<?>)data; 1728 data = null; // to make sure we don't evaluate this twice. 1729 try { 1730 data = eval.call(); 1731 } catch (Exception ex) { 1732 throw new AssertionError(ex); 1733 } 1734 } 1735 return data; 1736 } 1737 1738 public void setData(Object data) { 1739 Assert.check(!(data instanceof Env<?>), this); 1740 this.data = data; 1741 } 1742 1743 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 1744 return v.visitVarSymbol(this, p); 1745 } 1746 } 1747 1748 @SuppressWarnings("preview") 1749 public static class RecordComponent extends VarSymbol implements RecordComponentElement { 1750 public MethodSymbol accessor; 1751 public JCTree.JCMethodDecl accessorMeth; 1752 private final List<JCAnnotation> originalAnnos; 1753 1754 /** 1755 * Construct a record component, given its flags, name, type and owner. 1756 */ 1757 public RecordComponent(JCVariableDecl fieldDecl, List<JCAnnotation> annotations) { 1758 super(PUBLIC, fieldDecl.sym.name, fieldDecl.sym.type, fieldDecl.sym.owner); 1759 this.originalAnnos = annotations; 1760 } 1761 1762 public List<JCAnnotation> getOriginalAnnos() { return originalAnnos; } 1763 1764 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1765 @SuppressWarnings("preview") 1766 public ElementKind getKind() { 1767 return ElementKind.RECORD_COMPONENT; 1768 } 1769 1770 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1771 public ExecutableElement getAccessor() { 1772 return accessor; 1773 } 1774 1775 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1776 @SuppressWarnings("preview") 1777 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 1778 return v.visitRecordComponent(this, p); 1779 } 1780 } 1781 1782 public static class ParamSymbol extends VarSymbol { 1783 public ParamSymbol(long flags, Name name, Type type, Symbol owner) { 1784 super(flags, name, type, owner); 1785 } 1786 1787 @Override 1788 public Name getSimpleName() { 1789 if ((flags_field & NAME_FILLED) == 0) { 1790 flags_field |= NAME_FILLED; 1791 Symbol rootPack = this; 1792 while (rootPack != null && !(rootPack instanceof RootPackageSymbol)) { 1793 rootPack = rootPack.owner; 1794 } 1795 if (rootPack != null) { 1796 Name inferredName = 1797 ((RootPackageSymbol) rootPack).missingInfoHandler.getParameterName(this); 1798 if (inferredName != null) { 1799 this.name = inferredName; 1800 } 1801 } 1802 } 1803 return super.getSimpleName(); 1804 } 1805 1806 } 1807 1808 public static class BindingSymbol extends VarSymbol { 1809 1810 public BindingSymbol(Name name, Type type, Symbol owner) { 1811 super(Flags.FINAL | Flags.HASINIT | Flags.MATCH_BINDING, name, type, owner); 1812 } 1813 1814 public boolean isAliasFor(BindingSymbol b) { 1815 return aliases().containsAll(b.aliases()); 1816 } 1817 1818 List<BindingSymbol> aliases() { 1819 return List.of(this); 1820 } 1821 1822 public void preserveBinding() { 1823 flags_field |= Flags.MATCH_BINDING_TO_OUTER; 1824 } 1825 1826 public boolean isPreserved() { 1827 return (flags_field & Flags.MATCH_BINDING_TO_OUTER) != 0; 1828 } 1829 } 1830 1831 /** A class for method symbols. 1832 */ 1833 public static class MethodSymbol extends Symbol implements ExecutableElement { 1834 1835 /** The code of the method. */ 1836 public Code code = null; 1837 1838 /** The extra (synthetic/mandated) parameters of the method. */ 1839 public List<VarSymbol> extraParams = List.nil(); 1840 1841 /** The captured local variables in an anonymous class */ 1842 public List<VarSymbol> capturedLocals = List.nil(); 1843 1844 /** The parameters of the method. */ 1845 public List<VarSymbol> params = null; 1846 1847 /** For an annotation type element, its default value if any. 1848 * The value is null if none appeared in the method 1849 * declaration. 1850 */ 1851 public Attribute defaultValue = null; 1852 1853 /** Construct a method symbol, given its flags, name, type and owner. 1854 */ 1855 public MethodSymbol(long flags, Name name, Type type, Symbol owner) { 1856 super(MTH, flags, name, type, owner); 1857 if (owner.type.hasTag(TYPEVAR)) Assert.error(owner + "." + name); 1858 } 1859 1860 /** Clone this symbol with new owner. 1861 */ 1862 public MethodSymbol clone(Symbol newOwner) { 1863 MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner) { 1864 @Override 1865 public Symbol baseSymbol() { 1866 return MethodSymbol.this; 1867 } 1868 1869 @Override 1870 public Object poolKey(Types types) { 1871 return new Pair<>(newOwner, baseSymbol()); 1872 } 1873 }; 1874 m.code = code; 1875 return m; 1876 } 1877 1878 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1879 public Set<Modifier> getModifiers() { 1880 long flags = flags(); 1881 return Flags.asModifierSet((flags & DEFAULT) != 0 ? flags & ~ABSTRACT : flags); 1882 } 1883 1884 /** The Java source which this symbol represents. 1885 */ 1886 public String toString() { 1887 if ((flags() & BLOCK) != 0) { 1888 return owner.name.toString(); 1889 } else { 1890 String s = (name == name.table.names.init) 1891 ? owner.name.toString() 1892 : name.toString(); 1893 if (type != null) { 1894 if (type.hasTag(FORALL)) 1895 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s; 1896 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")"; 1897 } 1898 return s; 1899 } 1900 } 1901 1902 @Override 1903 public int poolTag() { 1904 return owner.isInterface() ? 1905 ClassFile.CONSTANT_InterfaceMethodref : ClassFile.CONSTANT_Methodref; 1906 } 1907 1908 public boolean isHandle() { 1909 return false; 1910 } 1911 1912 1913 public MethodHandleSymbol asHandle() { 1914 return new MethodHandleSymbol(this); 1915 } 1916 1917 /** find a symbol that this (proxy method) symbol implements. 1918 * @param c The class whose members are searched for 1919 * implementations 1920 */ 1921 public Symbol implemented(TypeSymbol c, Types types) { 1922 Symbol impl = null; 1923 for (List<Type> is = types.interfaces(c.type); 1924 impl == null && is.nonEmpty(); 1925 is = is.tail) { 1926 TypeSymbol i = is.head.tsym; 1927 impl = implementedIn(i, types); 1928 if (impl == null) 1929 impl = implemented(i, types); 1930 } 1931 return impl; 1932 } 1933 1934 public Symbol implementedIn(TypeSymbol c, Types types) { 1935 Symbol impl = null; 1936 for (Symbol sym : c.members().getSymbolsByName(name)) { 1937 if (this.overrides(sym, (TypeSymbol)owner, types, true) && 1938 // FIXME: I suspect the following requires a 1939 // subst() for a parametric return type. 1940 types.isSameType(type.getReturnType(), 1941 types.memberType(owner.type, sym).getReturnType())) { 1942 impl = sym; 1943 } 1944 } 1945 return impl; 1946 } 1947 1948 /** Will the erasure of this method be considered by the VM to 1949 * override the erasure of the other when seen from class `origin'? 1950 */ 1951 public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) { 1952 if (isConstructor() || _other.kind != MTH) return false; 1953 1954 if (this == _other) return true; 1955 MethodSymbol other = (MethodSymbol)_other; 1956 1957 // check for a direct implementation 1958 if (other.isOverridableIn((TypeSymbol)owner) && 1959 types.asSuper(owner.type, other.owner) != null && 1960 types.isSameType(erasure(types), other.erasure(types))) 1961 return true; 1962 1963 // check for an inherited implementation 1964 return 1965 (flags() & ABSTRACT) == 0 && 1966 other.isOverridableIn(origin) && 1967 this.isMemberOf(origin, types) && 1968 types.isSameType(erasure(types), other.erasure(types)); 1969 } 1970 1971 /** The implementation of this (abstract) symbol in class origin, 1972 * from the VM's point of view, null if method does not have an 1973 * implementation in class. 1974 * @param origin The class of which the implementation is a member. 1975 */ 1976 public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) { 1977 for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) { 1978 for (Symbol sym : c.members().getSymbolsByName(name)) { 1979 if (sym.kind == MTH && 1980 ((MethodSymbol)sym).binaryOverrides(this, origin, types)) 1981 return (MethodSymbol)sym; 1982 } 1983 } 1984 return null; 1985 } 1986 1987 /** Does this symbol override `other' symbol, when both are seen as 1988 * members of class `origin'? It is assumed that _other is a member 1989 * of origin. 1990 * 1991 * It is assumed that both symbols have the same name. The static 1992 * modifier is ignored for this test. 1993 * 1994 * A quirk in the works is that if the receiver is a method symbol for 1995 * an inherited abstract method we answer false summarily all else being 1996 * immaterial. Abstract "own" methods (i.e `this' is a direct member of 1997 * origin) don't get rejected as summarily and are put to test against the 1998 * suitable criteria. 1999 * 2000 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4 2001 */ 2002 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) { 2003 return overrides(_other, origin, types, checkResult, true); 2004 } 2005 2006 /** Does this symbol override `other' symbol, when both are seen as 2007 * members of class `origin'? It is assumed that _other is a member 2008 * of origin. 2009 * 2010 * Caveat: If `this' is an abstract inherited member of origin, it is 2011 * deemed to override `other' only when `requireConcreteIfInherited' 2012 * is false. 2013 * 2014 * It is assumed that both symbols have the same name. The static 2015 * modifier is ignored for this test. 2016 * 2017 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4 2018 */ 2019 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult, 2020 boolean requireConcreteIfInherited) { 2021 if (isConstructor() || _other.kind != MTH) return false; 2022 2023 if (this == _other) return true; 2024 MethodSymbol other = (MethodSymbol)_other; 2025 2026 // check for a direct implementation 2027 if (other.isOverridableIn((TypeSymbol)owner) && 2028 types.asSuper(owner.type, other.owner) != null) { 2029 Type mt = types.memberType(owner.type, this); 2030 Type ot = types.memberType(owner.type, other); 2031 if (types.isSubSignature(mt, ot)) { 2032 if (!checkResult) 2033 return true; 2034 if (types.returnTypeSubstitutable(mt, ot)) 2035 return true; 2036 } 2037 } 2038 2039 // check for an inherited implementation 2040 if (((flags() & ABSTRACT) != 0 && requireConcreteIfInherited) || 2041 ((other.flags() & ABSTRACT) == 0 && (other.flags() & DEFAULT) == 0) || 2042 !other.isOverridableIn(origin) || 2043 !this.isMemberOf(origin, types)) 2044 return false; 2045 2046 // assert types.asSuper(origin.type, other.owner) != null; 2047 Type mt = types.memberType(origin.type, this); 2048 Type ot = types.memberType(origin.type, other); 2049 return 2050 types.isSubSignature(mt, ot) && 2051 (!checkResult || types.resultSubtype(mt, ot, types.noWarnings)); 2052 } 2053 2054 private boolean isOverridableIn(TypeSymbol origin) { 2055 // JLS 8.4.6.1 2056 switch ((int)(flags_field & Flags.AccessFlags)) { 2057 case Flags.PRIVATE: 2058 return false; 2059 case Flags.PUBLIC: 2060 return !this.owner.isInterface() || 2061 (flags_field & STATIC) == 0; 2062 case Flags.PROTECTED: 2063 return (origin.flags() & INTERFACE) == 0; 2064 case 0: 2065 // for package private: can only override in the same 2066 // package 2067 return 2068 this.packge() == origin.packge() && 2069 (origin.flags() & INTERFACE) == 0; 2070 default: 2071 return false; 2072 } 2073 } 2074 2075 @Override 2076 public boolean isInheritedIn(Symbol clazz, Types types) { 2077 switch ((int)(flags_field & Flags.AccessFlags)) { 2078 case PUBLIC: 2079 return !this.owner.isInterface() || 2080 clazz == owner || 2081 (flags_field & STATIC) == 0; 2082 default: 2083 return super.isInheritedIn(clazz, types); 2084 } 2085 } 2086 2087 public boolean isLambdaMethod() { 2088 return (flags() & LAMBDA_METHOD) == LAMBDA_METHOD; 2089 } 2090 2091 /** override this method to point to the original enclosing method if this method symbol represents a synthetic 2092 * lambda method 2093 */ 2094 public MethodSymbol originalEnclosingMethod() { 2095 return this; 2096 } 2097 2098 /** The implementation of this (abstract) symbol in class origin; 2099 * null if none exists. Synthetic methods are not considered 2100 * as possible implementations. 2101 */ 2102 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) { 2103 return implementation(origin, types, checkResult, implementation_filter); 2104 } 2105 // where 2106 public static final Filter<Symbol> implementation_filter = s -> 2107 s.kind == MTH && (s.flags() & SYNTHETIC) == 0; 2108 2109 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) { 2110 MethodSymbol res = types.implementation(this, origin, checkResult, implFilter); 2111 if (res != null) 2112 return res; 2113 // if origin is derived from a raw type, we might have missed 2114 // an implementation because we do not know enough about instantiations. 2115 // in this case continue with the supertype as origin. 2116 if (types.isDerivedRaw(origin.type) && !origin.isInterface()) 2117 return implementation(types.supertype(origin.type).tsym, types, checkResult); 2118 else 2119 return null; 2120 } 2121 2122 public List<VarSymbol> params() { 2123 owner.complete(); 2124 if (params == null) { 2125 ListBuffer<VarSymbol> newParams = new ListBuffer<>(); 2126 int i = 0; 2127 for (Type t : type.getParameterTypes()) { 2128 Name paramName = name.table.fromString("arg" + i); 2129 VarSymbol param = new VarSymbol(PARAMETER, paramName, t, this); 2130 newParams.append(param); 2131 i++; 2132 } 2133 params = newParams.toList(); 2134 } 2135 Assert.checkNonNull(params); 2136 return params; 2137 } 2138 2139 public Symbol asMemberOf(Type site, Types types) { 2140 return new MethodSymbol(flags_field, name, types.memberType(site, this), owner); 2141 } 2142 2143 @DefinedBy(Api.LANGUAGE_MODEL) 2144 public ElementKind getKind() { 2145 if (name == name.table.names.init) 2146 return ElementKind.CONSTRUCTOR; 2147 else if (name == name.table.names.clinit) 2148 return ElementKind.STATIC_INIT; 2149 else if ((flags() & BLOCK) != 0) 2150 return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT; 2151 else 2152 return ElementKind.METHOD; 2153 } 2154 2155 public boolean isStaticOrInstanceInit() { 2156 return getKind() == ElementKind.STATIC_INIT || 2157 getKind() == ElementKind.INSTANCE_INIT; 2158 } 2159 2160 @DefinedBy(Api.LANGUAGE_MODEL) 2161 public Attribute getDefaultValue() { 2162 return defaultValue; 2163 } 2164 2165 @DefinedBy(Api.LANGUAGE_MODEL) 2166 public List<VarSymbol> getParameters() { 2167 return params(); 2168 } 2169 2170 @DefinedBy(Api.LANGUAGE_MODEL) 2171 public boolean isVarArgs() { 2172 return (flags() & VARARGS) != 0; 2173 } 2174 2175 @DefinedBy(Api.LANGUAGE_MODEL) 2176 public boolean isDefault() { 2177 return (flags() & DEFAULT) != 0; 2178 } 2179 2180 @DefinedBy(Api.LANGUAGE_MODEL) 2181 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 2182 return v.visitExecutable(this, p); 2183 } 2184 2185 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 2186 return v.visitMethodSymbol(this, p); 2187 } 2188 2189 @DefinedBy(Api.LANGUAGE_MODEL) 2190 public Type getReceiverType() { 2191 Type result = asType().getReceiverType(); 2192 return (result == null) ? Type.noType : result; 2193 } 2194 2195 @DefinedBy(Api.LANGUAGE_MODEL) 2196 public Type getReturnType() { 2197 return asType().getReturnType(); 2198 } 2199 2200 @DefinedBy(Api.LANGUAGE_MODEL) 2201 public List<Type> getThrownTypes() { 2202 return asType().getThrownTypes(); 2203 } 2204 } 2205 2206 /** A class for invokedynamic method calls. 2207 */ 2208 public static class DynamicMethodSymbol extends MethodSymbol implements Dynamic { 2209 2210 public LoadableConstant[] staticArgs; 2211 public MethodHandleSymbol bsm; 2212 2213 public DynamicMethodSymbol(Name name, Symbol owner, MethodHandleSymbol bsm, Type type, LoadableConstant[] staticArgs) { 2214 super(0, name, type, owner); 2215 this.bsm = bsm; 2216 this.staticArgs = staticArgs; 2217 } 2218 2219 @Override 2220 public boolean isDynamic() { 2221 return true; 2222 } 2223 2224 @Override 2225 public LoadableConstant[] staticArgs() { 2226 return staticArgs; 2227 } 2228 2229 @Override 2230 public MethodHandleSymbol bootstrapMethod() { 2231 return bsm; 2232 } 2233 2234 @Override 2235 public int poolTag() { 2236 return ClassFile.CONSTANT_InvokeDynamic; 2237 } 2238 2239 @Override 2240 public Type dynamicType() { 2241 return type; 2242 } 2243 } 2244 2245 /** A class for condy. 2246 */ 2247 public static class DynamicVarSymbol extends VarSymbol implements Dynamic, LoadableConstant { 2248 public LoadableConstant[] staticArgs; 2249 public MethodHandleSymbol bsm; 2250 2251 public DynamicVarSymbol(Name name, Symbol owner, MethodHandleSymbol bsm, Type type, LoadableConstant[] staticArgs) { 2252 super(0, name, type, owner); 2253 this.bsm = bsm; 2254 this.staticArgs = staticArgs; 2255 } 2256 2257 @Override 2258 public boolean isDynamic() { 2259 return true; 2260 } 2261 2262 @Override 2263 public PoolConstant dynamicType() { 2264 return type; 2265 } 2266 2267 @Override 2268 public LoadableConstant[] staticArgs() { 2269 return staticArgs; 2270 } 2271 2272 @Override 2273 public LoadableConstant bootstrapMethod() { 2274 return bsm; 2275 } 2276 2277 @Override 2278 public int poolTag() { 2279 return ClassFile.CONSTANT_Dynamic; 2280 } 2281 } 2282 2283 /** A class for method handles. 2284 */ 2285 public static class MethodHandleSymbol extends MethodSymbol implements LoadableConstant { 2286 2287 private Symbol refSym; 2288 private boolean getter; 2289 2290 public MethodHandleSymbol(Symbol msym) { 2291 this(msym, false); 2292 } 2293 2294 public MethodHandleSymbol(Symbol msym, boolean getter) { 2295 super(msym.flags_field, msym.name, msym.type, msym.owner); 2296 this.refSym = msym; 2297 this.getter = getter; 2298 } 2299 2300 /** 2301 * Returns the kind associated with this method handle. 2302 */ 2303 public int referenceKind() { 2304 if (refSym.kind == VAR) { 2305 return getter ? 2306 refSym.isStatic() ? ClassFile.REF_getStatic : ClassFile.REF_getField : 2307 refSym.isStatic() ? ClassFile.REF_putStatic : ClassFile.REF_putField; 2308 } else { 2309 if (refSym.isConstructor()) { 2310 return ClassFile.REF_newInvokeSpecial; 2311 } else { 2312 if (refSym.isStatic()) { 2313 return ClassFile.REF_invokeStatic; 2314 } else if (refSym.enclClass().isInterface()) { 2315 return ClassFile.REF_invokeInterface; 2316 } else { 2317 return ClassFile.REF_invokeVirtual; 2318 } 2319 } 2320 } 2321 } 2322 2323 @Override 2324 public int poolTag() { 2325 return ClassFile.CONSTANT_MethodHandle; 2326 } 2327 2328 @Override 2329 public Object poolKey(Types types) { 2330 return new Pair<>(baseSymbol(), referenceKind()); 2331 } 2332 2333 @Override 2334 public MethodHandleSymbol asHandle() { 2335 return this; 2336 } 2337 2338 @Override 2339 public Symbol baseSymbol() { 2340 return refSym; 2341 } 2342 2343 2344 @Override 2345 public boolean isHandle() { 2346 return true; 2347 } 2348 } 2349 2350 /** A class for predefined operators. 2351 */ 2352 public static class OperatorSymbol extends MethodSymbol { 2353 2354 public int opcode; 2355 private int accessCode = Integer.MIN_VALUE; 2356 2357 public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) { 2358 super(PUBLIC | STATIC, name, type, owner); 2359 this.opcode = opcode; 2360 } 2361 2362 @Override 2363 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 2364 return v.visitOperatorSymbol(this, p); 2365 } 2366 2367 public int getAccessCode(Tag tag) { 2368 if (accessCode != Integer.MIN_VALUE && !tag.isIncOrDecUnaryOp()) { 2369 return accessCode; 2370 } 2371 accessCode = AccessCode.from(tag, opcode); 2372 return accessCode; 2373 } 2374 2375 /** Access codes for dereferencing, assignment, 2376 * and pre/post increment/decrement. 2377 2378 * All access codes for accesses to the current class are even. 2379 * If a member of the superclass should be accessed instead (because 2380 * access was via a qualified super), add one to the corresponding code 2381 * for the current class, making the number odd. 2382 * This numbering scheme is used by the backend to decide whether 2383 * to issue an invokevirtual or invokespecial call. 2384 * 2385 * @see Gen#visitSelect(JCFieldAccess tree) 2386 */ 2387 public enum AccessCode { 2388 UNKNOWN(-1, Tag.NO_TAG), 2389 DEREF(0, Tag.NO_TAG), 2390 ASSIGN(2, Tag.ASSIGN), 2391 PREINC(4, Tag.PREINC), 2392 PREDEC(6, Tag.PREDEC), 2393 POSTINC(8, Tag.POSTINC), 2394 POSTDEC(10, Tag.POSTDEC), 2395 FIRSTASGOP(12, Tag.NO_TAG); 2396 2397 public final int code; 2398 public final Tag tag; 2399 public static final int numberOfAccessCodes = (lushrl - ishll + lxor + 2 - iadd) * 2 + FIRSTASGOP.code + 2; 2400 2401 AccessCode(int code, Tag tag) { 2402 this.code = code; 2403 this.tag = tag; 2404 } 2405 2406 static public AccessCode getFromCode(int code) { 2407 for (AccessCode aCodes : AccessCode.values()) { 2408 if (aCodes.code == code) { 2409 return aCodes; 2410 } 2411 } 2412 return UNKNOWN; 2413 } 2414 2415 static int from(Tag tag, int opcode) { 2416 /** Map bytecode of binary operation to access code of corresponding 2417 * assignment operation. This is always an even number. 2418 */ 2419 switch (tag) { 2420 case PREINC: 2421 return AccessCode.PREINC.code; 2422 case PREDEC: 2423 return AccessCode.PREDEC.code; 2424 case POSTINC: 2425 return AccessCode.POSTINC.code; 2426 case POSTDEC: 2427 return AccessCode.POSTDEC.code; 2428 } 2429 if (iadd <= opcode && opcode <= lxor) { 2430 return (opcode - iadd) * 2 + FIRSTASGOP.code; 2431 } else if (opcode == string_add) { 2432 return (lxor + 1 - iadd) * 2 + FIRSTASGOP.code; 2433 } else if (ishll <= opcode && opcode <= lushrl) { 2434 return (opcode - ishll + lxor + 2 - iadd) * 2 + FIRSTASGOP.code; 2435 } 2436 return -1; 2437 } 2438 } 2439 } 2440 2441 /** Symbol completer interface. 2442 */ 2443 public static interface Completer { 2444 2445 /** Dummy completer to be used when the symbol has been completed or 2446 * does not need completion. 2447 */ 2448 public final static Completer NULL_COMPLETER = new Completer() { 2449 public void complete(Symbol sym) { } 2450 public boolean isTerminal() { return true; } 2451 }; 2452 2453 void complete(Symbol sym) throws CompletionFailure; 2454 2455 /** Returns true if this completer is <em>terminal</em>. A terminal 2456 * completer is used as a place holder when the symbol is completed. 2457 * Calling complete on a terminal completer will not affect the symbol. 2458 * 2459 * The dummy NULL_COMPLETER and the GraphDependencies completer are 2460 * examples of terminal completers. 2461 * 2462 * @return true iff this completer is terminal 2463 */ 2464 default boolean isTerminal() { 2465 return false; 2466 } 2467 } 2468 2469 public static class CompletionFailure extends RuntimeException { 2470 private static final long serialVersionUID = 0; 2471 public final transient DeferredCompletionFailureHandler dcfh; 2472 public transient Symbol sym; 2473 2474 /** A diagnostic object describing the failure 2475 */ 2476 private transient JCDiagnostic diag; 2477 2478 private transient Supplier<JCDiagnostic> diagSupplier; 2479 2480 public CompletionFailure(Symbol sym, Supplier<JCDiagnostic> diagSupplier, DeferredCompletionFailureHandler dcfh) { 2481 this.dcfh = dcfh; 2482 this.sym = sym; 2483 this.diagSupplier = diagSupplier; 2484 // this.printStackTrace();//DEBUG 2485 } 2486 2487 public JCDiagnostic getDiagnostic() { 2488 if (diag == null && diagSupplier != null) { 2489 diag = diagSupplier.get(); 2490 } 2491 return diag; 2492 } 2493 2494 @Override 2495 public String getMessage() { 2496 return getDiagnostic().getMessage(null); 2497 } 2498 2499 public JCDiagnostic getDetailValue() { 2500 return getDiagnostic(); 2501 } 2502 2503 @Override 2504 public CompletionFailure initCause(Throwable cause) { 2505 super.initCause(cause); 2506 return this; 2507 } 2508 2509 public void resetDiagnostic(Supplier<JCDiagnostic> diagSupplier) { 2510 this.diagSupplier = diagSupplier; 2511 this.diag = null; 2512 } 2513 2514 } 2515 2516 /** 2517 * A visitor for symbols. A visitor is used to implement operations 2518 * (or relations) on symbols. Most common operations on types are 2519 * binary relations and this interface is designed for binary 2520 * relations, that is, operations on the form 2521 * Symbol × P → R. 2522 * <!-- In plain text: Type x P -> R --> 2523 * 2524 * @param <R> the return type of the operation implemented by this 2525 * visitor; use Void if no return type is needed. 2526 * @param <P> the type of the second argument (the first being the 2527 * symbol itself) of the operation implemented by this visitor; use 2528 * Void if a second argument is not needed. 2529 */ 2530 public interface Visitor<R,P> { 2531 R visitClassSymbol(ClassSymbol s, P arg); 2532 R visitMethodSymbol(MethodSymbol s, P arg); 2533 R visitPackageSymbol(PackageSymbol s, P arg); 2534 R visitOperatorSymbol(OperatorSymbol s, P arg); 2535 R visitVarSymbol(VarSymbol s, P arg); 2536 R visitTypeSymbol(TypeSymbol s, P arg); 2537 R visitSymbol(Symbol s, P arg); 2538 } 2539 }