Search in sources :

Example 1 with FixableUsageInfo

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

the class IntroduceParameterObjectProcessor method findUsages.

@Override
protected void findUsages(@NotNull List<FixableUsageInfo> usages) {
    if (myClassDescriptor.isUseExistingClass()) {
        myClassDescriptor.setExistingClassCompatibleConstructor(myClassDescriptor.findCompatibleConstructorInExistingClass(myMethod));
    }
    List<PsiNamedElement> methodHierarchy = new ArrayList<>();
    methodHierarchy.add(myMethod);
    for (UsageInfo info : ChangeSignatureProcessorBase.findUsages(myChangeInfo)) {
        if (info instanceof OverriderMethodUsageInfo) {
            methodHierarchy.add(((OverriderMethodUsageInfo) info).getOverridingMethod());
        }
        usages.add(new ChangeSignatureUsageWrapper(info));
    }
    final P[] paramsToMerge = myClassDescriptor.getParamsToMerge();
    for (PsiElement element : methodHierarchy) {
        final IntroduceParameterObjectDelegate delegate = IntroduceParameterObjectDelegate.findDelegate(element);
        if (delegate != null) {
            for (int i = 0; i < paramsToMerge.length; i++) {
                ReadWriteAccessDetector.Access access = delegate.collectInternalUsages(usages, (PsiNamedElement) element, myClassDescriptor, paramsToMerge[i], myMergedParameterInfo.getName());
                if (myAccessors[i] == null || access == ReadWriteAccessDetector.Access.Write) {
                    myAccessors[i] = access;
                }
            }
        }
    }
    myDelegate.collectUsagesToGenerateMissedFieldAccessors(usages, myMethod, myClassDescriptor, myAccessors);
    myDelegate.collectAdditionalFixes(usages, myMethod, myClassDescriptor);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) ReadWriteAccessDetector(com.intellij.codeInsight.highlighting.ReadWriteAccessDetector) UsageInfo(com.intellij.usageView.UsageInfo) OverriderMethodUsageInfo(com.intellij.refactoring.changeSignature.OverriderMethodUsageInfo) FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo) OverriderMethodUsageInfo(com.intellij.refactoring.changeSignature.OverriderMethodUsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 2 with FixableUsageInfo

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

the class ExtractEnumProcessor method findEnumConstantConflicts.

public void findEnumConstantConflicts(final Ref<UsageInfo[]> refUsages) {
    if (hasUsages2Migrate()) {
        final List<UsageInfo> resolvableConflicts = new ArrayList<>();
        for (UsageInfo failedUsage : myTypeMigrationProcessor.getLabeler().getFailedUsages()) {
            final PsiElement element = failedUsage.getElement();
            if (element instanceof PsiReferenceExpression) {
                resolvableConflicts.add(new FixableUsageInfo(element) {

                    @Override
                    public void fixUsage() throws IncorrectOperationException {
                        final PsiReferenceExpression expression = (PsiReferenceExpression) element;
                        final String link = GenerateMembersUtil.suggestGetterName("value", expression.getType(), myProject) + "()";
                        MutationUtils.replaceExpression(expression.getReferenceName() + "." + link, expression);
                    }
                });
            } else if (element != null) {
                resolvableConflicts.add(new ConflictUsageInfo(element, null));
            }
        }
        if (!resolvableConflicts.isEmpty()) {
            final List<UsageInfo> usageInfos = new ArrayList<>(Arrays.asList(refUsages.get()));
            for (Iterator<UsageInfo> iterator = resolvableConflicts.iterator(); iterator.hasNext(); ) {
                final UsageInfo conflict = iterator.next();
                for (UsageInfo usageInfo : usageInfos) {
                    if (conflict.getElement() == usageInfo.getElement()) {
                        iterator.remove();
                        break;
                    }
                }
            }
            resolvableConflicts.addAll(0, usageInfos);
            refUsages.set(resolvableConflicts.toArray(new UsageInfo[resolvableConflicts.size()]));
        }
    }
}
Also used : FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo) IncorrectOperationException(com.intellij.util.IncorrectOperationException) UsageInfo(com.intellij.usageView.UsageInfo) FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo)

Example 3 with FixableUsageInfo

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

