Search in sources :

Example 11 with ParameterInfoImpl

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

the class ChangeSignatureTest method testRemoveVarargParameter.

public void testRemoveVarargParameter() {
    try {
        BaseRefactoringProcessor.ConflictsInTestsException.setTestIgnore(true);
        doTest(null, null, null, new ParameterInfoImpl[] { new ParameterInfoImpl(0) }, new ThrownExceptionInfo[0], false);
    } finally {
        BaseRefactoringProcessor.ConflictsInTestsException.setTestIgnore(false);
    }
}
Also used : ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl)

Example 12 with ParameterInfoImpl

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

the class InlineParameterExpressionProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    int parameterIndex = myMethod.getParameterList().getParameterIndex(myParameter);
    final Map<PsiVariable, PsiElement> localToParamRef = new HashMap<>();
    final PsiExpression[] arguments = myMethodCall.getArgumentList().getExpressions();
    for (int i = 0; i < arguments.length; i++) {
        if (i != parameterIndex && arguments[i] instanceof PsiReferenceExpression) {
            final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) arguments[i];
            final PsiElement element = referenceExpression.resolve();
            if (element instanceof PsiLocalVariable || element instanceof PsiParameter) {
                final PsiParameter param = myMethod.getParameterList().getParameters()[i];
                final PsiExpression paramRef = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory().createExpressionFromText(param.getName(), myMethod);
                localToParamRef.put((PsiVariable) element, paramRef);
            }
        }
    }
    final List<UsageInfo> result = new ArrayList<>();
    myInitializer.accept(new JavaRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(final PsiReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            final PsiElement element = expression.resolve();
            if (element instanceof PsiLocalVariable) {
                final PsiLocalVariable localVariable = (PsiLocalVariable) element;
                final PsiElement[] elements = DefUseUtil.getDefs(myCallingBlock, localVariable, expression);
                if (elements.length == 1) {
                    PsiExpression localInitializer = null;
                    if (elements[0] instanceof PsiLocalVariable) {
                        localInitializer = ((PsiLocalVariable) elements[0]).getInitializer();
                    } else if (elements[0] instanceof PsiAssignmentExpression) {
                        localInitializer = ((PsiAssignmentExpression) elements[0]).getRExpression();
                    } else if (elements[0] instanceof PsiReferenceExpression) {
                        final PsiReferenceExpression refElement = (PsiReferenceExpression) elements[0];
                        final PsiElement parent = refElement.getParent();
                        if (parent instanceof PsiAssignmentExpression && ((PsiAssignmentExpression) parent).getLExpression() == refElement) {
                            localInitializer = ((PsiAssignmentExpression) parent).getRExpression();
                        }
                    }
                    if (localInitializer != null) {
                        final PsiElement replacement;
                        if (localToParamRef.containsKey(localVariable)) {
                            replacement = localToParamRef.get(localVariable);
                        } else {
                            replacement = replaceArgs(localToParamRef, localInitializer.copy());
                        }
                        result.add(new LocalReplacementUsageInfo(expression, replacement));
                    }
                }
            }
        }
    });
    if (!myCreateLocal) {
        for (PsiReference ref : ReferencesSearch.search(myParameter).findAll()) {
            result.add(new UsageInfo(ref));
        }
    }
    final PsiParameter[] parameters = myMethod.getParameterList().getParameters();
    final List<ParameterInfoImpl> psiParameters = new ArrayList<>();
    int paramIdx = 0;
    final String paramName = myParameter.getName();
    for (PsiParameter param : parameters) {
        if (!Comparing.strEqual(paramName, param.getName())) {
            psiParameters.add(new ParameterInfoImpl(paramIdx, param.getName(), param.getType()));
        }
        paramIdx++;
    }
    PsiType returnType = myMethod.getReturnType();
    myChangeInfo = new JavaChangeInfoImpl(VisibilityUtil.getVisibilityModifier(myMethod.getModifierList()), myMethod, myMethod.getName(), returnType != null ? CanonicalTypes.createTypeWrapper(returnType) : null, psiParameters.toArray(new ParameterInfoImpl[psiParameters.size()]), null, false, Collections.emptySet(), Collections.emptySet());
    myChangeSignatureUsages = ChangeSignatureProcessorBase.findUsages(myChangeInfo);
    final UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
    return UsageViewUtil.removeDuplicatedUsages(usageInfos);
}
Also used : UsageInfo(com.intellij.usageView.UsageInfo) JavaChangeInfoImpl(com.intellij.refactoring.changeSignature.JavaChangeInfoImpl) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with ParameterInfoImpl

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

the class CreateConstructorParameterFromFieldFix method addParameterToConstructor.

