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