Search in sources :

Example 6 with ChangeSignatureProcessor

use of com.intellij.refactoring.changeSignature.ChangeSignatureProcessor in project intellij-community by JetBrains.

the class MatchUtil method changeSignature.

public static void changeSignature(@NotNull Match match, @NotNull PsiMethod psiMethod) {
    final PsiType expressionType = match.getChangedReturnType(psiMethod);
    if (expressionType == null && match.myChangedParams.isEmpty())
        return;
    final List<ParameterInfoImpl> newParameters = patchParams(match.myChangedParams, psiMethod);
    final ChangeSignatureProcessor csp = new ChangeSignatureProcessor(psiMethod.getProject(), psiMethod, false, null, psiMethod.getName(), expressionType != null ? expressionType : psiMethod.getReturnType(), newParameters.toArray(new ParameterInfoImpl[newParameters.size()]));
    csp.run();
}
Also used : ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 7 with ChangeSignatureProcessor

use of com.intellij.refactoring.changeSignature.ChangeSignatureProcessor in project intellij-community by JetBrains.

the class ChangeMethodSignatureFromUsageFix method performChange.

public static List<ParameterInfoImpl> performChange(final Project project, final Editor editor, final PsiFile file, final PsiMethod method, final int minUsagesNumber, final ParameterInfoImpl[] newParametersInfo, final boolean changeAllUsages, final boolean allowDelegation) {
    if (!FileModificationService.getInstance().prepareFileForWrite(method.getContainingFile()))
        return null;
    final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(project)).getFindUsagesManager();
    final FindUsagesHandler handler = findUsagesManager.getFindUsagesHandler(method, false);
    //on failure or cancel (e.g. cancel of super methods dialog)
    if (handler == null)
        return null;
    final JavaMethodFindUsagesOptions options = new JavaMethodFindUsagesOptions(project);
    options.isImplementingMethods = true;
    options.isOverridingMethods = true;
    options.isUsages = true;
    options.isSearchForTextOccurrences = false;
    final int[] usagesFound = new int[1];
    Runnable runnable = () -> {
        Processor<UsageInfo> processor = t -> ++usagesFound[0] < minUsagesNumber;
        handler.processElementUsages(method, processor, options);
    };
    String progressTitle = QuickFixBundle.message("searching.for.usages.progress.title");
    if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, progressTitle, true, project))
        return null;
    if (ApplicationManager.getApplication().isUnitTestMode() || usagesFound[0] < minUsagesNumber) {
        ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, method, false, null, method.getName(), method.getReturnType(), newParametersInfo) {

            @Override
            @NotNull
            protected UsageInfo[] findUsages() {
                return changeAllUsages ? super.findUsages() : UsageInfo.EMPTY_ARRAY;
            }

            @Override
            protected void performRefactoring(@NotNull UsageInfo[] usages) {
                CommandProcessor.getInstance().setCurrentCommandName(getCommandName());
                super.performRefactoring(usages);
            }
        };
        processor.run();
        ApplicationManager.getApplication().runWriteAction(() -> UndoUtil.markPsiFileForUndo(file));
        return Arrays.asList(newParametersInfo);
    } else {
        final List<ParameterInfoImpl> parameterInfos = newParametersInfo != null ? new ArrayList<>(Arrays.asList(newParametersInfo)) : new ArrayList<>();
        final PsiReferenceExpression refExpr = JavaTargetElementEvaluator.findReferenceExpression(editor);
        JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew(project, method, parameterInfos, allowDelegation, refExpr);
        dialog.setParameterInfos(parameterInfos);
        dialog.show();
        return dialog.isOK() ? dialog.getParameters() : null;
    }
}
Also used : FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor) CommandProcessor(com.intellij.openapi.command.CommandProcessor) Processor(com.intellij.util.Processor) JavaMethodFindUsagesOptions(com.intellij.find.findUsages.JavaMethodFindUsagesOptions) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) NotNull(org.jetbrains.annotations.NotNull) FindManagerImpl(com.intellij.find.impl.FindManagerImpl) JavaChangeSignatureDialog(com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) UsageInfo(com.intellij.usageView.UsageInfo) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 8 with ChangeSignatureProcessor

