Search in sources :

Example 1 with ElementToWorkOn

use of com.intellij.refactoring.introduceField.ElementToWorkOn in project intellij-community by JetBrains.

the class IntroduceParameterTest method perform.

private static boolean perform(boolean replaceAllOccurrences, int replaceFieldsWithGetters, @NonNls String parameterName, boolean searchForSuper, boolean declareFinal, boolean removeUnusedParameters, boolean generateDelegate, int enclosingLevel, final boolean replaceDuplicates) {
    final ElementToWorkOn[] elementToWorkOn = new ElementToWorkOn[1];
    ElementToWorkOn.processElementToWorkOn(myEditor, myFile, "INtr param", HelpID.INTRODUCE_PARAMETER, getProject(), new ElementToWorkOn.ElementsProcessor<ElementToWorkOn>() {

        @Override
        public boolean accept(ElementToWorkOn el) {
            return true;
        }

        @Override
        public void pass(final ElementToWorkOn e) {
            if (e != null) {
                elementToWorkOn[0] = e;
            }
        }
    });
    final PsiExpression expr = elementToWorkOn[0].getExpression();
    final PsiLocalVariable localVar = elementToWorkOn[0].getLocalVariable();
    PsiElement context = expr == null ? localVar : expr;
    PsiMethod method = Util.getContainingMethod(context);
    if (method == null)
        return false;
    final List<PsiMethod> methods = com.intellij.refactoring.introduceParameter.IntroduceParameterHandler.getEnclosingMethods(method);
    assertTrue(methods.size() > enclosingLevel);
    method = methods.get(enclosingLevel);
    final PsiMethod methodToSearchFor;
    if (searchForSuper) {
        methodToSearchFor = method.findDeepestSuperMethods()[0];
    } else {
        methodToSearchFor = method;
    }
    PsiExpression[] occurrences;
    PsiExpression initializer;
    if (expr == null) {
        initializer = localVar.getInitializer();
        assertNotNull(initializer);
        occurrences = CodeInsightUtil.findReferenceExpressions(method, localVar);
    } else {
        initializer = expr;
        occurrences = new ExpressionOccurrenceManager(expr, method, null).findExpressionOccurrences();
    }
    TIntArrayList parametersToRemove = removeUnusedParameters ? Util.findParametersToRemove(method, initializer, occurrences) : new TIntArrayList();
    IntroduceParameterProcessor processor = new IntroduceParameterProcessor(getProject(), method, methodToSearchFor, initializer, expr, localVar, true, parameterName, replaceAllOccurrences, replaceFieldsWithGetters, declareFinal, generateDelegate, null, parametersToRemove) {

        @Override
        protected boolean isReplaceDuplicates() {
            return replaceDuplicates;
        }
    };
    PsiType initializerType = initializer.getType();
    if (initializerType != null && initializerType != PsiType.NULL) {
        PsiExpression lambda = AnonymousCanBeLambdaInspection.replaceAnonymousWithLambda(initializer, initializerType);
        if (lambda != null) {
            processor.setParameterInitializer(lambda);
        }
    }
    processor.run();
    myEditor.getSelectionModel().removeSelection();
    return true;
}
Also used : ExpressionOccurrenceManager(com.intellij.refactoring.util.occurrences.ExpressionOccurrenceManager) TIntArrayList(gnu.trove.TIntArrayList) ElementToWorkOn(com.intellij.refactoring.introduceField.ElementToWorkOn) IntroduceParameterProcessor(com.intellij.refactoring.introduceParameter.IntroduceParameterProcessor)

Aggregations

ElementToWorkOn (com.intellij.refactoring.introduceField.ElementToWorkOn)1 IntroduceParameterProcessor (com.intellij.refactoring.introduceParameter.IntroduceParameterProcessor)1 ExpressionOccurrenceManager (com.intellij.refactoring.util.occurrences.ExpressionOccurrenceManager)1 TIntArrayList (gnu.trove.TIntArrayList)1