@Override @DefinedBy(Api.COMPILER) public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException { // TODO: Do this lazily by returning an iterable with a filtering Iterator // Acquire the list of files. Iterable<JavaFileObject> files = super.list(location, packageName, kinds, recurse); if (visibleSources.isEmpty()) { return locWrapMany(files, location); } // Now filter! ListBuffer<JavaFileObject> filteredFiles = new ListBuffer<>(); for (JavaFileObject f : files) { URI uri = f.toUri(); String t = uri.toString(); if (t.startsWith("jar:") || t.endsWith(".class") || visibleSources.contains(uri)) { filteredFiles.add(f); } } return locWrapMany(filteredFiles, location); }
@Override public JavaFileObject getJavaFileForInput(Location l, String className, Kind kind) { if (kind == JavaFileObject.Kind.CLASS) { int dot = className.lastIndexOf('.'); String dir = dot == -1 ? "" : FileObjects.convertPackage2Folder(className.substring(0, dot)); javax.tools.FileObject fo = tx.readFileObject(l, dir, className.substring(dot + 1)); if (fo != null) { return (JavaFileObject)fo; } } if (!ModuleLocation.isInstance(l)) { //File in output return super.getJavaFileForInput(l, className, kind); } else { //File in module return getJavaFileForInputImpl( ModuleLocation.WithExcludes.cast(l).getModuleEntries(), className, kind ); } }
@Override public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException { // validatePackageName(packageName); nullCheck(packageName); nullCheck(kinds); Iterable<? extends Path> paths = getLocation(location); if (paths == null) return List.nil(); ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>(); for (Path path : paths) list(path, packageName, kinds, recurse, results); return results.toList(); }
@Override public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException { JavaFileObject file = super.getJavaFileForInput(location, className, kind); if (file == null || visibleSources.isEmpty()) { return file; } if (visibleSources.contains(file.toUri())) { return file; } return null; }
@Override public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException { JavaFileObject file = super.getJavaFileForOutput(location, className, kind, sibling); if (file == null) return file; int dp = className.lastIndexOf('.'); String pkg_name = ""; if (dp != -1) { pkg_name = className.substring(0, dp); } // When modules are in use, then the mod_name might be something like "jdk_base" String mod_name = ""; addArtifact(mod_name+":"+pkg_name, file.toUri()); return file; }
@Override public JavaFileObject getJavaFileForOutput( final Location location, final String className, final Kind kind, final FileObject sibling) throws IOException { final URI uri = createUri(location, className, kind); if (!files.containsKey(uri)) { files.put(uri, new InMemoryJavaFileObject(uri)); } return files.get(uri); }
public ModuleSymbol findSingleModule() { try { JavaFileObject src_fo = getModuleInfoFromLocation(StandardLocation.SOURCE_PATH, Kind.SOURCE); JavaFileObject class_fo = getModuleInfoFromLocation(StandardLocation.CLASS_OUTPUT, Kind.CLASS); JavaFileObject fo = (src_fo == null) ? class_fo : (class_fo == null) ? src_fo : classFinder.preferredFileObject(src_fo, class_fo); ModuleSymbol msym; if (fo == null) { msym = syms.unnamedModule; } else { msym = readModule(fo); } if (msym.patchLocation == null) { msym.classLocation = StandardLocation.CLASS_OUTPUT; } else { msym.patchOutputLocation = StandardLocation.CLASS_OUTPUT; } return msym; } catch (IOException e) { throw new Error(e); // FIXME } }
@Override public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException { if (location != StandardLocation.PLATFORM_CLASS_PATH || !kinds.contains(Kind.CLASS)) return Collections.emptyList(); if (!packageName.isEmpty()) packageName += "."; List<JavaFileObject> result = new ArrayList<>(); for (Entry<String, JavaFileObject> e : className2File.entrySet()) { String currentPackage = e.getKey().substring(0, e.getKey().lastIndexOf(".") + 1); if (recurse ? currentPackage.startsWith(packageName) : packageName.equals(currentPackage)) result.add(e.getValue()); } return result; }
public void visitTopLevel(JCCompilationUnit node) { if (node.packge != null) { if (node.packge.package_info != null) { node.packge.package_info.reset(); } node.packge.reset(); } boolean isModuleInfo = node.sourcefile.isNameCompatible("module-info", Kind.SOURCE); if (isModuleInfo) { node.modle.reset(); node.modle.completer = sym -> modules.enter(List.of(node), node.modle.module_info); node.modle.module_info.reset(); node.modle.module_info.members_field = WriteableScope.create(node.modle.module_info); } node.packge = null; topLevel = node; try { super.visitTopLevel(node); } finally { topLevel = null; } }
@Test public void testRejects(Path outerBase) throws Exception { doRunTest(outerBase, (base, fm) -> { assertRefused(() -> fm.getClassLoader(StandardLocation.MODULE_SOURCE_PATH)); assertRefused(() -> fm.getFileForInput(StandardLocation.MODULE_SOURCE_PATH, "", "")); assertRefused(() -> fm.getFileForOutput(StandardLocation.MODULE_SOURCE_PATH, "", "", null)); assertRefused(() -> fm.getJavaFileForInput(StandardLocation.MODULE_SOURCE_PATH, "", Kind.SOURCE)); assertRefused(() -> fm.getJavaFileForOutput(StandardLocation.MODULE_SOURCE_PATH, "", Kind.SOURCE, null)); assertRefused(() -> fm.getLocationForModule(StandardLocation.SOURCE_PATH, "test")); JavaFileObject out = fm.getJavaFileForInput(StandardLocation.CLASS_OUTPUT, "test.Test", Kind.CLASS); assertRefused(() -> fm.inferBinaryName(StandardLocation.MODULE_PATH, out)); assertRefused(() -> fm.inferModuleName(StandardLocation.MODULE_SOURCE_PATH)); assertRefused(() -> fm.list(StandardLocation.MODULE_SOURCE_PATH, "test", EnumSet.allOf(Kind.class), false)); assertRefused(() -> fm.listLocationsForModules(StandardLocation.SOURCE_PATH)); }); }
@Override public Iterable<JavaFileObject> list(Location l, String packageName, Set<Kind> kinds, boolean recursive) { final Iterable<JavaFileObject> sr; if (!ModuleLocation.isInstance(l)) { //List output sr = super.list(l, packageName, kinds, recursive); } else { //List module final ModuleLocation.WithExcludes ml = ModuleLocation.WithExcludes.cast(l); sr = listImpl(l, ml.getModuleEntries(), packageName, kinds, recursive); } return tx.filter(l, packageName, sr); }
@Override public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException { List<JavaFileObject> result = new ArrayList<>(); for (JavaFileObject jfo : super.list(location, packageName, kinds, recurse)) { result.add(new OnlyOneReadJavaFileObject(jfo)); } return result; }
@Override public JavaFileObject getJavaFileForOutput (Location l, String className, JavaFileObject.Kind kind, javax.tools.FileObject sibling) throws IOException, UnsupportedOperationException, IllegalArgumentException { URL aptRoot = getAptRoot(sibling); if (ModuleLocation.isInstance(l)) { ModuleLocation mloc = ModuleLocation.cast(l); l = mloc.getBaseLocation(); if (aptRoot == null) { final Iterator<? extends URL> it = mloc.getModuleRoots().iterator(); aptRoot = it.hasNext() ? it.next() : null; } else if (!mloc.getModuleRoots().contains(aptRoot)) { throw new UnsupportedOperationException("ModuleLocation's APT root differs from the sibling's APT root"); } } final Location location = l; if (StandardLocation.SOURCE_OUTPUT != location) { throw new UnsupportedOperationException("Only apt output is supported."); // NOI18N } if (aptRoot == null) { throw new UnsupportedOperationException(noAptRootDebug(sibling)); } final String nameStr = className.replace('.', File.separatorChar) + kind.extension; //NOI18N //Always on master fs -> file is save. return Optional.ofNullable(URLMapper.findFileObject(aptRoot)) .map(fo -> { File f = FileUtil.toFile(fo); return fileTx.createFileObject(location, new File(f, nameStr), f, null, null); }).get(); }
private void generateSource(String name, String code) { Filer filer = processingEnv.getFiler(); try (Writer out = filer.createSourceFile(name).openWriter()) { out.write(code); out.close(); } catch (IOException e) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString()); } }
/** * Verify that exceptions from a bad file manager are thrown as expected. */ @Test public void testBadFileManager() throws Exception { JavaFileObject srcFile = createSimpleJavaFileObject(); DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); StandardJavaFileManager fm = new TestFileManager() { @Override public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException { throw new UnexpectedError(); } }; fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(getOutDir())); Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); DocumentationTask t = tool.getTask(null, fm, null, null, null, files); try { t.call(); error("call completed without exception"); } catch (RuntimeException e) { Throwable c = e.getCause(); if (c.getClass() == UnexpectedError.class) System.err.println("exception caught as expected: " + c); else throw e; } }
@Override public JavaFileObject getJavaFileForOutput(Location location, String qualifiedName, Kind kind, FileObject outputFile) throws IOException { JavaFileObject file = new JavaFileObjectImpl(qualifiedName, kind); classLoader.add(qualifiedName, file); return file; }
@Override public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException { try { JavaFileObject jfo = new ByteArrayJFO(className, kind); store.put(className, jfo); return jfo; } catch(Exception e) { System.out.println(e); return null; } }
/** * @throws IllegalStateException {@inheritDoc} */ public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException { return wrap(super.list(location, packageName, kinds, recurse)); }
/** * @throws IllegalArgumentException {@inheritDoc} * @throws UnsupportedOperationException {@inheritDoc} * @throws IllegalStateException {@inheritDoc} */ public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException { return wrap(super.getJavaFileForInput(location, className, kind)); }
/** * @throws IllegalArgumentException {@inheritDoc} * @throws UnsupportedOperationException {@inheritDoc} * @throws IllegalStateException {@inheritDoc} */ public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException { return wrap(super.getJavaFileForOutput(location, className, kind, unwrap(sibling))); }
public static Kind getKind(String name) { if (name.endsWith(Kind.CLASS.extension)) return Kind.CLASS; else if (name.endsWith(Kind.SOURCE.extension)) return Kind.SOURCE; else if (name.endsWith(Kind.HTML.extension)) return Kind.HTML; else return Kind.OTHER; }
/** * @throws IllegalArgumentException {@inheritDoc} * @throws IllegalStateException {@inheritDoc} */ public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException { return fileManager.getJavaFileForInput(location, className, kind); }
/** * @throws IllegalArgumentException {@inheritDoc} * @throws IllegalStateException {@inheritDoc} */ public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException { return fileManager.getJavaFileForOutput(location, className, kind, sibling); }
@Override public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException { if (location != StandardLocation.PLATFORM_CLASS_PATH || kind != Kind.CLASS) return null; return className2File.get(className); }
/** * Scans platform class path for files in given package. */ private void scanPlatformPath(PackageSymbol p) throws IOException { fillIn(p, PLATFORM_CLASS_PATH, list(PLATFORM_CLASS_PATH, p, p.fullname.toString(), allowSigFiles ? EnumSet.of(JavaFileObject.Kind.CLASS, JavaFileObject.Kind.OTHER) : EnumSet.of(JavaFileObject.Kind.CLASS))); }
@Override @DefinedBy(Api.COMPILER) public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException { JavaFileObject file = super.getJavaFileForInput(location, className, kind); file = locWrap(file, location); if (file == null || visibleSources.isEmpty()) { return file; } if (visibleSources.contains(file.toUri()) || isModuleInfo(file)) { return file; } return null; }
@Test public void testGetModuleForPath(Path outerBase) throws Exception { doRunTest(outerBase, (base, fm) -> { Location cOutput = fm.getLocationForModule(StandardLocation.SOURCE_OUTPUT, "c"); JavaFileObject testFO = fm.getJavaFileForOutput(cOutput, "test.Test", Kind.CLASS, null); testFO.openOutputStream().close(); Location cOutput2 = fm.getLocationForModule(StandardLocation.SOURCE_OUTPUT, testFO); if (cOutput != cOutput2) { throw new AssertionError("Unexpected location: " + cOutput2 + ", expected: " +cOutput); } }); }
@Override public void complete(Symbol sym) throws CompletionFailure { ModuleSymbol msym = moduleFinder.findModule((ModuleSymbol) sym); if (msym.kind == ERR) { //make sure the module is initialized: msym.directives = List.nil(); msym.exports = List.nil(); msym.provides = List.nil(); msym.requires = List.nil(); msym.uses = List.nil(); } else if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) { setupAutomaticModule(msym); } else { msym.module_info.complete(); } // If module-info comes from a .java file, the underlying // call of classFinder.fillIn will have called through the // source completer, to Enter, and then to Modules.enter, // which will call completeModule. // But, if module-info comes from a .class file, the underlying // call of classFinder.fillIn will just call ClassReader to read // the .class file, and so we call completeModule here. if (msym.module_info.classfile == null || msym.module_info.classfile.getKind() == Kind.CLASS) { completeModule(msym); } }
/** * @throws IOException {@inheritDoc} * @throws IllegalStateException {@inheritDoc} */ public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException { return fileManager.list(location, packageName, kinds, recurse); }
@Override public Output getJavaFileForOutput(Location location, String name, Kind kind, FileObject source) { Output mc = this.map.get(name); if (mc == null) { mc = new Output(name); this.map.put(name, mc); } return mc; }
private void setupAutomaticModule(ModuleSymbol msym) throws CompletionFailure { try { ListBuffer<Directive> directives = new ListBuffer<>(); ListBuffer<ExportsDirective> exports = new ListBuffer<>(); Set<String> seenPackages = new HashSet<>(); for (JavaFileObject clazz : fileManager.list(msym.classLocation, "", EnumSet.of(Kind.CLASS), true)) { String binName = fileManager.inferBinaryName(msym.classLocation, clazz); String pack = binName.lastIndexOf('.') != (-1) ? binName.substring(0, binName.lastIndexOf('.')) : ""; //unnamed package???? if (seenPackages.add(pack)) { ExportsDirective d = new ExportsDirective(syms.enterPackage(msym, names.fromString(pack)), null); //TODO: opens? directives.add(d); exports.add(d); } } msym.exports = exports.toList(); msym.provides = List.nil(); msym.requires = List.nil(); msym.uses = List.nil(); msym.directives = directives.toList(); msym.flags_field |= Flags.ACYCLIC; } catch (IOException ex) { throw new IllegalStateException(ex); } }
@Override public JavaFileObject getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject sibling) { return new MemoryFileObject(location, name, kind); }