1 /*
   2  * Copyright (c) 1999, 2016, 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.io.IOException;
  29 import java.nio.file.Path;
  30 import java.util.EnumSet;
  31 import java.util.HashMap;
  32 import java.util.Iterator;
  33 import java.util.Map;
  34 import java.util.NoSuchElementException;
  35 import java.util.Set;
  36 
  37 import javax.lang.model.SourceVersion;
  38 import javax.tools.JavaFileManager;
  39 import javax.tools.JavaFileManager.Location;
  40 import javax.tools.JavaFileObject;
  41 import javax.tools.JavaFileObject.Kind;
  42 import javax.tools.StandardJavaFileManager;
  43 import javax.tools.StandardLocation;
  44 
  45 import com.sun.tools.javac.code.Scope.WriteableScope;
  46 import com.sun.tools.javac.code.Symbol.ClassSymbol;
  47 import com.sun.tools.javac.code.Symbol.Completer;
  48 import com.sun.tools.javac.code.Symbol.CompletionFailure;
  49 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
  50 import com.sun.tools.javac.code.Symbol.PackageSymbol;
  51 import com.sun.tools.javac.code.Symbol.TypeSymbol;
  52 import com.sun.tools.javac.comp.Annotate;
  53 import com.sun.tools.javac.file.JRTIndex;
  54 import com.sun.tools.javac.file.JavacFileManager;
  55 import com.sun.tools.javac.jvm.ClassReader;
  56 import com.sun.tools.javac.jvm.Profile;
  57 import com.sun.tools.javac.main.Option;
  58 import com.sun.tools.javac.platform.PlatformDescription;
  59 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  60 import com.sun.tools.javac.util.*;
  61 
  62 import static javax.tools.StandardLocation.*;
  63 
  64 import static com.sun.tools.javac.code.Flags.*;
  65 import static com.sun.tools.javac.code.Kinds.Kind.*;
  66 import com.sun.tools.javac.code.Symbol;
  67 import com.sun.tools.javac.code.Symbol.CompletionFailure;
  68 import com.sun.tools.javac.main.DelegatingJavaFileManager;
  69 
  70 import com.sun.tools.javac.util.Dependencies.CompletionCause;
  71 
  72 /**
  73  *  This class provides operations to locate class definitions
  74  *  from the source and class files on the paths provided to javac.
  75  *
  76  *  <p><b>This is NOT part of any supported API.
  77  *  If you write code that depends on this, you do so at your own risk.
  78  *  This code and its internal interfaces are subject to change or
  79  *  deletion without notice.</b>
  80  */
  81 public class ClassFinder {
  82     /** The context key for the class finder. */
  83     protected static final Context.Key<ClassFinder> classFinderKey = new Context.Key<>();
  84 
  85     ClassReader reader;
  86 
  87     private final Annotate annotate;
  88 
  89     /** Switch: verbose output.
  90      */
  91     boolean verbose;
  92 
  93     /**
  94      * Switch: cache completion failures unless -XDdev is used
  95      */
  96     private boolean cacheCompletionFailure;
  97 
  98     /**
  99      * Switch: prefer source files instead of newer when both source
 100      * and class are available
 101      **/
 102     protected boolean preferSource;
 103 
 104     /**
 105      * Switch: Search classpath and sourcepath for classes before the
 106      * bootclasspath
 107      */
 108     protected boolean userPathsFirst;
 109 
 110     /** The log to use for verbose output
 111      */
 112     final Log log;
 113 
 114     /** The symbol table. */
 115     Symtab syms;
 116 
 117     /** The name table. */
 118     final Names names;
 119 
 120     /** Force a completion failure on this name
 121      */
 122     final Name completionFailureName;
 123 
 124     /** Access to files
 125      */
 126     private final JavaFileManager fileManager;
 127 
 128     /** Dependency tracker
 129      */
 130     private final Dependencies dependencies;
 131 
 132     /** Factory for diagnostics
 133      */
 134     JCDiagnostic.Factory diagFactory;
 135 
 136     final DeferredCompletionFailureHandler dcfh;
 137 
 138     /** Can be reassigned from outside:
 139      *  the completer to be used for ".java" files. If this remains unassigned
 140      *  ".java" files will not be loaded.
 141      */
 142     public Completer sourceCompleter = Completer.NULL_COMPLETER;
 143 
 144     /** The path name of the class file currently being read.
 145      */
 146     protected JavaFileObject currentClassFile = null;
 147 
 148     /** The class or method currently being read.
 149      */
 150     protected Symbol currentOwner = null;
 151 
 152     /**
 153      * The currently selected profile.
 154      */
 155     private final Profile profile;
 156 
 157     /**
 158      * Use direct access to the JRTIndex to access the temporary
 159      * replacement for the info that used to be in ct.sym.
 160      * In time, this will go away and be replaced by the module system.
 161      */
 162     private final JRTIndex jrtIndex;
 163 
 164     /**
 165      * Completer that delegates to the complete-method of this class.
 166      */
 167     private final Completer thisCompleter = this::complete;
 168 
 169     public Completer getCompleter() {
 170         return thisCompleter;
 171     }
 172 
 173     /** Get the ClassFinder instance for this invocation. */
 174     public static ClassFinder instance(Context context) {
 175         ClassFinder instance = context.get(classFinderKey);
 176         if (instance == null)
 177             instance = new ClassFinder(context);
 178         return instance;
 179     }
 180 
 181     /** Construct a new class finder. */
 182     protected ClassFinder(Context context) {
 183         context.put(classFinderKey, this);
 184         reader = ClassReader.instance(context);
 185         names = Names.instance(context);
 186         syms = Symtab.instance(context);
 187         fileManager = context.get(JavaFileManager.class);
 188         dependencies = Dependencies.instance(context);
 189         if (fileManager == null)
 190             throw new AssertionError("FileManager initialization error");
 191         diagFactory = JCDiagnostic.Factory.instance(context);
 192         dcfh = DeferredCompletionFailureHandler.instance(context);
 193 
 194         log = Log.instance(context);
 195         annotate = Annotate.instance(context);
 196 
 197         Options options = Options.instance(context);
 198         verbose = options.isSet(Option.VERBOSE);
 199         cacheCompletionFailure = options.isUnset("dev");
 200         preferSource = "source".equals(options.get("-Xprefer"));
 201         userPathsFirst = options.isSet(Option.XXUSERPATHSFIRST);
 202 
 203         completionFailureName =
 204             options.isSet("failcomplete")
 205             ? names.fromString(options.get("failcomplete"))
 206             : null;
 207 
 208         // Temporary, until more info is available from the module system.
 209         boolean useCtProps;
 210         JavaFileManager fm = context.get(JavaFileManager.class);
 211         if (fm instanceof DelegatingJavaFileManager) {
 212             fm = ((DelegatingJavaFileManager) fm).getBaseFileManager();
 213         }
 214         if (fm instanceof JavacFileManager) {
 215             JavacFileManager jfm = (JavacFileManager) fm;
 216             useCtProps = jfm.isDefaultBootClassPath() && jfm.isSymbolFileEnabled();
 217         } else if (fm.getClass().getName().equals("com.sun.tools.sjavac.comp.SmartFileManager")) {
 218             useCtProps = !options.isSet("ignore.symbol.file");
 219         } else {
 220             useCtProps = false;
 221         }
 222         jrtIndex = useCtProps && JRTIndex.isAvailable() ? JRTIndex.getSharedInstance() : null;
 223 
 224         profile = Profile.instance(context);
 225         cachedCompletionFailure = new CompletionFailure(null, (JCDiagnostic) null, dcfh);
 226         cachedCompletionFailure.setStackTrace(new StackTraceElement[0]);
 227     }
 228 
 229 
 230 /************************************************************************
 231  * Temporary ct.sym replacement
 232  *
 233  * The following code is a temporary substitute for the ct.sym mechanism
 234  * used in JDK 6 thru JDK 8.
 235  * This mechanism will eventually be superseded by the Jigsaw module system.
 236  ***********************************************************************/
 237 
 238     /**
 239      * Returns any extra flags for a class symbol.
 240      * This information used to be provided using private annotations
 241      * in the class file in ct.sym; in time, this information will be
 242      * available from the module system.
 243      */
 244     long getSupplementaryFlags(ClassSymbol c) {
 245         if (jrtIndex == null || !jrtIndex.isInJRT(c.classfile) || c.name == names.module_info) {
 246             return 0;
 247         }
 248 
 249         if (supplementaryFlags == null) {
 250             supplementaryFlags = new HashMap<>();
 251         }
 252 
 253         Long flags = supplementaryFlags.get(c.packge());
 254         if (flags == null) {
 255             long newFlags = 0;
 256             try {
 257                 JRTIndex.CtSym ctSym = jrtIndex.getCtSym(c.packge().flatName());
 258                 Profile minProfile = Profile.DEFAULT;
 259                 if (ctSym.proprietary)
 260                     newFlags |= PROPRIETARY;
 261                 if (ctSym.minProfile != null)
 262                     minProfile = Profile.lookup(ctSym.minProfile);
 263                 if (profile != Profile.DEFAULT && minProfile.value > profile.value) {
 264                     newFlags |= NOT_IN_PROFILE;
 265                 }
 266             } catch (IOException ignore) {
 267             }
 268             supplementaryFlags.put(c.packge(), flags = newFlags);
 269         }
 270         return flags;
 271     }
 272 
 273     private Map<PackageSymbol, Long> supplementaryFlags;
 274 
 275 /************************************************************************
 276  * Loading Classes
 277  ***********************************************************************/
 278 
 279     /** Completion for classes to be loaded. Before a class is loaded
 280      *  we make sure its enclosing class (if any) is loaded.
 281      */
 282     private void complete(Symbol sym) throws CompletionFailure {
 283         if (sym.kind == TYP) {
 284             try {
 285                 ClassSymbol c = (ClassSymbol) sym;
 286                 dependencies.push(c, CompletionCause.CLASS_READER);
 287                 annotate.blockAnnotations();
 288                 c.members_field = new Scope.ErrorScope(c); // make sure it's always defined
 289                 completeOwners(c.owner);
 290                 completeEnclosing(c);
 291                 fillIn(c);
 292             } finally {
 293                 annotate.unblockAnnotationsNoFlush();
 294                 dependencies.pop();
 295             }
 296         } else if (sym.kind == PCK) {
 297             PackageSymbol p = (PackageSymbol)sym;
 298             try {
 299                 fillIn(p);
 300             } catch (IOException ex) {
 301                 JCDiagnostic msg =
 302                         diagFactory.fragment(Fragments.ExceptionMessage(ex.getLocalizedMessage()));
 303                 throw new CompletionFailure(sym, msg, dcfh).initCause(ex);
 304             }
 305         }
 306         if (!reader.filling)
 307             annotate.flush(); // finish attaching annotations
 308     }
 309 
 310     /** complete up through the enclosing package. */
 311     private void completeOwners(Symbol o) {
 312         if (o.kind != PCK) completeOwners(o.owner);
 313         o.complete();
 314     }
 315 
 316     /**
 317      * Tries to complete lexically enclosing classes if c looks like a
 318      * nested class.  This is similar to completeOwners but handles
 319      * the situation when a nested class is accessed directly as it is
 320      * possible with the Tree API or javax.lang.model.*.
 321      */
 322     private void completeEnclosing(ClassSymbol c) {
 323         if (c.owner.kind == PCK) {
 324             Symbol owner = c.owner;
 325             for (Name name : Convert.enclosingCandidates(Convert.shortName(c.name))) {
 326                 Symbol encl = owner.members().findFirst(name);
 327                 if (encl == null)
 328                     encl = syms.getClass(c.packge().modle, TypeSymbol.formFlatName(name, owner));
 329                 if (encl != null)
 330                     encl.complete();
 331             }
 332         }
 333     }
 334 
 335     /** Fill in definition of class `c' from corresponding class or
 336      *  source file.
 337      */
 338     void fillIn(ClassSymbol c) {
 339         if (completionFailureName == c.fullname) {
 340             JCDiagnostic msg =
 341                     diagFactory.fragment(Fragments.UserSelectedCompletionFailure);
 342             throw new CompletionFailure(c, msg, dcfh);
 343         }
 344         currentOwner = c;
 345         JavaFileObject classfile = c.classfile;
 346         if (classfile != null) {
 347             JavaFileObject previousClassFile = currentClassFile;
 348             Symbol prevOwner = c.owner;
 349             Name prevName = c.fullname;
 350             try {
 351                 if (reader.filling) {
 352                     Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
 353                 }
 354                 currentClassFile = classfile;
 355                 if (verbose) {
 356                     log.printVerbose("loading", currentClassFile.getName());
 357                 }
 358                 if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
 359                     reader.readClassFile(c);
 360                     c.flags_field |= getSupplementaryFlags(c);
 361                 } else {
 362                     if (!sourceCompleter.isTerminal()) {
 363                         sourceCompleter.complete(c);
 364                     } else {
 365                         throw new IllegalStateException("Source completer required to read "
 366                                                         + classfile.toUri());
 367                     }
 368                 }
 369             } catch (BadClassFile cf) {
 370                 //the symbol may be partially initialized, purge it:
 371                 c.owner = prevOwner;
 372                 c.members_field.getSymbols(sym -> sym.kind == TYP).forEach(sym -> {
 373                     ClassSymbol csym = (ClassSymbol) sym;
 374                     csym.owner = sym.packge();
 375                     csym.owner.members().enter(sym);
 376                     csym.fullname = sym.flatName();
 377                     csym.name = Convert.shortName(sym.flatName());
 378                     csym.reset();
 379                 });
 380                 c.fullname = prevName;
 381                 c.name = Convert.shortName(prevName);
 382                 c.reset();
 383                 throw cf;
 384             } finally {
 385                 currentClassFile = previousClassFile;
 386             }
 387         } else {
 388             throw classFileNotFound(c);
 389         }
 390     }
 391     // where
 392         private CompletionFailure classFileNotFound(ClassSymbol c) {
 393             JCDiagnostic diag =
 394                 diagFactory.fragment(Fragments.ClassFileNotFound(c.flatname));
 395             return newCompletionFailure(c, diag);
 396         }
 397         /** Static factory for CompletionFailure objects.
 398          *  In practice, only one can be used at a time, so we share one
 399          *  to reduce the expense of allocating new exception objects.
 400          */
 401         private CompletionFailure newCompletionFailure(TypeSymbol c,
 402                                                        JCDiagnostic diag) {
 403             if (!cacheCompletionFailure) {
 404                 // log.warning("proc.messager",
 405                 //             Log.getLocalizedString("class.file.not.found", c.flatname));
 406                 // c.debug.printStackTrace();
 407                 return new CompletionFailure(c, diag, dcfh);
 408             } else {
 409                 CompletionFailure result = cachedCompletionFailure;
 410                 result.sym = c;
 411                 result.diag = diag;
 412                 return result;
 413             }
 414         }
 415         private final CompletionFailure cachedCompletionFailure;
 416 
 417 
 418     /** Load a toplevel class with given fully qualified name
 419      *  The class is entered into `classes' only if load was successful.
 420      */
 421     public ClassSymbol loadClass(ModuleSymbol msym, Name flatname) throws CompletionFailure {
 422         Assert.checkNonNull(msym);
 423         Name packageName = Convert.packagePart(flatname);
 424         PackageSymbol ps = syms.lookupPackage(msym, packageName);
 425 
 426         Assert.checkNonNull(ps.modle, () -> "msym=" + msym + "; flatName=" + flatname);
 427 
 428         boolean absent = syms.getClass(ps.modle, flatname) == null;
 429         ClassSymbol c = syms.enterClass(ps.modle, flatname);
 430 
 431         if (c.members_field == null) {
 432             try {
 433                 c.complete();
 434             } catch (CompletionFailure ex) {
 435                 if (absent) syms.removeClass(ps.modle, flatname);
 436                 throw ex;
 437             }
 438         }
 439         return c;
 440     }
 441 
 442 /************************************************************************
 443  * Loading Packages
 444  ***********************************************************************/
 445 
 446     /** Include class corresponding to given class file in package,
 447      *  unless (1) we already have one the same kind (.class or .java), or
 448      *         (2) we have one of the other kind, and the given class file
 449      *             is older.
 450      */
 451     protected void includeClassFile(PackageSymbol p, JavaFileObject file) {
 452         if ((p.flags_field & EXISTS) == 0)
 453             for (Symbol q = p; q != null && q.kind == PCK; q = q.owner)
 454                 q.flags_field |= EXISTS;
 455         JavaFileObject.Kind kind = file.getKind();
 456         int seen;
 457         if (kind == JavaFileObject.Kind.CLASS)
 458             seen = CLASS_SEEN;
 459         else
 460             seen = SOURCE_SEEN;
 461         String binaryName = fileManager.inferBinaryName(currentLoc, file);
 462         int lastDot = binaryName.lastIndexOf(".");
 463         Name classname = names.fromString(binaryName.substring(lastDot + 1));
 464         boolean isPkgInfo = classname == names.package_info;
 465         ClassSymbol c = isPkgInfo
 466             ? p.package_info
 467             : (ClassSymbol) p.members_field.findFirst(classname);
 468         if (c == null) {
 469             c = syms.enterClass(p.modle, classname, p);
 470             if (c.classfile == null) // only update the file if's it's newly created
 471                 c.classfile = file;
 472             if (isPkgInfo) {
 473                 p.package_info = c;
 474             } else {
 475                 if (c.owner == p)  // it might be an inner class
 476                     p.members_field.enter(c);
 477             }
 478         } else if (!preferCurrent && c.classfile != null && (c.flags_field & seen) == 0) {
 479             // if c.classfile == null, we are currently compiling this class
 480             // and no further action is necessary.
 481             // if (c.flags_field & seen) != 0, we have already encountered
 482             // a file of the same kind; again no further action is necessary.
 483             if ((c.flags_field & (CLASS_SEEN | SOURCE_SEEN)) != 0)
 484                 c.classfile = preferredFileObject(file, c.classfile);
 485         }
 486         c.flags_field |= seen;
 487     }
 488 
 489     /** Implement policy to choose to derive information from a source
 490      *  file or a class file when both are present.  May be overridden
 491      *  by subclasses.
 492      */
 493     protected JavaFileObject preferredFileObject(JavaFileObject a,
 494                                            JavaFileObject b) {
 495 
 496         if (preferSource)
 497             return (a.getKind() == JavaFileObject.Kind.SOURCE) ? a : b;
 498         else {
 499             long adate = a.getLastModified();
 500             long bdate = b.getLastModified();
 501             // 6449326: policy for bad lastModifiedTime in ClassReader
 502             //assert adate >= 0 && bdate >= 0;
 503             return (adate > bdate) ? a : b;
 504         }
 505     }
 506 
 507     /**
 508      * specifies types of files to be read when filling in a package symbol
 509      */
 510     // Note: overridden by JavadocClassFinder
 511     protected EnumSet<JavaFileObject.Kind> getPackageFileKinds() {
 512         return EnumSet.of(JavaFileObject.Kind.CLASS, JavaFileObject.Kind.SOURCE);
 513     }
 514 
 515     /**
 516      * this is used to support javadoc
 517      */
 518     protected void extraFileActions(PackageSymbol pack, JavaFileObject fe) {
 519     }
 520 
 521     protected Location currentLoc; // FIXME
 522 
 523     private boolean verbosePath = true;
 524 
 525     // Set to true when the currently selected file should be kept
 526     private boolean preferCurrent;
 527 
 528     /** Load directory of package into members scope.
 529      */
 530     private void fillIn(PackageSymbol p) throws IOException {
 531         if (p.members_field == null)
 532             p.members_field = WriteableScope.create(p);
 533 
 534         ModuleSymbol msym = p.modle;
 535 
 536         Assert.checkNonNull(msym, p::toString);
 537 
 538         msym.complete();
 539 
 540         if (msym == syms.noModule) {
 541             preferCurrent = false;
 542             if (userPathsFirst) {
 543                 scanUserPaths(p, true);
 544                 preferCurrent = true;
 545                 scanPlatformPath(p);
 546             } else {
 547                 scanPlatformPath(p);
 548                 scanUserPaths(p, true);
 549             }
 550         } else if (msym.classLocation == StandardLocation.CLASS_PATH) {
 551             scanUserPaths(p, msym.sourceLocation == StandardLocation.SOURCE_PATH);
 552         } else {
 553             scanModulePaths(p, msym);
 554         }
 555     }
 556 
 557     // TODO: for now, this is a much simplified form of scanUserPaths
 558     // and (deliberately) does not default sourcepath to classpath.
 559     // But, we need to think about retaining existing behavior for
 560     // -classpath and -sourcepath for single module mode.
 561     // One plausible solution is to detect if the module's sourceLocation
 562     // is the same as the module's classLocation.
 563     private void scanModulePaths(PackageSymbol p, ModuleSymbol msym) throws IOException {
 564         Set<JavaFileObject.Kind> kinds = getPackageFileKinds();
 565 
 566         Set<JavaFileObject.Kind> classKinds = EnumSet.copyOf(kinds);
 567         classKinds.remove(JavaFileObject.Kind.SOURCE);
 568         boolean wantClassFiles = !classKinds.isEmpty();
 569 
 570         Set<JavaFileObject.Kind> sourceKinds = EnumSet.copyOf(kinds);
 571         sourceKinds.remove(JavaFileObject.Kind.CLASS);
 572         boolean wantSourceFiles = !sourceKinds.isEmpty();
 573 
 574         String packageName = p.fullname.toString();
 575 
 576         Location classLocn = msym.classLocation;
 577         Location sourceLocn = msym.sourceLocation;
 578         Location patchLocn = msym.patchLocation;
 579         Location patchOutLocn = msym.patchOutputLocation;
 580 
 581         boolean prevPreferCurrent = preferCurrent;
 582 
 583         try {
 584             preferCurrent = false;
 585             if (wantClassFiles && (patchOutLocn != null)) {
 586                 fillIn(p, patchOutLocn,
 587                        list(patchOutLocn,
 588                             p,
 589                             packageName,
 590                             classKinds));
 591             }
 592             if ((wantClassFiles || wantSourceFiles) && (patchLocn != null)) {
 593                 Set<JavaFileObject.Kind> combined = EnumSet.noneOf(JavaFileObject.Kind.class);
 594                 combined.addAll(classKinds);
 595                 combined.addAll(sourceKinds);
 596                 fillIn(p, patchLocn,
 597                        list(patchLocn,
 598                             p,
 599                             packageName,
 600                             combined));
 601             }
 602             preferCurrent = true;
 603             if (wantClassFiles && (classLocn != null)) {
 604                 fillIn(p, classLocn,
 605                        list(classLocn,
 606                             p,
 607                             packageName,
 608                             classKinds));
 609             }
 610             if (wantSourceFiles && (sourceLocn != null)) {
 611                 fillIn(p, sourceLocn,
 612                        list(sourceLocn,
 613                             p,
 614                             packageName,
 615                             sourceKinds));
 616             }
 617         } finally {
 618             preferCurrent = prevPreferCurrent;
 619         }
 620     }
 621 
 622     /**
 623      * Scans class path and source path for files in given package.
 624      */
 625     private void scanUserPaths(PackageSymbol p, boolean includeSourcePath) throws IOException {
 626         Set<JavaFileObject.Kind> kinds = getPackageFileKinds();
 627 
 628         Set<JavaFileObject.Kind> classKinds = EnumSet.copyOf(kinds);
 629         classKinds.remove(JavaFileObject.Kind.SOURCE);
 630         boolean wantClassFiles = !classKinds.isEmpty();
 631 
 632         Set<JavaFileObject.Kind> sourceKinds = EnumSet.copyOf(kinds);
 633         sourceKinds.remove(JavaFileObject.Kind.CLASS);
 634         boolean wantSourceFiles = !sourceKinds.isEmpty();
 635 
 636         boolean haveSourcePath = includeSourcePath && fileManager.hasLocation(SOURCE_PATH);
 637 
 638         if (verbose && verbosePath) {
 639             verbosePath = false; // print once per compile
 640             if (fileManager instanceof StandardJavaFileManager) {
 641                 StandardJavaFileManager fm = (StandardJavaFileManager)fileManager;
 642                 if (haveSourcePath && wantSourceFiles) {
 643                     List<Path> path = List.nil();
 644                     for (Path sourcePath : fm.getLocationAsPaths(SOURCE_PATH)) {
 645                         path = path.prepend(sourcePath);
 646                     }
 647                     log.printVerbose("sourcepath", path.reverse().toString());
 648                 } else if (wantSourceFiles) {
 649                     List<Path> path = List.nil();
 650                     for (Path classPath : fm.getLocationAsPaths(CLASS_PATH)) {
 651                         path = path.prepend(classPath);
 652                     }
 653                     log.printVerbose("sourcepath", path.reverse().toString());
 654                 }
 655                 if (wantClassFiles) {
 656                     List<Path> path = List.nil();
 657                     for (Path platformPath : fm.getLocationAsPaths(PLATFORM_CLASS_PATH)) {
 658                         path = path.prepend(platformPath);
 659                     }
 660                     for (Path classPath : fm.getLocationAsPaths(CLASS_PATH)) {
 661                         path = path.prepend(classPath);
 662                     }
 663                     log.printVerbose("classpath",  path.reverse().toString());
 664                 }
 665             }
 666         }
 667 
 668         String packageName = p.fullname.toString();
 669         if (wantSourceFiles && !haveSourcePath) {
 670             fillIn(p, CLASS_PATH,
 671                    list(CLASS_PATH,
 672                         p,
 673                         packageName,
 674                         kinds));
 675         } else {
 676             if (wantClassFiles)
 677                 fillIn(p, CLASS_PATH,
 678                        list(CLASS_PATH,
 679                             p,
 680                             packageName,
 681                             classKinds));
 682             if (wantSourceFiles)
 683                 fillIn(p, SOURCE_PATH,
 684                        list(SOURCE_PATH,
 685                             p,
 686                             packageName,
 687                             sourceKinds));
 688         }
 689     }
 690 
 691     /**
 692      * Scans platform class path for files in given package.
 693      */
 694     private void scanPlatformPath(PackageSymbol p) throws IOException {
 695         fillIn(p, PLATFORM_CLASS_PATH,
 696                list(PLATFORM_CLASS_PATH,
 697                     p,
 698                     p.fullname.toString(),
 699                     EnumSet.of(JavaFileObject.Kind.CLASS)));
 700     }
 701     // where
 702         @SuppressWarnings("fallthrough")
 703         private void fillIn(PackageSymbol p,
 704                             Location location,
 705                             Iterable<JavaFileObject> files)
 706         {
 707             currentLoc = location;
 708             for (JavaFileObject fo : files) {
 709                 switch (fo.getKind()) {
 710                 case OTHER:
 711                     extraFileActions(p, fo);
 712                     break;
 713                 case CLASS:
 714                 case SOURCE: {
 715                     // TODO pass binaryName to includeClassFile
 716                     String binaryName = fileManager.inferBinaryName(currentLoc, fo);
 717                     String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1);
 718                     if (SourceVersion.isIdentifier(simpleName) ||
 719                         simpleName.equals("package-info"))
 720                         includeClassFile(p, fo);
 721                     break;
 722                 }
 723                 default:
 724                     extraFileActions(p, fo);
 725                     break;
 726                 }
 727             }
 728         }
 729 
 730         Iterable<JavaFileObject> list(Location location,
 731                                       PackageSymbol p,
 732                                       String packageName,
 733                                       Set<Kind> kinds) throws IOException {
 734             Iterable<JavaFileObject> listed = fileManager.list(location,
 735                                                                packageName,
 736                                                                EnumSet.allOf(Kind.class),
 737                                                                false);
 738             return () -> new Iterator<JavaFileObject>() {
 739                 private final Iterator<JavaFileObject> original = listed.iterator();
 740                 private JavaFileObject next;
 741                 @Override
 742                 public boolean hasNext() {
 743                     if (next == null) {
 744                         while (original.hasNext()) {
 745                             JavaFileObject fo = original.next();
 746 
 747                             if (fo.getKind() != Kind.CLASS &&
 748                                 fo.getKind() != Kind.SOURCE) {
 749                                 p.flags_field |= Flags.HAS_RESOURCE;
 750                             }
 751 
 752                             if (kinds.contains(fo.getKind())) {
 753                                 next = fo;
 754                                 break;
 755                             }
 756                         }
 757                     }
 758                     return next != null;
 759                 }
 760 
 761                 @Override
 762                 public JavaFileObject next() {
 763                     if (!hasNext())
 764                         throw new NoSuchElementException();
 765                     JavaFileObject result = next;
 766                     next = null;
 767                     return result;
 768                 }
 769 
 770             };
 771         }
 772 
 773     /**
 774      * Used for bad class definition files, such as bad .class files or
 775      * for .java files with unexpected package or class names.
 776      */
 777     public static class BadClassFile extends CompletionFailure {
 778         private static final long serialVersionUID = 0;
 779 
 780         public BadClassFile(TypeSymbol sym, JavaFileObject file, JCDiagnostic diag,
 781                 JCDiagnostic.Factory diagFactory, DeferredCompletionFailureHandler dcfh) {
 782             super(sym, createBadClassFileDiagnostic(file, diag, diagFactory), dcfh);
 783         }
 784         // where
 785         private static JCDiagnostic createBadClassFileDiagnostic(
 786                 JavaFileObject file, JCDiagnostic diag, JCDiagnostic.Factory diagFactory) {
 787             String key = (file.getKind() == JavaFileObject.Kind.SOURCE
 788                         ? "bad.source.file.header" : "bad.class.file.header");
 789             return diagFactory.fragment(key, file, diag);
 790         }
 791     }
 792 
 793     public static class BadEnclosingMethodAttr extends BadClassFile {
 794         private static final long serialVersionUID = 0;
 795 
 796         public BadEnclosingMethodAttr(TypeSymbol sym, JavaFileObject file, JCDiagnostic diag,
 797                 JCDiagnostic.Factory diagFactory, DeferredCompletionFailureHandler dcfh) {
 798             super(sym, file, diag, diagFactory, dcfh);
 799         }
 800     }
 801 }