Search in sources :

Example 1 with ParameterInfo

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls by eclipse.

the class ExtractMethodRefactoring method initializeUsedNames.

private void initializeUsedNames() {
    fUsedNames = UsedNamesCollector.perform(fAnalyzer.getSelectedNodes());
    for (Iterator<ParameterInfo> iter = fParameterInfos.iterator(); iter.hasNext(); ) {
        ParameterInfo parameter = iter.next();
        fUsedNames.remove(parameter.getOldName());
    }
}
Also used : ParameterInfo(org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo)

Example 2 with ParameterInfo

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls by eclipse.

the class ExtractMethodRefactoring method createCallNodes.

// ---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
    List<ASTNode> result = new ArrayList<>(2);
    IVariableBinding[] locals = fAnalyzer.getCallerLocals();
    for (int i = 0; i < locals.length; i++) {
        result.add(createDeclaration(locals[i], null));
    }
    MethodInvocation invocation = fAST.newMethodInvocation();
    invocation.setName(fAST.newSimpleName(fMethodName));
    ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration());
    RefactoringStatus status = new RefactoringStatus();
    while (fDestination != typeNode) {
        fAnalyzer.checkInput(status, fMethodName, typeNode);
        if (!status.isOK()) {
            SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName());
            if ((modifiers & Modifier.STATIC) == 0) {
                ThisExpression thisExpression = fAST.newThisExpression();
                thisExpression.setQualifier(destinationTypeName);
                invocation.setExpression(thisExpression);
            } else {
                invocation.setExpression(destinationTypeName);
            }
            break;
        }
        typeNode = typeNode.getParent();
    }
    List<Expression> arguments = invocation.arguments();
    for (int i = 0; i < fParameterInfos.size(); i++) {
        ParameterInfo parameter = fParameterInfos.get(i);
        arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter)));
    }
    if (fLinkedProposalModel != null) {
        LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
        nameGroup.addPosition(fRewriter.track(invocation.getName()), false);
    }
    ASTNode call;
    int returnKind = fAnalyzer.getReturnKind();
    switch(returnKind) {
        case ExtractMethodAnalyzer.ACCESS_TO_LOCAL:
            IVariableBinding binding = fAnalyzer.getReturnLocal();
            if (binding != null) {
                VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation);
                call = decl;
            } else {
                Assignment assignment = fAST.newAssignment();
                assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName()));
                assignment.setRightHandSide(invocation);
                call = assignment;
            }
            break;
        case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE:
            ReturnStatement rs = fAST.newReturnStatement();
            rs.setExpression(invocation);
            call = rs;
            break;
        default:
            call = invocation;
    }
    if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) {
        call = fAST.newExpressionStatement((Expression) call);
    }
    result.add(call);
    // return;
    if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) {
        result.add(fAST.newReturnStatement());
    }
    return result.toArray(new ASTNode[result.size()]);
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ParameterInfo(org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Assignment(org.eclipse.jdt.core.dom.Assignment) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) LinkedProposalPositionGroup(org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalPositionGroup)

Example 3 with ParameterInfo

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls by eclipse.

the class ExtractMethodRefactoring method computeLocalTypeVariables.

private ITypeBinding[] computeLocalTypeVariables(int modifier) {
    List<ITypeBinding> result = new ArrayList<>(Arrays.asList(fAnalyzer.getTypeVariables()));
    for (int i = 0; i < fParameterInfos.size(); i++) {
        ParameterInfo info = fParameterInfos.get(i);
        processVariable(result, info.getOldBinding(), modifier);
    }
    IVariableBinding[] methodLocals = fAnalyzer.getMethodLocals();
    for (int i = 0; i < methodLocals.length; i++) {
        processVariable(result, methodLocals[i], modifier);
    }
    return result.toArray(new ITypeBinding[result.size()]);
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) ParameterInfo(org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 4 with ParameterInfo

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls 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;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ParameterInfo(org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 5 with ParameterInfo

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls by eclipse.

the class ExtractMethodRefactoring method initializeParameterInfos.

// ---- Helper methods ------------------------------------------------------------------------
private void initializeParameterInfos() {
    IVariableBinding[] arguments = fAnalyzer.getArguments();
    fParameterInfos = new ArrayList<>(arguments.length);
    ASTNode root = fAnalyzer.getEnclosingBodyDeclaration();
    ParameterInfo vararg = null;
    for (int i = 0; i < arguments.length; i++) {
        IVariableBinding argument = arguments[i];
        if (argument == null) {
            continue;
        }
        VariableDeclaration declaration = ASTNodes.findVariableDeclaration(argument, root);
        boolean isVarargs = declaration instanceof SingleVariableDeclaration ? ((SingleVariableDeclaration) declaration).isVarargs() : false;
        ParameterInfo info = new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i);
        if (isVarargs) {
            vararg = info;
        } else {
            fParameterInfos.add(info);
        }
    }
    if (vararg != null) {
        fParameterInfos.add(vararg);
    }
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ParameterInfo(org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

ParameterInfo (org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 ArrayList (java.util.ArrayList)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)2 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 Assignment (org.eclipse.jdt.core.dom.Assignment)1 Block (org.eclipse.jdt.core.dom.Block)1 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)1 Expression (org.eclipse.jdt.core.dom.Expression)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)1 Type (org.eclipse.jdt.core.dom.Type)1