1 /*
   2  * Copyright (c) 2003, 2019, 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.comp;
  27 
  28 import java.util.HashSet;
  29 import java.util.Set;
  30 import java.util.function.BiConsumer;
  31 
  32 import javax.tools.JavaFileObject;
  33 
  34 import com.sun.tools.javac.code.*;
  35 import com.sun.tools.javac.code.Lint.LintCategory;
  36 import com.sun.tools.javac.code.Scope.ImportFilter;
  37 import com.sun.tools.javac.code.Scope.NamedImportScope;
  38 import com.sun.tools.javac.code.Scope.StarImportScope;
  39 import com.sun.tools.javac.code.Scope.WriteableScope;
  40 import com.sun.tools.javac.code.Source.Feature;
  41 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
  42 import com.sun.tools.javac.tree.*;
  43 import com.sun.tools.javac.util.*;
  44 import com.sun.tools.javac.util.DefinedBy.Api;
  45 
  46 import com.sun.tools.javac.code.Symbol.*;
  47 import com.sun.tools.javac.code.Type.*;
  48 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  49 import com.sun.tools.javac.tree.JCTree.*;
  50 
  51 import static com.sun.tools.javac.code.Flags.*;
  52 import static com.sun.tools.javac.code.Flags.ANNOTATION;
  53 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  54 import static com.sun.tools.javac.code.Kinds.Kind.*;
  55 import static com.sun.tools.javac.code.TypeTag.CLASS;
  56 import static com.sun.tools.javac.code.TypeTag.ERROR;
  57 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  58 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  59 
  60 import com.sun.tools.javac.util.Dependencies.CompletionCause;
  61 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  62 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  63 
  64 /** This is the second phase of Enter, in which classes are completed
  65  *  by resolving their headers and entering their members in the into
  66  *  the class scope. See Enter for an overall overview.
  67  *
  68  *  This class uses internal phases to process the classes. When a phase
  69  *  processes classes, the lower phases are not invoked until all classes
  70  *  pass through the current phase. Note that it is possible that upper phases
  71  *  are run due to recursive completion. The internal phases are:
  72  *  - ImportPhase: shallow pass through imports, adds information about imports
  73  *                 the NamedImportScope and StarImportScope, but avoids queries
  74  *                 about class hierarchy.
  75  *  - HierarchyPhase: resolves the supertypes of the given class. Does not handle
  76  *                    type parameters of the class or type argument of the supertypes.
  77  *  - HeaderPhase: finishes analysis of the header of the given class by resolving
  78  *                 type parameters, attributing supertypes including type arguments
  79  *                 and scheduling full annotation attribution. This phase also adds
  80  *                 a synthetic default constructor if needed and synthetic "this" field.
  81  *  - MembersPhase: resolves headers for fields, methods and constructors in the given class.
  82  *                  Also generates synthetic enum members.
  83  *
  84  *  <p><b>This is NOT part of any supported API.
  85  *  If you write code that depends on this, you do so at your own risk.
  86  *  This code and its internal interfaces are subject to change or
  87  *  deletion without notice.</b>
  88  */
  89 public class TypeEnter implements Completer {
  90     protected static final Context.Key<TypeEnter> typeEnterKey = new Context.Key<>();
  91 
  92     /** A switch to determine whether we check for package/class conflicts
  93      */
  94     final static boolean checkClash = true;
  95 
  96     private final Names names;
  97     private final Enter enter;
  98     private final MemberEnter memberEnter;
  99     private final Log log;
 100     private final Check chk;
 101     private final Attr attr;
 102     private final Symtab syms;
 103     private final TreeMaker make;
 104     private final Todo todo;
 105     private final Annotate annotate;
 106     private final TypeAnnotations typeAnnotations;
 107     private final Types types;
 108     private final JCDiagnostic.Factory diags;
 109     private final DeferredLintHandler deferredLintHandler;
 110     private final Lint lint;
 111     private final TypeEnvs typeEnvs;
 112     private final Dependencies dependencies;
 113 
 114     public static TypeEnter instance(Context context) {
 115         TypeEnter instance = context.get(typeEnterKey);
 116         if (instance == null)
 117             instance = new TypeEnter(context);
 118         return instance;
 119     }
 120 
 121     protected TypeEnter(Context context) {
 122         context.put(typeEnterKey, this);
 123         names = Names.instance(context);
 124         enter = Enter.instance(context);
 125         memberEnter = MemberEnter.instance(context);
 126         log = Log.instance(context);
 127         chk = Check.instance(context);
 128         attr = Attr.instance(context);
 129         syms = Symtab.instance(context);
 130         make = TreeMaker.instance(context);
 131         todo = Todo.instance(context);
 132         annotate = Annotate.instance(context);
 133         typeAnnotations = TypeAnnotations.instance(context);
 134         types = Types.instance(context);
 135         diags = JCDiagnostic.Factory.instance(context);
 136         deferredLintHandler = DeferredLintHandler.instance(context);
 137         lint = Lint.instance(context);
 138         typeEnvs = TypeEnvs.instance(context);
 139         dependencies = Dependencies.instance(context);
 140         Source source = Source.instance(context);
 141         allowTypeAnnos = Feature.TYPE_ANNOTATIONS.allowedInSource(source);
 142         allowDeprecationOnImport = Feature.DEPRECATION_ON_IMPORT.allowedInSource(source);
 143     }
 144 
 145     /** Switch: support type annotations.
 146      */
 147     boolean allowTypeAnnos;
 148 
 149     /**
 150      * Switch: should deprecation warnings be issued on import
 151      */
 152     boolean allowDeprecationOnImport;
 153 
 154     /** A flag to disable completion from time to time during member
 155      *  enter, as we only need to look up types.  This avoids
 156      *  unnecessarily deep recursion.
 157      */
 158     boolean completionEnabled = true;
 159 
 160     /* Verify Imports:
 161      */
 162     protected void ensureImportsChecked(List<JCCompilationUnit> trees) {
 163         // if there remain any unimported toplevels (these must have
 164         // no classes at all), process their import statements as well.
 165         for (JCCompilationUnit tree : trees) {
 166             if (!tree.starImportScope.isFilled()) {
 167                 Env<AttrContext> topEnv = enter.topLevelEnv(tree);
 168                 finishImports(tree, () -> { completeClass.resolveImports(tree, topEnv); });
 169             }
 170         }
 171     }
 172 
 173 /* ********************************************************************
 174  * Source completer
 175  *********************************************************************/
 176 
 177     /** Complete entering a class.
 178      *  @param sym         The symbol of the class to be completed.
 179      */
 180     @Override
 181     public void complete(Symbol sym) throws CompletionFailure {
 182         // Suppress some (recursive) MemberEnter invocations
 183         if (!completionEnabled) {
 184             // Re-install same completer for next time around and return.
 185             Assert.check((sym.flags() & Flags.COMPOUND) == 0);
 186             sym.completer = this;
 187             return;
 188         }
 189 
 190         try {
 191             annotate.blockAnnotations();
 192             sym.flags_field |= UNATTRIBUTED;
 193 
 194             List<Env<AttrContext>> queue;
 195 
 196             dependencies.push((ClassSymbol) sym, CompletionCause.MEMBER_ENTER);
 197             try {
 198                 queue = completeClass.completeEnvs(List.of(typeEnvs.get((ClassSymbol) sym)));
 199             } finally {
 200                 dependencies.pop();
 201             }
 202 
 203             if (!queue.isEmpty()) {
 204                 Set<JCCompilationUnit> seen = new HashSet<>();
 205 
 206                 for (Env<AttrContext> env : queue) {
 207                     if (env.toplevel.defs.contains(env.enclClass) && seen.add(env.toplevel)) {
 208                         finishImports(env.toplevel, () -> {});
 209                     }
 210                 }
 211             }
 212         } finally {
 213             annotate.unblockAnnotations();
 214         }
 215     }
 216 
 217     void finishImports(JCCompilationUnit toplevel, Runnable resolve) {
 218         JavaFileObject prev = log.useSource(toplevel.sourcefile);
 219         try {
 220             resolve.run();
 221             chk.checkImportsUnique(toplevel);
 222             chk.checkImportsResolvable(toplevel);
 223             chk.checkImportedPackagesObservable(toplevel);
 224             toplevel.namedImportScope.finalizeScope();
 225             toplevel.starImportScope.finalizeScope();
 226         } catch (CompletionFailure cf) {
 227             chk.completionError(toplevel.pos(), cf);
 228         } finally {
 229             log.useSource(prev);
 230         }
 231     }
 232 
 233     abstract class Phase {
 234         private final ListBuffer<Env<AttrContext>> queue = new ListBuffer<>();
 235         private final Phase next;
 236         private final CompletionCause phaseName;
 237 
 238         Phase(CompletionCause phaseName, Phase next) {
 239             this.phaseName = phaseName;
 240             this.next = next;
 241         }
 242 
 243         public final List<Env<AttrContext>> completeEnvs(List<Env<AttrContext>> envs) {
 244             boolean firstToComplete = queue.isEmpty();
 245 
 246             Phase prevTopLevelPhase = topLevelPhase;
 247             boolean success = false;
 248 
 249             try {
 250                 topLevelPhase = this;
 251                 doCompleteEnvs(envs);
 252                 success = true;
 253             } finally {
 254                 topLevelPhase = prevTopLevelPhase;
 255                 if (!success && firstToComplete) {
 256                     //an exception was thrown, e.g. BreakAttr:
 257                     //the queue would become stale, clear it:
 258                     queue.clear();
 259                 }
 260             }
 261 
 262             if (firstToComplete) {
 263                 List<Env<AttrContext>> out = queue.toList();
 264 
 265                 queue.clear();
 266                 return next != null ? next.completeEnvs(out) : out;
 267             } else {
 268                 return List.nil();
 269             }
 270         }
 271 
 272         protected void doCompleteEnvs(List<Env<AttrContext>> envs) {
 273             for (Env<AttrContext> env : envs) {
 274                 JCClassDecl tree = (JCClassDecl)env.tree;
 275 
 276                 queue.add(env);
 277 
 278                 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
 279                 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
 280                 try {
 281                     dependencies.push(env.enclClass.sym, phaseName);
 282                     runPhase(env);
 283                 } catch (CompletionFailure ex) {
 284                     chk.completionError(tree.pos(), ex);
 285                 } finally {
 286                     dependencies.pop();
 287                     deferredLintHandler.setPos(prevLintPos);
 288                     log.useSource(prev);
 289                 }
 290             }
 291         }
 292 
 293         protected abstract void runPhase(Env<AttrContext> env);
 294     }
 295 
 296     private final ImportsPhase completeClass = new ImportsPhase();
 297     private Phase topLevelPhase;
 298 
 299     /**Analyze import clauses.
 300      */
 301     private final class ImportsPhase extends Phase {
 302 
 303         public ImportsPhase() {
 304             super(CompletionCause.IMPORTS_PHASE, new HierarchyPhase());
 305         }
 306 
 307         Env<AttrContext> env;
 308         ImportFilter staticImportFilter;
 309         ImportFilter typeImportFilter;
 310         BiConsumer<JCImport, CompletionFailure> cfHandler =
 311                 (imp, cf) -> chk.completionError(imp.pos(), cf);
 312 
 313         @Override
 314         protected void runPhase(Env<AttrContext> env) {
 315             JCClassDecl tree = env.enclClass;
 316             ClassSymbol sym = tree.sym;
 317 
 318             // If sym is a toplevel-class, make sure any import
 319             // clauses in its source file have been seen.
 320             if (sym.owner.kind == PCK) {
 321                 resolveImports(env.toplevel, env.enclosing(TOPLEVEL));
 322                 todo.append(env);
 323             }
 324 
 325             if (sym.owner.kind == TYP)
 326                 sym.owner.complete();
 327         }
 328 
 329         private void resolveImports(JCCompilationUnit tree, Env<AttrContext> env) {
 330             if (tree.starImportScope.isFilled()) {
 331                 // we must have already processed this toplevel
 332                 return;
 333             }
 334 
 335             ImportFilter prevStaticImportFilter = staticImportFilter;
 336             ImportFilter prevTypeImportFilter = typeImportFilter;
 337             DiagnosticPosition prevLintPos = deferredLintHandler.immediate();
 338             Lint prevLint = chk.setLint(lint);
 339             Env<AttrContext> prevEnv = this.env;
 340             try {
 341                 this.env = env;
 342                 final PackageSymbol packge = env.toplevel.packge;
 343                 this.staticImportFilter =
 344                         (origin, sym) -> sym.isStatic() &&
 345                                          chk.importAccessible(sym, packge) &&
 346                                          sym.isMemberOf((TypeSymbol) origin.owner, types);
 347                 this.typeImportFilter =
 348                         (origin, sym) -> sym.kind == TYP &&
 349                                          chk.importAccessible(sym, packge);
 350 
 351                 // Import-on-demand java.lang.
 352                 PackageSymbol javaLang = syms.enterPackage(syms.java_base, names.java_lang);
 353                 if (javaLang.members().isEmpty() && !javaLang.exists())
 354                     throw new FatalError(diags.fragment(Fragments.FatalErrNoJavaLang));
 355                 importAll(make.at(tree.pos()).Import(make.QualIdent(javaLang), false), javaLang, env);
 356 
 357                 JCModuleDecl decl = tree.getModuleDecl();
 358 
 359                 // Process the package def and all import clauses.
 360                 if (tree.getPackage() != null && decl == null)
 361                     checkClassPackageClash(tree.getPackage());
 362 
 363                 for (JCImport imp : tree.getImports()) {
 364                     doImport(imp);
 365                 }
 366 
 367                 if (decl != null) {
 368                     //check @Deprecated:
 369                     markDeprecated(decl.sym, decl.mods.annotations, env);
 370                     // process module annotations
 371                     annotate.annotateLater(decl.mods.annotations, env, env.toplevel.modle, null);
 372                 }
 373             } finally {
 374                 this.env = prevEnv;
 375                 chk.setLint(prevLint);
 376                 deferredLintHandler.setPos(prevLintPos);
 377                 this.staticImportFilter = prevStaticImportFilter;
 378                 this.typeImportFilter = prevTypeImportFilter;
 379             }
 380         }
 381 
 382         private void checkClassPackageClash(JCPackageDecl tree) {
 383             // check that no class exists with same fully qualified name as
 384             // toplevel package
 385             if (checkClash && tree.pid != null) {
 386                 Symbol p = env.toplevel.packge;
 387                 while (p.owner != syms.rootPackage) {
 388                     p.owner.complete(); // enter all class members of p
 389                     //need to lookup the owning module/package:
 390                     PackageSymbol pack = syms.lookupPackage(env.toplevel.modle, p.owner.getQualifiedName());
 391                     if (syms.getClass(pack.modle, p.getQualifiedName()) != null) {
 392                         log.error(tree.pos,
 393                                   Errors.PkgClashesWithClassOfSameName(p));
 394                     }
 395                     p = p.owner;
 396                 }
 397             }
 398             // process package annotations
 399             annotate.annotateLater(tree.annotations, env, env.toplevel.packge, null);
 400         }
 401 
 402         private void doImport(JCImport tree) {
 403             JCFieldAccess imp = (JCFieldAccess)tree.qualid;
 404             Name name = TreeInfo.name(imp);
 405 
 406             // Create a local environment pointing to this tree to disable
 407             // effects of other imports in Resolve.findGlobalType
 408             Env<AttrContext> localEnv = env.dup(tree);
 409 
 410             TypeSymbol p = attr.attribImportQualifier(tree, localEnv).tsym;
 411             if (name == names.asterisk) {
 412                 // Import on demand.
 413                 chk.checkCanonical(imp.selected);
 414                 if (tree.staticImport)
 415                     importStaticAll(tree, p, env);
 416                 else
 417                     importAll(tree, p, env);
 418             } else {
 419                 // Named type import.
 420                 if (tree.staticImport) {
 421                     importNamedStatic(tree, p, name, localEnv);
 422                     chk.checkCanonical(imp.selected);
 423                 } else {
 424                     Type importedType = attribImportType(imp, localEnv);
 425                     Type originalType = importedType.getOriginalType();
 426                     TypeSymbol c = originalType.hasTag(CLASS) ? originalType.tsym : importedType.tsym;
 427                     chk.checkCanonical(imp);
 428                     importNamed(tree.pos(), c, env, tree);
 429                 }
 430             }
 431         }
 432 
 433         Type attribImportType(JCTree tree, Env<AttrContext> env) {
 434             Assert.check(completionEnabled);
 435             Lint prevLint = chk.setLint(allowDeprecationOnImport ?
 436                     lint : lint.suppress(LintCategory.DEPRECATION, LintCategory.REMOVAL));
 437             try {
 438                 // To prevent deep recursion, suppress completion of some
 439                 // types.
 440                 completionEnabled = false;
 441                 return attr.attribType(tree, env);
 442             } finally {
 443                 completionEnabled = true;
 444                 chk.setLint(prevLint);
 445             }
 446         }
 447 
 448         /** Import all classes of a class or package on demand.
 449          *  @param imp           The import that is being handled.
 450          *  @param tsym          The class or package the members of which are imported.
 451          *  @param env           The env in which the imported classes will be entered.
 452          */
 453         private void importAll(JCImport imp,
 454                                final TypeSymbol tsym,
 455                                Env<AttrContext> env) {
 456             env.toplevel.starImportScope.importAll(types, tsym.members(), typeImportFilter, imp, cfHandler);
 457         }
 458 
 459         /** Import all static members of a class or package on demand.
 460          *  @param imp           The import that is being handled.
 461          *  @param tsym          The class or package the members of which are imported.
 462          *  @param env           The env in which the imported classes will be entered.
 463          */
 464         private void importStaticAll(JCImport imp,
 465                                      final TypeSymbol tsym,
 466                                      Env<AttrContext> env) {
 467             final StarImportScope toScope = env.toplevel.starImportScope;
 468             final TypeSymbol origin = tsym;
 469 
 470             toScope.importAll(types, origin.members(), staticImportFilter, imp, cfHandler);
 471         }
 472 
 473         /** Import statics types of a given name.  Non-types are handled in Attr.
 474          *  @param imp           The import that is being handled.
 475          *  @param tsym          The class from which the name is imported.
 476          *  @param name          The (simple) name being imported.
 477          *  @param env           The environment containing the named import
 478          *                  scope to add to.
 479          */
 480         private void importNamedStatic(final JCImport imp,
 481                                        final TypeSymbol tsym,
 482                                        final Name name,
 483                                        final Env<AttrContext> env) {
 484             if (tsym.kind != TYP) {
 485                 log.error(DiagnosticFlag.RECOVERABLE, imp.pos(), Errors.StaticImpOnlyClassesAndInterfaces);
 486                 return;
 487             }
 488 
 489             final NamedImportScope toScope = env.toplevel.namedImportScope;
 490             final Scope originMembers = tsym.members();
 491 
 492             imp.importScope = toScope.importByName(types, originMembers, name, staticImportFilter, imp, cfHandler);
 493         }
 494 
 495         /** Import given class.
 496          *  @param pos           Position to be used for error reporting.
 497          *  @param tsym          The class to be imported.
 498          *  @param env           The environment containing the named import
 499          *                  scope to add to.
 500          */
 501         private void importNamed(DiagnosticPosition pos, final Symbol tsym, Env<AttrContext> env, JCImport imp) {
 502             if (tsym.kind == TYP)
 503                 imp.importScope = env.toplevel.namedImportScope.importType(tsym.owner.members(), tsym.owner.members(), tsym);
 504         }
 505 
 506     }
 507 
 508     /**Defines common utility methods used by the HierarchyPhase and HeaderPhase.
 509      */
 510     private abstract class AbstractHeaderPhase extends Phase {
 511 
 512         public AbstractHeaderPhase(CompletionCause phaseName, Phase next) {
 513             super(phaseName, next);
 514         }
 515 
 516         protected Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) {
 517             WriteableScope baseScope = WriteableScope.create(tree.sym);
 518             //import already entered local classes into base scope
 519             for (Symbol sym : env.outer.info.scope.getSymbols(NON_RECURSIVE)) {
 520                 if (sym.isLocal()) {
 521                     baseScope.enter(sym);
 522                 }
 523             }
 524             //import current type-parameters into base scope
 525             if (tree.typarams != null)
 526                 for (List<JCTypeParameter> typarams = tree.typarams;
 527                      typarams.nonEmpty();
 528                      typarams = typarams.tail)
 529                     baseScope.enter(typarams.head.type.tsym);
 530             Env<AttrContext> outer = env.outer; // the base clause can't see members of this class
 531             Env<AttrContext> localEnv = outer.dup(tree, outer.info.dup(baseScope));
 532             localEnv.baseClause = true;
 533             localEnv.outer = outer;
 534             localEnv.info.isSelfCall = false;
 535             return localEnv;
 536         }
 537 
 538         /** Generate a base clause for an enum type.
 539          *  @param pos              The position for trees and diagnostics, if any
 540          *  @param c                The class symbol of the enum
 541          */
 542         protected  JCExpression enumBase(int pos, ClassSymbol c) {
 543             JCExpression result = make.at(pos).
 544                 TypeApply(make.QualIdent(syms.enumSym),
 545                           List.of(make.Type(c.type)));
 546             return result;
 547         }
 548 
 549         protected Type modelMissingTypes(Env<AttrContext> env, Type t, final JCExpression tree, final boolean interfaceExpected) {
 550             if (!t.hasTag(ERROR))
 551                 return t;
 552 
 553             return new ErrorType(t.getOriginalType(), t.tsym) {
 554                 private Type modelType;
 555 
 556                 @Override
 557                 public Type getModelType() {
 558                     if (modelType == null)
 559                         modelType = new Synthesizer(env.toplevel.modle, getOriginalType(), interfaceExpected).visit(tree);
 560                     return modelType;
 561                 }
 562             };
 563         }
 564             // where:
 565             private class Synthesizer extends JCTree.Visitor {
 566                 ModuleSymbol msym;
 567                 Type originalType;
 568                 boolean interfaceExpected;
 569                 List<ClassSymbol> synthesizedSymbols = List.nil();
 570                 Type result;
 571 
 572                 Synthesizer(ModuleSymbol msym, Type originalType, boolean interfaceExpected) {
 573                     this.msym = msym;
 574                     this.originalType = originalType;
 575                     this.interfaceExpected = interfaceExpected;
 576                 }
 577 
 578                 Type visit(JCTree tree) {
 579                     tree.accept(this);
 580                     return result;
 581                 }
 582 
 583                 List<Type> visit(List<? extends JCTree> trees) {
 584                     ListBuffer<Type> lb = new ListBuffer<>();
 585                     for (JCTree t: trees)
 586                         lb.append(visit(t));
 587                     return lb.toList();
 588                 }
 589 
 590                 @Override
 591                 public void visitTree(JCTree tree) {
 592                     result = syms.errType;
 593                 }
 594 
 595                 @Override
 596                 public void visitIdent(JCIdent tree) {
 597                     if (!tree.type.hasTag(ERROR)) {
 598                         result = tree.type;
 599                     } else {
 600                         result = synthesizeClass(tree.name, msym.unnamedPackage).type;
 601                     }
 602                 }
 603 
 604                 @Override
 605                 public void visitSelect(JCFieldAccess tree) {
 606                     if (!tree.type.hasTag(ERROR)) {
 607                         result = tree.type;
 608                     } else {
 609                         Type selectedType;
 610                         boolean prev = interfaceExpected;
 611                         try {
 612                             interfaceExpected = false;
 613                             selectedType = visit(tree.selected);
 614                         } finally {
 615                             interfaceExpected = prev;
 616                         }
 617                         ClassSymbol c = synthesizeClass(tree.name, selectedType.tsym);
 618                         result = c.type;
 619                     }
 620                 }
 621 
 622                 @Override
 623                 public void visitTypeApply(JCTypeApply tree) {
 624                     if (!tree.type.hasTag(ERROR)) {
 625                         result = tree.type;
 626                     } else {
 627                         ClassType clazzType = (ClassType) visit(tree.clazz);
 628                         if (synthesizedSymbols.contains(clazzType.tsym))
 629                             synthesizeTyparams((ClassSymbol) clazzType.tsym, tree.arguments.size());
 630                         final List<Type> actuals = visit(tree.arguments);
 631                         result = new ErrorType(tree.type, clazzType.tsym) {
 632                             @Override @DefinedBy(Api.LANGUAGE_MODEL)
 633                             public List<Type> getTypeArguments() {
 634                                 return actuals;
 635                             }
 636                         };
 637                     }
 638                 }
 639 
 640                 ClassSymbol synthesizeClass(Name name, Symbol owner) {
 641                     int flags = interfaceExpected ? INTERFACE : 0;
 642                     ClassSymbol c = new ClassSymbol(flags, name, owner);
 643                     c.members_field = new Scope.ErrorScope(c);
 644                     c.type = new ErrorType(originalType, c) {
 645                         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 646                         public List<Type> getTypeArguments() {
 647                             return typarams_field;
 648                         }
 649                     };
 650                     synthesizedSymbols = synthesizedSymbols.prepend(c);
 651                     return c;
 652                 }
 653 
 654                 void synthesizeTyparams(ClassSymbol sym, int n) {
 655                     ClassType ct = (ClassType) sym.type;
 656                     Assert.check(ct.typarams_field.isEmpty());
 657                     if (n == 1) {
 658                         TypeVar v = new TypeVar(names.fromString("T"), sym, syms.botType);
 659                         ct.typarams_field = ct.typarams_field.prepend(v);
 660                     } else {
 661                         for (int i = n; i > 0; i--) {
 662                             TypeVar v = new TypeVar(names.fromString("T" + i), sym,
 663                                                     syms.botType);
 664                             ct.typarams_field = ct.typarams_field.prepend(v);
 665                         }
 666                     }
 667                 }
 668             }
 669 
 670         protected void attribSuperTypes(Env<AttrContext> env, Env<AttrContext> baseEnv) {
 671             JCClassDecl tree = env.enclClass;
 672             ClassSymbol sym = tree.sym;
 673             ClassType ct = (ClassType)sym.type;
 674             // Determine supertype.
 675             Type supertype;
 676             JCExpression extending;
 677             final boolean isValueType = (tree.mods.flags & Flags.VALUE) != 0;
 678 
 679             if (tree.extending != null) {
 680                 if (isValueType)
 681                     log.error(tree.pos(), Errors.ValueMayNotExtend);
 682                 extending = clearTypeParams(tree.extending);
 683                 supertype = attr.attribBase(extending, baseEnv, true, false, true);
 684             } else {
 685                 extending = null;
 686                 supertype = ((tree.mods.flags & Flags.ENUM) != 0)
 687                 ? attr.attribBase(enumBase(tree.pos, sym), baseEnv,
 688                                   true, false, false)
 689                 : (sym.fullname == names.java_lang_Object)
 690                 ? Type.noType
 691                 : syms.objectType;
 692             }
 693             ct.supertype_field = modelMissingTypes(baseEnv, supertype, extending, false);
 694 
 695             // Determine interfaces.
 696             ListBuffer<Type> interfaces = new ListBuffer<>();
 697             ListBuffer<Type> all_interfaces = null; // lazy init
 698             List<JCExpression> interfaceTrees = tree.implementing;
 699             for (JCExpression iface : interfaceTrees) {
 700                 iface = clearTypeParams(iface);
 701                 Type it = attr.attribBase(iface, baseEnv, false, true, true);
 702                 if (it.hasTag(CLASS)) {
 703                     interfaces.append(it);
 704                     if (all_interfaces != null) all_interfaces.append(it);
 705                 } else {
 706                     if (all_interfaces == null)
 707                         all_interfaces = new ListBuffer<Type>().appendList(interfaces);
 708                     all_interfaces.append(modelMissingTypes(baseEnv, it, iface, true));
 709                 }
 710             }
 711 
 712             if ((sym.flags_field & ANNOTATION) != 0) {
 713                 ct.interfaces_field = List.of(syms.annotationType);
 714                 ct.all_interfaces_field = ct.interfaces_field;
 715             }  else {
 716                 ct.interfaces_field = interfaces.toList();
 717                 ct.all_interfaces_field = (all_interfaces == null)
 718                         ? ct.interfaces_field : all_interfaces.toList();
 719             }
 720         }
 721             //where:
 722             protected JCExpression clearTypeParams(JCExpression superType) {
 723                 return superType;
 724             }
 725     }
 726 
 727     private final class HierarchyPhase extends AbstractHeaderPhase implements Completer {
 728 
 729         public HierarchyPhase() {
 730             super(CompletionCause.HIERARCHY_PHASE, new HeaderPhase());
 731         }
 732 
 733         @Override
 734         protected void doCompleteEnvs(List<Env<AttrContext>> envs) {
 735             //The ClassSymbols in the envs list may not be in the dependency order.
 736             //To get proper results, for every class or interface C, the supertypes of
 737             //C must be processed by the HierarchyPhase phase before C.
 738             //To achieve that, the HierarchyPhase is registered as the Completer for
 739             //all the classes first, and then all the classes are completed.
 740             for (Env<AttrContext> env : envs) {
 741                 env.enclClass.sym.completer = this;
 742             }
 743             for (Env<AttrContext> env : envs) {
 744                 env.enclClass.sym.complete();
 745             }
 746         }
 747 
 748         @Override
 749         protected void runPhase(Env<AttrContext> env) {
 750             JCClassDecl tree = env.enclClass;
 751             ClassSymbol sym = tree.sym;
 752             ClassType ct = (ClassType)sym.type;
 753 
 754             Env<AttrContext> baseEnv = baseEnv(tree, env);
 755 
 756             attribSuperTypes(env, baseEnv);
 757 
 758             if (sym.fullname == names.java_lang_Object) {
 759                 if (tree.extending != null) {
 760                     chk.checkNonCyclic(tree.extending.pos(),
 761                                        ct.supertype_field);
 762                     ct.supertype_field = Type.noType;
 763                 }
 764                 else if (tree.implementing.nonEmpty()) {
 765                     chk.checkNonCyclic(tree.implementing.head.pos(),
 766                                        ct.interfaces_field.head);
 767                     ct.interfaces_field = List.nil();
 768                 }
 769             }
 770 
 771             markDeprecated(sym, tree.mods.annotations, baseEnv);
 772 
 773             chk.checkNonCyclicDecl(tree);
 774         }
 775             //where:
 776             @Override
 777             protected JCExpression clearTypeParams(JCExpression superType) {
 778                 switch (superType.getTag()) {
 779                     case TYPEAPPLY:
 780                         return ((JCTypeApply) superType).clazz;
 781                 }
 782 
 783                 return superType;
 784             }
 785 
 786         @Override
 787         public void complete(Symbol sym) throws CompletionFailure {
 788             Assert.check((topLevelPhase instanceof ImportsPhase) ||
 789                          (topLevelPhase == this));
 790 
 791             if (topLevelPhase != this) {
 792                 //only do the processing based on dependencies in the HierarchyPhase:
 793                 sym.completer = this;
 794                 return ;
 795             }
 796 
 797             Env<AttrContext> env = typeEnvs.get((ClassSymbol) sym);
 798 
 799             super.doCompleteEnvs(List.of(env));
 800         }
 801 
 802     }
 803 
 804     private final class HeaderPhase extends AbstractHeaderPhase {
 805 
 806         public HeaderPhase() {
 807             super(CompletionCause.HEADER_PHASE, new MembersPhase());
 808         }
 809 
 810         @Override
 811         protected void runPhase(Env<AttrContext> env) {
 812             JCClassDecl tree = env.enclClass;
 813             ClassSymbol sym = tree.sym;
 814             ClassType ct = (ClassType)sym.type;
 815 
 816             // create an environment for evaluating the base clauses
 817             Env<AttrContext> baseEnv = baseEnv(tree, env);
 818 
 819             if (tree.extending != null)
 820                 annotate.queueScanTreeAndTypeAnnotate(tree.extending, baseEnv, sym, tree.pos());
 821             for (JCExpression impl : tree.implementing)
 822                 annotate.queueScanTreeAndTypeAnnotate(impl, baseEnv, sym, tree.pos());
 823             annotate.flush();
 824 
 825             attribSuperTypes(env, baseEnv);
 826 
 827             Set<Type> interfaceSet = new HashSet<>();
 828 
 829             for (JCExpression iface : tree.implementing) {
 830                 Type it = iface.type;
 831                 if (it.hasTag(CLASS))
 832                     chk.checkNotRepeated(iface.pos(), types.erasure(it), interfaceSet);
 833             }
 834 
 835             annotate.annotateLater(tree.mods.annotations, baseEnv,
 836                         sym, tree.pos());
 837             attr.attribTypeVariables(tree.typarams, baseEnv, false);
 838 
 839             for (JCTypeParameter tp : tree.typarams)
 840                 annotate.queueScanTreeAndTypeAnnotate(tp, baseEnv, sym, tree.pos());
 841 
 842             // check that no package exists with same fully qualified name,
 843             // but admit classes in the unnamed package which have the same
 844             // name as a top-level package.
 845             if (checkClash &&
 846                 sym.owner.kind == PCK && sym.owner != env.toplevel.modle.unnamedPackage &&
 847                 syms.packageExists(env.toplevel.modle, sym.fullname)) {
 848                 log.error(tree.pos, Errors.ClashWithPkgOfSameName(Kinds.kindName(sym),sym));
 849             }
 850             if (sym.owner.kind == PCK && (sym.flags_field & PUBLIC) == 0 &&
 851                 !env.toplevel.sourcefile.isNameCompatible(sym.name.toString(),JavaFileObject.Kind.SOURCE)) {
 852                 sym.flags_field |= AUXILIARY;
 853             }
 854         }
 855     }
 856 
 857     /** Enter member fields and methods of a class
 858      */
 859     private final class MembersPhase extends Phase {
 860 
 861         public MembersPhase() {
 862             super(CompletionCause.MEMBERS_PHASE, null);
 863         }
 864 
 865         private boolean completing;
 866         private List<Env<AttrContext>> todo = List.nil();
 867 
 868         @Override
 869         protected void doCompleteEnvs(List<Env<AttrContext>> envs) {
 870             todo = todo.prependList(envs);
 871             if (completing) {
 872                 return ; //the top-level invocation will handle all envs
 873             }
 874             boolean prevCompleting = completing;
 875             completing = true;
 876             try {
 877                 while (todo.nonEmpty()) {
 878                     Env<AttrContext> head = todo.head;
 879                     todo = todo.tail;
 880                     super.doCompleteEnvs(List.of(head));
 881                 }
 882             } finally {
 883                 completing = prevCompleting;
 884             }
 885         }
 886 
 887         @Override
 888         protected void runPhase(Env<AttrContext> env) {
 889             JCClassDecl tree = env.enclClass;
 890             ClassSymbol sym = tree.sym;
 891             ClassType ct = (ClassType)sym.type;
 892 
 893             // Add default constructor if needed.
 894             if ((sym.flags() & INTERFACE) == 0 &&
 895                 !TreeInfo.hasConstructors(tree.defs)) {
 896                 List<Type> argtypes = List.nil();
 897                 List<Type> typarams = List.nil();
 898                 List<Type> thrown = List.nil();
 899                 long ctorFlags = 0;
 900                 boolean based = false;
 901                 boolean addConstructor = true;
 902                 JCNewClass nc = null;
 903                 if (sym.name.isEmpty()) {
 904                     nc = (JCNewClass)env.next.tree;
 905                     if (nc.constructor != null) {
 906                         addConstructor = nc.constructor.kind != ERR;
 907                         Type superConstrType = types.memberType(sym.type,
 908                                                                 nc.constructor);
 909                         argtypes = superConstrType.getParameterTypes();
 910                         typarams = superConstrType.getTypeArguments();
 911                         ctorFlags = nc.constructor.flags() & VARARGS;
 912                         if (nc.encl != null) {
 913                             argtypes = argtypes.prepend(nc.encl.type);
 914                             based = true;
 915                         }
 916                         thrown = superConstrType.getThrownTypes();
 917                     }
 918                 }
 919                 if (addConstructor) {
 920                     MethodSymbol basedConstructor = nc != null ?
 921                             (MethodSymbol)nc.constructor : null;
 922                     JCTree constrDef = DefaultConstructor(make.at(tree.pos), sym,
 923                                                         basedConstructor,
 924                                                         typarams, argtypes, thrown,
 925                                                         ctorFlags, based);
 926                     tree.defs = tree.defs.prepend(constrDef);
 927                 }
 928             }
 929 
 930             // enter symbols for 'this' into current scope.
 931             VarSymbol thisSym =
 932                 new VarSymbol(FINAL | HASINIT, names._this, sym.type, sym);
 933             thisSym.pos = Position.FIRSTPOS;
 934             env.info.scope.enter(thisSym);
 935             // if this is a class, enter symbol for 'super' into current scope.
 936             if ((sym.flags_field & INTERFACE) == 0 &&
 937                     ct.supertype_field.hasTag(CLASS)) {
 938                 VarSymbol superSym =
 939                     new VarSymbol(FINAL | HASINIT, names._super,
 940                                   ct.supertype_field, sym);
 941                 superSym.pos = Position.FIRSTPOS;
 942                 env.info.scope.enter(superSym);
 943             }
 944 
 945             if (!tree.typarams.isEmpty()) {
 946                 for (JCTypeParameter tvar : tree.typarams) {
 947                     chk.checkNonCyclic(tvar, (TypeVar)tvar.type);
 948                 }
 949             }
 950 
 951             finishClass(tree, env);
 952 
 953             if (allowTypeAnnos) {
 954                 typeAnnotations.organizeTypeAnnotationsSignatures(env, (JCClassDecl)env.tree);
 955                 typeAnnotations.validateTypeAnnotationsSignatures(env, (JCClassDecl)env.tree);
 956             }
 957         }
 958 
 959         /** Enter members for a class.
 960          */
 961         void finishClass(JCClassDecl tree, Env<AttrContext> env) {
 962             if ((tree.mods.flags & Flags.ENUM) != 0 &&
 963                 !tree.sym.type.hasTag(ERROR) &&
 964                 (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) {
 965                 addEnumMembers(tree, env);
 966             }
 967             memberEnter.memberEnter(tree.defs, env);
 968 
 969             if ((tree.mods.flags & (Flags.VALUE | Flags.INTERFACE)) == Flags.VALUE && !tree.sym.type.hasTag(ERROR)) {
 970                 addValueMembers(tree, env);
 971             }
 972 
 973             if (tree.sym.isAnnotationType()) {
 974                 Assert.check(tree.sym.isCompleted());
 975                 tree.sym.setAnnotationTypeMetadata(new AnnotationTypeMetadata(tree.sym, annotate.annotationTypeSourceCompleter()));
 976             }
 977         }
 978 
 979         /** Add the implicit members for an enum type
 980          *  to the symbol table.
 981          */
 982         private void addEnumMembers(JCClassDecl tree, Env<AttrContext> env) {
 983             JCExpression valuesType = make.Type(new ArrayType(tree.sym.type, syms.arrayClass));
 984 
 985             // public static T[] values() { return ???; }
 986             JCMethodDecl values = make.
 987                 MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC),
 988                           names.values,
 989                           valuesType,
 990                           List.nil(),
 991                           List.nil(),
 992                           List.nil(), // thrown
 993                           null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))),
 994                           null);
 995             memberEnter.memberEnter(values, env);
 996 
 997             // public static T valueOf(String name) { return ???; }
 998             JCMethodDecl valueOf = make.
 999                 MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC),
