/** * Creates a new CompilationInfoImpl for a class file * @param cpInfo classpaths * @param file to be analyzed * @param root the owner of analyzed file */ CompilationInfoImpl (final ClasspathInfo cpInfo, final FileObject file, final FileObject root) throws IOException { assert cpInfo != null; assert file != null; assert root != null; this.parser = null; this.file = file; this.root = root; this.jfo = FileObjects.sourceFileObject(file, root); this.snapshot = null; this.cpInfo = cpInfo; this.isClassFile = true; this.isDetached = false; }
/** * Creates {@link JavaFileObject} for a NetBeans {@link FileObject} * Any client which needs to create {@link JavaFileObject} for java * source file should use this factory method. * @param {@link FileObject} for which the {@link JavaFileObject} should be created * @param {@link FileObject} root owning the file * @return {@link JavaFileObject}, never returns null */ public static AbstractSourceFileObject sourceFileObject (final FileObject file, final FileObject root) { try { return sourceFileObject (file, root, null, false); } catch (IOException ioe) { //Never thrown for renderNow == false throw new IllegalArgumentException(ioe); } }
@Override public JavaFileObject getFile(String name) throws IOException { final FileObject file = root.getFileObject(name); return file == null ? null : FileObjects.sourceFileObject(file, root, null, false); }
@Override public javax.tools.FileObject getFileForInput (final Location l, final String pkgName, final String relativeName) { final String rp = FileObjects.resolveRelativePath (pkgName, relativeName); final FileObject[] fileRootPair = findFile(rp); return fileRootPair == null ? null : FileObjects.sourceFileObject(fileRootPair[0], fileRootPair[1]); }
@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 } }
final FileObject resource = srcRootFo.getFileObject(name); if (resource != null) { return FileObjects.sourceFileObject(resource, srcRootFo);
@Override public Iterable<JavaFileObject> list(final Location l, final String packageName, final Set<JavaFileObject.Kind> kinds, final boolean recursive) { //Todo: Caching of results, needs listening on FS List<JavaFileObject> result = new ArrayList<JavaFileObject> (); String _name = packageName.replace('.','/'); //NOI18N if (_name.length() != 0) { _name+='/'; //NOI18N } for (ClassPath.Entry entry : this.sourceRoots.entries()) { if (ignoreExcludes || entry.includes(_name)) { FileObject root = entry.getRoot(); if (root != null) { FileObject tmpFile = root.getFileObject(_name); if (tmpFile != null && tmpFile.isFolder()) { Enumeration<? extends FileObject> files = tmpFile.getChildren (recursive); while (files.hasMoreElements()) { FileObject file = files.nextElement(); if (ignoreExcludes || entry.includes(file)) { final JavaFileObject.Kind kind = FileObjects.getKind(file.getExt()); if (kinds.contains(kind)) { result.add (FileObjects.sourceFileObject(file, root)); } } } } } } } return result; }
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; }
assert file == null || snapshot != null; this.jfo = file != null ? FileObjects.sourceFileObject(file, root, JavaFileFilterQuery.getFilter(file), snapshot.getText()) : null; this.javacTask = javacTask;
@Override public JavaFileObject getJavaFileForInput (Location l, final String className, JavaFileObject.Kind kind) { String[] namePair = FileObjects.getParentRelativePathAndName (className); String ext = kind == JavaFileObject.Kind.CLASS ? FileObjects.SIG : kind.extension.substring(1); //tzezula: Clearly wrong in compile on save, but "class" is also wrong for (ClassPath.Entry entry : this.sourceRoots.entries()) { FileObject root = entry.getRoot(); if (root != null) { FileObject parent = root.getFileObject(namePair[0]); if (parent != null) { FileObject[] children = parent.getChildren(); for (FileObject child : children) { if (namePair[1].equals(child.getName()) && ext.equalsIgnoreCase(child.getExt()) && (ignoreExcludes || entry.includes(child))) { return FileObjects.sourceFileObject(child, root); } } } } } return null; }
private List<Difference> processExternalCUs(Map<?, int[]> tag2Span, Set<Tree> syntheticTrees) { if (externalChanges == null) { return Collections.<Difference>emptyList(); } List<Difference> result = new LinkedList<Difference>(); for (CompilationUnitTree t : externalChanges.values()) { try { FileObject targetFile = doCreateFromTemplate(t); CompilationUnitTree templateCUT = impl.getJavacTask().parse(FileObjects.sourceFileObject(targetFile, targetFile.getParent())).iterator().next(); CompilationUnitTree importComments = GeneratorUtilities.get(this).importComments(templateCUT, templateCUT); rewrite(importComments, getTreeMaker().asRemoved(t)); //changes.put(importComments, t); StringWriter target = new StringWriter(); ModificationResult.commit(targetFile, processCurrentCompilationUnit(new DiffContext(this, templateCUT, codeForCompilationUnit(templateCUT), new PositionConverter(), targetFile, syntheticTrees, getFileObject() != null ? getCompilationUnit() : null, getFileObject() != null ? getText() : null), tag2Span), target); result.add(new CreateChange(t.getSourceFile(), target.toString())); target.close(); } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } catch (IOException ex) { Exceptions.printStackTrace(ex); } } return result; }
private static CompileTuple createTuple(Context context, JavaParsingContext javaContext, Indexable indexable) { File root = null; if (!context.checkForEditorModifications() && "file".equals(indexable.getURL().getProtocol()) && (root = FileUtil.toFile(context.getRoot())) != null) { //NOI18N try { return new CompileTuple( FileObjects.fileFileObject( indexable, root, javaContext.getJavaFileFilter(), javaContext.getEncoding()), indexable); } catch (Exception ex) { //pass } } FileObject fo = URLMapper.findFileObject(indexable.getURL()); return fo != null ? new CompileTuple(FileObjects.sourceFileObject(fo, context.getRoot()), indexable) : null; }
log.nerrors = 0; final JavaFileManager jfm = context.get(JavaFileManager.class); JavaFileObject jfo = FileObjects.sourceFileObject(fo, null); Map<ClassSymbol, StringBuilder> oldCouplingErrors = couplingErrors; boolean oldSkipAPT = jc.skipAnnotationProcessing;
/** * 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; }