Search in sources :

Example 31 with VariableDeclaration

use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse.jdt.ls by eclipse.

the class ExtractMethodRefactoring method findValidDuplicates.

private List<SnippetFinder.Match> findValidDuplicates(ASTNode startNode) {
    List<Match> duplicates = SnippetFinder.perform(startNode, fAnalyzer.getSelectedNodes());
    List<SnippetFinder.Match> validDuplicates = new ArrayList<>();
    for (Match duplicate : duplicates) {
        if (duplicate != null && !duplicate.isInvalidNode()) {
            try {
                ASTNode[] nodes = duplicate.getNodes();
                int duplicateStart = nodes[0].getStartPosition();
                ASTNode lastNode = nodes[nodes.length - 1];
                int duplicateEnd = lastNode.getStartPosition() + lastNode.getLength();
                int duplicateLength = duplicateEnd - duplicateStart;
                ExtractMethodAnalyzer analyzer = new ExtractMethodAnalyzer(fCUnit, Selection.createFromStartLength(duplicateStart, duplicateLength));
                fRoot.accept(analyzer);
                RefactoringStatus result = new RefactoringStatus();
                result.merge(analyzer.checkInitialConditions(fImportRewriter));
                if (!result.hasFatalError()) {
                    ITypeBinding originalReturnTypeBinding = fAnalyzer.getReturnTypeBinding();
                    ITypeBinding duplicateReturnTypeBinding = analyzer.getReturnTypeBinding();
                    if (originalReturnTypeBinding == null && duplicateReturnTypeBinding == null) {
                        validDuplicates.add(duplicate);
                    } else if (originalReturnTypeBinding != null && duplicateReturnTypeBinding != null) {
                        if (!originalReturnTypeBinding.equals(duplicateReturnTypeBinding)) {
                            if (duplicateReturnTypeBinding.equals(startNode.getAST().resolveWellKnownType("void"))) {
                                // $NON-NLS-1$
                                // extracted snippet returns non-void and duplicate snippet returns void => OK
                                validDuplicates.add(duplicate);
                            }
                        } else {
                            IVariableBinding originalReturnValBinding = fAnalyzer.getReturnValue();
                            IVariableBinding duplicateReturnValBinding = analyzer.getReturnValue();
                            if (originalReturnValBinding == null && duplicateReturnValBinding == null) {
                                validDuplicates.add(duplicate);
                            } else if (originalReturnValBinding != null && duplicateReturnValBinding != null) {
                                BodyDeclaration originalEnclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
                                BodyDeclaration duplicateEnclosingBodyDeclaration = analyzer.getEnclosingBodyDeclaration();
                                VariableDeclaration originalReturnNode = ASTNodes.findVariableDeclaration(originalReturnValBinding, originalEnclosingBodyDeclaration);
                                VariableDeclaration duplicateReturnNode = ASTNodes.findVariableDeclaration(duplicateReturnValBinding, duplicateEnclosingBodyDeclaration);
                                if (originalReturnNode != null && duplicateReturnNode != null) {
                                    boolean matches;
                                    if (!fAnalyzer.getSelection().covers(originalReturnNode) && !analyzer.getSelection().covers(duplicateReturnNode)) {
                                        // returned variables are defined outside of the selection => always OK
                                        matches = true;
                                    } else {
                                        matches = matchesLocationInEnclosingBodyDecl(originalEnclosingBodyDeclaration, duplicateEnclosingBodyDeclaration, originalReturnNode, duplicateReturnNode);
                                    }
                                    if (matches) {
                                        validDuplicates.add(duplicate);
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (CoreException e) {
            // consider as invalid duplicate
            }
        }
    }
    return validDuplicates;
}
Also used : ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Match(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.SnippetFinder.Match) CoreException(org.eclipse.core.runtime.CoreException) 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 32 with VariableDeclaration

use of org.eclipse.jdt.core.dom.VariableDeclaration 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)

Example 33 with VariableDeclaration

use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse.jdt.ls by eclipse.

the class ExtractMethodRefactoring method createDeclaration.

private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) {
    VariableDeclaration original = ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
    VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment();
    fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName()));
    fragment.setInitializer(intilizer);
    VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment);
    result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
    result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
    return result;
}
Also used : ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 34 with VariableDeclaration

use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse.jdt.ls 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 (!"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, TypeLocation.EXCEPTION));
    }
    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.ls.core.internal.corext.refactoring.ParameterInfo) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.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 35 with VariableDeclaration

use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse.jdt.ls by eclipse.

the class SurroundWithTryCatchRefactoring method getSpecialVariableDeclarationStatements.

private List<ASTNode> getSpecialVariableDeclarationStatements() {
    List<ASTNode> result = new ArrayList<>(3);
    VariableDeclaration[] locals = fAnalyzer.getAffectedLocals();
    for (int i = 0; i < locals.length; i++) {
        ASTNode parent = locals[i].getParent();
        if (parent instanceof VariableDeclarationStatement && !result.contains(parent)) {
            result.add(parent);
        }
    }
    return result;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Aggregations

VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)40 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)19 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)15 ArrayList (java.util.ArrayList)12 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)10 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)8 Expression (org.eclipse.jdt.core.dom.Expression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 Type (org.eclipse.jdt.core.dom.Type)7 AST (org.eclipse.jdt.core.dom.AST)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)6 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)5 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)5