Search in sources :

Example 1 with ChangeSignatureProcessor

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

the class CreateParameterFromUsageFix method invokeImpl.

@Override
protected void invokeImpl(PsiClass targetClass) {
    TransactionGuard.getInstance().submitTransactionLater(targetClass.getProject(), () -> {
        if (!myReferenceExpression.isValid())
            return;
        if (CreateFromUsageUtils.isValidReference(myReferenceExpression, false))
            return;
        final Project project = myReferenceExpression.getProject();
        PsiType[] expectedTypes = CreateFromUsageUtils.guessType(myReferenceExpression, false);
        PsiType type = expectedTypes[0];
        final String varName = myReferenceExpression.getReferenceName();
        PsiMethod method = PsiTreeUtil.getParentOfType(myReferenceExpression, PsiMethod.class);
        LOG.assertTrue(method != null);
        method = IntroduceParameterHandler.chooseEnclosingMethod(method);
        if (method == null)
            return;
        method = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
        if (method == null)
            return;
        final List<ParameterInfoImpl> parameterInfos = new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
        ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, varName, type, varName, false);
        if (!method.isVarArgs()) {
            parameterInfos.add(parameterInfo);
        } else {
            parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
        }
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            ParameterInfoImpl[] array = parameterInfos.toArray(new ParameterInfoImpl[parameterInfos.size()]);
            String modifier = PsiUtil.getAccessModifier(PsiUtil.getAccessLevel(method.getModifierList()));
            ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, method, false, modifier, method.getName(), method.getReturnType(), array);
            processor.run();
        } else {
            try {
                JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew(project, method, parameterInfos, true, myReferenceExpression);
                dialog.setParameterInfos(parameterInfos);
                if (dialog.showAndGet()) {
                    for (ParameterInfoImpl info : parameterInfos) {
                        if (info.getOldIndex() == -1) {
                            final String newParamName = info.getName();
                            if (!Comparing.strEqual(varName, newParamName)) {
                                final PsiExpression newExpr = JavaPsiFacade.getElementFactory(project).createExpressionFromText(newParamName, method);
                                new WriteCommandAction(project) {

                                    @Override
                                    protected void run(@NotNull Result result) throws Throwable {
                                        final PsiReferenceExpression[] refs = CreateFromUsageUtils.collectExpressions(myReferenceExpression, PsiMember.class, PsiFile.class);
                                        for (PsiReferenceExpression ref : refs) {
                                            ref.replace(newExpr.copy());
                                        }
                                    }
                                }.execute();
                            }
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ArrayList(java.util.ArrayList) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) JavaChangeSignatureDialog(com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 2 with ChangeSignatureProcessor

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

the class MethodParameterFix method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull final PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final PsiMethod myMethod = (PsiMethod) startElement;
    if (!FileModificationService.getInstance().prepareFileForWrite(myMethod.getContainingFile()))
        return;
    try {
        PsiMethod method = myMethod;
        if (myFixWholeHierarchy) {
            method = myMethod.findDeepestSuperMethod();
            if (method == null)
                method = myMethod;
        }
        final PsiMethod finalMethod = method;
        ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, finalMethod, false, null, finalMethod.getName(), finalMethod.getReturnType(), getNewParametersInfo(finalMethod));
        processor.run();
        UndoUtil.markPsiFileForUndo(file);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 3 with ChangeSignatureProcessor

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

the class MethodReturnTypeFix method changeReturnType.

@NotNull
private List<PsiMethod> changeReturnType(final PsiMethod method, @NotNull final PsiType returnType) {
    final PsiMethod[] methods = getChangeRoots(method, returnType);
    if (methods == null) {
        // canceled
        return Collections.emptyList();
    }
    final MethodSignatureChangeVisitor methodSignatureChangeVisitor = new MethodSignatureChangeVisitor();
    for (PsiMethod targetMethod : methods) {
        methodSignatureChangeVisitor.addBase(targetMethod);
        ChangeSignatureProcessor processor = new UsagesAwareChangeSignatureProcessor(method.getProject(), targetMethod, false, null, myName, returnType, RemoveUnusedParameterFix.getNewParametersInfo(targetMethod, null), methodSignatureChangeVisitor);
        processor.run();
    }
    return methodSignatureChangeVisitor.getAffectedMethods();
}
Also used : ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ChangeSignatureProcessor

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

the class SuperMethodReturnFix method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
    if (!FileModificationService.getInstance().prepareFileForWrite(mySuperMethod.getContainingFile()))
        return;
    ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, mySuperMethod, false, null, mySuperMethod.getName(), mySuperMethodType, ParameterInfoImpl.fromMethod(mySuperMethod));
    processor.run();
}
Also used : ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 5 with ChangeSignatureProcessor

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

the class VariableTypeFix method changeMethodSignatureIfNeeded.

private boolean changeMethodSignatureIfNeeded(PsiVariable myVariable) {
    if (myVariable instanceof PsiParameter) {
        final PsiElement scope = ((PsiParameter) myVariable).getDeclarationScope();
        if (scope instanceof PsiMethod) {
            final PsiMethod method = (PsiMethod) scope;
            final PsiMethod psiMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
            if (psiMethod == null)
                return true;
            final int parameterIndex = method.getParameterList().getParameterIndex((PsiParameter) myVariable);
            if (!FileModificationService.getInstance().prepareFileForWrite(psiMethod.getContainingFile()))
                return true;
            final ArrayList<ParameterInfoImpl> infos = new ArrayList<>();
            int i = 0;
            for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) {
                final boolean changeType = i == parameterIndex;
                infos.add(new ParameterInfoImpl(i++, parameter.getName(), changeType ? getReturnType() : parameter.getType()));
            }
            if (!ApplicationManager.getApplication().isUnitTestMode()) {
                final JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(psiMethod.getProject(), psiMethod, false, myVariable);
                dialog.setParameterInfos(infos);
                dialog.show();
            } else {
                ChangeSignatureProcessor processor = new ChangeSignatureProcessor(psiMethod.getProject(), psiMethod, false, null, psiMethod.getName(), psiMethod.getReturnType(), infos.toArray(new ParameterInfoImpl[infos.size()]));
                processor.run();
            }
            return true;
        }
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) JavaChangeSignatureDialog(com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) 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