public void generate() throws IOException { Task<WorkingCopy> task = new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(Phase.RESOLVED); TreeMaker make = workingCopy.getTreeMaker(); GenerationUtils gu = GenerationUtils.newInstance(workingCopy); TypeElement typeElement = SourceUtils.getPublicTopLevelElement(workingCopy); ClassTree oldClassTree = workingCopy.getTrees().getTree(typeElement); ClassTree classTree = addFields(gu, make, typeElement, oldClassTree); if (isBodyTag) { classTree = make.addClassMember(classTree, addBodyEvaluatorCheck(evaluateBody, make)); } classTree = addSetters(gu, make, typeElement, classTree); workingCopy.rewrite(oldClassTree, classTree); } }; ModificationResult result = clazz.runModificationTask(task); result.commit(); }
public static Object execute(Write task, JavaSource js) { WriteTaskWrapper taskWrapper = new WriteTaskWrapper(task); ModificationResult result = null; try { result = js.runModificationTask(taskWrapper); if(result != null) { result.commit(); } } catch (IOException ex) { ex.printStackTrace(); } return taskWrapper.result; }
public void generate(JavaSource clazz) throws IOException { Task<WorkingCopy> task = new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); gu = GenerationUtils.newInstance(workingCopy); for (Tree typeDecl : cut.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) { Element e = workingCopy.getTrees().getElement(new TreePath(new TreePath(workingCopy.getCompilationUnit()), typeDecl)); if (e != null && e.getKind().isClass()) { TypeElement te = (TypeElement) e; ClassTree ct = (ClassTree) typeDecl; workingCopy.rewrite(ct, generateInterfaces(workingCopy, te, ct, gu)); } } } } }; ModificationResult result = clazz.runModificationTask(task); result.commit(); // if (isContext) addContextListenerMethods(); // if (isContextAttr) addContextAttrListenerMethods(); // if (isSession) addSessionListenerMethods(); // if (isSessionAttr) addSessionAttrListenerMethods(); // if (isRequest) addRequestListenerMethods(); // if (isRequestAttr) addRequestAttrListenerMethods(); }
private void addImports(final StyledDocument styledDocument ) { try { JavaSource.forDocument(styledDocument).runModificationTask(new CancellableTask<WorkingCopy>() { public void cancel() { } public void run(WorkingCopy parameter) throws Exception { parameter.toPhase(JavaSource.Phase.PARSED); for (String fqn : fqns) { SourceUtils.resolveImport(parameter, new TreePath(parameter.getCompilationUnit()), fqn); } } }).commit(); } catch (IOException e) { Exceptions.printStackTrace(e); } } };
private void addImports(final StyledDocument styledDocument) { try { JavaSource.forDocument(styledDocument).runModificationTask(new CancellableTask<WorkingCopy>() { public void cancel() { } public void run(WorkingCopy parameter) throws Exception { parameter.toPhase(JavaSource.Phase.PARSED); String typeFqn = getComponent().getType().getString(); if (!fullyNamesList.contains(typeFqn)) { fullyNamesList.add(typeFqn); } DesignComponent connector = MidpDatabindingSupport.getConnector(getComponent(), bindedProperty); for (String fqn : fullyNamesList) { if (bindedProperty == null) { SourceUtils.resolveImport(parameter, new TreePath(parameter.getCompilationUnit()), fqn); } else if (connector != null) { SourceUtils.resolveImport(parameter, new TreePath(parameter.getCompilationUnit()), fqn); } } } }).commit(); } catch (IOException e) { Exceptions.printStackTrace(e); } } }
@Override public void run() { try { ModificationResult.runModificationTask(Collections.singletonList(info.getSnapshot().getSource()), new UserTask() { @Override public void run(ResultIterator resultIterator) throws Exception { WorkingCopy copy = WorkingCopy.get(resultIterator.getParserResult()); copy.toPhase(Phase.ELEMENTS_RESOLVED); Element elementToImport = handle.resolve(copy); if (elementToImport == null) { // the text was possibly changed ? return; } copy.rewrite(copy.getCompilationUnit(), GeneratorUtilities.get(copy).addImports(copy.getCompilationUnit(), Collections.singleton(elementToImport))); } }).commit(); } catch (Exception e) { Exceptions.printStackTrace(e); } } });
public void commit() { try { if (commited) { for (BackupFacility.Handle id:ids) { try { id.restore(); } catch (IOException ex) { throw (RuntimeException) new RuntimeException().initCause(ex); } } } else { commited = true; for (ModificationResult result:results) { ids.add(BackupFacility.getDefault().backup(result.getModifiedFileObjects())); result.commit(); } } } catch (IOException ex) { throw (RuntimeException) new RuntimeException().initCause(ex); } }
result.commit (); } catch (IOException e) { throw Debug.error (e);
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; }
targetSource.runModificationTask(modificationTask).commit(); }catch(IOException e){ ErrorManager.getDefault().notify(e);