the class ExtractEnumProcessor method findEnumConstantUsages.

public List<FixableUsageInfo> findEnumConstantUsages(List<FixableUsageInfo> fieldUsages) {
    final List<FixableUsageInfo> result = new ArrayList<>();
    if (!myEnumConstants.isEmpty()) {
        final Set<PsiSwitchStatement> switchStatements = new HashSet<>();
        for (UsageInfo usage : fieldUsages) {
            if (usage instanceof ReplaceStaticVariableAccess) {
                final PsiElement element = usage.getElement();
                final PsiSwitchStatement switchStatement = PsiTreeUtil.getParentOfType(element, PsiSwitchStatement.class);
                if (switchStatement != null) {
                    switchStatements.add(switchStatement);
                }
            }
        }
        final PsiConstantEvaluationHelper evaluationHelper = JavaPsiFacade.getInstance(myProject).getConstantEvaluationHelper();
        final Set<Object> enumValues = new HashSet<>();
        for (PsiField enumConstant : myEnumConstants) {
            enumValues.add(evaluationHelper.computeConstantExpression(enumConstant.getInitializer()));
        }
        final PsiType enumValueType = myEnumConstants.get(0).getType();
        for (PsiSwitchStatement switchStatement : switchStatements) {
            final PsiStatement errStatement = EnumConstantsUtil.isEnumSwitch(switchStatement, enumValueType, enumValues);
            if (errStatement != null) {
                String description = null;
                if (errStatement instanceof PsiSwitchLabelStatement) {
                    final PsiExpression caseValue = ((PsiSwitchLabelStatement) errStatement).getCaseValue();
                    if (caseValue != null) {
                        description = caseValue.getText() + " can not be replaced with enum";
                    }
                }
                result.add(new ConflictUsageInfo(errStatement, description));
            } else {
                final PsiExpression expression = switchStatement.getExpression();
                if (expression instanceof PsiReferenceExpression) {
                    final PsiElement element = ((PsiReferenceExpression) expression).resolve();
                    if (element != null) {
                        if (!element.getManager().isInProject(element)) {
                            result.add(new ConflictUsageInfo(expression, StringUtil.capitalize(RefactoringUIUtil.getDescription(element, false)) + " is out of project"));
                        }
                    }
                } else {
                    result.add(new ConflictUsageInfo(expression, null));
                }
            }
        }
        final TypeMigrationRules rules = new TypeMigrationRules();
        rules.addConversionDescriptor(new EnumTypeConversionRule(myEnumConstants));
        rules.setBoundScope(GlobalSearchScope.projectScope(myProject));
        myTypeMigrationProcessor = new TypeMigrationProcessor(myProject, PsiUtilCore.toPsiElementArray(myEnumConstants), Functions.<PsiElement, PsiType>constant(JavaPsiFacade.getElementFactory(myProject).createType(myClass)), rules);
        for (UsageInfo usageInfo : myTypeMigrationProcessor.findUsages()) {
            final PsiElement migrateElement = usageInfo.getElement();
            if (migrateElement instanceof PsiField) {
                final PsiField enumConstantField = (PsiField) migrateElement;
                if (enumConstantField.hasModifierProperty(PsiModifier.STATIC) && enumConstantField.hasModifierProperty(PsiModifier.FINAL) && enumConstantField.hasInitializer() && !myEnumConstants.contains(enumConstantField)) {
                    continue;
                }
            }
            result.add(new EnumTypeMigrationUsageInfo(usageInfo));
        }
    }
    return result;
}
Also used : TypeMigrationRules(com.intellij.refactoring.typeMigration.TypeMigrationRules) FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo) ReplaceStaticVariableAccess(com.intellij.refactoring.extractclass.usageInfo.ReplaceStaticVariableAccess) TypeMigrationProcessor(com.intellij.refactoring.typeMigration.TypeMigrationProcessor) UsageInfo(com.intellij.usageView.UsageInfo) FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo)

Example 4 with FixableUsageInfo

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

the class InlineSuperClassRefactoringProcessor method performRefactoring.