use of com.intellij.refactoring.changeSignature.ChangeSignatureProcessor in project intellij-community by JetBrains.

the class RemoveUnusedParameterFix method removeReferences.

private static void removeReferences(PsiParameter parameter) {
    PsiMethod method = (PsiMethod) parameter.getDeclarationScope();
    ChangeSignatureProcessor processor = new ChangeSignatureProcessor(parameter.getProject(), method, false, null, method.getName(), method.getReturnType(), getNewParametersInfo(method, parameter));
    processor.run();
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 9 with ChangeSignatureProcessor

use of com.intellij.refactoring.changeSignature.ChangeSignatureProcessor in project intellij-community by JetBrains.

the class ChangeSignatureBaseTest method doTest.

protected void doTest(@PsiModifier.ModifierConstant @Nullable String newVisibility, @Nullable String newName, @Nullable String newReturnType, GenParams genParams, GenExceptions genExceptions, boolean generateDelegate, boolean skipConflict) {
    String basePath = getRelativePath() + getTestName(false);
    configureByFile(basePath + ".java");
    PsiElement targetElement = TargetElementUtil.findTargetElement(getEditor(), TargetElementUtil.ELEMENT_NAME_ACCEPTED);
    assertTrue("<caret> is not on method name", targetElement instanceof PsiMethod);
    PsiMethod method = (PsiMethod) targetElement;
    PsiType newType = newReturnType != null ? myFactory.createTypeFromText(newReturnType, method) : method.getReturnType();
    new ChangeSignatureProcessor(getProject(), method, generateDelegate, newVisibility, newName != null ? newName : method.getName(), newType, genParams.genParams(method), genExceptions.genExceptions(method)) {

        @Override
        protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
            try {
                return super.preprocessUsages(refUsages);
            } catch (ConflictsInTestsException e) {
                if (skipConflict) {
                    return true;
                }
                throw e;
            }
        }
    }.run();
    checkResultByFile(basePath + "_after.java");
}
Also used : ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 10 with ChangeSignatureProcessor

use of com.intellij.refactoring.changeSignature.ChangeSignatureProcessor in project intellij-community by JetBrains.

the class ChangeSignaturePropagationTest method doTest.

private void doTest(ParameterInfoImpl[] newParameters, final ThrownExceptionInfo[] newExceptions, Set<PsiMethod> methodsToPropagateParameterChanges, Set<PsiMethod> methodsToPropagateExceptionChanges, PsiMethod primaryMethod) throws Exception {
    final String filePath = getBasePath() + getTestName(false) + ".java";
    final PsiType returnType = primaryMethod.getReturnType();
    final CanonicalTypes.Type type = returnType == null ? null : CanonicalTypes.createTypeWrapper(returnType);
    new ChangeSignatureProcessor(getProject(), primaryMethod, false, null, primaryMethod.getName(), type, generateParameterInfos(primaryMethod, newParameters), generateExceptionInfos(primaryMethod, newExceptions), methodsToPropagateParameterChanges, methodsToPropagateExceptionChanges).run();
    checkResultByFile(filePath + ".after");
}
Also used : CanonicalTypes(com.intellij.refactoring.util.CanonicalTypes) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Aggregations

ChangeSignatureProcessor (com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)14 ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)6 JavaChangeSignatureDialog (com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog)3 NotNull (org.jetbrains.annotations.NotNull)3 Project (com.intellij.openapi.project.Project)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 CreateMethodQuickFix (com.intellij.codeInsight.daemon.impl.quickfix.CreateMethodQuickFix)1 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 FindUsagesHandler (com.intellij.find.findUsages.FindUsagesHandler)1 FindUsagesManager (com.intellij.find.findUsages.FindUsagesManager)1 JavaMethodFindUsagesOptions (com.intellij.find.findUsages.JavaMethodFindUsagesOptions)1 FindManagerImpl (com.intellij.find.impl.FindManagerImpl)1 Result (com.intellij.openapi.application.Result)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 PsiMethod (com.intellij.psi.PsiMethod)1 CanonicalTypes (com.intellij.refactoring.util.CanonicalTypes)1 UsageInfo (com.intellij.usageView.UsageInfo)1