Search in sources :

Example 16 with VariableData

use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.

the class ExtractMethodSignatureSuggester method inlineSameArguments.

private void inlineSameArguments(PsiMethod method, List<PsiExpression> copies, InputVariables variables, List<Match> duplicates) {
    final List<VariableData> variableDatum = variables.getInputVariables();
    final Map<PsiVariable, PsiExpression> toInline = new HashMap<>();
    final int strongParamsCound = method.getParameterList().getParametersCount();
    for (int i = strongParamsCound; i < variableDatum.size(); i++) {
        VariableData variableData = variableDatum.get(i);
        final THashSet<PsiExpression> map = new THashSet<>(ourEquivalenceStrategy);
        if (!collectParamValues(duplicates, variableData, map)) {
            continue;
        }
        final PsiExpression currentExpression = copies.get(i - strongParamsCound);
        map.add(currentExpression);
        if (map.size() == 1) {
            toInline.put(variableData.variable, currentExpression);
        }
    }
    if (!toInline.isEmpty()) {
        copies.removeAll(toInline.values());
        inlineArgumentsInMethodBody(toInline);
        removeRedundantParametersFromMethodSignature(toInline);
    }
    removeUnusedStongParams(strongParamsCound);
}
Also used : THashMap(gnu.trove.THashMap) VariableData(com.intellij.refactoring.util.VariableData) THashSet(gnu.trove.THashSet)

Example 17 with VariableData

use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.

the class InputVariables method wrapInputVariables.

public ArrayList<VariableData> wrapInputVariables(final List<? extends PsiVariable> inputVariables) {
    UniqueNameGenerator nameGenerator = new UniqueNameGenerator();
    final ArrayList<VariableData> inputData = new ArrayList<>(inputVariables.size());
    for (PsiVariable var : inputVariables) {
        final String defaultName = getParameterName(var);
        String name = nameGenerator.generateUniqueName(defaultName);
        PsiType type = GenericsUtil.getVariableTypeByExpressionType(var.getType());
        if (type instanceof PsiEllipsisType) {
            type = ((PsiEllipsisType) type).toArrayType();
        }
        final Map<PsiCodeBlock, PsiType> casts = new HashMap<>();
        for (PsiReference reference : ReferencesSearch.search(var, myScope)) {
            final PsiElement element = reference.getElement();
            final PsiElement parent = element.getParent();
            final PsiCodeBlock block = PsiTreeUtil.getParentOfType(parent, PsiCodeBlock.class);
            if (parent instanceof PsiTypeCastExpression) {
                final PsiType currentType = casts.get(block);
                final PsiType castType = ((PsiTypeCastExpression) parent).getType();
                casts.put(block, casts.containsKey(block) && currentType == null ? null : getBroaderType(currentType, castType));
            } else {
                casts.put(block, null);
            }
        }
        if (!casts.containsValue(null)) {
            PsiType currentType = null;
            for (PsiType psiType : casts.values()) {
                currentType = getBroaderType(currentType, psiType);
                if (currentType == null) {
                    break;
                }
            }
            if (currentType != null) {
                currentType = checkTopLevelInstanceOf(currentType);
                if (currentType != null) {
                    type = currentType;
                }
            }
        }
        VariableData data = new VariableData(var, type);
        data.name = name;
        data.passAsParameter = true;
        inputData.add(data);
        if (myFoldingAvailable)
            myFolding.isParameterFoldable(data, myScope, inputVariables, nameGenerator, defaultName);
    }
    if (myFoldingAvailable) {
        final Set<VariableData> toDelete = new HashSet<>();
        for (int i = inputData.size() - 1; i >= 0; i--) {
            final VariableData data = inputData.get(i);
            if (myFolding.isParameterSafeToDelete(data, myScope)) {
                toDelete.add(data);
            }
        }
        inputData.removeAll(toDelete);
    }
    if (myPassFields && myUsedInstanceFields != null) {
        for (PsiField var : myUsedInstanceFields) {
            final VariableData data = new VariableData(var, var.getType());
            data.name = nameGenerator.generateUniqueName(getParameterName(var));
            data.passAsParameter = true;
            inputData.add(data);
        }
    }
    return inputData;
}
Also used : VariableData(com.intellij.refactoring.util.VariableData) UniqueNameGenerator(com.intellij.util.text.UniqueNameGenerator)

Example 18 with VariableData

use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.

the class InputVariables method annotateWithParameter.