private static boolean addParameterToConstructor(final Project project, final PsiFile file, final Editor editor, final PsiMethod constructor, final PsiField[] fields, final List<SmartPsiElementPointer<PsiElement>> cleanupElements) throws IncorrectOperationException {
    final PsiParameterList parameterList = constructor.getParameterList();
    final PsiParameter[] parameters = parameterList.getParameters();
    ParameterInfoImpl[] newParamInfos = new ParameterInfoImpl[parameters.length + fields.length];
    final List<PsiVariable> params = new ArrayList<>(Arrays.asList(parameters));
    Collections.addAll(params, fields);
    Collections.sort(params, new FieldParameterComparator(parameterList));
    int i = 0;
    final Map<PsiField, String> usedFields = new HashMap<>();
    final MultiMap<PsiType, PsiVariable> types = new MultiMap<>();
    for (PsiVariable param : params) {
        types.putValue(param.getType(), param);
    }
    final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
    final boolean preferLongerNames = settings.PREFER_LONGER_NAMES;
    for (PsiVariable param : params) {
        final PsiType paramType = param.getType();
        if (param instanceof PsiParameter) {
            newParamInfos[i++] = new ParameterInfoImpl(parameterList.getParameterIndex((PsiParameter) param), param.getName(), paramType, param.getName());
        } else {
            try {
                settings.PREFER_LONGER_NAMES = preferLongerNames || types.get(paramType).size() > 1;
                final String uniqueParameterName = getUniqueParameterName(parameters, param, usedFields);
                usedFields.put((PsiField) param, uniqueParameterName);
                newParamInfos[i++] = new ParameterInfoImpl(-1, uniqueParameterName, paramType, uniqueParameterName);
            } finally {
                settings.PREFER_LONGER_NAMES = preferLongerNames;
            }
        }
    }
    final SmartPointerManager manager = SmartPointerManager.getInstance(project);
    final SmartPsiElementPointer constructorPointer = manager.createSmartPsiElementPointer(constructor);
    final PsiMethod fromText = JavaPsiFacade.getElementFactory(project).createMethodFromText(createDummyMethod(constructor, newParamInfos), constructor);
    final PsiClass containingClass = constructor.getContainingClass();
    if (containingClass == null)
        return false;
    final int minUsagesNumber = containingClass.findMethodsBySignature(fromText, false).length > 0 ? 0 : 1;
    final List<ParameterInfoImpl> parameterInfos = ChangeMethodSignatureFromUsageFix.performChange(project, editor, file, constructor, minUsagesNumber, newParamInfos, true, true);
    if (parameterInfos == null)
        return false;
    final ParameterInfoImpl[] resultParams = parameterInfos.toArray(new ParameterInfoImpl[parameterInfos.size()]);
    return ApplicationManager.getApplication().runWriteAction(new Computable<Boolean>() {

        @Override
        public Boolean compute() {
            return doCreate(project, editor, parameters, constructorPointer, resultParams, usedFields, cleanupElements);
        }
    });
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl)

Example 14 with ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl 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 15 with ParameterInfoImpl

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

the class RemoveUnusedParameterFix method getNewParametersInfo.

@NotNull
public static ParameterInfoImpl[] getNewParametersInfo(PsiMethod method, PsiParameter parameterToRemove) {
    List<ParameterInfoImpl> result = new ArrayList<>();
    PsiParameter[] parameters = method.getParameterList().getParameters();
    for (int i = 0; i < parameters.length; i++) {
        PsiParameter parameter = parameters[i];
        if (!Comparing.equal(parameter, parameterToRemove)) {
            result.add(new ParameterInfoImpl(i, parameter.getName(), parameter.getType()));
        }
    }
    return result.toArray(new ParameterInfoImpl[result.size()]);
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) ArrayList(java.util.ArrayList) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)34 ArrayList (java.util.ArrayList)10 ChangeSignatureProcessor (com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)6 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)4 JavaChangeSignatureDialog (com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog)4 HashSet (java.util.HashSet)3 JavaChangeInfoImpl (com.intellij.refactoring.changeSignature.JavaChangeInfoImpl)2 JavaMethodDescriptor (com.intellij.refactoring.changeSignature.JavaMethodDescriptor)2 ThrownExceptionInfo (com.intellij.refactoring.changeSignature.ThrownExceptionInfo)2 IntroduceParameterObjectProcessor (com.intellij.refactoring.introduceParameterObject.IntroduceParameterObjectProcessor)2 JavaIntroduceParameterObjectClassDescriptor (com.intellij.refactoring.introduceparameterobject.JavaIntroduceParameterObjectClassDescriptor)2 UsageInfo (com.intellij.usageView.UsageInfo)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)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