use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.
the class AdvancedQuickAssistProcessor method getConvertToIfReturnProposals.
private static boolean getConvertToIfReturnProposals(IInvocationContext context, ASTNode coveringNode, ArrayList<ICommandAccess> resultingCollections) {
if (!(coveringNode instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) coveringNode;
if (ifStatement.getElseStatement() != null) {
return false;
}
// enclosing lambda or method should return 'void'
LambdaExpression enclosingLambda = ASTResolving.findEnclosingLambdaExpression(ifStatement);
if (enclosingLambda != null) {
IMethodBinding lambdaMethodBinding = enclosingLambda.resolveMethodBinding();
if (lambdaMethodBinding == null) {
return false;
}
if (!(ifStatement.getAST().resolveWellKnownType("void").equals(lambdaMethodBinding.getReturnType()))) {
//$NON-NLS-1$
return false;
}
} else {
MethodDeclaration coveringMethod = ASTResolving.findParentMethodDeclaration(ifStatement);
if (coveringMethod == null) {
return false;
}
Type returnType = coveringMethod.getReturnType2();
if (!isVoid(returnType)) {
return false;
}
}
// should be present in a block
if (!(ifStatement.getParent() instanceof Block)) {
return false;
}
// should have at least one statement in 'then' part other than 'return'
Statement thenStatement = ifStatement.getThenStatement();
if (thenStatement instanceof ReturnStatement) {
return false;
}
if (thenStatement instanceof Block) {
List<Statement> thenStatements = ((Block) thenStatement).statements();
if (thenStatements.isEmpty() || (thenStatements.size() == 1 && (thenStatements.get(0) instanceof ReturnStatement))) {
return false;
}
}
// should have no further executable statement
if (!isLastStatementInEnclosingMethodOrLambda(ifStatement)) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
AST ast = coveringNode.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// create inverted 'if' statement
Expression inversedExpression = getInversedExpression(rewrite, ifStatement.getExpression());
IfStatement newIf = ast.newIfStatement();
newIf.setExpression(inversedExpression);
newIf.setThenStatement(ast.newReturnStatement());
ListRewrite listRewriter = rewrite.getListRewrite(ifStatement.getParent(), (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
listRewriter.replace(ifStatement, newIf, null);
// remove last 'return' in 'then' block
ArrayList<Statement> statements = getUnwrappedStatements(ifStatement.getThenStatement());
Statement lastStatement = statements.get(statements.size() - 1);
if (lastStatement instanceof ReturnStatement) {
statements.remove(lastStatement);
}
// add statements from 'then' to the end of block
for (Statement statement : statements) {
listRewriter.insertLast(rewrite.createMoveTarget(statement), null);
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CONVERT_TO_IF_RETURN, image);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.
the class LocalCorrectionsSubProcessor method getUnnecessaryElseProposals.
public static void getUnnecessaryElseProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit root = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(root);
if (selectedNode == null) {
return;
}
ASTNode parent = selectedNode.getParent();
if (parent instanceof ExpressionStatement) {
parent = parent.getParent();
}
if (!(parent instanceof IfStatement)) {
return;
}
IfStatement ifStatement = (IfStatement) parent;
ASTNode ifParent = ifStatement.getParent();
if (!(ifParent instanceof Block) && !(ifParent instanceof SwitchStatement) && !ASTNodes.isControlStatementBody(ifStatement.getLocationInParent())) {
return;
}
ASTRewrite rewrite = ASTRewrite.create(root.getAST());
ASTNode placeholder = QuickAssistProcessor.getCopyOfInner(rewrite, ifStatement.getElseStatement(), false);
if (placeholder == null) {
return;
}
rewrite.remove(ifStatement.getElseStatement(), null);
if (ifParent instanceof Block) {
ListRewrite listRewrite = rewrite.getListRewrite(ifParent, Block.STATEMENTS_PROPERTY);
listRewrite.insertAfter(placeholder, ifStatement, null);
} else if (ifParent instanceof SwitchStatement) {
ListRewrite listRewrite = rewrite.getListRewrite(ifParent, SwitchStatement.STATEMENTS_PROPERTY);
listRewrite.insertAfter(placeholder, ifStatement, null);
} else {
Block block = root.getAST().newBlock();
rewrite.replace(ifStatement, block, null);
block.statements().add(rewrite.createCopyTarget(ifStatement));
block.statements().add(placeholder);
}
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeelse_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_ELSE, image);
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.
the class LocalCorrectionsSubProcessor method getInterfaceExtendsClassProposals.
public static void getInterfaceExtendsClassProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit root = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(root);
if (selectedNode == null) {
return;
}
while (selectedNode.getParent() instanceof Type) {
selectedNode = selectedNode.getParent();
}
StructuralPropertyDescriptor locationInParent = selectedNode.getLocationInParent();
if (locationInParent != TypeDeclaration.SUPERCLASS_TYPE_PROPERTY) {
return;
}
TypeDeclaration typeDecl = (TypeDeclaration) selectedNode.getParent();
{
ASTRewrite rewrite = ASTRewrite.create(root.getAST());
ASTNode placeHolder = rewrite.createMoveTarget(selectedNode);
ListRewrite interfaces = rewrite.getListRewrite(typeDecl, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
interfaces.insertFirst(placeHolder, null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_extendstoimplements_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_EXTENDS_TO_IMPLEMENTS, image);
proposals.add(proposal);
}
{
ASTRewrite rewrite = ASTRewrite.create(root.getAST());
rewrite.set(typeDecl, TypeDeclaration.INTERFACE_PROPERTY, Boolean.TRUE, null);
String typeName = typeDecl.getName().getIdentifier();
String label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_classtointerface_description, BasicElementLabels.getJavaElementName(typeName));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_CLASS_TO_INTERFACE, image);
proposals.add(proposal);
}
}
use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.
the class LocalCorrectionsSubProcessor method getUnreachableCodeProposals.
public static void getUnreachableCodeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit root = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(root);
if (selectedNode == null) {
return;
}
ASTNode parent = selectedNode.getParent();
while (parent instanceof ExpressionStatement) {
selectedNode = parent;
parent = selectedNode.getParent();
}
if (parent instanceof WhileStatement) {
addRemoveIncludingConditionProposal(context, parent, null, proposals);
} else if (selectedNode.getLocationInParent() == IfStatement.THEN_STATEMENT_PROPERTY) {
Statement elseStatement = ((IfStatement) parent).getElseStatement();
addRemoveIncludingConditionProposal(context, parent, elseStatement, proposals);
} else if (selectedNode.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
Statement thenStatement = ((IfStatement) parent).getThenStatement();
addRemoveIncludingConditionProposal(context, parent, thenStatement, proposals);
} else if (selectedNode.getLocationInParent() == ForStatement.BODY_PROPERTY) {
Statement body = ((ForStatement) parent).getBody();
addRemoveIncludingConditionProposal(context, parent, body, proposals);
} else if (selectedNode.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY) {
Expression elseExpression = ((ConditionalExpression) parent).getElseExpression();
addRemoveIncludingConditionProposal(context, parent, elseExpression, proposals);
} else if (selectedNode.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
Expression thenExpression = ((ConditionalExpression) parent).getThenExpression();
addRemoveIncludingConditionProposal(context, parent, thenExpression, proposals);
} else if (selectedNode.getLocationInParent() == InfixExpression.RIGHT_OPERAND_PROPERTY) {
// also offer split && / || condition proposals:
InfixExpression infixExpression = (InfixExpression) parent;
Expression leftOperand = infixExpression.getLeftOperand();
List<Expression> extendedOperands = infixExpression.extendedOperands();
ASTRewrite rewrite = ASTRewrite.create(parent.getAST());
if (extendedOperands.size() == 0) {
rewrite.replace(infixExpression, rewrite.createMoveTarget(leftOperand), null);
} else {
ASTNode firstExtendedOp = rewrite.createMoveTarget(extendedOperands.get(0));
rewrite.set(infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY, firstExtendedOp, null);
rewrite.remove(leftOperand, null);
}
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
addRemoveProposal(context, rewrite, label, proposals);
AssistContext assistContext = new AssistContext(context.getCompilationUnit(), infixExpression.getRightOperand().getStartPosition() - 1, 0);
assistContext.setASTRoot(root);
AdvancedQuickAssistProcessor.getSplitAndConditionProposals(assistContext, infixExpression, proposals);
AdvancedQuickAssistProcessor.getSplitOrConditionProposals(assistContext, infixExpression, proposals);
} else if (selectedNode instanceof Statement && selectedNode.getLocationInParent().isChildListProperty()) {
// remove all statements following the unreachable:
List<Statement> statements = ASTNodes.<Statement>getChildListProperty(selectedNode.getParent(), (ChildListPropertyDescriptor) selectedNode.getLocationInParent());
int idx = statements.indexOf(selectedNode);
ASTRewrite rewrite = ASTRewrite.create(selectedNode.getAST());
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
if (idx > 0) {
Object prevStatement = statements.get(idx - 1);
if (prevStatement instanceof IfStatement) {
IfStatement ifStatement = (IfStatement) prevStatement;
if (ifStatement.getElseStatement() == null) {
// remove if (true), see https://bugs.eclipse.org/bugs/show_bug.cgi?id=261519
rewrite.replace(ifStatement, rewrite.createMoveTarget(ifStatement.getThenStatement()), null);
label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
}
}
}
for (int i = idx; i < statements.size(); i++) {
ASTNode statement = statements.get(i);
if (statement instanceof SwitchCase)
// stop at case *: and default:
break;
rewrite.remove(statement, null);
}
addRemoveProposal(context, rewrite, label, proposals);
} else {
// no special case, just remove the node:
addRemoveProposal(context, selectedNode, proposals);
}
}
use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.
the class QuickAssistProcessor method getChangeLambdaBodyToExpressionProposal.
private static boolean getChangeLambdaBodyToExpressionProposal(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
LambdaExpression lambda;
if (covering instanceof LambdaExpression) {
lambda = (LambdaExpression) covering;
} else if (covering.getLocationInParent() == LambdaExpression.BODY_PROPERTY) {
lambda = (LambdaExpression) covering.getParent();
} else {
return false;
}
if (!(lambda.getBody() instanceof Block))
return false;
Block lambdaBody = (Block) lambda.getBody();
if (lambdaBody.statements().size() != 1)
return false;
Expression exprBody;
Statement singleStatement = (Statement) lambdaBody.statements().get(0);
if (singleStatement instanceof ReturnStatement) {
Expression returnExpr = ((ReturnStatement) singleStatement).getExpression();
if (returnExpr == null)
return false;
exprBody = returnExpr;
} else if (singleStatement instanceof ExpressionStatement) {
Expression expression = ((ExpressionStatement) singleStatement).getExpression();
if (isValidExpressionBody(expression)) {
exprBody = expression;
} else {
return false;
}
} else {
return false;
}
if (resultingCollections == null)
return true;
AST ast = lambda.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
Expression movedBody = (Expression) rewrite.createMoveTarget(exprBody);
rewrite.set(lambda, LambdaExpression.BODY_PROPERTY, movedBody, null);
// add proposal
String label = CorrectionMessages.QuickAssistProcessor_change_lambda_body_to_expression;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_LAMBDA_BODY_TO_EXPRESSION, image);
resultingCollections.add(proposal);
return true;
}
Aggregations