use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class QuickAssistProcessor method getUnWrapProposals.
private static boolean getUnWrapProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
ASTNode outer = node;
Block block = null;
if (outer.getNodeType() == ASTNode.BLOCK) {
block = (Block) outer;
outer = block.getParent();
}
ASTNode body = null;
String label = null;
if (outer instanceof IfStatement) {
IfStatement ifStatement = (IfStatement) outer;
Statement elseBlock = ifStatement.getElseStatement();
if (elseBlock == null || elseBlock instanceof Block && ((Block) elseBlock).statements().isEmpty()) {
body = ifStatement.getThenStatement();
}
label = CorrectionMessages.QuickAssistProcessor_unwrap_ifstatement;
} else if (outer instanceof WhileStatement) {
body = ((WhileStatement) outer).getBody();
label = CorrectionMessages.QuickAssistProcessor_unwrap_whilestatement;
} else if (outer instanceof ForStatement) {
body = ((ForStatement) outer).getBody();
label = CorrectionMessages.QuickAssistProcessor_unwrap_forstatement;
} else if (outer instanceof EnhancedForStatement) {
body = ((EnhancedForStatement) outer).getBody();
label = CorrectionMessages.QuickAssistProcessor_unwrap_forstatement;
} else if (outer instanceof SynchronizedStatement) {
body = ((SynchronizedStatement) outer).getBody();
label = CorrectionMessages.QuickAssistProcessor_unwrap_synchronizedstatement;
} else if (outer instanceof SimpleName && outer.getParent() instanceof LabeledStatement) {
LabeledStatement labeledStatement = (LabeledStatement) outer.getParent();
outer = labeledStatement;
body = labeledStatement.getBody();
label = CorrectionMessages.QuickAssistProcessor_unwrap_labeledstatement;
} else if (outer instanceof LabeledStatement) {
body = ((LabeledStatement) outer).getBody();
label = CorrectionMessages.QuickAssistProcessor_unwrap_labeledstatement;
} else if (outer instanceof DoStatement) {
body = ((DoStatement) outer).getBody();
label = CorrectionMessages.QuickAssistProcessor_unwrap_dostatement;
} else if (outer instanceof TryStatement) {
TryStatement tryStatement = (TryStatement) outer;
if (tryStatement.catchClauses().isEmpty() && tryStatement.resources().isEmpty()) {
body = tryStatement.getBody();
}
label = CorrectionMessages.QuickAssistProcessor_unwrap_trystatement;
} else if (outer instanceof AnonymousClassDeclaration) {
List<BodyDeclaration> decls = ((AnonymousClassDeclaration) outer).bodyDeclarations();
for (int i = 0; i < decls.size(); i++) {
BodyDeclaration elem = decls.get(i);
if (elem instanceof MethodDeclaration) {
Block curr = ((MethodDeclaration) elem).getBody();
if (curr != null && !curr.statements().isEmpty()) {
if (body != null) {
return false;
}
body = curr;
}
} else if (elem instanceof TypeDeclaration) {
return false;
}
}
label = CorrectionMessages.QuickAssistProcessor_unwrap_anonymous;
outer = ASTResolving.findParentStatement(outer);
if (outer == null) {
// private Object o= new Object() { ... };
return false;
}
} else if (outer instanceof Block) {
// -> a block in a block
body = block;
outer = block;
label = CorrectionMessages.QuickAssistProcessor_unwrap_block;
} else if (outer instanceof ParenthesizedExpression) {
//ParenthesizedExpression expression= (ParenthesizedExpression) outer;
//body= expression.getExpression();
//label= CorrectionMessages.getString("QuickAssistProcessor.unwrap.parenthesis"); //$NON-NLS-1$
} else if (outer instanceof MethodInvocation) {
MethodInvocation invocation = (MethodInvocation) outer;
if (invocation.arguments().size() == 1) {
body = (ASTNode) invocation.arguments().get(0);
if (invocation.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
int kind = body.getNodeType();
if (kind != ASTNode.ASSIGNMENT && kind != ASTNode.PREFIX_EXPRESSION && kind != ASTNode.POSTFIX_EXPRESSION && kind != ASTNode.METHOD_INVOCATION && kind != ASTNode.SUPER_METHOD_INVOCATION) {
body = null;
}
}
label = CorrectionMessages.QuickAssistProcessor_unwrap_methodinvocation;
}
}
if (body == null) {
return false;
}
ASTRewrite rewrite = ASTRewrite.create(outer.getAST());
ASTNode inner = getCopyOfInner(rewrite, body, ASTNodes.isControlStatementBody(outer.getLocationInParent()));
if (inner == null) {
return false;
}
if (resultingCollections == null) {
return true;
}
rewrite.replace(outer, inner, null);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.UNWRAP_STATEMENTS, image);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class AdvancedQuickAssistProcessor method getSplitOrConditionProposals.
public static boolean getSplitOrConditionProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
Operator orOperator = InfixExpression.Operator.CONDITIONAL_OR;
// check that user invokes quick assist on infix expression
if (!(node instanceof InfixExpression)) {
return false;
}
InfixExpression infixExpression = (InfixExpression) node;
if (infixExpression.getOperator() != orOperator) {
return false;
}
int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
if (offset == -1) {
return false;
}
// check that infix expression belongs to IfStatement
Statement statement = ASTResolving.findParentStatement(node);
if (!(statement instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) statement;
// check that infix expression is part of first level || condition of IfStatement
InfixExpression topInfixExpression = infixExpression;
while (topInfixExpression.getParent() instanceof InfixExpression && ((InfixExpression) topInfixExpression.getParent()).getOperator() == orOperator) {
topInfixExpression = (InfixExpression) topInfixExpression.getParent();
}
if (ifStatement.getExpression() != topInfixExpression) {
return false;
}
//
if (resultingCollections == null) {
return true;
}
AST ast = ifStatement.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare left and right conditions
Expression[] newOperands = { null, null };
breakInfixOperationAtOperation(rewrite, topInfixExpression, orOperator, offset, true, newOperands);
Expression leftCondition = newOperands[0];
Expression rightCondition = newOperands[1];
// prepare first statement
rewrite.replace(ifStatement.getExpression(), leftCondition, null);
IfStatement secondIf = ast.newIfStatement();
secondIf.setExpression(rightCondition);
secondIf.setThenStatement((Statement) rewrite.createCopyTarget(ifStatement.getThenStatement()));
Statement elseStatement = ifStatement.getElseStatement();
if (elseStatement == null) {
rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, secondIf, null);
} else {
rewrite.replace(elseStatement, secondIf, null);
secondIf.setElseStatement((Statement) rewrite.createMoveTarget(elseStatement));
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_splitOrCondition_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_OR_CONDITION, image);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal 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.ui.text.java.correction.ASTRewriteCorrectionProposal 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.ui.text.java.correction.ASTRewriteCorrectionProposal 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);
}
}
Aggregations