/** * 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); }
/** * 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); }
/** * 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; }