use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class QuickAssistProcessor method getConvertStringConcatenationProposals.
private static boolean getConvertStringConcatenationProposals(IInvocationContext context, Collection<ICommandAccess> resultingCollections) {
ASTNode node = context.getCoveringNode();
BodyDeclaration parentDecl = ASTResolving.findParentBodyDeclaration(node);
if (!(parentDecl instanceof MethodDeclaration || parentDecl instanceof Initializer))
return false;
AST ast = node.getAST();
//$NON-NLS-1$
ITypeBinding stringBinding = ast.resolveWellKnownType("java.lang.String");
if (node instanceof Expression && !(node instanceof InfixExpression)) {
node = node.getParent();
}
if (node instanceof VariableDeclarationFragment) {
node = ((VariableDeclarationFragment) node).getInitializer();
} else if (node instanceof Assignment) {
node = ((Assignment) node).getRightHandSide();
}
InfixExpression oldInfixExpression = null;
while (node instanceof InfixExpression) {
InfixExpression curr = (InfixExpression) node;
if (curr.resolveTypeBinding() == stringBinding && curr.getOperator() == InfixExpression.Operator.PLUS) {
// is a infix expression we can use
oldInfixExpression = curr;
} else {
break;
}
node = node.getParent();
}
if (oldInfixExpression == null)
return false;
if (resultingCollections == null) {
return true;
}
LinkedCorrectionProposal stringBufferProposal = getConvertToStringBufferProposal(context, ast, oldInfixExpression);
resultingCollections.add(stringBufferProposal);
ASTRewriteCorrectionProposal messageFormatProposal = getConvertToMessageFormatProposal(context, ast, oldInfixExpression);
if (messageFormatProposal != null)
resultingCollections.add(messageFormatProposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class QuickAssistProcessor method getConvertToStringBufferProposal.
private static LinkedCorrectionProposal getConvertToStringBufferProposal(IInvocationContext context, AST ast, InfixExpression oldInfixExpression) {
String bufferOrBuilderName;
ICompilationUnit cu = context.getCompilationUnit();
if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
//$NON-NLS-1$
bufferOrBuilderName = "StringBuilder";
} else {
//$NON-NLS-1$
bufferOrBuilderName = "StringBuffer";
}
ASTRewrite rewrite = ASTRewrite.create(ast);
SimpleName existingBuffer = getEnclosingAppendBuffer(oldInfixExpression);
String mechanismName = BasicElementLabels.getJavaElementName(existingBuffer == null ? bufferOrBuilderName : existingBuffer.getIdentifier());
String label = Messages.format(CorrectionMessages.QuickAssistProcessor_convert_to_string_buffer_description, mechanismName);
//Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.CONVERT_TO_STRING_BUFFER);
proposal.setCommandId(CONVERT_TO_STRING_BUFFER_ID);
Statement insertAfter;
String bufferName;
//$NON-NLS-1$
String groupID = "nameId";
ListRewrite listRewrite;
Statement enclosingStatement = ASTResolving.findParentStatement(oldInfixExpression);
if (existingBuffer != null) {
if (ASTNodes.isControlStatementBody(enclosingStatement.getLocationInParent())) {
Block newBlock = ast.newBlock();
listRewrite = rewrite.getListRewrite(newBlock, Block.STATEMENTS_PROPERTY);
insertAfter = null;
rewrite.replace(enclosingStatement, newBlock, null);
} else {
listRewrite = rewrite.getListRewrite(enclosingStatement.getParent(), (ChildListPropertyDescriptor) enclosingStatement.getLocationInParent());
insertAfter = enclosingStatement;
}
bufferName = existingBuffer.getIdentifier();
} else {
// create buffer
VariableDeclarationFragment frag = ast.newVariableDeclarationFragment();
// check if name is already in use and provide alternative
List<String> fExcludedVariableNames = Arrays.asList(ASTResolving.getUsedVariableNames(oldInfixExpression));
SimpleType bufferType = ast.newSimpleType(ast.newName(bufferOrBuilderName));
ClassInstanceCreation newBufferExpression = ast.newClassInstanceCreation();
//StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, cu.getJavaProject(), bufferOrBuilderName, 0, fExcludedVariableNames, true);
String[] newBufferNames = new String[] {};
bufferName = newBufferNames[0];
SimpleName bufferNameDeclaration = ast.newSimpleName(bufferName);
frag.setName(bufferNameDeclaration);
proposal.addLinkedPosition(rewrite.track(bufferNameDeclaration), true, groupID);
for (int i = 0; i < newBufferNames.length; i++) {
proposal.addLinkedPositionProposal(groupID, newBufferNames[i], null);
}
newBufferExpression.setType(bufferType);
frag.setInitializer(newBufferExpression);
VariableDeclarationStatement bufferDeclaration = ast.newVariableDeclarationStatement(frag);
bufferDeclaration.setType(ast.newSimpleType(ast.newName(bufferOrBuilderName)));
insertAfter = bufferDeclaration;
Statement statement = ASTResolving.findParentStatement(oldInfixExpression);
if (ASTNodes.isControlStatementBody(statement.getLocationInParent())) {
Block newBlock = ast.newBlock();
listRewrite = rewrite.getListRewrite(newBlock, Block.STATEMENTS_PROPERTY);
listRewrite.insertFirst(bufferDeclaration, null);
listRewrite.insertLast(rewrite.createMoveTarget(statement), null);
rewrite.replace(statement, newBlock, null);
} else {
listRewrite = rewrite.getListRewrite(statement.getParent(), (ChildListPropertyDescriptor) statement.getLocationInParent());
listRewrite.insertBefore(bufferDeclaration, statement, null);
}
}
List<Expression> operands = new ArrayList<Expression>();
collectInfixPlusOperands(oldInfixExpression, operands);
Statement lastAppend = insertAfter;
for (Iterator<Expression> iter = operands.iterator(); iter.hasNext(); ) {
Expression operand = iter.next();
MethodInvocation appendIncovationExpression = ast.newMethodInvocation();
//$NON-NLS-1$
appendIncovationExpression.setName(ast.newSimpleName("append"));
SimpleName bufferNameReference = ast.newSimpleName(bufferName);
// If there was an existing name, don't offer to rename it
if (existingBuffer == null) {
proposal.addLinkedPosition(rewrite.track(bufferNameReference), true, groupID);
}
appendIncovationExpression.setExpression(bufferNameReference);
appendIncovationExpression.arguments().add(rewrite.createCopyTarget(operand));
ExpressionStatement appendExpressionStatement = ast.newExpressionStatement(appendIncovationExpression);
if (lastAppend == null) {
listRewrite.insertFirst(appendExpressionStatement, null);
} else {
listRewrite.insertAfter(appendExpressionStatement, lastAppend, null);
}
lastAppend = appendExpressionStatement;
}
if (existingBuffer != null) {
proposal.setEndPosition(rewrite.track(lastAppend));
if (insertAfter != null) {
rewrite.remove(enclosingStatement, null);
}
} else {
// replace old expression with toString
MethodInvocation bufferToString = ast.newMethodInvocation();
//$NON-NLS-1$
bufferToString.setName(ast.newSimpleName("toString"));
SimpleName bufferNameReference = ast.newSimpleName(bufferName);
bufferToString.setExpression(bufferNameReference);
proposal.addLinkedPosition(rewrite.track(bufferNameReference), true, groupID);
rewrite.replace(oldInfixExpression, bufferToString, null);
proposal.setEndPosition(rewrite.track(bufferToString));
}
return proposal;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class QuickAssistProcessor method getInvertEqualsProposal.
private static boolean getInvertEqualsProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
if (!(node instanceof MethodInvocation)) {
node = node.getParent();
if (!(node instanceof MethodInvocation)) {
return false;
}
}
MethodInvocation method = (MethodInvocation) node;
String identifier = method.getName().getIdentifier();
if (!"equals".equals(identifier) && !"equalsIgnoreCase".equals(identifier)) {
//$NON-NLS-1$ //$NON-NLS-2$
return false;
}
List<Expression> arguments = method.arguments();
if (arguments.size() != 1) {
//overloaded equals w/ more than 1 argument
return false;
}
Expression right = arguments.get(0);
ITypeBinding binding = right.resolveTypeBinding();
if (binding != null && !(binding.isClass() || binding.isInterface() || binding.isEnum())) {
//overloaded equals w/ non-class/interface argument or null
return false;
}
if (resultingCollections == null) {
return true;
}
Expression left = method.getExpression();
AST ast = method.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
if (left == null) {
// equals(x) -> x.equals(this)
MethodInvocation replacement = ast.newMethodInvocation();
replacement.setName((SimpleName) rewrite.createCopyTarget(method.getName()));
replacement.arguments().add(ast.newThisExpression());
replacement.setExpression((Expression) rewrite.createCopyTarget(right));
rewrite.replace(method, replacement, null);
} else if (right instanceof ThisExpression) {
// x.equals(this) -> equals(x)
MethodInvocation replacement = ast.newMethodInvocation();
replacement.setName((SimpleName) rewrite.createCopyTarget(method.getName()));
replacement.arguments().add(rewrite.createCopyTarget(left));
rewrite.replace(method, replacement, null);
} else {
ASTNode leftExpression = left;
while (leftExpression instanceof ParenthesizedExpression) {
leftExpression = ((ParenthesizedExpression) left).getExpression();
}
rewrite.replace(right, rewrite.createCopyTarget(leftExpression), null);
if (right instanceof CastExpression || right instanceof Assignment || right instanceof ConditionalExpression || right instanceof InfixExpression) {
ParenthesizedExpression paren = ast.newParenthesizedExpression();
paren.setExpression((Expression) rewrite.createCopyTarget(right));
rewrite.replace(left, paren, null);
} else {
rewrite.replace(left, rewrite.createCopyTarget(right), null);
}
}
String label = CorrectionMessages.QuickAssistProcessor_invertequals_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERT_EQUALS);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getSplitAndConditionProposals.
public static boolean getSplitAndConditionProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
Operator andOperator = InfixExpression.Operator.CONDITIONAL_AND;
// check that user invokes quick assist on infix expression
if (!(node instanceof InfixExpression)) {
return false;
}
InfixExpression infixExpression = (InfixExpression) node;
if (infixExpression.getOperator() != andOperator) {
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() == andOperator) {
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, andOperator, offset, true, newOperands);
Expression leftCondition = newOperands[0];
Expression rightCondition = newOperands[1];
// replace conditions in outer IfStatement
rewrite.set(ifStatement, IfStatement.EXPRESSION_PROPERTY, leftCondition, null);
// prepare inner IfStatement
IfStatement innerIf = ast.newIfStatement();
innerIf.setExpression(rightCondition);
innerIf.setThenStatement((Statement) rewrite.createMoveTarget(ifStatement.getThenStatement()));
Block innerBlock = ast.newBlock();
innerBlock.statements().add(innerIf);
Statement elseStatement = ifStatement.getElseStatement();
if (elseStatement != null) {
innerIf.setElseStatement((Statement) rewrite.createCopyTarget(elseStatement));
}
// replace outer thenStatement
rewrite.replace(ifStatement.getThenStatement(), innerBlock, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_splitAndCondition_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_AND_CONDITION);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method breakInfixOperationAtOperation.
/*
* Breaks an infix operation with possible extended operators at the given operator and returns the new left and right operands.
* a & b & c -> [[a' & b' ] & c' ] (c' == copy of c)
*/
private static void breakInfixOperationAtOperation(ASTRewrite rewrite, Expression expression, Operator operator, int operatorOffset, boolean removeParentheses, Expression[] res) {
if (expression.getStartPosition() + expression.getLength() <= operatorOffset) {
// add to the left
res[0] = combineOperands(rewrite, res[0], expression, removeParentheses, operator);
return;
}
if (operatorOffset <= expression.getStartPosition()) {
// add to the right
res[1] = combineOperands(rewrite, res[1], expression, removeParentheses, operator);
return;
}
if (!(expression instanceof InfixExpression)) {
//$NON-NLS-1$
throw new IllegalArgumentException("Cannot break up non-infix expression");
}
InfixExpression infixExpression = (InfixExpression) expression;
if (infixExpression.getOperator() != operator) {
//$NON-NLS-1$
throw new IllegalArgumentException("Incompatible operator");
}
breakInfixOperationAtOperation(rewrite, infixExpression.getLeftOperand(), operator, operatorOffset, removeParentheses, res);
breakInfixOperationAtOperation(rewrite, infixExpression.getRightOperand(), operator, operatorOffset, removeParentheses, res);
List<Expression> extended = infixExpression.extendedOperands();
for (int i = 0; i < extended.size(); i++) {
breakInfixOperationAtOperation(rewrite, extended.get(i), operator, operatorOffset, removeParentheses, res);
}
}
Aggregations