@Override public javax.tools.FileObject getFileForOutput(Location location, String packageName, String relativeName, javax.tools.FileObject sibling) throws IOException { assertLocation(location, true); final String resourceName = FileObjects.resolveRelativePath(packageName, relativeName); final String[] ncs = FileObjects.getFolderAndBaseName(resourceName, FileObjects.NBFS_SEPARATOR_CHAR); return new MemOutFileObject( FileObjects.convertFolder2Package(ncs[0]), ncs[1]); }
@Override public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException { if (!hasLocation(location)) { throw new IllegalArgumentException(String.valueOf(location)); } final File root = new File (outputRoot); final String nameStr = FileObjects.convertPackage2Folder(className, File.separatorChar) + '.' + FileObjects.SIG; final File file = new File (root, nameStr); if (FileObjects.isValidFileName(className)) { return tx.createFileObject(location, file, root, null, null); } else { LOG.log( Level.WARNING, "Invalid class name: {0} sibling: {1}", //NOI18N new Object[]{ className, sibling }); return FileObjects.nullWriteFileObject(FileObjects.fileFileObject(file, root, null, null)); } }
private static void deleteSigFiles(final URL root, final List<? extends ElementHandle<TypeElement>> toRemove) throws IOException { File cacheFolder = JavaIndex.getClassFolder(root); if (cacheFolder.exists()) { if (toRemove.size() > CLEAN_ALL_LIMIT) { //Todo: do as SlowIOTask FileObjects.deleteRecursively(cacheFolder); } else { for (ElementHandle<TypeElement> eh : toRemove) { final StringBuilder sb = new StringBuilder(FileObjects.convertPackage2Folder(eh.getBinaryName(),File.separatorChar)); sb.append('.'); //NOI18N sb.append(FileObjects.SIG); final File f = new File (cacheFolder, sb.toString()); f.delete(); } } } }
@Override public boolean accept(Path entry) throws IOException { if (Files.isDirectory(entry)) { return false; } if (kinds == null) { return true; } return kinds.contains(FileObjects.getKind( FileObjects.getExtension(entry.getFileName().toString()))); } }
@Override public String getName(boolean includeExtension) { String name = super.getName(includeExtension); if (name == null) { name = FileObjects.getBaseName(path.getPath(),NBFS_SEPARATOR_CHAR); if (!includeExtension) { name = FileObjects.stripExtension(name); } } return name; }
final String simpleName = file.getName(file.getNameCount()-1).toString(); if (accepts(simpleName)) { final String fqn = FileObjects.convertFolder2Package ( FileObjects.stripExtension(rootPath.relativize(file).toString()), separator); report (
/** * Creates {@link JavaFileObject} for a regular {@link File} * @param file for which the {@link JavaFileObject} should be created * @param root - the classpath root owning the file * @param encoding - the file's encoding * @return {@link JavaFileObject}, never returns null */ public static @NonNull PrefetchableJavaFileObject fileFileObject( final @NonNull File file, final @NonNull File root, final @NullAllowed JavaFileFilterImplementation filter, final @NullAllowed Charset encoding) { assert file != null; assert root != null; final String[] pkgNamePair = getFolderAndBaseName(getRelativePath(root,file),File.separatorChar); return new FileBase( file, convertFolder2Package(pkgNamePair[0], File.separatorChar), pkgNamePair[1], filter, encoding); }
public static Collection<? extends ElementHandle<TypeElement>> getRelatedTypes (final File source, final File root) throws IOException { final List<ElementHandle<TypeElement>> result = new LinkedList<ElementHandle<TypeElement>>(); final File classFolder = JavaIndex.getClassFolder(root); final String path = FileObjects.getRelativePath(root, source); final String ext = FileObjects.getExtension(path); final String pathNoExt = FileObjects.stripExtension(path); final boolean dieIfNoRefFile = VirtualSourceProviderQuery.hasVirtualSource(ext); File file; cont = false; try { String binaryName = FileObjects.getBinaryName(file, classFolder); for (String className : readRSFile(file)) { if (!binaryName.equals(className)) { String className = FileObjects.getBinaryName (f, classFolder); result.add(ElementHandleAccessor.getInstance().create(ElementKind.CLASS, className));
File file; if (aptFolder.exists()) { file = new File(classFolder, FileObjects.stripExtension(sourceRelative) + '.' + FileObjects.RAPT); if (file.exists()) { try { for (String fileName : readRSFile(file)) { File f = new File (aptFolder, fileName); if (f.exists() && FileObjects.JAVA.equals(FileObjects.getExtension(f.getName()))) { sourceRelativeURLPairs.add(Pair.of(fileName, BaseUtilities.toURI(f).toURL())); final String ext = FileObjects.getExtension(relURLPair.first()); final String withoutExt = FileObjects.stripExtension(relURLPair.first()); final boolean dieIfNoRefFile = VirtualSourceProviderQuery.hasVirtualSource(ext); if (dieIfNoRefFile) { cont = false; try { String binaryName = FileObjects.getBinaryName(file, classFolder); for (String className : readRSFile(file)) { File f = new File(classFolder, FileObjects.convertPackage2Folder(className) + '.' + FileObjects.SIG); if (!binaryName.equals(className)) { if (javaContext.getFQNs().remove(className, relURLPair.second())) { if (!javaContext.getFQNs().check(FileObjects.getBinaryName(file, classFolder), relURLPair.second())) { String fileName = file.getName(); fileName = fileName.substring(0, fileName.lastIndexOf('.')); if (children != null) { for (File f : children) {
srcRoot)); final String[] ncs = FileObjects.getPackageAndName( FileObjects.convertFolder2Package(FileObjects.stripExtension(path))); final JavaFileObject toCompile = FileObjects.memoryFileObject( ncs[0], ncs[1]+'.'+file.getExt(),
public void add(final File source, final String packageName, final String relativeName, final CharSequence content) { try { final Indexable indexable = this.file2indexables.get(source); assert indexable != null : "Unknown file: " + source.getAbsolutePath(); final String baseName = relativeName + '.' + FileObjects.getExtension(source.getName()); String folder = FileObjects.convertPackage2Folder(packageName); if (folder.length() > 0) { folder += '/'; } res.add(new CompileTuple( new ForwardingPrefetchableJavaFileObject( FileObjects.memoryFileObject(packageName, baseName,new URI(rootURL + folder + baseName), System.currentTimeMillis(), content)) { @Override public JavaFileObject.Kind getKind() { return JavaFileObject.Kind.SOURCE; } }, indexable,true, this.currentProvider.index())); } catch (URISyntaxException ex) { Exceptions.printStackTrace(ex); } } }
@Override public javax.tools.FileObject getFileForOutput(final Location l, final String pkgName, final String relativeName, final javax.tools.FileObject sibling) throws IOException, UnsupportedOperationException, IllegalArgumentException { if (StandardLocation.SOURCE_PATH != l) { throw new UnsupportedOperationException("Only StandardLocation.SOURCE_PATH is supported."); // NOI18N } final String rp = FileObjects.resolveRelativePath (pkgName, relativeName); final FileObject[] fileRootPair = findFile(rp); if (fileRootPair == null) { final FileObject[] roots = this.sourceRoots.getRoots(); if (roots.length == 0) { throw new UnsupportedOperationException("No source path"); //NOI18N } final File rootFile = FileUtil.toFile(roots[0]); if (rootFile == null) { throw new UnsupportedOperationException("No source path"); //NOI18N } return FileObjects.sourceFileObject( BaseUtilities.toURI(new File(rootFile,FileObjects.convertFolder2Package(rp, File.separatorChar))).toURL(), roots[0]); } else { return FileObjects.sourceFileObject(fileRootPair[0], fileRootPair[1]); //Todo: wrap to protect from write } }
private static Query createClassesInFileQuery (final String resourceName, final String sourceName) { if (sourceName != null) { final BooleanQuery result = new BooleanQuery(); result.add(createFQNQuery(FileObjects.convertFolder2Package(FileObjects.stripExtension(sourceName))), Occur.SHOULD); result.add(new TermQuery(new Term (DocumentUtil.FIELD_SOURCE,sourceName)),Occur.SHOULD); return result; } else { final BooleanQuery result = new BooleanQuery(); result.add(createFQNQuery(resourceName), Occur.SHOULD); result.add(new TermQuery(new Term ( DocumentUtil.FIELD_SOURCE, new StringBuilder(FileObjects.convertPackage2Folder(resourceName)).append('.').append(FileObjects.JAVA).toString())), Occur.SHOULD); return result; } }
@Override @CheckForNull protected URL map(@NonNull final FileObject fo) { final String relative = FileObjects.convertFolder2Package( FileObjects.stripExtension(FileObjects.getRelativePath(cacheRoot, FileUtil.toFile(fo))), File.separatorChar); final FileObject sourceFile = SourceUtils.getFile( ElementHandleAccessor.getInstance().create(ElementKind.CLASS, relative), resolveCps); return sourceFile == null ? null : sourceFile.toURL(); } }
List<JavaFileObject> result = new ArrayList<>(content.length); for (File f : content) { final JavaFileObject.Kind fKind = FileObjects.getKind(FileObjects.getExtension(f.getName())); if ((kinds == null || kinds.contains(fKind)) && f.isFile() && (entry == null || entry.includes(BaseUtilities.toURI(f).toURL()))) { result.add(FileObjects.fileFileObject( f, this.root,
public Iterable<JavaFileObject> getFiles(String folderName, ClassPath.Entry entry, Set<JavaFileObject.Kind> kinds, JavaFileFilterImplementation filter) throws IOException { FileObject folder = root.getFileObject(folderName); if (folder == null || !(entry == null || entry.includes(folder))) { return Collections.<JavaFileObject>emptySet(); } FileObject[] children = folder.getChildren(); List<JavaFileObject> result = new ArrayList<JavaFileObject>(children.length); for (FileObject fo : children) { if (fo.isData() && (entry == null || entry.includes(fo))) { final Kind kind = FileObjects.getKind(fo.getExt()); if (kinds == null || kinds.contains (kind)) { JavaFileObject file; if (kind == Kind.CLASS) { file = FileObjects.fileObjectFileObject(fo, root, filter, null); } else { file = FileObjects.sourceFileObject(fo, root, filter,false); } result.add(file); } } } return result; }
@Override public URI toUri () { if (this.uri != null) { return this.uri; } else { return URI.create (convertPackage2Folder(this.pkgName) + NBFS_SEPARATOR_CHAR + this.nameWithoutExt); } }
@Override @NonNull public synchronized Iterable<JavaFileObject> getFiles( @NonNull String folderName, @NullAllowed final ClassPath.Entry entry, @NullAllowed Set<JavaFileObject.Kind> kinds, @NullAllowed JavaFileFilterImplementation filter) throws IOException { init(); final int[] pkgContent = data.get(folderName); if (pkgContent == null || pkgContent == EMPTY_FOLDER) { return Collections.emptyList(); } final List<JavaFileObject> res = new ArrayList<>(pkgContent.length>>>1); for (int i = 0; i < pkgContent.length; i+=2) { final String name = getName(pkgContent[i], pkgContent[i+1]); if (kinds == null || kinds.contains(FileObjects.getKind(FileObjects.getExtension(name)))) { res.add(FileObjects.pathFileObject( folderName, name, root, rootURI, null)); } } return res; }
final char separator = file.getFileSystem().getSeparator().charAt(0); final Path relPath = root.relativize(file); final String[] path = getFolderAndBaseName(relPath.toString(), separator); String fileUri; if (rootUri != null) { convertFolder2Package(path[0], separator), path[1], fileUri,
/** * Create a new CompilationUnitTree from a template. * * @param sourceRoot a source root under which the new file is created * @param path a relative path to file separated by '/' * @param kind the kind of Element to use for the template, can be null or * CLASS, INTERFACE, ANNOTATION_TYPE, ENUM, PACKAGE * @return new CompilationUnitTree created from a template * @throws IOException when an exception occurs while creating the template * @since 0.101 */ public CompilationUnitTree createFromTemplate(FileObject sourceRoot, String path, ElementKind kind) throws IOException { String[] nameComponent = FileObjects.getFolderAndBaseName(path, '/'); JavaFileObject sourceFile = FileObjects.templateFileObject(sourceRoot, nameComponent[0], nameComponent[1]); FileObject template = FileUtil.getConfigFile(copy.template(kind)); FileObject targetFile = copy.doCreateFromTemplate(template, sourceFile); CompilationUnitTree templateCUT = copy.impl.getJavacTask().parse(FileObjects.sourceFileObject(targetFile, targetFile.getParent())).iterator().next(); CompilationUnitTree importComments = GeneratorUtilities.get(copy).importComments(templateCUT, templateCUT); CompilationUnitTree result = copy.getTreeMaker().CompilationUnit(importComments.getPackageAnnotations(), sourceRoot, path, importComments.getImports(), importComments.getTypeDecls()); return result; }