public void annotateWithParameter(PsiJavaCodeReferenceElement reference) {
    for (VariableData data : myInputVariables) {
        final PsiElement element = reference.resolve();
        if (data.variable.equals(element)) {
            PsiType type = data.variable.getType();
            final PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(reference, PsiMethodCallExpression.class);
            if (methodCallExpression != null) {
                int idx = ArrayUtil.find(methodCallExpression.getArgumentList().getExpressions(), reference);
                if (idx > -1) {
                    final PsiMethod psiMethod = methodCallExpression.resolveMethod();
                    if (psiMethod != null) {
                        final PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
                        if (idx >= parameters.length) {
                            //vararg parameter
                            idx = parameters.length - 1;
                            if (idx >= 0) {
                                //incomplete code
                                type = parameters[idx].getType();
                            }
                        }
                        if (type instanceof PsiEllipsisType) {
                            type = ((PsiEllipsisType) type).getComponentType();
                        }
                    }
                }
            }
            if (!myFoldingAvailable || !myFolding.annotateWithParameter(data, reference)) {
                reference.putUserData(DuplicatesFinder.PARAMETER, Pair.create(data.variable, type));
            }
        }
    }
}
Also used : VariableData(com.intellij.refactoring.util.VariableData)

Example 19 with VariableData

use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.

the class MakeStaticUtil method collectVariableData.

public static boolean collectVariableData(PsiMember member, InternalUsageInfo[] internalUsages, ArrayList<VariableData> variableDatum) {
    HashSet<PsiField> reported = new HashSet<>();
    HashSet<PsiField> accessedForWriting = new HashSet<>();
    boolean needClassParameter = false;
    for (InternalUsageInfo usage : internalUsages) {
        final PsiElement referencedElement = usage.getReferencedElement();
        if (usage.isWriting()) {
            accessedForWriting.add((PsiField) referencedElement);
            needClassParameter = true;
        } else if (referencedElement instanceof PsiField) {
            PsiField field = (PsiField) referencedElement;
            reported.add(field);
        } else {
            needClassParameter = true;
        }
    }
    final ArrayList<PsiField> psiFields = new ArrayList<>(reported);
    Collections.sort(psiFields, (psiField, psiField1) -> psiField.getName().compareTo(psiField1.getName()));
    for (final PsiField field : psiFields) {
        if (accessedForWriting.contains(field))
            continue;
        VariableData data = new VariableData(field);
        JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(member.getProject());
        String name = field.getName();
        name = codeStyleManager.variableNameToPropertyName(name, VariableKind.FIELD);
        name = codeStyleManager.propertyNameToVariableName(name, VariableKind.PARAMETER);
        name = RefactoringUtil.suggestUniqueVariableName(name, member, field);
        data.name = name;
        data.passAsParameter = true;
        variableDatum.add(data);
    }
    return needClassParameter;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) ArrayList(java.util.ArrayList) VariableData(com.intellij.refactoring.util.VariableData) HashSet(java.util.HashSet)

Example 20 with VariableData

use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.

the class MakeClassStaticTest method performWithFields.

private void performWithFields() throws Exception {
    configureByFile(TEST_ROOT + getTestName(false) + ".java");
    PsiElement element = TargetElementUtil.findTargetElement(myEditor, TargetElementUtil.ELEMENT_NAME_ACCEPTED);
    assertTrue(element instanceof PsiClass);
    PsiClass aClass = (PsiClass) element;
    final ArrayList<VariableData> parametersForFields = new ArrayList<>();
    final boolean addClassParameter = MakeStaticUtil.buildVariableData(aClass, parametersForFields);
    new MakeClassStaticProcessor(getProject(), aClass, new Settings(true, addClassParameter ? "anObject" : null, parametersForFields.toArray(new VariableData[parametersForFields.size()]))).run();
    checkResultByFile(TEST_ROOT + getTestName(false) + "_after.java");
}
Also used : PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) VariableData(com.intellij.refactoring.util.VariableData) MakeClassStaticProcessor(com.intellij.refactoring.makeStatic.MakeClassStaticProcessor) PsiElement(com.intellij.psi.PsiElement) Settings(com.intellij.refactoring.makeStatic.Settings) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings)

Aggregations

VariableData (com.intellij.refactoring.util.VariableData)20 ArrayList (java.util.ArrayList)4 PsiElement (com.intellij.psi.PsiElement)3 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)2 Settings (com.intellij.refactoring.makeStatic.Settings)2 MultiMap (com.intellij.util.containers.MultiMap)2 THashMap (gnu.trove.THashMap)2 NullableNotNullManager (com.intellij.codeInsight.NullableNotNullManager)1 Editor (com.intellij.openapi.editor.Editor)1 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)1 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)1 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiExpression (com.intellij.psi.PsiExpression)1 PsiFile (com.intellij.psi.PsiFile)1 PsiMethod (com.intellij.psi.PsiMethod)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)1 ExtractMethodProcessor (com.intellij.refactoring.extractMethod.ExtractMethodProcessor)1 MakeClassStaticProcessor (com.intellij.refactoring.makeStatic.MakeClassStaticProcessor)1