/** * Returns the basename name without folder path. You can specify * the path separator since eg zip files uses '/' regardless of platform. * @param file name, eg. obtained from {@link FileObjects#getPath} or {java.io.File.getPath} * @param separator path separator * @return the base name */ public static String getBaseName( String fileName, char separator ) { return getFolderAndBaseName(fileName, separator)[1]; }
/** * Returns a tuple {package,simpleName} for given fully qualified name * @param fqn to get the package simpleName tuple for * @return a tuple {package,simpleName} */ @NonNull public static String[] getPackageAndName (final @NonNull String fqn) { return getFolderAndBaseName(fqn, '.'); //NOI18N }
/** * Creates a new CompilationUnitTree. * @param sourceRoot a source root under which the new file is created * @param path a relative path to file separated by '/' * @param imports a list of import statements. * @param typeDeclarations a list of type (class, interface or enum) declarations. * @see com.sun.source.tree.CompilationUnitTree */ public CompilationUnitTree CompilationUnit(FileObject sourceRoot, String path, List<? extends ImportTree> imports, List<? extends Tree> typeDeclarations) { String[] nameComponent = FileObjects.getFolderAndBaseName(path,'/'); //NOI18N JavaFileObject sourceFile = FileObjects.templateFileObject(sourceRoot, nameComponent[0], nameComponent[1]); IdentifierTree pkg = nameComponent[0].length() == 0 ? null : Identifier(nameComponent[0].replace('/', '.')); return delegate.CompilationUnit(Collections.<AnnotationTree>emptyList(), pkg, imports, typeDeclarations, sourceFile); }
@Override public javax.tools.FileObject getFileForInput(Location l, String pkgName, String relativeName) { final String[] names = FileObjects.getFolderAndBaseName( FileObjects.resolveRelativePath(pkgName, relativeName), FileObjects.NBFS_SEPARATOR_CHAR); javax.tools.FileObject fo = tx.readFileObject(l, names[0], names[1]); if (fo != null) { return fo; } return super.getFileForInput(l, pkgName, relativeName); }
/** * Creates a new CompilationUnitTree. * @param packageAnnotations package annotations * @param sourceRoot a source root under which the new file is created * @param path a relative path to file separated by '/' * @param imports a list of import statements. * @param typeDeclarations a list of type (class, interface or enum) declarations. * @see com.sun.source.tree.CompilationUnitTree * @since 0.66 */ public CompilationUnitTree CompilationUnit(List<? extends AnnotationTree> packageAnnotations, FileObject sourceRoot, String path, List<? extends ImportTree> imports, List<? extends Tree> typeDeclarations) { String[] nameComponent = FileObjects.getFolderAndBaseName(path,'/'); //NOI18N JavaFileObject sourceFile = FileObjects.templateFileObject(sourceRoot, nameComponent[0], nameComponent[1]); IdentifierTree pkg = nameComponent[0].length() == 0 ? null : Identifier(nameComponent[0].replace('/', '.')); return delegate.CompilationUnit(packageAnnotations, pkg, imports, typeDeclarations, sourceFile); }
@Override public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException { if (!hasLocation(location)) { throw new IllegalArgumentException(String.valueOf(location)); } final File root = new File(outputRoot); final String path = FileObjects.resolveRelativePath(packageName, relativeName); final String[] names = FileObjects.getFolderAndBaseName(path, FileObjects.NBFS_SEPARATOR_CHAR); final javax.tools.FileObject jfo = tx.readFileObject(location, names[0], names[1]); if (jfo != null) { return (JavaFileObject) jfo; } final Archive archive = provider.getArchive(BaseUtilities.toURI(root).toURL(), false); return archive != null ? archive.getFile(path) : null; }
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) {
@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 @CheckForNull public synchronized JavaFileObject getFile(@NonNull final String name) throws IOException { init(); final String[] fnPair = FileObjects.getFolderAndBaseName(name, FileObjects.NBFS_SEPARATOR_CHAR); final int[] pkgContent = data.get(fnPair[0]); if (pkgContent != null) { for (int i = 0; i < pkgContent.length; i+=2) { final String baseName = getName(pkgContent[i], pkgContent[i+1]); if (fnPair[1].equals(baseName)) { return FileObjects.pathFileObject( fnPair[0], fnPair[1], root, rootURI, null); } } } return null; }
@Override @NonNull JavaFileObject createFileObject( @NonNull final Location location, @NonNull final File file, @NonNull final File root, @NullAllowed final JavaFileFilterImplementation filter, @NullAllowed final Charset encoding) { final String[] pkgNamePair = FileObjects.getFolderAndBaseName(FileObjects.getRelativePath(root, file), File.separatorChar); String pname = FileObjects.convertFolder2Package(pkgNamePair[0], File.separatorChar); CachedFileObject cfo = getFileObject(location, pname, pkgNamePair[1], false); if (cfo != null) { return cfo; } String relPath = FileObjects.getRelativePath(root, file); File shadowRoot = new File(root.getParent(), root.getName() + WORK_SUFFIX); File shadowFile = new File(shadowRoot, relPath); workDirs.add(shadowRoot); cfo = new CachedFileObject(this, file, pname, pkgNamePair[1], filter, encoding); cfo.setShadowFile(shadowFile); addFile(location, pname, cfo); if (!shadowRoot.mkdirs() && !shadowRoot.exists() && !shadowRoot.isDirectory()) { throw new IllegalStateException(); } return cfo; }
/** * Creates {@link File} based {@link JavaFileObject} for {@link Indexable}. * @param indexable 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 */ @NonNull public static PrefetchableJavaFileObject fileFileObject( @NonNull final Indexable indexable, @NonNull final File root, @NullAllowed final JavaFileFilterImplementation filter, @NullAllowed final Charset encoding) throws IOException { assert indexable != null; assert root != null; final String[] pkgNamePair = getFolderAndBaseName( indexable.getRelativePath(), NBFS_SEPARATOR_CHAR); try { final File file = BaseUtilities.toFile(indexable.getURL().toURI()); return new FileBase( file, convertFolder2Package(pkgNamePair[0]), pkgNamePair[1], filter, encoding); } catch (URISyntaxException use) { throw new IOException(use); } }
/** * 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); }
/** * Creates {@link JavaFileObject} for a {@link FileObject} * @param file for which the {@link JavaFileObject} should be created * @param root - the classpath root owning the file * @param filter the preprocessor filter or null * @param encoding - the file's encoding or null for non source file * @return {@link JavaFileObject}, never returns null */ @NonNull public static PrefetchableJavaFileObject fileObjectFileObject( final @NonNull FileObject file, final @NonNull FileObject root, final @NullAllowed JavaFileFilterImplementation filter, final @NullAllowed Charset encoding) { assert file != null; assert root != null; final String[] pkgNamePair = getFolderAndBaseName(FileUtil.getRelativePath(root,file), NBFS_SEPARATOR_CHAR); return new FileObjectBase(file, convertFolder2Package(pkgNamePair[0], NBFS_SEPARATOR_CHAR), pkgNamePair[1], filter, encoding); }
/** * 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; }
@NonNull public static PrefetchableJavaFileObject asyncWriteFileObject( @NonNull final File file, @NonNull final File root, @NullAllowed JavaFileFilterImplementation filter, @NullAllowed Charset encoding, @NonNull final Executor pool, @NonNull final CompletionHandler<Void,Void> done) { final String[] pkgNamePair = getFolderAndBaseName(getRelativePath(root,file),File.separatorChar); return new AsyncWriteFileObject( file, convertFolder2Package(pkgNamePair[0], File.separatorChar), pkgNamePair[1], filter, encoding, pool, done); }