Search in sources :

Example 1 with InputVariables

use of com.intellij.refactoring.extractMethod.InputVariables in project intellij-community by JetBrains.

the class MethodDuplicatesHandler method createDuplicatesFinder.

@Nullable
public static DuplicatesFinder createDuplicatesFinder(PsiMember member) {
    PsiElement[] pattern;
    ReturnValue matchedReturnValue = null;
    if (member instanceof PsiMethod) {
        final PsiCodeBlock body = ((PsiMethod) member).getBody();
        LOG.assertTrue(body != null);
        final PsiStatement[] statements = body.getStatements();
        pattern = statements;
        matchedReturnValue = null;
        if (statements.length != 1 || !(statements[0] instanceof PsiReturnStatement)) {
            final PsiStatement lastStatement = statements.length > 0 ? statements[statements.length - 1] : null;
            if (lastStatement instanceof PsiReturnStatement) {
                final PsiExpression returnValue = ((PsiReturnStatement) lastStatement).getReturnValue();
                if (returnValue instanceof PsiReferenceExpression) {
                    final PsiElement resolved = ((PsiReferenceExpression) returnValue).resolve();
                    if (resolved instanceof PsiVariable) {
                        pattern = new PsiElement[statements.length - 1];
                        System.arraycopy(statements, 0, pattern, 0, statements.length - 1);
                        matchedReturnValue = new VariableReturnValue((PsiVariable) resolved);
                    }
                }
            }
        } else {
            final PsiExpression returnValue = ((PsiReturnStatement) statements[0]).getReturnValue();
            if (returnValue != null) {
                pattern = new PsiElement[] { returnValue };
            }
        }
    } else {
        pattern = new PsiElement[] { ((PsiField) member).getInitializer() };
    }
    if (pattern.length == 0) {
        return null;
    }
    final List<? extends PsiVariable> inputVariables = member instanceof PsiMethod ? Arrays.asList(((PsiMethod) member).getParameterList().getParameters()) : new ArrayList<>();
    return new DuplicatesFinder(pattern, new InputVariables(inputVariables, member.getProject(), new LocalSearchScope(pattern), false), matchedReturnValue, new ArrayList<>());
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) InputVariables(com.intellij.refactoring.extractMethod.InputVariables) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 InputVariables (com.intellij.refactoring.extractMethod.InputVariables)1 Nullable (org.jetbrains.annotations.Nullable)1