Search in sources :

Example 1 with IntroduceParameterProcessor

use of com.intellij.refactoring.introduceParameter.IntroduceParameterProcessor in project intellij-community by JetBrains.

the class IntroduceParameterTest method performForLocal.

private static void performForLocal(boolean searchForSuper, boolean removeLocalVariable, boolean replaceAllOccurrences, boolean declareFinal, final boolean removeUnusedParameters) {
    final int offset = myEditor.getCaretModel().getOffset();
    final PsiElement element = ObjectUtils.assertNotNull(myFile.findElementAt(offset)).getParent();
    assertTrue(element instanceof PsiLocalVariable);
    PsiMethod method = Util.getContainingMethod(element);
    final PsiMethod methodToSearchFor;
    if (searchForSuper) {
        PsiMethod[] deepestSuperMethods = method.findDeepestSuperMethods();
        methodToSearchFor = deepestSuperMethods.length > 0 ? deepestSuperMethods[0] : method;
    } else {
        methodToSearchFor = method;
    }
    assertNotNull(method);
    assertNotNull(methodToSearchFor);
    final PsiLocalVariable localVariable = (PsiLocalVariable) element;
    final PsiExpression parameterInitializer = localVariable.getInitializer();
    assertNotNull(parameterInitializer);
    TIntArrayList parametersToRemove = removeUnusedParameters ? Util.findParametersToRemove(method, parameterInitializer, null) : new TIntArrayList();
    new IntroduceParameterProcessor(getProject(), method, methodToSearchFor, parameterInitializer, null, localVariable, removeLocalVariable, localVariable.getName(), replaceAllOccurrences, IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, declareFinal, false, null, parametersToRemove).run();
}
Also used : IntroduceParameterProcessor(com.intellij.refactoring.introduceParameter.IntroduceParameterProcessor) TIntArrayList(gnu.trove.TIntArrayList)

Example 2 with IntroduceParameterProcessor

use of com.intellij.refactoring.introduceParameter.IntroduceParameterProcessor 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

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