use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class BooleanRefactoring method noThenReturnStmt.
private boolean noThenReturnStmt(final IfStatement node) {
final Assignment thenA = asExpression(node.getThenStatement(), Assignment.class);
if (hasOperator(thenA, ASSIGN) && asList(node.getElseStatement()).isEmpty() && (thenA.getLeftHandSide() instanceof Name || thenA.getLeftHandSide() instanceof FieldAccess)) {
final Statement previousSibling = getPreviousSibling(node);
if (previousSibling instanceof VariableDeclarationStatement) {
final VariableDeclarationStatement vds = (VariableDeclarationStatement) previousSibling;
VariableDeclarationFragment vdf = getVariableDeclarationFragment(vds, thenA.getLeftHandSide());
if (vdf != null) {
final VariableDefinitionsUsesVisitor variableUseVisitor = new VariableDefinitionsUsesVisitor(vdf.resolveBinding(), node.getExpression()).find();
if (variableUseVisitor.getUses().isEmpty()) {
final ITypeBinding typeBinding = vds.getType().resolveBinding();
return maybeReplace(node, thenA, typeBinding, vdf.getInitializer());
}
}
} else if (previousSibling instanceof ExpressionStatement) {
final Assignment elseA = asExpression(previousSibling, Assignment.class);
if (hasOperator(elseA, ASSIGN) && isSameVariable(thenA.getLeftHandSide(), elseA.getLeftHandSide())) {
final ITypeBinding typeBinding = elseA.resolveTypeBinding();
return maybeReplace(node, thenA, typeBinding, elseA.getRightHandSide());
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class CommonCodeInIfElseStatementRefactoring method collectAllCases.
/**
* Collects all cases (if/else, if/else if/else, etc.) and returns whether all are covered.
*
* @param allCases the output collection for all the cases
* @param node the {@link IfStatement} to examine
* @return true if all cases (if/else, if/else if/else, etc.) are covered,
* false otherwise
*/
private boolean collectAllCases(List<List<Statement>> allCases, IfStatement node) {
final List<Statement> thenStmts = asList(node.getThenStatement());
final List<Statement> elseStmts = asList(node.getElseStatement());
if (thenStmts.isEmpty() || elseStmts.isEmpty()) {
// let other refactorings take care of removing empty blocks.
return false;
}
allCases.add(thenStmts);
if (elseStmts.size() == 1) {
final IfStatement is = as(elseStmts.get(0), IfStatement.class);
if (is != null) {
return collectAllCases(allCases, is);
}
}
allCases.add(elseStmts);
return true;
}
use of org.eclipse.jdt.core.dom.Statement in project eclipse-pmd by acanda.
the class DefaultLabelNotLastInSwitchStmtQuickFix method apply.
/**
* Moves the default case to the last position. The default case includes the default {@code SwitchCase} and all
* following statements up to the next {@code SwitchCase}.
*/
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final SwitchStatement node) {
final List<Statement> statements = node.statements();
final List<Statement> defaultCaseStatements = new ArrayList<>(statements.size());
boolean isDefaultCaseStatement = false;
for (final Statement statement : statements) {
if (statement instanceof SwitchCase) {
if (((SwitchCase) statement).getExpression() == DEFAULT_LABEL) {
isDefaultCaseStatement = true;
} else {
isDefaultCaseStatement = false;
}
}
if (isDefaultCaseStatement) {
defaultCaseStatements.add(statement);
}
}
statements.removeAll(defaultCaseStatements);
statements.addAll(defaultCaseStatements);
return true;
}
use of org.eclipse.jdt.core.dom.Statement in project flux 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);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.Statement in project flux by eclipse.
the class QuickAssistProcessor method getChangeLambdaBodyToBlockProposal.
private static boolean getChangeLambdaBodyToBlockProposal(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 Expression))
return false;
if (lambda.resolveMethodBinding() == null)
return false;
if (resultingCollections == null)
return true;
AST ast = lambda.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
Statement statementInBlockBody;
Expression bodyExpr = (Expression) rewrite.createMoveTarget(lambda.getBody());
if (ast.resolveWellKnownType("void").isEqualTo(lambda.resolveMethodBinding().getReturnType())) {
//$NON-NLS-1$
ExpressionStatement expressionStatement = ast.newExpressionStatement(bodyExpr);
statementInBlockBody = expressionStatement;
} else {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(bodyExpr);
statementInBlockBody = returnStatement;
}
Block blockBody = ast.newBlock();
blockBody.statements().add(statementInBlockBody);
rewrite.set(lambda, LambdaExpression.BODY_PROPERTY, blockBody, null);
// add proposal
String label = CorrectionMessages.QuickAssistProcessor_change_lambda_body_to_block;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_LAMBDA_BODY_TO_BLOCK);
resultingCollections.add(proposal);
return true;
}
Aggregations