Search in sources :

Example 6 with ChildListPropertyDescriptor

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);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 7 with ChildListPropertyDescriptor

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);
}
Also used : ChildPropertyDescriptor(org.eclipse.jdt.core.dom.ChildPropertyDescriptor) SimplePropertyDescriptor(org.eclipse.jdt.core.dom.SimplePropertyDescriptor) ASTNode(org.eclipse.jdt.core.dom.ASTNode) List(java.util.List) ArrayList(java.util.ArrayList) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)

Example 8 with ChildListPropertyDescriptor

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)

Example 9 with ChildListPropertyDescriptor

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);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) IfStatement(org.eclipse.jdt.core.dom.IfStatement) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) List(java.util.List) ArrayList(java.util.ArrayList)

Example 10 with ChildListPropertyDescriptor

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)

Aggregations

ChildListPropertyDescriptor (org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)26 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)17 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)14 AST (org.eclipse.jdt.core.dom.AST)10 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)10 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)10 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)8 Expression (org.eclipse.jdt.core.dom.Expression)7 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)6 Statement (org.eclipse.jdt.core.dom.Statement)6 Type (org.eclipse.jdt.core.dom.Type)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 Block (org.eclipse.jdt.core.dom.Block)5 ForStatement (org.eclipse.jdt.core.dom.ForStatement)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)5