protected void performRefactoring(@NotNull final UsageInfo[] usages) {
    try {
        final UsageInfo[] infos = ContainerUtil.map2Array(myTargetClasses, UsageInfo.class, UsageInfo::new);
        new PushDownProcessor<>(mySuperClass, Arrays.asList(myMemberInfos), new DocCommentPolicy(myPolicy)).pushDownToClasses(infos);
        CommonRefactoringUtil.sortDepthFirstRightLeftOrder(usages);
        for (UsageInfo usageInfo : usages) {
            if (!(usageInfo instanceof ReplaceExtendsListUsageInfo || usageInfo instanceof RemoveImportUsageInfo)) {
                try {
                    ((FixableUsageInfo) usageInfo).fixUsage();
                } catch (IncorrectOperationException e) {
                    LOG.info(e);
                }
            }
        }
        replaceInnerTypeUsages();
        //postpone broken hierarchy
        for (UsageInfo usage : usages) {
            if (usage instanceof ReplaceExtendsListUsageInfo || usage instanceof RemoveImportUsageInfo) {
                ((FixableUsageInfo) usage).fixUsage();
            }
        }
        //delete the class if all refs replaced
        if (myCurrentInheritor == null) {
            mySuperClass.delete();
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo) IncorrectOperationException(com.intellij.util.IncorrectOperationException) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy) UsageInfo(com.intellij.usageView.UsageInfo) FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo)

Example 5 with FixableUsageInfo

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

the class IntroduceParameterObjectProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    final UsageInfo[] usageInfos = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    myDelegate.collectConflicts(conflicts, usageInfos, myMethod, myClassDescriptor);
    List<UsageInfo> changeSignatureUsages = new ArrayList<>();
    Set<PsiFile> filesWithUsages = new HashSet<>();
    for (UsageInfo usageInfo : usageInfos) {
        if (usageInfo instanceof ChangeSignatureUsageWrapper) {
            final UsageInfo info = ((ChangeSignatureUsageWrapper) usageInfo).getInfo();
            final PsiElement element = info.getElement();
            if (element != null && IntroduceParameterObjectDelegate.findDelegate(element) == null) {
                final PsiFile containingFile = element.getContainingFile();
                if (filesWithUsages.add(containingFile)) {
                    conflicts.putValue(element, "Method is overridden in a language that doesn't support this refactoring: " + containingFile.getName());
                }
            }
            changeSignatureUsages.add(info);
        } else if (usageInfo instanceof FixableUsageInfo) {
            final String conflictMessage = ((FixableUsageInfo) usageInfo).getConflictMessage();
            if (conflictMessage != null) {
                conflicts.putValue(usageInfo.getElement(), conflictMessage);
            }
        }
    }
    ChangeSignatureProcessorBase.collectConflictsFromExtensions(new Ref<>(changeSignatureUsages.toArray(new UsageInfo[changeSignatureUsages.size()])), conflicts, myChangeInfo);
    return showConflicts(conflicts, usageInfos);
}
Also used : FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo) ArrayList(java.util.ArrayList) MultiMap(com.intellij.util.containers.MultiMap) PsiFile(com.intellij.psi.PsiFile) UsageInfo(com.intellij.usageView.UsageInfo) OverriderMethodUsageInfo(com.intellij.refactoring.changeSignature.OverriderMethodUsageInfo) FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet)

Aggregations

FixableUsageInfo (com.intellij.refactoring.util.FixableUsageInfo)5 UsageInfo (com.intellij.usageView.UsageInfo)5 PsiElement (com.intellij.psi.PsiElement)2 OverriderMethodUsageInfo (com.intellij.refactoring.changeSignature.OverriderMethodUsageInfo)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 ArrayList (java.util.ArrayList)2 ReadWriteAccessDetector (com.intellij.codeInsight.highlighting.ReadWriteAccessDetector)1 PsiFile (com.intellij.psi.PsiFile)1 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 ReplaceStaticVariableAccess (com.intellij.refactoring.extractclass.usageInfo.ReplaceStaticVariableAccess)1 TypeMigrationProcessor (com.intellij.refactoring.typeMigration.TypeMigrationProcessor)1 TypeMigrationRules (com.intellij.refactoring.typeMigration.TypeMigrationRules)1 DocCommentPolicy (com.intellij.refactoring.util.DocCommentPolicy)1 MultiMap (com.intellij.util.containers.MultiMap)1 HashSet (java.util.HashSet)1