Search in sources :

Example 16 with ParameterInfo

use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo 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;
}
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.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 17 with ParameterInfo

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

the class ExtractMethodRefactoring method computeLocalTypeVariables.

private ITypeBinding[] computeLocalTypeVariables(int modifier) {
    List<ITypeBinding> result = new ArrayList<ITypeBinding>(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.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 18 with ParameterInfo

use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project che 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.internal.corext.refactoring.ParameterInfo)

Example 19 with ParameterInfo

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

the class ExtractMethodRefactoring method createNewMethodDeclaration.

private MethodDeclaration createNewMethodDeclaration() {
    MethodDeclaration result = fAST.newMethodDeclaration();
    int modifiers = fVisibility;
    BodyDeclaration enclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
    boolean isDestinationInterface = isDestinationInterface();
    if (isDestinationInterface && !(enclosingBodyDeclaration instanceof MethodDeclaration && enclosingBodyDeclaration.getParent() == fDestination && Modifier.isPublic(enclosingBodyDeclaration.getModifiers()))) {
        modifiers = Modifier.NONE;
    }
    boolean shouldBeStatic = false;
    ASTNode currentParent = enclosingBodyDeclaration;
    do {
        if (currentParent instanceof BodyDeclaration) {
            shouldBeStatic = shouldBeStatic || JdtFlags.isStatic((BodyDeclaration) currentParent);
        }
        currentParent = currentParent.getParent();
    } while (!shouldBeStatic && currentParent != null && currentParent != fDestination);
    if (shouldBeStatic || fAnalyzer.getForceStatic() || forceStatic()) {
        modifiers |= Modifier.STATIC;
    } else if (isDestinationInterface) {
        modifiers |= Modifier.DEFAULT;
    }
    ITypeBinding[] typeVariables = computeLocalTypeVariables(modifiers);
    List<TypeParameter> typeParameters = result.typeParameters();
    for (int i = 0; i < typeVariables.length; i++) {
        TypeParameter parameter = fAST.newTypeParameter();
        parameter.setName(fAST.newSimpleName(typeVariables[i].getName()));
        ITypeBinding[] bounds = typeVariables[i].getTypeBounds();
        for (int j = 0; j < bounds.length; j++) if (//$NON-NLS-1$
        !"java.lang.Object".equals(bounds[j].getQualifiedName()))
            parameter.typeBounds().add(fImportRewriter.addImport(bounds[j], fAST));
        typeParameters.add(parameter);
    }
    result.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, modifiers));
    result.setReturnType2((Type) ASTNode.copySubtree(fAST, fAnalyzer.getReturnType()));
    result.setName(fAST.newSimpleName(fMethodName));
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(enclosingBodyDeclaration, fImportRewriter);
    List<SingleVariableDeclaration> parameters = result.parameters();
    for (int i = 0; i < fParameterInfos.size(); i++) {
        ParameterInfo info = fParameterInfos.get(i);
        VariableDeclaration infoDecl = getVariableDeclaration(info);
        SingleVariableDeclaration parameter = fAST.newSingleVariableDeclaration();
        parameter.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, ASTNodes.getModifiers(infoDecl)));
        parameter.setType(ASTNodeFactory.newType(fAST, infoDecl, fImportRewriter, context));
        parameter.setName(fAST.newSimpleName(info.getNewName()));
        parameter.setVarargs(info.isNewVarargs());
        parameters.add(parameter);
    }
    List<Type> exceptions = result.thrownExceptionTypes();
    ITypeBinding[] exceptionTypes = fAnalyzer.getExceptions(fThrowRuntimeExceptions);
    for (int i = 0; i < exceptionTypes.length; i++) {
        ITypeBinding exceptionType = exceptionTypes[i];
        exceptions.add(fImportRewriter.addImport(exceptionType, fAST, context));
    }
    return result;
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 20 with ParameterInfo

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

the class ExtractMethodRefactoring method initializeParameterInfos.

//---- Helper methods ------------------------------------------------------------------------
private void initializeParameterInfos() {
    IVariableBinding[] arguments = fAnalyzer.getArguments();
    fParameterInfos = new ArrayList<ParameterInfo>(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.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

ParameterInfo (org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)30 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)6 ArrayList (java.util.ArrayList)5 JavaModelException (org.eclipse.jdt.core.JavaModelException)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)4 ExceptionInfo (org.eclipse.jdt.internal.corext.refactoring.ExceptionInfo)4 RichString (org.eclipse.xtend.core.xtend.RichString)4 HashSet (java.util.HashSet)3 ExtractMethodRefactoring (org.eclipse.xtend.ide.refactoring.ExtractMethodRefactoring)3 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)3 Test (org.junit.Test)3 Expression (org.eclipse.jdt.core.dom.Expression)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