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