Search in sources :

Example 1 with Match

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.SnippetFinder.Match 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 2 with Match

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.SnippetFinder.Match in project eclipse.jdt.ls by eclipse.

the class ExtractMethodRefactoring method replaceDuplicates.

private void replaceDuplicates(CompilationUnitChange result, int modifiers) {
    int numberOf = getNumberOfDuplicates();
    if (numberOf == 0 || !fReplaceDuplicates) {
        return;
    }
    String label = null;
    if (numberOf == 1) {
        label = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_duplicates_single, BasicElementLabels.getJavaElementName(fMethodName));
    } else {
        label = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_duplicates_multi, BasicElementLabels.getJavaElementName(fMethodName));
    }
    TextEditGroup description = new TextEditGroup(label);
    result.addTextEditGroup(description);
    for (Match duplicate : fDuplicates) {
        if (!duplicate.isInvalidNode()) {
            if (isDestinationReachable(duplicate.getEnclosingMethod())) {
                ASTNode[] callNodes = createCallNodes(duplicate, modifiers);
                ASTNode[] duplicateNodes = duplicate.getNodes();
                for (int i = 0; i < duplicateNodes.length; i++) {
                    ASTNode parent = duplicateNodes[i].getParent();
                    if (parent instanceof ParenthesizedExpression) {
                        duplicateNodes[i] = parent;
                    }
                }
                new StatementRewrite(fRewriter, duplicateNodes).replace(callNodes, description);
            }
        }
    }
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) StatementRewrite(org.eclipse.jdt.ls.core.internal.corext.dom.StatementRewrite) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TextEditGroup(org.eclipse.text.edits.TextEditGroup) Match(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.SnippetFinder.Match)

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 Match (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.SnippetFinder.Match)2 ArrayList (java.util.ArrayList)1 CoreException (org.eclipse.core.runtime.CoreException)1 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)1 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)1 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)1 VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)1 StatementRewrite (org.eclipse.jdt.ls.core.internal.corext.dom.StatementRewrite)1 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)1 TextEditGroup (org.eclipse.text.edits.TextEditGroup)1