List of usage examples for org.eclipse.jdt.core.dom.rewrite ListRewrite getASTRewrite
public ASTRewrite getASTRewrite()
From source file:com.google.gdt.eclipse.appengine.rpc.markers.quickfixes.CreateRequestFactoryMethodProposal.java
License:Open Source License
/** * Add service methods for entities// ww w .j av a 2 s. co m */ private boolean addServiceMethods() { IMethod method = (IMethod) serviceMethod.resolveBinding().getJavaElement(); ICompilationUnit cu = method.getCompilationUnit(); String source = null; try { source = cu.getSource(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(cu); CompilationUnit astRoot = (CompilationUnit) parser.createAST(null); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); ListRewrite listRewriter = null; AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) astRoot.types().get(0); if (declaration != null) { listRewriter = rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()); } StringBuffer buf = new StringBuffer(); for (IType entity : entityList) { buf.append(CodegenUtils.format(RequestFactoryCodegenUtils.constructServiceMethods(entity, method), CodeFormatter.K_CLASS_BODY_DECLARATIONS)); } MethodDeclaration methodDecl = (MethodDeclaration) listRewriter.getASTRewrite() .createStringPlaceholder(buf.toString(), ASTNode.METHOD_DECLARATION); listRewriter.insertLast(methodDecl, null); TextEdit edits = rewrite.rewriteAST(document, cu.getJavaProject().getOptions(true)); edits.apply(document); cu.getBuffer().setContents(document.get()); cu.reconcile(ICompilationUnit.NO_AST, false, null, null); } catch (CoreException e) { AppEngineRPCPlugin.log(e); return false; } catch (MalformedTreeException e) { AppEngineRPCPlugin.log(e); return false; } catch (BadLocationException e) { AppEngineRPCPlugin.log(e); return false; } return true; }
From source file:com.google.gdt.eclipse.appengine.rpc.wizards.helpers.RpcServiceLayerCreator.java
License:Open Source License
private void addReqFactoryBody(IType type, IProgressMonitor monitor) throws MalformedTreeException, BadLocationException, CoreException { ICompilationUnit cu = type.getCompilationUnit(); cu.becomeWorkingCopy(monitor);/*w w w . j av a 2 s.c o m*/ String source = cu.getSource(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(cu); CompilationUnit astRoot = (CompilationUnit) parser.createAST(null); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); ListRewrite listRewriter = null; AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) astRoot.types().get(0); if (declaration != null) { listRewriter = rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()); } ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(astRoot, true); StringBuffer buf = new StringBuffer(); for (IType request : requestTypes) { importRewrite.addImport(request.getFullyQualifiedName()); buf.append(request.getElementName()); buf.append(" "); //$NON-NLS-N$ String name = request.getElementName(); buf.append(name.substring(0, 1).toLowerCase() + name.substring(1)); buf.append("();"); //$NON-NLS-N$ buf.append(lineDelimiter); } MethodDeclaration methodDecl = (MethodDeclaration) listRewriter.getASTRewrite() .createStringPlaceholder(buf.toString(), ASTNode.METHOD_DECLARATION); listRewriter.insertLast(methodDecl, null); TextEdit edits = rewrite.rewriteAST(document, cu.getJavaProject().getOptions(true)); edits.addChild(importRewrite.rewriteImports(monitor)); edits.apply(document); cu.getBuffer().setContents(document.get()); cu.reconcile(ICompilationUnit.NO_AST, false, null, null); ISourceRange range = type.getSourceRange(); IBuffer buffer = cu.getBuffer(); String originalContent = buffer.getText(range.getOffset(), range.getLength()); String formattedContent = format(originalContent, CodeFormatter.K_CLASS_BODY_DECLARATIONS); buffer.replace(range.getOffset(), range.getLength(), formattedContent); cu.commitWorkingCopy(true, monitor); cu.discardWorkingCopy(); }
From source file:org.eclipse.fx.ide.jdt.ui.internal.handler.AddFXBeanGetterSetterHandler.java
License:Open Source License
private static void addNewAccessor(final IType type, final IField field, final String contents, final ListRewrite rewrite, final ASTNode insertion) throws JavaModelException { final String delimiter = StubUtility.getLineDelimiterUsed(type); final MethodDeclaration declaration = (MethodDeclaration) rewrite.getASTRewrite() .createStringPlaceholder(CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, contents, 0, delimiter, field.getJavaProject()), ASTNode.METHOD_DECLARATION); if (insertion != null) rewrite.insertBefore(declaration, insertion, null); else// w w w .j a v a2 s .c om rewrite.insertLast(declaration, null); }