List of usage examples for org.eclipse.jdt.core.dom.rewrite ListRewrite insertFirst
public void insertFirst(ASTNode node, TextEditGroup editGroup)
From source file:ch.acanda.eclipse.pmd.java.resolution.design.UseUtilityClassQuickFix.java
License:Open Source License
@SuppressWarnings("unchecked") private void addPrivateConstructor(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) { final AST ast = typeDeclaration.getAST(); final MethodDeclaration constructor = (MethodDeclaration) ast.createInstance(MethodDeclaration.class); constructor.setConstructor(true);//from w w w . ja va2 s .c om final Modifier modifier = (Modifier) ast.createInstance(Modifier.class); modifier.setKeyword(ModifierKeyword.PRIVATE_KEYWORD); constructor.modifiers().add(modifier); constructor.setName(ASTUtil.copy(typeDeclaration.getName())); final Block body = (Block) ast.createInstance(Block.class); constructor.setBody(body); final ListRewrite statementRewrite = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY); final ASTNode comment = rewrite.createStringPlaceholder("// hide constructor of utility class", ASTNode.EMPTY_STATEMENT); statementRewrite.insertFirst(comment, null); final int position = findConstructorPosition(typeDeclaration); final ListRewrite bodyDeclarationRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); bodyDeclarationRewrite.insertAt(constructor, position, null); }
From source file:com.android.icu4j.srcgen.RunWithAnnotator.java
License:Apache License
private boolean addRunWithAnnotation(CompilationUnit cu, ASTRewrite rewrite, TypeDeclaration type, String runnerClass, boolean imported) { AST ast = cu.getAST();/* ww w .j a v a 2s . co m*/ QualifiedName qRunWith = (QualifiedName) ast.newName(RUN_WITH_CLASS_NAME); QualifiedName qRunner = (QualifiedName) ast.newName(runnerClass); if (!imported) { appendImport(cu, rewrite, qRunWith); appendImport(cu, rewrite, qRunner); } String runWithName = qRunWith.getName().getIdentifier(); String runnerName = qRunner.getName().getIdentifier(); SingleMemberAnnotation annotation = ast.newSingleMemberAnnotation(); annotation.setTypeName(ast.newSimpleName(runWithName)); TypeLiteral junit4Literal = ast.newTypeLiteral(); junit4Literal.setType(ast.newSimpleType(ast.newSimpleName(runnerName))); annotation.setValue(junit4Literal); ListRewrite lrw = rewrite.getListRewrite(type, type.getModifiersProperty()); lrw.insertFirst(annotation, null); return imported; }
From source file:com.android.ide.eclipse.adt.internal.lint.AddSuppressAnnotation.java
License:Open Source License
@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types private MultiTextEdit addSuppressAnnotation(IDocument document, ICompilationUnit compilationUnit, BodyDeclaration declaration) throws CoreException { List modifiers = declaration.modifiers(); SingleMemberAnnotation existing = null; for (Object o : modifiers) { if (o instanceof SingleMemberAnnotation) { SingleMemberAnnotation annotation = (SingleMemberAnnotation) o; String type = annotation.getTypeName().getFullyQualifiedName(); if (type.equals(FQCN_SUPPRESS_LINT) || type.endsWith(SUPPRESS_LINT)) { existing = annotation;/* www . j av a 2 s . c o m*/ break; } } } ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true); String local = importRewrite.addImport(FQCN_SUPPRESS_LINT); AST ast = declaration.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); if (existing == null) { SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation(); newAnnotation.setTypeName(ast.newSimpleName(local)); StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); newAnnotation.setValue(value); ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty()); listRewrite.insertFirst(newAnnotation, null); } else { Expression existingValue = existing.getValue(); if (existingValue instanceof StringLiteral) { StringLiteral stringLiteral = (StringLiteral) existingValue; if (mId.equals(stringLiteral.getLiteralValue())) { // Already contains the id return null; } // Create a new array initializer holding the old string plus the new id ArrayInitializer array = ast.newArrayInitializer(); StringLiteral old = ast.newStringLiteral(); old.setLiteralValue(stringLiteral.getLiteralValue()); array.expressions().add(old); StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); array.expressions().add(value); rewriter.set(existing, VALUE_PROPERTY, array, null); } else if (existingValue instanceof ArrayInitializer) { // Existing array: just append the new string ArrayInitializer array = (ArrayInitializer) existingValue; List expressions = array.expressions(); if (expressions != null) { for (Object o : expressions) { if (o instanceof StringLiteral) { if (mId.equals(((StringLiteral) o).getLiteralValue())) { // Already contains the id return null; } } } } StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); ListRewrite listRewrite = rewriter.getListRewrite(array, EXPRESSIONS_PROPERTY); listRewrite.insertLast(value, null); } else { assert false : existingValue; return null; } } TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor()); TextEdit annotationEdits = rewriter.rewriteAST(document, null); // Apply to the document MultiTextEdit edit = new MultiTextEdit(); // Create the edit to change the imports, only if // anything changed if (importEdits.hasChildren()) { edit.addChild(importEdits); } edit.addChild(annotationEdits); return edit; }
From source file:com.android.ide.eclipse.adt.internal.lint.AddSuppressAnnotation.java
License:Open Source License
@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types private MultiTextEdit addTargetApiAnnotation(IDocument document, ICompilationUnit compilationUnit, BodyDeclaration declaration) throws CoreException { List modifiers = declaration.modifiers(); SingleMemberAnnotation existing = null; for (Object o : modifiers) { if (o instanceof SingleMemberAnnotation) { SingleMemberAnnotation annotation = (SingleMemberAnnotation) o; String type = annotation.getTypeName().getFullyQualifiedName(); if (type.equals(FQCN_TARGET_API) || type.endsWith(TARGET_API)) { existing = annotation;/* w ww . ja va 2 s.co m*/ break; } } } ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true); importRewrite.addImport("android.os.Build"); //$NON-NLS-1$ String local = importRewrite.addImport(FQCN_TARGET_API); AST ast = declaration.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); if (existing == null) { SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation(); newAnnotation.setTypeName(ast.newSimpleName(local)); Expression value = createLiteral(ast); newAnnotation.setValue(value); ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty()); listRewrite.insertFirst(newAnnotation, null); } else { Expression value = createLiteral(ast); rewriter.set(existing, VALUE_PROPERTY, value, null); } TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor()); TextEdit annotationEdits = rewriter.rewriteAST(document, null); MultiTextEdit edit = new MultiTextEdit(); if (importEdits.hasChildren()) { edit.addChild(importEdits); } edit.addChild(annotationEdits); return edit; }
From source file:com.android.ide.eclipse.auidt.internal.lint.AddSuppressAnnotation.java
License:Open Source License
@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types private MultiTextEdit addSuppressAnnotation(IDocument document, ICompilationUnit compilationUnit, BodyDeclaration declaration) throws CoreException { List modifiers = declaration.modifiers(); SingleMemberAnnotation existing = null; for (Object o : modifiers) { if (o instanceof SingleMemberAnnotation) { SingleMemberAnnotation annotation = (SingleMemberAnnotation) o; String type = annotation.getTypeName().getFullyQualifiedName(); if (type.equals(FQCN_SUPPRESS_LINT) || type.endsWith(SUPPRESS_LINT)) { existing = annotation;//from w w w. j a v a 2 s .co m break; } } } ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true); String local = importRewrite.addImport(FQCN_SUPPRESS_LINT); AST ast = declaration.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); if (existing == null) { SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation(); newAnnotation.setTypeName(ast.newSimpleName(local)); StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); newAnnotation.setValue(value); ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty()); listRewrite.insertFirst(newAnnotation, null); } else { Expression existingValue = existing.getValue(); if (existingValue instanceof StringLiteral) { // Create a new array initializer holding the old string plus the new id ArrayInitializer array = ast.newArrayInitializer(); StringLiteral old = ast.newStringLiteral(); StringLiteral stringLiteral = (StringLiteral) existingValue; old.setLiteralValue(stringLiteral.getLiteralValue()); array.expressions().add(old); StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); array.expressions().add(value); rewriter.set(existing, VALUE_PROPERTY, array, null); } else if (existingValue instanceof ArrayInitializer) { // Existing array: just append the new string ArrayInitializer array = (ArrayInitializer) existingValue; StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); ListRewrite listRewrite = rewriter.getListRewrite(array, EXPRESSIONS_PROPERTY); listRewrite.insertLast(value, null); } else { assert false : existingValue; return null; } } TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor()); TextEdit annotationEdits = rewriter.rewriteAST(document, null); // Apply to the document MultiTextEdit edit = new MultiTextEdit(); // Create the edit to change the imports, only if // anything changed if (importEdits.hasChildren()) { edit.addChild(importEdits); } edit.addChild(annotationEdits); return edit; }
From source file:com.android.ide.eclipse.auidt.internal.lint.AddSuppressAnnotation.java
License:Open Source License
@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types private MultiTextEdit addTargetApiAnnotation(IDocument document, ICompilationUnit compilationUnit, BodyDeclaration declaration) throws CoreException { List modifiers = declaration.modifiers(); SingleMemberAnnotation existing = null; for (Object o : modifiers) { if (o instanceof SingleMemberAnnotation) { SingleMemberAnnotation annotation = (SingleMemberAnnotation) o; String type = annotation.getTypeName().getFullyQualifiedName(); if (type.equals(FQCN_TARGET_API) || type.endsWith(TARGET_API)) { existing = annotation;// ww w .j a v a2 s . c o m break; } } } ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true); String local = importRewrite.addImport(FQCN_TARGET_API); AST ast = declaration.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); if (existing == null) { SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation(); newAnnotation.setTypeName(ast.newSimpleName(local)); NumberLiteral value = ast.newNumberLiteral(Integer.toString(mTargetApi)); //value.setLiteralValue(mId); newAnnotation.setValue(value); ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty()); listRewrite.insertFirst(newAnnotation, null); } else { Expression existingValue = existing.getValue(); if (existingValue instanceof NumberLiteral) { // Change the value to the new value NumberLiteral value = ast.newNumberLiteral(Integer.toString(mTargetApi)); rewriter.set(existing, VALUE_PROPERTY, value, null); } else { assert false : existingValue; return null; } } TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor()); TextEdit annotationEdits = rewriter.rewriteAST(document, null); MultiTextEdit edit = new MultiTextEdit(); if (importEdits.hasChildren()) { edit.addChild(importEdits); } edit.addChild(annotationEdits); return edit; }
From source file:com.arcbees.gwtp.plugin.core.presenter.CreatePresenterPage.java
License:Apache License
private void addMethodsToNameTokens(ICompilationUnit unit, String nameToken, IProgressMonitor monitor) throws JavaModelException, MalformedTreeException, BadLocationException { String fieldName = getFieldNameFromNameToken(nameToken); Document document = new Document(unit.getSource()); CompilationUnit astRoot = initAstRoot(unit, monitor); // creation of ASTRewrite ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); // find existing method MethodDeclaration method = findMethod(astRoot, getNameTokenMethod(fieldName)); if (method != null) { logger.severe("FYI: the method in nameTokens already exists." + method.toString()); return;//from w ww .j av a 2s . co m } List types = astRoot.types(); ASTNode rootNode = (ASTNode) types.get(0); ListRewrite listRewrite = rewrite.getListRewrite(rootNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); ASTNode fieldNode = rewrite.createStringPlaceholder( "public static final String " + fieldName + " = \"" + nameToken + "\";", ASTNode.EMPTY_STATEMENT); StringBuilder nameTokenMethod = new StringBuilder(); nameTokenMethod.append("public static String ").append(getNameTokenMethod(fieldName)).append("() {\n") .append("return " + fieldName + ";\n").append("}\n"); ASTNode methodNode = rewrite.createStringPlaceholder(nameTokenMethod.toString(), ASTNode.EMPTY_STATEMENT); listRewrite.insertFirst(fieldNode, null); listRewrite.insertLast(methodNode, null); // computation of the text edits TextEdit edits = rewrite.rewriteAST(document, unit.getJavaProject().getOptions(true)); // computation of the new source code edits.apply(document); // format code String newSource = new CodeFormattingUtil(getJavaProject(), monitor).formatCodeJavaClass(document); // update of the compilation unit and save it IBuffer buffer = unit.getBuffer(); buffer.setContents(newSource); buffer.save(monitor, true); }
From source file:com.arcbees.gwtp.plugin.core.presenter.CreatePresenterPage.java
License:Apache License
/** * TODO extract this possibly, but I think I'll wait till I get into slots * before I do it see what is common.// w w w .java2 s. c om */ private void createPresenterGinlink(ICompilationUnit unit, String modulePackageName, String moduleName, IProgressMonitor monitor) throws JavaModelException, MalformedTreeException, BadLocationException { unit.createImport(modulePackageName + "." + moduleName, null, monitor); Document document = new Document(unit.getSource()); CompilationUnit astRoot = initAstRoot(unit, monitor); // creation of ASTRewrite ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); // find the configure method MethodDeclaration method = findMethod(astRoot, "configure"); if (method == null) { logger.severe("createPresenterGinLink() unit did not have configure implementation."); return; } // presenter configure method install(new Module()); String installModuleStatement = "install(new " + moduleName + "());"; Block block = method.getBody(); ListRewrite listRewrite = rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY); ASTNode placeHolder = rewrite.createStringPlaceholder(installModuleStatement, ASTNode.EMPTY_STATEMENT); listRewrite.insertFirst(placeHolder, null); // computation of the text edits TextEdit edits = rewrite.rewriteAST(document, unit.getJavaProject().getOptions(true)); // computation of the new source code edits.apply(document); String newSource = document.get(); // update of the compilation unit and save it IBuffer buffer = unit.getBuffer(); buffer.setContents(newSource); buffer.save(monitor, true); }
From source file:com.google.gwt.eclipse.core.clientbundle.ClientBundleResource.java
License:Open Source License
public MethodDeclaration createMethodDeclaration(IType clientBundle, ASTRewrite astRewrite, ImportRewrite importRewrite, boolean addComments) throws CoreException { AST ast = astRewrite.getAST();// w w w. j av a 2s . com MethodDeclaration methodDecl = ast.newMethodDeclaration(); // Method is named after the resource it accesses methodDecl.setName(ast.newSimpleName(getMethodName())); // Method return type is a ResourcePrototype subtype ITypeBinding resourceTypeBinding = JavaASTUtils.resolveType(clientBundle.getJavaProject(), getReturnTypeName()); Type resourceType = importRewrite.addImport(resourceTypeBinding, ast); methodDecl.setReturnType2(resourceType); // Add @Source annotation if necessary String sourceAnnotationValue = getSourceAnnotationValue(clientBundle); if (sourceAnnotationValue != null) { // Build the annotation SingleMemberAnnotation sourceAnnotation = ast.newSingleMemberAnnotation(); sourceAnnotation.setTypeName(ast.newName("Source")); StringLiteral annotationValue = ast.newStringLiteral(); annotationValue.setLiteralValue(sourceAnnotationValue); sourceAnnotation.setValue(annotationValue); // Add the annotation to the method ChildListPropertyDescriptor modifiers = methodDecl.getModifiersProperty(); ListRewrite modifiersRewriter = astRewrite.getListRewrite(methodDecl, modifiers); modifiersRewriter.insertFirst(sourceAnnotation, null); } return methodDecl; }
From source file:com.gowan.plugin.handlers.JUnit3Handler.java
License:Open Source License
private void transformMethod(AST ast, ASTRewrite rewrite, MethodDeclaration original, String annotationName) { ListRewrite setupRw = rewrite.getListRewrite(original, MethodDeclaration.MODIFIERS2_PROPERTY); MarkerAnnotation annotation = ast.newMarkerAnnotation(); annotation.setTypeName(ast.newSimpleName(annotationName)); setupRw.insertFirst(annotation, null); List<IExtendedModifier> modifiers = original.modifiers(); Annotation override = null;/*from w w w .ja v a2s.c o m*/ for (IExtendedModifier iExtendedModifier : modifiers) { if (iExtendedModifier.isAnnotation()) { Annotation localAnnotation = (Annotation) iExtendedModifier; String fullName = localAnnotation.getTypeName().getFullyQualifiedName(); if ("java.lang.Override".equals(fullName) || "Override".equals(fullName)) { override = localAnnotation; break; } } } try { setupRw.remove(override, null); } catch (IllegalArgumentException e) { } }