FileObject doCreateFromTemplate(CompilationUnitTree cut) throws IOException { ElementKind kind; if ("package-info.java".equals(cut.getSourceFile().getName())) { kind = ElementKind.PACKAGE; } else if (cut.getTypeDecls().isEmpty()) { kind = null; } else { switch (cut.getTypeDecls().get(0).getKind()) { case CLASS: kind = ElementKind.CLASS; break; case INTERFACE: kind = ElementKind.INTERFACE; break; case ANNOTATION_TYPE: kind = ElementKind.ANNOTATION_TYPE; break; case ENUM: kind = ElementKind.ENUM; break; default: Logger.getLogger(WorkingCopy.class.getName()).log(Level.SEVERE, "Cannot resolve template for {0}", cut.getTypeDecls().get(0).getKind()); kind = null; } } FileObject template = FileUtil.getConfigFile(template(kind)); return doCreateFromTemplate(template, cut.getSourceFile()); }
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; }
/** * 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; }