1000                           names.valueOf,
1001                           make.Type(tree.sym.type),
1002                           List.nil(),
1003                           List.of(make.VarDef(make.Modifiers(Flags.PARAMETER |
1004                                                              Flags.MANDATED),
1005                                                 names.fromString("name"),
1006                                                 make.Type(syms.stringType), null)),
1007                           List.nil(), // thrown
1008                           null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))),
1009                           null);
1010             memberEnter.memberEnter(valueOf, env);
1011         }
1012 
1013         /** Add the implicit members for a value type to the parse tree and the symbol table.
1014          */
1015         private void addValueMembers(JCClassDecl tree, Env<AttrContext> env) {
1016 
1017             boolean requireHashCode = true, requireEquals = true, requireToString = true;
1018 
1019             for (JCTree def : tree.defs) {
1020                 if (def.getTag() == METHODDEF) {
1021                     JCMethodDecl methodDecl = (JCMethodDecl)def;
1022                     if (methodDecl.sym != null
1023                             && methodDecl.sym.type != null
1024                             && !methodDecl.sym.type.isErroneous()
1025                             && (methodDecl.sym.flags() & STATIC) == 0) {
1026                         final List<Type> parameterTypes = methodDecl.sym.type.getParameterTypes();
1027                         switch (parameterTypes.size()) {
1028                             case 0:
1029                                 String name = methodDecl.name.toString();
1030                                 if (name.equals("hashCode"))
1031                                     requireHashCode = false;
1032                                 else if (name.equals("toString"))
1033                                     requireToString = false;
1034                                 break;
1035                             case 1:
1036                                 name = methodDecl.name.toString();
1037                                 if (name.equals("equals") && parameterTypes.head.tsym == syms.objectType.tsym)
1038                                     requireEquals = false;
1039                                 break;
1040                         }
1041                     }
1042                 }
1043             }
1044 
1045             make.at(tree.pos);
1046             // Make a body comprising { throw new RuntimeException(""Internal error: This method must have been replaced by javac"); }
1047             JCBlock body = make.Block(Flags.SYNTHETIC, List.of(make.Throw(
1048                     make.NewClass(null,
1049                             null,
1050                             make.Ident(names.fromString("RuntimeException")),
1051                             List.of(make.Literal(CLASS, "Internal error: This method must have been replaced by javac")),
1052                             null))));
1053 
1054             if (requireHashCode) {
1055                 // public int hashCode() { throw new RuntimeException(message); }
1056                 JCMethodDecl hashCode = make.
1057                         MethodDef(make.Modifiers(Flags.PUBLIC | Flags.FINAL),
1058                                 names.hashCode,
1059                                 make.TypeIdent(TypeTag.INT),
1060                                 List.nil(),
1061                                 List.nil(),
1062                                 List.nil(), // thrown
1063                                 body,
1064                                 null);
1065                 memberEnter.memberEnter(hashCode, env);
1066                 tree.defs = tree.defs.append(hashCode);
1067             }
1068 
1069             if (requireEquals) {
1070                 // public boolean equals(Object o) { throw new RuntimeException(message); }
1071                 JCMethodDecl equals = make.
1072                         MethodDef(make.Modifiers(Flags.PUBLIC | Flags.FINAL),
1073                                 names.equals,
1074                                 make.TypeIdent(TypeTag.BOOLEAN),
1075                                 List.nil(),
1076                                 List.of(make.VarDef(make.Modifiers(PARAMETER), names.fromString("o"), make.Ident(names.fromString("Object")), null )),
1077                                 List.nil(), // thrown
1078                                 body,
1079                                 null);
1080                 memberEnter.memberEnter(equals, env);
1081                 tree.defs = tree.defs.append(equals);
1082             }
1083 
1084             if (requireToString) {
1085                 // public String toString() { throw new RuntimeException(message); }
1086                 JCMethodDecl toString = make.
1087                         MethodDef(make.Modifiers(Flags.PUBLIC | Flags.FINAL),
1088                                 names.toString,
1089                                 make.Ident(names.fromString("String")),
1090                                 List.nil(),
1091                                 List.nil(),
1092                                 List.nil(), // thrown
1093                                 body,
1094                                 null);
1095                 memberEnter.memberEnter(toString, env);
1096                 tree.defs = tree.defs.append(toString);
1097             }
1098 
1099         }
1100     }
1101 
1102 /* ***************************************************************************
1103  * tree building
1104  ****************************************************************************/
1105 
1106     /** Generate default constructor for given class. For classes different
1107      *  from java.lang.Object, this is:
1108      *
1109      *    c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
1110      *      super(x_0, ..., x_n)
1111      *    }
1112      *
1113      *  or, if based == true:
1114      *
1115      *    c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
1116      *      x_0.super(x_1, ..., x_n)
1117      *    }
1118      *
1119      *  @param make     The tree factory.
1120      *  @param c        The class owning the default constructor.
1121      *  @param argtypes The parameter types of the constructor.
1122      *  @param thrown   The thrown exceptions of the constructor.
1123      *  @param based    Is first parameter a this$n?
1124      */
1125     JCTree DefaultConstructor(TreeMaker make,
1126                             ClassSymbol c,
1127                             MethodSymbol baseInit,
1128                             List<Type> typarams,
1129                             List<Type> argtypes,
1130                             List<Type> thrown,
1131                             long flags,
1132                             boolean based) {
1133         JCTree result;
1134         if ((c.flags() & ENUM) != 0 &&
1135             (types.supertype(c.type).tsym == syms.enumSym)) {
1136             // constructors of true enums are private
1137             flags = (flags & ~AccessFlags) | PRIVATE | GENERATEDCONSTR;
1138         } else
1139             flags |= (c.flags() & AccessFlags) | GENERATEDCONSTR;
1140         if (c.name.isEmpty()) {
1141             flags |= ANONCONSTR;
1142         }
1143         if (based) {
1144             flags |= ANONCONSTR_BASED;
1145         }
1146         Type mType = new MethodType(argtypes, null, thrown, c);
1147         Type initType = typarams.nonEmpty() ?
1148             new ForAll(typarams, mType) :
1149             mType;
1150         MethodSymbol init = new MethodSymbol(flags, names.init,
1151                 initType, c);
1152         init.params = createDefaultConstructorParams(make, baseInit, init,
1153                 argtypes, based);
1154         List<JCVariableDecl> params = make.Params(argtypes, init);
1155         List<JCStatement> stats = List.nil();
1156         if (c.type != syms.objectType) {
1157             stats = stats.prepend(SuperCall(make, typarams, params, based));
1158         }
1159         result = make.MethodDef(init, make.Block(0, stats));
1160         return result;
1161     }
1162 
1163     private List<VarSymbol> createDefaultConstructorParams(
1164             TreeMaker make,
1165             MethodSymbol baseInit,
1166             MethodSymbol init,
1167             List<Type> argtypes,
1168             boolean based) {
1169         List<VarSymbol> initParams = null;
1170         List<Type> argTypesList = argtypes;
1171         if (based) {
1172             /*  In this case argtypes will have an extra type, compared to baseInit,
1173              *  corresponding to the type of the enclosing instance i.e.:
1174              *
1175              *  Inner i = outer.new Inner(1){}
1176              *
1177              *  in the above example argtypes will be (Outer, int) and baseInit
1178              *  will have parameter's types (int). So in this case we have to add
1179              *  first the extra type in argtypes and then get the names of the
1180              *  parameters from baseInit.
1181              */
1182             initParams = List.nil();
1183             VarSymbol param = new VarSymbol(PARAMETER, make.paramName(0), argtypes.head, init);
1184             initParams = initParams.append(param);
1185             argTypesList = argTypesList.tail;
1186         }
1187         if (baseInit != null && baseInit.params != null &&
1188             baseInit.params.nonEmpty() && argTypesList.nonEmpty()) {
1189             initParams = (initParams == null) ? List.nil() : initParams;
1190             List<VarSymbol> baseInitParams = baseInit.params;
1191             while (baseInitParams.nonEmpty() && argTypesList.nonEmpty()) {
1192                 VarSymbol param = new VarSymbol(baseInitParams.head.flags() | PARAMETER,
1193                         baseInitParams.head.name, argTypesList.head, init);
1194                 initParams = initParams.append(param);
1195                 baseInitParams = baseInitParams.tail;
1196                 argTypesList = argTypesList.tail;
1197             }
1198         }
1199         return initParams;
1200     }
1201 
1202     /** Generate call to superclass constructor. This is:
1203      *
1204      *    super(id_0, ..., id_n)
1205      *
1206      * or, if based == true
1207      *
1208      *    id_0.super(id_1,...,id_n)
1209      *
1210      *  where id_0, ..., id_n are the names of the given parameters.
1211      *
1212      *  @param make    The tree factory
1213      *  @param params  The parameters that need to be passed to super
1214      *  @param typarams  The type parameters that need to be passed to super
1215      *  @param based   Is first parameter a this$n?
1216      */
1217     JCExpressionStatement SuperCall(TreeMaker make,
1218                    List<Type> typarams,
1219                    List<JCVariableDecl> params,
1220                    boolean based) {
1221         JCExpression meth;
1222         if (based) {
1223             meth = make.Select(make.Ident(params.head), names._super);
1224             params = params.tail;
1225         } else {
1226             meth = make.Ident(names._super);
1227         }
1228         List<JCExpression> typeargs = typarams.nonEmpty() ? make.Types(typarams) : null;
1229         return make.Exec(make.Apply(typeargs, meth, make.Idents(params)));
1230     }
1231 
1232     /**
1233      * Mark sym deprecated if annotations contain @Deprecated annotation.
1234      */
1235     public void markDeprecated(Symbol sym, List<JCAnnotation> annotations, Env<AttrContext> env) {
1236         // In general, we cannot fully process annotations yet,  but we
1237         // can attribute the annotation types and then check to see if the
1238         // @Deprecated annotation is present.
1239         attr.attribAnnotationTypes(annotations, env);
1240         handleDeprecatedAnnotations(annotations, sym);
1241     }
1242 
1243     /**
1244      * If a list of annotations contains a reference to java.lang.Deprecated,
1245      * set the DEPRECATED flag.
1246      * If the annotation is marked forRemoval=true, also set DEPRECATED_REMOVAL.
1247      **/
1248     private void handleDeprecatedAnnotations(List<JCAnnotation> annotations, Symbol sym) {
1249         for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) {
1250             JCAnnotation a = al.head;
1251             if (a.annotationType.type == syms.deprecatedType) {
1252                 sym.flags_field |= (Flags.DEPRECATED | Flags.DEPRECATED_ANNOTATION);
1253                 a.args.stream()
1254                         .filter(e -> e.hasTag(ASSIGN))
1255                         .map(e -> (JCAssign) e)
1256                         .filter(assign -> TreeInfo.name(assign.lhs) == names.forRemoval)
1257                         .findFirst()
1258                         .ifPresent(assign -> {
1259                             JCExpression rhs = TreeInfo.skipParens(assign.rhs);
1260                             if (rhs.hasTag(LITERAL)
1261                                     && Boolean.TRUE.equals(((JCLiteral) rhs).getValue())) {
1262                                 sym.flags_field |= DEPRECATED_REMOVAL;
1263                             }
1264                         });
1265             }
1266         }
1267     }
1268 }