use of org.eclipse.jdt.core.dom.ChildListPropertyDescriptor in project che by eclipse.
the class IntroduceFactoryRefactoring method rewriteFactoryMethodCall.
/**
* Updates the constructor call.
*
* @param ctorCall the ClassInstanceCreation to be marked as replaced
* @param unitRewriter the AST rewriter
* @param gd the edit group to use
*/
private void rewriteFactoryMethodCall(ClassInstanceCreation ctorCall, ASTRewrite unitRewriter, TextEditGroup gd) {
AST ast = unitRewriter.getAST();
MethodInvocation factoryMethodCall = ast.newMethodInvocation();
ASTNode ctorCallParent = ctorCall.getParent();
StructuralPropertyDescriptor ctorCallLocation = ctorCall.getLocationInParent();
if (ctorCallLocation instanceof ChildListPropertyDescriptor) {
ListRewrite ctorCallParentListRewrite = unitRewriter.getListRewrite(ctorCallParent, (ChildListPropertyDescriptor) ctorCallLocation);
int index = ctorCallParentListRewrite.getOriginalList().indexOf(ctorCall);
ctorCall = (ClassInstanceCreation) ctorCallParentListRewrite.getRewrittenList().get(index);
} else {
ctorCall = (ClassInstanceCreation) unitRewriter.get(ctorCallParent, ctorCallLocation);
}
ListRewrite actualFactoryArgs = unitRewriter.getListRewrite(factoryMethodCall, MethodInvocation.ARGUMENTS_PROPERTY);
ListRewrite actualCtorArgs = unitRewriter.getListRewrite(ctorCall, ClassInstanceCreation.ARGUMENTS_PROPERTY);
// Need to use a qualified name for the factory method if we're not
// in the context of the class holding the factory.
AbstractTypeDeclaration callOwner = (AbstractTypeDeclaration) ASTNodes.getParent(ctorCall, AbstractTypeDeclaration.class);
ITypeBinding callOwnerBinding = callOwner.resolveBinding();
if (callOwnerBinding == null || !Bindings.equals(callOwner.resolveBinding(), fFactoryOwningClass.resolveBinding())) {
String qualifier = fImportRewriter.addImport(fFactoryOwningClass.resolveBinding());
factoryMethodCall.setExpression(ASTNodeFactory.newName(ast, qualifier));
}
factoryMethodCall.setName(ast.newSimpleName(fNewMethodName));
List<Expression> actualCtorArgsList = actualCtorArgs.getRewrittenList();
for (int i = 0; i < actualCtorArgsList.size(); i++) {
Expression actualCtorArg = actualCtorArgsList.get(i);
ASTNode movedArg;
if (ASTNodes.isExistingNode(actualCtorArg)) {
movedArg = unitRewriter.createMoveTarget(actualCtorArg);
} else {
unitRewriter.remove(actualCtorArg, null);
movedArg = actualCtorArg;
}
actualFactoryArgs.insertLast(movedArg, gd);
}
unitRewriter.replace(ctorCall, factoryMethodCall, gd);
}
use of org.eclipse.jdt.core.dom.ChildListPropertyDescriptor in project compiler by boalang.
the class ASTDumper method walk.
/**
* Walks the tree of objects from the specified node, sending events to the
* specified printer.
*
* @param node
* the root node.
* @param parentIsList
* indicates whether the parent node is a list or not.
*/
private void walk(final ASTNode node, final boolean parentIsList) {
printer.startType(node.getClass().getSimpleName(), parentIsList);
final List<?> properties = node.structuralPropertiesForType();
if (comments != null) {
dumpComments(comments.leadingComments(node), true);
dumpComments(comments.trailingComments(node), false);
}
for (Iterator<?> iterator = properties.iterator(); iterator.hasNext(); ) {
final Object desciptor = iterator.next();
if (desciptor instanceof SimplePropertyDescriptor) {
SimplePropertyDescriptor simple = (SimplePropertyDescriptor) desciptor;
Object value = node.getStructuralProperty(simple);
printer.literal(simple.getId(), value);
} else if (desciptor instanceof ChildPropertyDescriptor) {
ChildPropertyDescriptor child = (ChildPropertyDescriptor) desciptor;
ASTNode childNode = (ASTNode) node.getStructuralProperty(child);
if (childNode != null) {
printer.startElement(child.getId(), false);
walk(childNode);
printer.endElement(child.getId(), false);
} else {
printer.literal(child.getId(), null);
}
} else {
ChildListPropertyDescriptor list = (ChildListPropertyDescriptor) desciptor;
List<ASTNode> value = new ArrayList<ASTNode>((List<ASTNode>) node.getStructuralProperty(list));
if (value.size() > 0) {
printer.startElement(list.getId(), true);
walk(value);
printer.endElement(list.getId(), true);
} else {
printer.literal(list.getId(), value);
}
}
}
printer.endType(node.getClass().getSimpleName(), parentIsList);
}
use of org.eclipse.jdt.core.dom.ChildListPropertyDescriptor in project eclipse.jdt.ls by eclipse.
the class SelfEncapsulateFieldProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode);
ASTNode typeDecl = astRoot.findDeclaringNode(fSenderBinding);
ASTNode newTypeDecl = null;
if (typeDecl != null) {
newTypeDecl = typeDecl;
} else {
astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
}
createImportRewrite(astRoot);
if (newTypeDecl != null) {
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(newTypeDecl);
int insertIndex = members.size();
ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property);
isGetter = true;
MethodDeclaration newStub = getStub(rewrite, newTypeDecl);
listRewriter.insertAt(newStub, insertIndex, null);
isGetter = false;
newStub = getStub(rewrite, newTypeDecl);
listRewriter.insertAt(newStub, insertIndex + 1, null);
return rewrite;
}
return null;
}
use of org.eclipse.jdt.core.dom.ChildListPropertyDescriptor in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method getUnreachableCodeProposals.
public static void getUnreachableCodeProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> 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();
ASTRewrite rewrite = ASTRewrite.create(parent.getAST());
Expression replacement = leftOperand;
while (replacement instanceof ParenthesizedExpression) {
replacement = ((ParenthesizedExpression) replacement).getExpression();
}
Expression toReplace = infixExpression;
while (toReplace.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
toReplace = (Expression) toReplace.getParent();
}
if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
if (leftOperand instanceof ParenthesizedExpression) {
replacement = (Expression) replacement.getParent();
} else if (infixExpression.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
toReplace = ((ParenthesizedExpression) toReplace).getExpression();
}
}
rewrite.replace(toReplace, rewrite.createMoveTarget(replacement), null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
addRemoveProposal(context, rewrite, label, proposals);
InnovationContext assistContext = new InnovationContext(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.ChildListPropertyDescriptor in project eclipse.jdt.ls by eclipse.
the class AbstractMethodCorrectionProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode);
ASTNode typeDecl = astRoot.findDeclaringNode(fSenderBinding);
ASTNode newTypeDecl = null;
boolean isInDifferentCU;
if (typeDecl != null) {
isInDifferentCU = false;
newTypeDecl = typeDecl;
} else {
isInDifferentCU = true;
astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
}
createImportRewrite(astRoot);
if (newTypeDecl != null) {
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
MethodDeclaration newStub = getStub(rewrite, newTypeDecl);
ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(newTypeDecl);
int insertIndex;
if (isConstructor()) {
insertIndex = findConstructorInsertIndex(members);
} else if (!isInDifferentCU) {
insertIndex = findMethodInsertIndex(members, fNode.getStartPosition());
} else {
insertIndex = members.size();
}
ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property);
listRewriter.insertAt(newStub, insertIndex, null);
return rewrite;
}
return null;
}
Aggregations