Java Code Examples for org.eclipse.jdt.internal.corext.dom.ASTNodes#isControlStatementBody()
The following examples show how to use
org.eclipse.jdt.internal.corext.dom.ASTNodes#isControlStatementBody() .
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: StatementRewrite.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Override protected void handleOneMany(ASTNode[] replacements, TextEditGroup description) { AST ast= fToReplace[0].getAST(); // to replace == 1, but more than one replacement. Have to check if we // need to insert a block to not change structure if (ASTNodes.isControlStatementBody(fDescriptor)) { Block block= ast.newBlock(); ListRewrite statements= fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY); for (int i= 0; i < replacements.length; i++) { statements.insertLast(replacements[i], description); } fRewrite.replace(fToReplace[0], block, description); } else { ListRewrite container= fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor)fDescriptor); container.replace(fToReplace[0], replacements[0], description); for (int i= 1; i < replacements.length; i++) { container.insertAfter(replacements[i], replacements[i - 1], description); } } }
Example 2
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 3
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 4
Source File: GetterSetterUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean isNotInBlock(ASTNode parent) { ASTNode statement= parent.getParent(); boolean isStatement= statement.getNodeType() != ASTNode.EXPRESSION_STATEMENT; ASTNode block= statement.getParent(); boolean isBlock= block.getNodeType() == ASTNode.BLOCK || block.getNodeType() == ASTNode.SWITCH_STATEMENT; boolean isControlStatemenBody= ASTNodes.isControlStatementBody(statement.getLocationInParent()); return isStatement || !(isBlock || isControlStatemenBody); }
Example 5
Source File: UnusedCodeFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void removeStatement(ASTRewrite rewrite, ASTNode statementNode, TextEditGroup group) { if (ASTNodes.isControlStatementBody(statementNode.getLocationInParent())) { rewrite.replace(statementNode, rewrite.getAST().newBlock(), group); } else { rewrite.remove(statementNode, group); } }
Example 6
Source File: RemoveDeclarationCorrectionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void removeVariableWithInitializer(ASTRewrite rewrite, ASTNode initializerNode, ASTNode statementNode) { ArrayList<Expression> sideEffectNodes= new ArrayList<Expression>(); initializerNode.accept(new SideEffectFinder(sideEffectNodes)); int nSideEffects= sideEffectNodes.size(); if (nSideEffects == 0) { if (ASTNodes.isControlStatementBody(statementNode.getLocationInParent())) { rewrite.replace(statementNode, rewrite.getAST().newBlock(), null); } else { rewrite.remove(statementNode, null); } } else { // do nothing yet } }
Example 7
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 8
Source File: AssignToVariableAssistProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private ASTRewrite doAddLocal() { ASTNode nodeToAssign= fNodesToAssign.get(0); Expression expression= ((ExpressionStatement) nodeToAssign).getExpression(); AST ast= nodeToAssign.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); createImportRewrite((CompilationUnit) nodeToAssign.getRoot()); String[] varNames= suggestLocalVariableNames(fTypeBinding, expression); for (int i= 0; i < varNames.length; i++) { addLinkedPositionProposal(KEY_NAME, varNames[i]); } VariableDeclarationFragment newDeclFrag= ast.newVariableDeclarationFragment(); newDeclFrag.setName(ast.newSimpleName(varNames[0])); newDeclFrag.setInitializer((Expression) rewrite.createCopyTarget(expression)); Type type= evaluateType(ast, nodeToAssign, fTypeBinding, KEY_TYPE, TypeLocation.LOCAL_VARIABLE); if (ASTNodes.isControlStatementBody(nodeToAssign.getLocationInParent())) { Block block= ast.newBlock(); block.statements().add(rewrite.createMoveTarget(nodeToAssign)); rewrite.replace(nodeToAssign, block, null); } if (needsSemicolon(expression)) { VariableDeclarationStatement varStatement= ast.newVariableDeclarationStatement(newDeclFrag); varStatement.setType(type); rewrite.replace(expression, varStatement, null); } else { // trick for bug 43248: use an VariableDeclarationExpression and keep the ExpressionStatement VariableDeclarationExpression varExpression= ast.newVariableDeclarationExpression(newDeclFrag); varExpression.setType(type); rewrite.replace(expression, varExpression, null); } addLinkedPosition(rewrite.track(newDeclFrag.getName()), true, KEY_NAME); addLinkedPosition(rewrite.track(type), false, KEY_TYPE); setEndPosition(rewrite.track(nodeToAssign)); // set cursor after expression statement return rewrite; }
Example 9
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 10
Source File: AssignToVariableAssistProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private ASTRewrite doAddLocal() { Expression expression= ((ExpressionStatement) fNodeToAssign).getExpression(); AST ast= fNodeToAssign.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); createImportRewrite((CompilationUnit) fNodeToAssign.getRoot()); String[] varNames= suggestLocalVariableNames(fTypeBinding, expression); for (int i= 0; i < varNames.length; i++) { addLinkedPositionProposal(KEY_NAME, varNames[i], null); } VariableDeclarationFragment newDeclFrag= ast.newVariableDeclarationFragment(); newDeclFrag.setName(ast.newSimpleName(varNames[0])); newDeclFrag.setInitializer((Expression) rewrite.createCopyTarget(expression)); Type type= evaluateType(ast); if (ASTNodes.isControlStatementBody(fNodeToAssign.getLocationInParent())) { Block block= ast.newBlock(); block.statements().add(rewrite.createMoveTarget(fNodeToAssign)); rewrite.replace(fNodeToAssign, block, null); } if (needsSemicolon(expression)) { VariableDeclarationStatement varStatement= ast.newVariableDeclarationStatement(newDeclFrag); varStatement.setType(type); rewrite.replace(expression, varStatement, null); } else { // trick for bug 43248: use an VariableDeclarationExpression and keep the ExpressionStatement VariableDeclarationExpression varExpression= ast.newVariableDeclarationExpression(newDeclFrag); varExpression.setType(type); rewrite.replace(expression, varExpression, null); } addLinkedPosition(rewrite.track(newDeclFrag.getName()), true, KEY_NAME); addLinkedPosition(rewrite.track(type), false, KEY_TYPE); setEndPosition(rewrite.track(fNodeToAssign)); // set cursor after expression statement return rewrite; }