Search in sources :

Example 1 with JavaChangeInfoImpl

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

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

the class MakeMethodStaticProcessor method changeSelf.

protected void changeSelf(PsiElementFactory factory, UsageInfo[] usages) throws IncorrectOperationException {
    final MethodJavaDocHelper javaDocHelper = new MethodJavaDocHelper(myMember);
    PsiParameterList paramList = myMember.getParameterList();
    PsiElement addParameterAfter = null;
    PsiDocTag anchor = null;
    List<PsiType> addedTypes = new ArrayList<>();
    final PsiClass containingClass = myMember.getContainingClass();
    LOG.assertTrue(containingClass != null);
    if (mySettings.isDelegate()) {
        List<ParameterInfoImpl> params = new ArrayList<>();
        PsiParameter[] parameters = myMember.getParameterList().getParameters();
        if (mySettings.isMakeClassParameter()) {
            params.add(new ParameterInfoImpl(-1, mySettings.getClassParameterName(), factory.createType(containingClass, PsiSubstitutor.EMPTY), "this"));
        }
        if (mySettings.isMakeFieldParameters()) {
            for (Settings.FieldParameter parameter : mySettings.getParameterOrderList()) {
                params.add(new ParameterInfoImpl(-1, mySettings.getClassParameterName(), parameter.type, parameter.field.getName()));
            }
        }
        for (int i = 0; i < parameters.length; i++) {
            params.add(new ParameterInfoImpl(i));
        }
        final PsiType returnType = myMember.getReturnType();
        LOG.assertTrue(returnType != null);
        JavaChangeSignatureUsageProcessor.generateDelegate(new JavaChangeInfoImpl(VisibilityUtil.getVisibilityModifier(myMember.getModifierList()), myMember, myMember.getName(), CanonicalTypes.createTypeWrapper(returnType), params.toArray(new ParameterInfoImpl[params.size()]), new ThrownExceptionInfo[0], false, Collections.<PsiMethod>emptySet(), Collections.<PsiMethod>emptySet()));
    }
    if (mySettings.isMakeClassParameter()) {
        // Add parameter for object
        PsiType parameterType = factory.createType(containingClass, PsiSubstitutor.EMPTY);
        addedTypes.add(parameterType);
        final String classParameterName = mySettings.getClassParameterName();
        PsiParameter parameter = factory.createParameter(classParameterName, parameterType);
        if (makeClassParameterFinal(usages)) {
            PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, true);
        }
        addParameterAfter = paramList.addAfter(parameter, null);
        anchor = javaDocHelper.addParameterAfter(classParameterName, anchor);
    }
    if (mySettings.isMakeFieldParameters()) {
        List<Settings.FieldParameter> parameters = mySettings.getParameterOrderList();
        for (Settings.FieldParameter fieldParameter : parameters) {
            final PsiType fieldParameterType = fieldParameter.field.getType();
            final PsiParameter parameter = factory.createParameter(fieldParameter.name, fieldParameterType);
            addedTypes.add(fieldParameterType);
            if (makeFieldParameterFinal(fieldParameter.field, usages)) {
                PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, true);
            }
            addParameterAfter = paramList.addAfter(parameter, addParameterAfter);
            anchor = javaDocHelper.addParameterAfter(fieldParameter.name, anchor);
        }
    }
    makeStatic(myMember);
    if (myAdditionalMethods != null) {
        for (PsiMethod method : myAdditionalMethods) {
            makeStatic(method);
        }
    }
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ArrayList(java.util.ArrayList) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) ThrownExceptionInfo(com.intellij.refactoring.changeSignature.ThrownExceptionInfo) MethodJavaDocHelper(com.intellij.refactoring.util.javadoc.MethodJavaDocHelper) JavaChangeInfoImpl(com.intellij.refactoring.changeSignature.JavaChangeInfoImpl)

Aggregations

JavaChangeInfoImpl (com.intellij.refactoring.changeSignature.JavaChangeInfoImpl)2 ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)2 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)1 ThrownExceptionInfo (com.intellij.refactoring.changeSignature.ThrownExceptionInfo)1 MethodJavaDocHelper (com.intellij.refactoring.util.javadoc.MethodJavaDocHelper)1 UsageInfo (com.intellij.usageView.UsageInfo)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1