/** * Converts a folder into package name * @param folderName to be converted, the folder name components has to be separated by '/' * @return the package name */ public static @NonNull String convertFolder2Package (@NonNull String folderName) { return convertFolder2Package (folderName, NBFS_SEPARATOR_CHAR); }
@NonNull final String baseName, final long mtime) { super (convertFolder2Package(folderName), baseName, FileObjects.SYSTEM_ENCODING, true); assert pathToRootInArchive == null || pathToRootInArchive.charAt(pathToRootInArchive.length()-1) == NBFS_SEPARATOR_CHAR; this.mtime = mtime;
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; } }
protected void computeFQNs(final Map<JavaFileObject, List<String>> file2FQNs, CompilationUnitTree cut, CompileTuple tuple) { String pack; if (cut.getPackageName() != null) { pack = cut.getPackageName().toString() + "."; //XXX } else { pack = ""; } String path = tuple.indexable.getRelativePath(); int i = path.lastIndexOf('.'); if (i >= 0) path = path.substring(0, i); path = FileObjects.convertFolder2Package(path); List<String> fqns = new LinkedList<String>(); boolean hasClassesLivingElsewhere = false; for (Tree t : cut.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(t.getKind())) { String fqn = pack + ((ClassTree) t).getSimpleName().toString(); fqns.add(fqn); if (!path.equals(fqn)) { hasClassesLivingElsewhere = true; } } } if (hasClassesLivingElsewhere) { file2FQNs.put(tuple.jfo, fqns); } }
@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 } }
/** * Creates a FileObject for newly created file * @param root owning the file * @param path the path (separated by '/') to target folder relative to root * @param name of the file * @return {@link JavaFileObject} */ public static @NonNull JavaFileObject templateFileObject (final @NonNull FileObject root, final @NonNull String path, final @NonNull String name) { assert root != null; assert path != null; JavaFileFilterImplementation filter = JavaFileFilterQuery.getFilter(root); Charset encoding = FileEncodingQuery.getEncoding(root); File rootFile = FileUtil.toFile(root); if (rootFile == null) { throw new IllegalArgumentException (); } File file = FileUtil.normalizeFile(new File (new File (rootFile, path.replace(NBFS_SEPARATOR_CHAR, File.separatorChar)), name)); return new NewFromTemplateFileObject (file, convertFolder2Package(path), name, filter, encoding); }
convertFolder2Package(path[0], separator), path[1], fileUri,
@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]); }
URI relative = root.relativize(mainClass.toURI()); final String resourceNameNoExt = FileObjects.stripExtension(relative.getPath()); final String ffqn = FileObjects.convertFolder2Package(resourceNameNoExt,'/'); //NOI18N if (qualifiedName.equals(ffqn)) { final ClassPath bootCp = cpInfo.getClassPath(PathKind.BOOT);
final String simpleName = file.getName(file.getNameCount()-1).toString(); if (accepts(simpleName)) { final String fqn = FileObjects.convertFolder2Package ( FileObjects.stripExtension(rootPath.relativize(file).toString()), separator);
@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; }
@Override @NonNull protected boolean executeImpl() throws IOException { while (todo.hasMoreElements()) { FileObject fo = todo.nextElement(); if (accepts(fo.getNameExt())) { final String rp = FileObjects.stripExtension(FileUtil.getRelativePath(root, fo)); report( ElementHandleAccessor.getInstance().create(ElementKind.OTHER, FileObjects.convertFolder2Package(rp)), 0L); final InputStream in = new BufferedInputStream (fo.getInputStream()); try { analyse (in); } catch (InvalidClassFormatException icf) { LOGGER.log(Level.WARNING, "Invalid class file format: {0}", FileUtil.getFileDisplayName(fo)); //NOI18N } finally { in.close(); } if (lmListener.isLowMemory()) { flush(); } } if (isCancelled()) { return false; } } return true; } }
assert elements != null; assert this.impl.getRoot() != null; String name = FileObjects.convertFolder2Package(FileObjects.stripExtension(FileUtil.getRelativePath(this.impl.getRoot(), this.impl.getFileObject()))); TypeElement e = ((JavacElements)elements).getTypeElementByBinaryName(name); if (e != null) {
public static void preBuildArgs( @NonNull final FileObject root, @NonNull final FileObject file) throws IOException { final String relativePath = FileObjects.convertFolder2Package( FileObjects.stripExtension( FileUtil.getRelativePath(root, file))); final TransactionContext txCtx = TransactionContext.beginTrans() .register(FileManagerTransaction.class, FileManagerTransaction.writeThrough()) .register(ProcessorGenerated.class, ProcessorGenerated.nullWrite()); try { final Collection<ClassPath.Entry> entries = JavaPlatform.getDefault().getBootstrapLibraries().entries(); final URL[] roots = new URL[1+entries.size()]; roots[0] = root.toURL(); final Iterator<ClassPath.Entry> eit = entries.iterator(); for (int i=1; eit.hasNext(); i++) { roots[i] = eit.next().getURL(); } preBuildArgs(relativePath, roots); } finally { txCtx.commit(); } }
/** * 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); } }
@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(); } }
/** * 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); }
/** * Creates an {@link InferableJavaFileObject} for NIO {@link Path}. * @param folder the folder * @param baseName the file basename with extension * @param root the classpath root * @param rootUri the root {@link URI} or null if the {@link URI} should be taken from file * @param encoding the optional encoding or null for system encoding * @return the {@link InferableJavaFileObject} */ @NonNull public static InferableJavaFileObject pathFileObject( @NonNull final String folder, @NonNull final String baseName, @NonNull final Path root, @NullAllowed final String rootUri, @NullAllowed Charset encoding) { return new LazyPathFileObject( convertFolder2Package(folder), baseName, root, rootUri, encoding); }
@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); }