Java Code Examples for org.eclipse.jdt.core.dom.rewrite.ListRewrite#insertLast()
The following examples show how to use
org.eclipse.jdt.core.dom.rewrite.ListRewrite#insertLast() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeParametersFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { TextEditGroup group= createTextEditGroup(FixMessages.TypeParametersFix_insert_inferred_type_arguments_description, cuRewrite); ASTRewrite rewrite= cuRewrite.getASTRewrite(); ImportRewrite importRewrite= cuRewrite.getImportRewrite(); AST ast= cuRewrite.getRoot().getAST(); for (int i= 0; i < fCreatedTypes.length; i++) { ParameterizedType createdType= fCreatedTypes[i]; ITypeBinding[] typeArguments= createdType.resolveBinding().getTypeArguments(); ContextSensitiveImportRewriteContext importContext= new ContextSensitiveImportRewriteContext(cuRewrite.getRoot(), createdType.getStartPosition(), importRewrite); ListRewrite argumentsRewrite= rewrite.getListRewrite(createdType, ParameterizedType.TYPE_ARGUMENTS_PROPERTY); for (int j= 0; j < typeArguments.length; j++) { ITypeBinding typeArgument= typeArguments[j]; Type argumentNode= importRewrite.addImport(typeArgument, ast, importContext); argumentsRewrite.insertLast(argumentNode, group); } } }
Example 2
Source File: CreateSuperCallResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
@Override protected void repairBug(ASTRewrite rewrite, CompilationUnit workingUnit, BugInstance bug) throws BugResolutionException { Assert.isNotNull(rewrite); Assert.isNotNull(workingUnit); Assert.isNotNull(bug); TypeDeclaration type = getTypeDeclaration(workingUnit, bug.getPrimaryClass()); MethodDeclaration method = getMethodDeclaration(type, bug.getPrimaryMethod()); AST ast = rewrite.getAST(); SuperMethodInvocation superCall = createSuperMethodInvocation(rewrite, method); ExpressionStatement statement = ast.newExpressionStatement(superCall); Block methodBody = method.getBody(); ListRewrite listRewrite = rewrite.getListRewrite(methodBody, Block.STATEMENTS_PROPERTY); if (isInsertFirst()) { listRewrite.insertFirst(statement, null); } else { listRewrite.insertLast(statement, null); } }
Example 3
Source File: ExtractMethodFragmentRefactoring.java From JDeodorant with MIT License | 6 votes |
protected TryStatement copyTryStatement(ASTRewrite sourceRewriter, AST ast, TryStatement tryStatementParent) { TryStatement newTryStatement = ast.newTryStatement(); ListRewrite resourceRewrite = sourceRewriter.getListRewrite(newTryStatement, TryStatement.RESOURCES_PROPERTY); List<VariableDeclarationExpression> resources = tryStatementParent.resources(); for(VariableDeclarationExpression expression : resources) { resourceRewrite.insertLast(expression, null); } ListRewrite catchClauseRewrite = sourceRewriter.getListRewrite(newTryStatement, TryStatement.CATCH_CLAUSES_PROPERTY); List<CatchClause> catchClauses = tryStatementParent.catchClauses(); for(CatchClause catchClause : catchClauses) { catchClauseRewrite.insertLast(catchClause, null); } if(tryStatementParent.getFinally() != null) { sourceRewriter.set(newTryStatement, TryStatement.FINALLY_PROPERTY, tryStatementParent.getFinally(), null); } return newTryStatement; }
Example 4
Source File: ASTUtil.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
private static void addImports(ListRewrite importRewrite, Comparator<? super ImportDeclaration> comparator, Iterator<ImportDeclaration> newImports) { try { ImportDeclaration newImport = newImports.next(); List<?> imports = importRewrite.getRewrittenList(); for (Object importObj : imports) { ImportDeclaration anImport = (ImportDeclaration) importObj; int comp = comparator.compare(newImport, anImport); if (comp > 0) { continue; } if (comp < 0) { importRewrite.insertBefore(newImport, anImport, null); } newImport = newImports.next(); } importRewrite.insertLast(newImport, null); while (newImports.hasNext()) { importRewrite.insertLast(newImports.next(), null); } } catch (NoSuchElementException e) { // do nothing } }
Example 5
Source File: ExtractMethodFragmentRefactoring.java From JDeodorant with MIT License | 6 votes |
protected ListRewrite createTryStatementIfNeeded(ASTRewrite sourceRewriter, AST ast, ListRewrite bodyRewrite, PDGNode node) { Statement statement = node.getASTStatement(); ASTNode statementParent = statement.getParent(); if(statementParent != null && statementParent instanceof Block) statementParent = statementParent.getParent(); if(statementParent != null && statementParent instanceof TryStatement) { TryStatement tryStatementParent = (TryStatement)statementParent; if(tryStatementsToBeRemoved.contains(tryStatementParent) || tryStatementsToBeCopied.contains(tryStatementParent)) { if(tryStatementBodyRewriteMap.containsKey(tryStatementParent)) { bodyRewrite = tryStatementBodyRewriteMap.get(tryStatementParent); } else { TryStatement newTryStatement = copyTryStatement(sourceRewriter, ast, tryStatementParent); Block tryMethodBody = ast.newBlock(); sourceRewriter.set(newTryStatement, TryStatement.BODY_PROPERTY, tryMethodBody, null); ListRewrite tryBodyRewrite = sourceRewriter.getListRewrite(tryMethodBody, Block.STATEMENTS_PROPERTY); tryStatementBodyRewriteMap.put(tryStatementParent, tryBodyRewrite); bodyRewrite.insertLast(newTryStatement, null); bodyRewrite = tryBodyRewrite; } } } return bodyRewrite; }
Example 6
Source File: CreateDoPrivilegedBlockResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
protected void updateMethodParams(ASTRewrite rewrite, Set<String> variables, List<?> params) { Assert.isNotNull(rewrite); Assert.isNotNull(variables); Assert.isNotNull(params); for (Object paramObj : params) { SingleVariableDeclaration param = (SingleVariableDeclaration) paramObj; if (variables.contains(param.getName().getFullyQualifiedName())) { ListRewrite listRewrite = rewrite.getListRewrite(param, SingleVariableDeclaration.MODIFIERS2_PROPERTY); listRewrite.insertLast(rewrite.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD), null); } } }
Example 7
Source File: GenerateToStringOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
protected void insertMethod(MethodDeclaration method, ListRewrite rewriter, BodyDeclaration replace) throws JavaModelException { if (replace != null) { rewriter.replace(replace, method, null); } else { ASTNode insertion= StubUtility2.getNodeToInsertBefore(rewriter, fInsert); if (insertion != null) rewriter.insertBefore(method, insertion, null); else rewriter.insertLast(method, null); } }
Example 8
Source File: SuperTypeRefactoringProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Creates the type parameters of the new supertype. * * @param targetRewrite * the target compilation unit rewrite * @param subType * the subtype * @param sourceDeclaration * the type declaration of the source type * @param targetDeclaration * the type declaration of the target type */ protected final void createTypeParameters(final ASTRewrite targetRewrite, final IType subType, final AbstractTypeDeclaration sourceDeclaration, final AbstractTypeDeclaration targetDeclaration) { Assert.isNotNull(targetRewrite); Assert.isNotNull(sourceDeclaration); Assert.isNotNull(targetDeclaration); if (sourceDeclaration instanceof TypeDeclaration) { TypeParameter parameter= null; final ListRewrite rewrite= targetRewrite.getListRewrite(targetDeclaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY); for (final Iterator<TypeParameter> iterator= ((TypeDeclaration) sourceDeclaration).typeParameters().iterator(); iterator.hasNext();) { parameter= iterator.next(); rewrite.insertLast(ASTNode.copySubtree(targetRewrite.getAST(), parameter), null); ImportRewriteUtil.collectImports(subType.getJavaProject(), sourceDeclaration, fTypeBindings, fStaticBindings, false); } } }
Example 9
Source File: NewCUProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private TextEdit constructEnclosingTypeEdit(ICompilationUnit icu) throws CoreException { ASTParser astParser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); astParser.setSource(icu); CompilationUnit cu = (CompilationUnit) astParser.createAST(null); TypeDeclaration enclosingDecl = findEnclosingTypeDeclaration(cu, fTypeContainer.getElementName()); AST ast = cu.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); final AbstractTypeDeclaration newDeclaration; switch (fTypeKind) { case K_CLASS: newDeclaration = ast.newTypeDeclaration(); ((TypeDeclaration) newDeclaration).setInterface(false); break; case K_INTERFACE: newDeclaration = ast.newTypeDeclaration(); ((TypeDeclaration) newDeclaration).setInterface(true); break; case K_ENUM: newDeclaration = ast.newEnumDeclaration(); break; case K_ANNOTATION: newDeclaration = ast.newAnnotationTypeDeclaration(); break; default: return null; } newDeclaration.setJavadoc(null); newDeclaration.setName(ast.newSimpleName(ASTNodes.getSimpleNameIdentifier(fNode))); newDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); if (isParameterizedType(fTypeKind, fNode)) { addTypeParameters((TypeDeclaration) newDeclaration); } ListRewrite lrw = rewrite.getListRewrite(enclosingDecl, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); lrw.insertLast(newDeclaration, null); return rewrite.rewriteAST(); }
Example 10
Source File: ReorgPolicyFactory.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void copyImportToDestination(IImportDeclaration declaration, ASTRewrite targetRewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException { ImportDeclaration sourceNode= ASTNodeSearchUtil.getImportDeclarationNode(declaration, sourceCuNode); ImportDeclaration copiedNode= (ImportDeclaration) ASTNode.copySubtree(targetRewrite.getAST(), sourceNode); ListRewrite listRewrite= targetRewrite.getListRewrite(destinationCuNode, CompilationUnit.IMPORTS_PROPERTY); if (getJavaElementDestination() instanceof IImportDeclaration) { ImportDeclaration destinationNode= ASTNodeSearchUtil.getImportDeclarationNode((IImportDeclaration) getJavaElementDestination(), destinationCuNode); insertRelative(copiedNode, destinationNode, listRewrite); } else { //dropped on container, we could be better here listRewrite.insertLast(copiedNode, null); } }
Example 11
Source File: FieldModifierResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
@Override protected void repairBug(ASTRewrite rewrite, CompilationUnit workingUnit, BugInstance bug) throws BugResolutionException { Assert.isNotNull(rewrite); Assert.isNotNull(workingUnit); Assert.isNotNull(bug); TypeDeclaration type = getTypeDeclaration(workingUnit, bug.getPrimaryClass()); FieldDeclaration field = getFieldDeclaration(type, bug.getPrimaryField()); Modifier finalModifier = workingUnit.getAST().newModifier(getModifierToAdd()); ListRewrite modRewrite = rewrite.getListRewrite(field, FieldDeclaration.MODIFIERS2_PROPERTY); modRewrite.insertLast(finalModifier, null); }
Example 12
Source File: AddResourcesToClientBundleAction.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
public void run(IProgressMonitor monitor) throws CoreException { ICompilationUnit icu = clientBundle.getCompilationUnit(); CompilationUnit cu = JavaASTUtils.parseCompilationUnit(icu); ImportRewrite importRewrite = StubUtility.createImportRewrite(cu, true); // Find the target type declaration TypeDeclaration typeDecl = JavaASTUtils.findTypeDeclaration(cu, clientBundle.getFullyQualifiedName()); if (typeDecl == null) { throw new CoreException( StatusUtilities.newErrorStatus("Missing ClientBundle type " + clientBundle.getFullyQualifiedName(), GWTPlugin.PLUGIN_ID)); } // We need to rewrite the AST of the ClientBundle type declaration ASTRewrite astRewrite = ASTRewrite.create(cu.getAST()); ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(typeDecl); ListRewrite listRewriter = astRewrite.getListRewrite(typeDecl, property); // Add the new resource methods boolean addComments = StubUtility.doAddComments(icu.getJavaProject()); for (ClientBundleResource resource : resources) { listRewriter.insertLast(resource.createMethodDeclaration(clientBundle, astRewrite, importRewrite, addComments), null); } // Create the edit to add the methods and update the imports TextEdit rootEdit = new MultiTextEdit(); rootEdit.addChild(astRewrite.rewriteAST()); rootEdit.addChild(importRewrite.rewriteImports(null)); // Apply the change to the compilation unit CompilationUnitChange cuChange = new CompilationUnitChange( "Update ClientBundle", icu); cuChange.setSaveMode(TextFileChange.KEEP_SAVE_STATE); cuChange.setEdit(rootEdit); cuChange.perform(new NullProgressMonitor()); }
Example 13
Source File: SuppressWarningsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean addSuppressArgument(ASTRewrite rewrite, Expression value, StringLiteral newStringLiteral) { if (value instanceof ArrayInitializer) { ListRewrite listRewrite= rewrite.getListRewrite(value, ArrayInitializer.EXPRESSIONS_PROPERTY); listRewrite.insertLast(newStringLiteral, null); } else if (value instanceof StringLiteral) { ArrayInitializer newArr= rewrite.getAST().newArrayInitializer(); newArr.expressions().add(rewrite.createMoveTarget(value)); newArr.expressions().add(newStringLiteral); rewrite.replace(value, newArr, null); } else { return false; } return true; }
Example 14
Source File: RefactoringUtility.java From JDeodorant with MIT License | 5 votes |
private static ParameterizedType createParameterizedType(AST ast, ITypeBinding typeBinding, ASTRewrite rewriter) { ITypeBinding erasure = typeBinding.getErasure(); ITypeBinding[] typeArguments = typeBinding.getTypeArguments(); ParameterizedType parameterizedType = ast.newParameterizedType(generateTypeFromTypeBinding(erasure, ast, rewriter)); ListRewrite typeArgumentsRewrite = rewriter.getListRewrite(parameterizedType, ParameterizedType.TYPE_ARGUMENTS_PROPERTY); for(ITypeBinding typeArgument : typeArguments) { typeArgumentsRewrite.insertLast(generateTypeFromTypeBinding(typeArgument, ast, rewriter), null); } return parameterizedType; }
Example 15
Source File: QuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static void addExceptionToThrows(AST ast, MethodDeclaration methodDeclaration, ASTRewrite rewrite, Type type2) { ITypeBinding binding= type2.resolveBinding(); if (binding == null || isNotYetThrown(binding, methodDeclaration.thrownExceptionTypes())) { Type newType= (Type) ASTNode.copySubtree(ast, type2); ListRewrite listRewriter= rewrite.getListRewrite(methodDeclaration, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY); listRewriter.insertLast(newType, null); } }
Example 16
Source File: LocalCorrectionsSubProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@SuppressWarnings("deprecation") private static void createMissingDefaultProposal(IInvocationContext context, ASTNode parent, Collection<ChangeCorrectionProposal> proposals) { List<Statement> statements; Expression expression; if (parent instanceof SwitchStatement) { SwitchStatement switchStatement = (SwitchStatement) parent; statements = switchStatement.statements(); expression = switchStatement.getExpression(); } else if (parent instanceof SwitchExpression) { SwitchExpression switchExpression = (SwitchExpression) parent; statements = switchExpression.statements(); expression = switchExpression.getExpression(); } else { return; } AST ast = parent.getAST(); ASTRewrite astRewrite = ASTRewrite.create(ast); ListRewrite listRewrite; if (parent instanceof SwitchStatement) { listRewrite = astRewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY); } else { listRewrite = astRewrite.getListRewrite(parent, SwitchExpression.STATEMENTS_PROPERTY); } String label = CorrectionMessages.LocalCorrectionsSubProcessor_add_default_case_description; LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_DEFAULT_CASE); SwitchCase newSwitchCase = ast.newSwitchCase(); listRewrite.insertLast(newSwitchCase, null); if (ASTHelper.isSwitchCaseExpressionsSupportedInAST(ast)) { if (statements.size() > 0) { Statement firstStatement = statements.get(0); SwitchCase switchCase = (SwitchCase) firstStatement; boolean isArrow = switchCase.isSwitchLabeledRule(); newSwitchCase.setSwitchLabeledRule(isArrow); if (isArrow || parent instanceof SwitchExpression) { ThrowStatement newThrowStatement = getThrowForUnexpectedDefault(expression, ast, astRewrite); listRewrite.insertLast(newThrowStatement, null); proposal.addLinkedPosition(astRewrite.track(newThrowStatement), true, null); } else { listRewrite.insertLast(ast.newBreakStatement(), null); } } else { listRewrite.insertLast(ast.newBreakStatement(), null); } } else { newSwitchCase.setExpression(null); listRewrite.insertLast(ast.newBreakStatement(), null); } proposals.add(proposal); }
Example 17
Source File: NewVariableCorrectionProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private ASTRewrite doAddParam(CompilationUnit cu) { AST ast= cu.getAST(); SimpleName node= fOriginalNode; BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(node); if (decl instanceof MethodDeclaration) { MethodDeclaration methodDeclaration= (MethodDeclaration) decl; ASTRewrite rewrite= ASTRewrite.create(ast); ImportRewrite imports= createImportRewrite((CompilationUnit) decl.getRoot()); ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports); SingleVariableDeclaration newDecl= ast.newSingleVariableDeclaration(); newDecl.setType(evaluateVariableType(ast, imports, importRewriteContext, methodDeclaration.resolveBinding(), TypeLocation.PARAMETER)); newDecl.setName(ast.newSimpleName(node.getIdentifier())); ListRewrite listRewriter= rewrite.getListRewrite(decl, MethodDeclaration.PARAMETERS_PROPERTY); listRewriter.insertLast(newDecl, null); // add javadoc tag Javadoc javadoc= methodDeclaration.getJavadoc(); if (javadoc != null) { HashSet<String> leadingNames= new HashSet<>(); for (Iterator<SingleVariableDeclaration> iter= methodDeclaration.parameters().iterator(); iter.hasNext();) { SingleVariableDeclaration curr= iter.next(); leadingNames.add(curr.getName().getIdentifier()); } SimpleName newTagRef= ast.newSimpleName(node.getIdentifier()); TagElement newTagElement= ast.newTagElement(); newTagElement.setTagName(TagElement.TAG_PARAM); newTagElement.fragments().add(newTagRef); TextElement commentStart= ast.newTextElement(); newTagElement.fragments().add(commentStart); ListRewrite tagsRewriter= rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY); JavadocTagsSubProcessor.insertTag(tagsRewriter, newTagElement, leadingNames); } return rewrite; } return null; }
Example 18
Source File: RegularBuilderBuilderMethodCreator.java From SparkBuilderGenerator with MIT License | 4 votes |
private void addBuilderMethod(AST ast, ListRewrite listRewrite, TypeDeclaration typeDeclaration, TypeDeclaration builderType) { Block builderMethodBlock = blockWithNewBuilderCreationFragment.createReturnBlock(ast, builderType); MethodDeclaration builderMethod = builderMethodDefinitionCreatorFragment.createBuilderMethod(ast, typeDeclaration, builderType.getName().toString()); builderMethod.setBody(builderMethodBlock); listRewrite.insertLast(builderMethod, null); }
Example 19
Source File: ExtractMethodRefactoring.java From JDeodorant with MIT License | 4 votes |
protected void processStatementNode(ListRewrite bodyRewrite, PDGNode dstPDGNode, AST ast, ASTRewrite sourceRewriter) { bodyRewrite.insertLast(dstPDGNode.getASTStatement(), null); }
Example 20
Source File: ImportPopulator.java From SparkBuilderGenerator with MIT License | 4 votes |
private void addImport(AST ast, ListRewrite importRewrite, String fullyQualifierImport) { ImportDeclaration importDeclaration = ast.newImportDeclaration(); importDeclaration.setName(ast.newName(fullyQualifierImport)); importRewrite.insertLast(importDeclaration, null); }