Java Code Examples for org.eclipse.jdt.core.dom.rewrite.ASTRewrite#createMoveTarget()
The following examples show how to use
org.eclipse.jdt.core.dom.rewrite.ASTRewrite#createMoveTarget() .
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: TypeArgumentMismatchSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public static void removeMismatchedArguments(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals){ ICompilationUnit cu= context.getCompilationUnit(); ASTNode selectedNode= problem.getCoveredNode(context.getASTRoot()); if (!(selectedNode instanceof SimpleName)) { return; } ASTNode normalizedNode=ASTNodes.getNormalizedNode(selectedNode); if (normalizedNode instanceof ParameterizedType) { ASTRewrite rewrite = ASTRewrite.create(normalizedNode.getAST()); ParameterizedType pt = (ParameterizedType) normalizedNode; ASTNode mt = rewrite.createMoveTarget(pt.getType()); rewrite.replace(pt, mt, null); String label= CorrectionMessages.TypeArgumentMismatchSubProcessor_removeTypeArguments; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_TYPE_ARGUMENTS, image); proposals.add(proposal); } }
Example 2
Source File: InvertBooleanUtility.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) { if (existing == null && removeParentheses) { while (originalNode instanceof ParenthesizedExpression) { originalNode = ((ParenthesizedExpression) originalNode).getExpression(); } } Expression newRight = (Expression) rewrite.createMoveTarget(originalNode); if (originalNode instanceof InfixExpression) { ((InfixExpression) newRight).setOperator(((InfixExpression) originalNode).getOperator()); } if (existing == null) { return newRight; } AST ast = rewrite.getAST(); InfixExpression infix = ast.newInfixExpression(); infix.setOperator(operator); infix.setLeftOperand(existing); infix.setRightOperand(newRight); return infix; }
Example 3
Source File: UnusedCodeFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static void replaceCast(CastExpression castExpression, Expression replacement, ASTRewrite rewrite, TextEditGroup group) { boolean castEnclosedInNecessaryParentheses= castExpression.getParent() instanceof ParenthesizedExpression && NecessaryParenthesesChecker.needsParentheses(castExpression, castExpression.getParent().getParent(), castExpression.getParent().getLocationInParent()); ASTNode toReplace= castEnclosedInNecessaryParentheses ? castExpression.getParent() : castExpression; ASTNode move; if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) { if (replacement.getParent() instanceof ParenthesizedExpression) { move= rewrite.createMoveTarget(replacement.getParent()); } else if (castEnclosedInNecessaryParentheses) { toReplace= castExpression; move= rewrite.createMoveTarget(replacement); } else { ParenthesizedExpression parentheses= replacement.getAST().newParenthesizedExpression(); parentheses.setExpression((Expression) rewrite.createMoveTarget(replacement)); move= parentheses; } } else { move= rewrite.createMoveTarget(replacement); } rewrite.replace(toReplace, move, group); }
Example 4
Source File: UnusedCodeFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private void removeVariableWithInitializer(ASTRewrite rewrite, ASTNode initializerNode, ASTNode statementNode, TextEditGroup group) { boolean performRemove= fForceRemove; if (!performRemove) { ArrayList<Expression> sideEffectNodes= new ArrayList<Expression>(); initializerNode.accept(new SideEffectFinder(sideEffectNodes)); performRemove= sideEffectNodes.isEmpty(); } if (performRemove) { removeStatement(rewrite, statementNode, group); fRemovedAssignmentsCount++; } else { ASTNode initNode = rewrite.createMoveTarget(initializerNode); ExpressionStatement statement = rewrite.getAST().newExpressionStatement((Expression) initNode); rewrite.replace(statementNode, statement, null); fAlteredAssignmentsCount++; } }
Example 5
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) { if (existing == null && removeParentheses) { while (originalNode instanceof ParenthesizedExpression) { originalNode= ((ParenthesizedExpression)originalNode).getExpression(); } } Expression newRight= (Expression)rewrite.createMoveTarget(originalNode); if (originalNode instanceof InfixExpression) { ((InfixExpression)newRight).setOperator(((InfixExpression)originalNode).getOperator()); } if (existing == null) { return newRight; } AST ast= rewrite.getAST(); InfixExpression infix= ast.newInfixExpression(); infix.setOperator(operator); infix.setLeftOperand(existing); infix.setRightOperand(newRight); return infix; }
Example 6
Source File: ExtractTempRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private void replaceSelectedExpressionWithTempDeclaration() throws CoreException { ASTRewrite rewrite= fCURewrite.getASTRewrite(); Expression selectedExpression= getSelectedExpression().getAssociatedExpression(); // whole expression selected Expression initializer= (Expression) rewrite.createMoveTarget(selectedExpression); ASTNode replacement= createTempDeclaration(initializer); // creates a VariableDeclarationStatement ExpressionStatement parent= (ExpressionStatement) selectedExpression.getParent(); if (ASTNodes.isControlStatementBody(parent.getLocationInParent())) { Block block= rewrite.getAST().newBlock(); block.statements().add(replacement); replacement= block; } rewrite.replace(parent, replacement, fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable)); }
Example 7
Source File: ControlStatementsFix.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 { ASTRewrite rewrite= cuRewrite.getASTRewrite(); String label; if (fBodyProperty == IfStatement.THEN_STATEMENT_PROPERTY) { label = FixMessages.CodeStyleFix_ChangeIfToBlock_desription; } else if (fBodyProperty == IfStatement.ELSE_STATEMENT_PROPERTY) { label = FixMessages.CodeStyleFix_ChangeElseToBlock_description; } else { label = FixMessages.CodeStyleFix_ChangeControlToBlock_description; } TextEditGroup group= createTextEditGroup(label, cuRewrite); ASTNode moveTarget= rewrite.createMoveTarget(fBody); Block replacingBody= cuRewrite.getRoot().getAST().newBlock(); replacingBody.statements().add(moveTarget); rewrite.set(fControlStatement, fBodyProperty, replacingBody, group); }
Example 8
Source File: CodeStyleFix.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 { ASTRewrite rewrite= cuRewrite.getASTRewrite(); CompilationUnit compilationUnit= cuRewrite.getRoot(); importType(fDeclaringClass, fName, cuRewrite.getImportRewrite(), compilationUnit); TextEditGroup group; if (fName.resolveBinding() instanceof IMethodBinding) { group= createTextEditGroup(FixMessages.CodeStyleFix_QualifyMethodWithDeclClass_description, cuRewrite); } else { group= createTextEditGroup(FixMessages.CodeStyleFix_QualifyFieldWithDeclClass_description, cuRewrite); } IJavaElement javaElement= fDeclaringClass.getJavaElement(); if (javaElement instanceof IType) { Name qualifierName= compilationUnit.getAST().newName(((IType)javaElement).getElementName()); SimpleName simpleName= (SimpleName)rewrite.createMoveTarget(fName); QualifiedName qualifiedName= compilationUnit.getAST().newQualifiedName(qualifierName, simpleName); rewrite.replace(fName, qualifiedName, group); } }
Example 9
Source File: ConvertIterableLoopOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Returns the expression for the enhanced for statement. * * @param rewrite * the AST rewrite to use * @return the expression node, or <code>null</code> */ private Expression getExpression(final ASTRewrite rewrite) { if (fThis) return rewrite.getAST().newThisExpression(); if (fExpression instanceof MethodInvocation) return (MethodInvocation)rewrite.createMoveTarget(fExpression); return (Expression)ASTNode.copySubtree(rewrite.getAST(), fExpression); }
Example 10
Source File: UnusedCodeFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void splitUpDeclarations(ASTRewrite rewrite, TextEditGroup group, VariableDeclarationFragment frag, VariableDeclarationStatement originalStatement, List<Expression> sideEffects) { if (sideEffects.size() > 0) { ListRewrite statementRewrite= rewrite.getListRewrite(originalStatement.getParent(), (ChildListPropertyDescriptor) originalStatement.getLocationInParent()); Statement previousStatement= originalStatement; for (int i= 0; i < sideEffects.size(); i++) { Expression sideEffect= sideEffects.get(i); Expression movedInit= (Expression) rewrite.createMoveTarget(sideEffect); ExpressionStatement wrapped= rewrite.getAST().newExpressionStatement(movedInit); statementRewrite.insertAfter(wrapped, previousStatement, group); previousStatement= wrapped; } VariableDeclarationStatement newDeclaration= null; List<VariableDeclarationFragment> fragments= originalStatement.fragments(); int fragIndex= fragments.indexOf(frag); ListIterator<VariableDeclarationFragment> fragmentIterator= fragments.listIterator(fragIndex+1); while (fragmentIterator.hasNext()) { VariableDeclarationFragment currentFragment= fragmentIterator.next(); VariableDeclarationFragment movedFragment= (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment); if (newDeclaration == null) { newDeclaration= rewrite.getAST().newVariableDeclarationStatement(movedFragment); Type copiedType= (Type) rewrite.createCopyTarget(originalStatement.getType()); newDeclaration.setType(copiedType); } else { newDeclaration.fragments().add(movedFragment); } } if (newDeclaration != null){ statementRewrite.insertAfter(newDeclaration, previousStatement, group); if (originalStatement.fragments().size() == newDeclaration.fragments().size() + 1){ rewrite.remove(originalStatement, group); } } } }
Example 11
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean getInverseIfProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) { if (!(covering instanceof IfStatement)) { return false; } IfStatement ifStatement= (IfStatement) covering; if (ifStatement.getElseStatement() == null) { return false; } // we could produce quick assist if (resultingCollections == null) { return true; } // AST ast= covering.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); Statement thenStatement= ifStatement.getThenStatement(); Statement elseStatement= ifStatement.getElseStatement(); // prepare original nodes Expression inversedExpression= getInversedExpression(rewrite, ifStatement.getExpression()); Statement newElseStatement= (Statement) rewrite.createMoveTarget(thenStatement); Statement newThenStatement= (Statement) rewrite.createMoveTarget(elseStatement); // set new nodes rewrite.set(ifStatement, IfStatement.EXPRESSION_PROPERTY, inversedExpression, null); if (elseStatement instanceof IfStatement) {// bug 79507 && bug 74580 Block elseBlock= ast.newBlock(); elseBlock.statements().add(newThenStatement); newThenStatement= elseBlock; } rewrite.set(ifStatement, IfStatement.THEN_STATEMENT_PROPERTY, newThenStatement, null); rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, newElseStatement, null); // add correction proposal String label= CorrectionMessages.AdvancedQuickAssistProcessor_inverseIf_description; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_IF_STATEMENT, image); resultingCollections.add(proposal); return true; }
Example 12
Source File: ControlStatementsFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { ASTRewrite rewrite= cuRewrite.getASTRewrite(); Block block= (Block)fStatement.getStructuralProperty(fChild); Statement statement= (Statement)block.statements().get(0); Statement moveTarget= (Statement)rewrite.createMoveTarget(statement); TextEditGroup group= createTextEditGroup(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, cuRewrite); rewrite.set(fStatement, fChild, moveTarget, group); }
Example 13
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static Statement copyStatementExceptBreak(AST ast, ASTRewrite rewrite, Statement source) { if (source instanceof Block) { Block block= (Block) source; Block newBlock= ast.newBlock(); for (Iterator<Statement> iter= block.statements().iterator(); iter.hasNext();) { Statement statement= iter.next(); if (statement instanceof BreakStatement) { continue; } newBlock.statements().add(copyStatementExceptBreak(ast, rewrite, statement)); } return newBlock; } return (Statement) rewrite.createMoveTarget(source); }
Example 14
Source File: MoveInstanceMethodProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private ASTNode getFieldReference(SimpleName oldNameNode, ASTRewrite rewrite) { String name= oldNameNode.getIdentifier(); AST ast= rewrite.getAST(); if (isParameterName(name) || StubUtility.useThisForFieldAccess(fTargetRewrite.getCu().getJavaProject())) { FieldAccess fieldAccess= ast.newFieldAccess(); fieldAccess.setExpression(ast.newThisExpression()); fieldAccess.setName((SimpleName) rewrite.createMoveTarget(oldNameNode)); return fieldAccess; } return rewrite.createMoveTarget(oldNameNode); }
Example 15
Source File: ParameterObjectFactory.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
protected static ASTNode moveNode(CompilationUnitRewrite cuRewrite, ASTNode node) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); if (rewrite.getAST() != node.getAST()) { String str= ASTNodes.getNodeSource(node, true, true); if (str != null) { return rewrite.createStringPlaceholder(str, node.getNodeType()); } return ASTNode.copySubtree(rewrite.getAST(), node); } return rewrite.createMoveTarget(node); }
Example 16
Source File: UseEqualsResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
private Expression createLeftOperand(ASTRewrite rewrite, Expression leftOperand) { Expression leftExp = (Expression) rewrite.createMoveTarget(leftOperand); if (leftOperand instanceof Name || leftOperand instanceof ParenthesizedExpression) { return leftExp; } final AST ast = rewrite.getAST(); ParenthesizedExpression parExp = ast.newParenthesizedExpression(); parExp.setExpression(leftExp); return parExp; }
Example 17
Source File: ExtractTempRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private void replaceSelectedExpressionWithTempDeclaration() throws CoreException { ASTRewrite rewrite = fCURewrite.getASTRewrite(); Expression selectedExpression = getSelectedExpression().getAssociatedExpression(); // whole expression selected Expression initializer = (Expression) rewrite.createMoveTarget(selectedExpression); VariableDeclarationStatement tempDeclaration = createTempDeclaration(initializer); ASTNode replacement; ASTNode parent = selectedExpression.getParent(); boolean isParentLambda = parent instanceof LambdaExpression; AST ast = rewrite.getAST(); if (isParentLambda) { Block blockBody = ast.newBlock(); blockBody.statements().add(tempDeclaration); if (!Bindings.isVoidType(((LambdaExpression) parent).resolveMethodBinding().getReturnType())) { List<VariableDeclarationFragment> fragments = tempDeclaration.fragments(); SimpleName varName = fragments.get(0).getName(); ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ast.newSimpleName(varName.getIdentifier())); blockBody.statements().add(returnStatement); } replacement = blockBody; } else if (ASTNodes.isControlStatementBody(parent.getLocationInParent())) { Block block = ast.newBlock(); block.statements().add(tempDeclaration); replacement = block; } else { replacement = tempDeclaration; } ASTNode replacee = isParentLambda || !ASTNodes.hasSemicolon((ExpressionStatement) parent, fCu) ? selectedExpression : parent; rewrite.replace(replacee, replacement, fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable)); }
Example 18
Source File: LocalCorrectionsSubProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static void addRemoveIncludingConditionProposal(IInvocationContext context, ASTNode toRemove, ASTNode replacement, Collection<ChangeCorrectionProposal> proposals) { String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description; AST ast = toRemove.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_UNREACHABLE_CODE_INCLUDING_CONDITION); if (replacement == null || replacement instanceof EmptyStatement || replacement instanceof Block && ((Block) replacement).statements().size() == 0) { if (ASTNodes.isControlStatementBody(toRemove.getLocationInParent())) { rewrite.replace(toRemove, toRemove.getAST().newBlock(), null); } else { rewrite.remove(toRemove, null); } } else if (toRemove instanceof Expression && replacement instanceof Expression) { Expression moved = (Expression) rewrite.createMoveTarget(replacement); Expression toRemoveExpression = (Expression) toRemove; Expression replacementExpression = (Expression) replacement; ITypeBinding explicitCast = ASTNodes.getExplicitCast(replacementExpression, toRemoveExpression); if (explicitCast != null) { CastExpression cast = ast.newCastExpression(); if (NecessaryParenthesesChecker.needsParentheses(replacementExpression, cast, CastExpression.EXPRESSION_PROPERTY)) { ParenthesizedExpression parenthesized = ast.newParenthesizedExpression(); parenthesized.setExpression(moved); moved = parenthesized; } cast.setExpression(moved); ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot()); ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(toRemove, imports); cast.setType(imports.addImport(explicitCast, ast, importRewriteContext, TypeLocation.CAST)); moved = cast; } rewrite.replace(toRemove, moved, null); } else { ASTNode parent = toRemove.getParent(); ASTNode moveTarget; if ((parent instanceof Block || parent instanceof SwitchStatement) && replacement instanceof Block) { ListRewrite listRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY); List<Statement> list = ((Block) replacement).statements(); int lastIndex = list.size() - 1; moveTarget = listRewrite.createMoveTarget(list.get(0), list.get(lastIndex)); } else { moveTarget = rewrite.createMoveTarget(replacement); } rewrite.replace(toRemove, moveTarget, null); } proposals.add(proposal); }
Example 19
Source File: LocalCorrectionsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static void addRemoveIncludingConditionProposal(IInvocationContext context, ASTNode toRemove, ASTNode replacement, Collection<ICommandAccess> proposals) { Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE); String label= CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description; AST ast= toRemove.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_UNREACHABLE_CODE_INCLUDING_CONDITION, image); if (replacement == null || replacement instanceof EmptyStatement || replacement instanceof Block && ((Block)replacement).statements().size() == 0) { if (ASTNodes.isControlStatementBody(toRemove.getLocationInParent())) { rewrite.replace(toRemove, toRemove.getAST().newBlock(), null); } else { rewrite.remove(toRemove, null); } } else if (toRemove instanceof Expression && replacement instanceof Expression) { Expression moved= (Expression) rewrite.createMoveTarget(replacement); Expression toRemoveExpression= (Expression) toRemove; Expression replacementExpression= (Expression) replacement; ITypeBinding explicitCast= ASTNodes.getExplicitCast(replacementExpression, toRemoveExpression); if (explicitCast != null) { CastExpression cast= ast.newCastExpression(); if (NecessaryParenthesesChecker.needsParentheses(replacementExpression, cast, CastExpression.EXPRESSION_PROPERTY)) { ParenthesizedExpression parenthesized= ast.newParenthesizedExpression(); parenthesized.setExpression(moved); moved= parenthesized; } cast.setExpression(moved); ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot()); ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(toRemove, imports); cast.setType(imports.addImport(explicitCast, ast, importRewriteContext)); moved= cast; } rewrite.replace(toRemove, moved, null); } else { ASTNode parent= toRemove.getParent(); ASTNode moveTarget; if ((parent instanceof Block || parent instanceof SwitchStatement) && replacement instanceof Block) { ListRewrite listRewrite= rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY); List<Statement> list= ((Block)replacement).statements(); int lastIndex= list.size() - 1; moveTarget= listRewrite.createMoveTarget(list.get(0), list.get(lastIndex)); } else { moveTarget= rewrite.createMoveTarget(replacement); } rewrite.replace(toRemove, moveTarget, null); } proposals.add(proposal); }
Example 20
Source File: ASTNodes.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 2 votes |
/** * Type-safe variant of {@link ASTRewrite#createMoveTarget(ASTNode)}. * * @param rewrite ASTRewrite for the given node * @param node the node to create a move placeholder for * @return the new placeholder node * @throws IllegalArgumentException if the node is null, or if the node * is not part of the rewrite's AST */ @SuppressWarnings("unchecked") public static <T extends ASTNode> T createMoveTarget(ASTRewrite rewrite, T node) { return (T) rewrite.createMoveTarget(node); }