use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.
the class ExtractMethodRefactoring method createMethodBody.
private Block createMethodBody(ASTNode[] selectedNodes, TextEditGroup substitute, int modifiers) {
Block result = fAST.newBlock();
ListRewrite statements = fRewriter.getListRewrite(result, Block.STATEMENTS_PROPERTY);
// Locals that are not passed as an arguments since the extracted method only
// writes to them
IVariableBinding[] methodLocals = fAnalyzer.getMethodLocals();
for (int i = 0; i < methodLocals.length; i++) {
if (methodLocals[i] != null) {
result.statements().add(createDeclaration(methodLocals[i], null));
}
}
for (Iterator<ParameterInfo> iter = fParameterInfos.iterator(); iter.hasNext(); ) {
ParameterInfo parameter = iter.next();
if (parameter.isRenamed()) {
for (int n = 0; n < selectedNodes.length; n++) {
SimpleName[] oldNames = LinkedNodeFinder.findByBinding(selectedNodes[n], parameter.getOldBinding());
for (int i = 0; i < oldNames.length; i++) {
fRewriter.replace(oldNames[i], fAST.newSimpleName(parameter.getNewName()), null);
}
}
}
}
boolean extractsExpression = fAnalyzer.isExpressionSelected();
ASTNode[] callNodes = createCallNodes(null, modifiers);
ASTNode replacementNode;
if (callNodes.length == 1) {
replacementNode = callNodes[0];
} else {
replacementNode = fRewriter.createGroupNode(callNodes);
}
if (extractsExpression) {
// if we have an expression then only one node is selected.
ITypeBinding binding = fAnalyzer.getExpressionBinding();
if (binding != null && (!binding.isPrimitive() || !"void".equals(binding.getName()))) {
//$NON-NLS-1$
ReturnStatement rs = fAST.newReturnStatement();
rs.setExpression((Expression) fRewriter.createMoveTarget(selectedNodes[0] instanceof ParenthesizedExpression ? ((ParenthesizedExpression) selectedNodes[0]).getExpression() : selectedNodes[0]));
statements.insertLast(rs, null);
} else {
ExpressionStatement st = fAST.newExpressionStatement((Expression) fRewriter.createMoveTarget(selectedNodes[0]));
statements.insertLast(st, null);
}
fRewriter.replace(selectedNodes[0].getParent() instanceof ParenthesizedExpression ? selectedNodes[0].getParent() : selectedNodes[0], replacementNode, substitute);
} else {
if (selectedNodes.length == 1) {
statements.insertLast(fRewriter.createMoveTarget(selectedNodes[0]), substitute);
fRewriter.replace(selectedNodes[0], replacementNode, substitute);
} else {
ListRewrite source = fRewriter.getListRewrite(selectedNodes[0].getParent(), (ChildListPropertyDescriptor) selectedNodes[0].getLocationInParent());
ASTNode toMove = source.createMoveTarget(selectedNodes[0], selectedNodes[selectedNodes.length - 1], replacementNode, substitute);
statements.insertLast(toMove, substitute);
}
IVariableBinding returnValue = fAnalyzer.getReturnValue();
if (returnValue != null) {
ReturnStatement rs = fAST.newReturnStatement();
rs.setExpression(fAST.newSimpleName(getName(returnValue)));
statements.insertLast(rs, null);
}
}
return result;
}
use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getPullNegationUpProposals.
private static boolean getPullNegationUpProposals(IInvocationContext context, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
if (coveredNodes.size() != 1) {
return false;
}
//
ASTNode fullyCoveredNode = coveredNodes.get(0);
Expression expression = getBooleanExpression(fullyCoveredNode);
if (expression == null || (!(expression instanceof InfixExpression) && !(expression instanceof ConditionalExpression))) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//
AST ast = expression.getAST();
final ASTRewrite rewrite = ASTRewrite.create(ast);
// prepared inverted expression
Expression inversedExpression = getInversedExpression(rewrite, expression);
// prepare ParenthesizedExpression
ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
parenthesizedExpression.setExpression(inversedExpression);
// prepare NOT prefix expression
PrefixExpression prefixExpression = ast.newPrefixExpression();
prefixExpression.setOperator(PrefixExpression.Operator.NOT);
prefixExpression.setOperand(parenthesizedExpression);
// replace old expression
rewrite.replace(expression, prefixExpression, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_pullNegationUp;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PULL_NEGATION_UP);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getInversedNotExpression.
private static Expression getInversedNotExpression(ASTRewrite rewrite, Expression expression, AST ast) {
PrefixExpression prefixExpression = ast.newPrefixExpression();
prefixExpression.setOperator(PrefixExpression.Operator.NOT);
ParenthesizedExpression parenthesizedExpression = getParenthesizedExpression(ast, (Expression) rewrite.createCopyTarget(expression));
prefixExpression.setOperand(parenthesizedExpression);
return prefixExpression;
}
use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getReplaceConditionalWithIfElseProposals.
private static boolean getReplaceConditionalWithIfElseProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
ASTNode node = covering;
while (!(node instanceof ConditionalExpression) && node instanceof Expression) {
node = node.getParent();
}
if (!(node instanceof ConditionalExpression)) {
node = covering;
while (node != null && !(node instanceof Statement)) {
node = node.getParent();
}
if (node instanceof VariableDeclarationStatement) {
node = (ASTNode) (((VariableDeclarationStatement) node).fragments().get(0));
node = ((VariableDeclarationFragment) node).getInitializer();
}
if (node instanceof ExpressionStatement) {
node = ((ExpressionStatement) node).getExpression();
if (node instanceof Assignment) {
node = ((Assignment) node).getRightHandSide();
}
}
if (node instanceof ReturnStatement) {
node = ((ReturnStatement) node).getExpression();
}
}
if (!(node instanceof ConditionalExpression)) {
return false;
}
covering = node;
StructuralPropertyDescriptor locationInParent = covering.getLocationInParent();
if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
if (covering.getParent().getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
return false;
}
} else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
ASTNode statement = covering.getParent().getParent();
if (!(statement instanceof VariableDeclarationStatement) || statement.getLocationInParent() != Block.STATEMENTS_PROPERTY) {
return false;
}
} else if (locationInParent != ReturnStatement.EXPRESSION_PROPERTY) {
return false;
}
ConditionalExpression conditional = (ConditionalExpression) covering;
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//
AST ast = covering.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare new 'if' statement
Expression expression = conditional.getExpression();
while (expression instanceof ParenthesizedExpression) {
expression = ((ParenthesizedExpression) expression).getExpression();
}
IfStatement ifStatement = ast.newIfStatement();
ifStatement.setExpression((Expression) rewrite.createCopyTarget(expression));
if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
Assignment assignment = (Assignment) covering.getParent();
Expression assignee = assignment.getLeftHandSide();
Assignment.Operator op = assignment.getOperator();
ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression()));
ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
// replace return conditional expression with if/then/else/return
rewrite.replace(covering.getParent().getParent(), ifStatement, null);
} else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
ifStatement.setThenStatement(createReturnExpression(rewrite, conditional.getThenExpression()));
ifStatement.setElseStatement(createReturnExpression(rewrite, conditional.getElseExpression()));
//
// replace return conditional expression with if/then/else/return
rewrite.replace(conditional.getParent(), ifStatement, null);
} else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) covering.getParent();
Assignment.Operator op = Assignment.Operator.ASSIGN;
Expression assignee = frag.getName();
ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression()));
ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
// clear initializer
rewrite.set(frag, VariableDeclarationFragment.INITIALIZER_PROPERTY, null, null);
ASTNode statement = frag.getParent();
rewrite.getListRewrite(statement.getParent(), Block.STATEMENTS_PROPERTY).insertAfter(ifStatement, statement, null);
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_replaceConditionalWithIf;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_CONDITIONAL_WITH_IF_ELSE);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getInversedExpression.
private static Expression getInversedExpression(ASTRewrite rewrite, Expression expression, SimpleNameRenameProvider provider) {
AST ast = rewrite.getAST();
//
if (expression instanceof BooleanLiteral) {
return ast.newBooleanLiteral(!((BooleanLiteral) expression).booleanValue());
}
if (expression instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) expression;
InfixExpression.Operator operator = infixExpression.getOperator();
if (operator == InfixExpression.Operator.LESS) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.GREATER_EQUALS, provider);
}
if (operator == InfixExpression.Operator.GREATER) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.LESS_EQUALS, provider);
}
if (operator == InfixExpression.Operator.LESS_EQUALS) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.GREATER, provider);
}
if (operator == InfixExpression.Operator.GREATER_EQUALS) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.LESS, provider);
}
if (operator == InfixExpression.Operator.EQUALS) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.NOT_EQUALS, provider);
}
if (operator == InfixExpression.Operator.NOT_EQUALS) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.EQUALS, provider);
}
if (operator == InfixExpression.Operator.CONDITIONAL_AND) {
return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.CONDITIONAL_OR, provider);
}
if (operator == InfixExpression.Operator.CONDITIONAL_OR) {
return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.CONDITIONAL_AND, provider);
}
if (operator == InfixExpression.Operator.AND) {
return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.OR, provider);
}
if (operator == InfixExpression.Operator.OR) {
return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.AND, provider);
}
if (operator == InfixExpression.Operator.XOR) {
return getInversedNotExpression(rewrite, expression, ast);
}
}
if (expression instanceof PrefixExpression) {
PrefixExpression prefixExpression = (PrefixExpression) expression;
if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT) {
Expression operand = prefixExpression.getOperand();
if ((operand instanceof ParenthesizedExpression) && NecessaryParenthesesChecker.canRemoveParentheses(operand, expression.getParent(), expression.getLocationInParent())) {
operand = ((ParenthesizedExpression) operand).getExpression();
}
Expression renamedNameCopy = getRenamedNameCopy(provider, rewrite, operand);
if (renamedNameCopy instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) renamedNameCopy;
infixExpression.setOperator(((InfixExpression) operand).getOperator());
}
return renamedNameCopy;
}
}
if (expression instanceof InstanceofExpression) {
return getInversedNotExpression(rewrite, expression, ast);
}
if (expression instanceof ParenthesizedExpression) {
ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) expression;
Expression innerExpression = parenthesizedExpression.getExpression();
while (innerExpression instanceof ParenthesizedExpression) {
innerExpression = ((ParenthesizedExpression) innerExpression).getExpression();
}
if (innerExpression instanceof InstanceofExpression) {
return getInversedExpression(rewrite, innerExpression, provider);
}
parenthesizedExpression = getParenthesizedExpression(ast, getInversedExpression(rewrite, innerExpression, provider));
return parenthesizedExpression;
}
if (expression instanceof ConditionalExpression) {
ConditionalExpression conditionalExpression = (ConditionalExpression) expression;
ConditionalExpression newExpression = ast.newConditionalExpression();
newExpression.setExpression((Expression) rewrite.createCopyTarget(conditionalExpression.getExpression()));
newExpression.setThenExpression(getInversedExpression(rewrite, conditionalExpression.getThenExpression()));
newExpression.setElseExpression(getInversedExpression(rewrite, conditionalExpression.getElseExpression()));
return newExpression;
}
PrefixExpression prefixExpression = ast.newPrefixExpression();
prefixExpression.setOperator(PrefixExpression.Operator.NOT);
Expression renamedNameCopy = getRenamedNameCopy(provider, rewrite, expression);
if (NecessaryParenthesesChecker.needsParentheses(renamedNameCopy, prefixExpression, PrefixExpression.OPERAND_PROPERTY)) {
renamedNameCopy = getParenthesizedExpression(ast, renamedNameCopy);
}
prefixExpression.setOperand(renamedNameCopy);
return prefixExpression;
}
Aggregations