Search in sources :

Example 1 with TypeMigrationProcessor

use of com.intellij.refactoring.typeMigration.TypeMigrationProcessor in project intellij-community by JetBrains.

the class TypeMigrationDialog method doAction.

@Override
protected void doAction() {
    if (myScopeChooserCombo.getSelectedScope() == null) {
        Messages.showErrorDialog("Scope is not chosen", "Error");
        return;
    }
    FindSettings.getInstance().setDefaultScopeName(myScopeChooserCombo.getSelectedScopeName());
    if (myRules == null) {
        myRules = new TypeMigrationRules();
        myRules.setBoundScope(myScopeChooserCombo.getSelectedScope());
    }
    invokeRefactoring(new TypeMigrationProcessor(myProject, myRoots, getMigrationTypeFunction(), myRules));
}
Also used : TypeMigrationRules(com.intellij.refactoring.typeMigration.TypeMigrationRules) TypeMigrationProcessor(com.intellij.refactoring.typeMigration.TypeMigrationProcessor)

Example 2 with TypeMigrationProcessor

use of com.intellij.refactoring.typeMigration.TypeMigrationProcessor in project intellij-community by JetBrains.

the class ChangeTypeSignatureTest method doTest.

private void doTest(boolean success, String migrationTypeText) throws Exception {
    String dataPath = "/refactoring/changeTypeSignature/";
    configureByFile(dataPath + getTestName(false) + ".java");
    final PsiFile file = getFile();
    final PsiElement element = file.findElementAt(getEditor().getCaretModel().getOffset());
    final PsiReferenceParameterList parameterList = PsiTreeUtil.getParentOfType(element, PsiReferenceParameterList.class);
    assert parameterList != null;
    final PsiClass superClass = (PsiClass) ((PsiJavaCodeReferenceElement) parameterList.getParent()).resolve();
    assert superClass != null;
    PsiType migrationType = getJavaFacade().getElementFactory().createTypeFromText(migrationTypeText, null);
    try {
        final TypeMigrationRules rules = new TypeMigrationRules();
        rules.setBoundScope(GlobalSearchScope.projectScope(getProject()));
        new TypeMigrationProcessor(getProject(), new PsiElement[] { parameterList }, Functions.<PsiElement, PsiType>constant(PsiSubstitutor.EMPTY.put(superClass.getTypeParameters()[0], migrationType).substitute(new PsiImmediateClassType(superClass, PsiSubstitutor.EMPTY))), rules).run();
        if (success) {
            checkResultByFile(dataPath + getTestName(false) + ".java.after");
        } else {
            fail("Conflicts should be detected");
        }
    } catch (RuntimeException e) {
        if (success) {
            e.printStackTrace();
            fail("Conflicts should not appear");
        }
    }
}
Also used : TypeMigrationRules(com.intellij.refactoring.typeMigration.TypeMigrationRules) PsiImmediateClassType(com.intellij.psi.impl.source.PsiImmediateClassType) TypeMigrationProcessor(com.intellij.refactoring.typeMigration.TypeMigrationProcessor)

Example 3 with TypeMigrationProcessor

use of com.intellij.refactoring.typeMigration.TypeMigrationProcessor 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)

Aggregations

TypeMigrationProcessor (com.intellij.refactoring.typeMigration.TypeMigrationProcessor)3 TypeMigrationRules (com.intellij.refactoring.typeMigration.TypeMigrationRules)3 PsiImmediateClassType (com.intellij.psi.impl.source.PsiImmediateClassType)1 ReplaceStaticVariableAccess (com.intellij.refactoring.extractclass.usageInfo.ReplaceStaticVariableAccess)1 FixableUsageInfo (com.intellij.refactoring.util.FixableUsageInfo)1 UsageInfo (com.intellij.usageView.UsageInfo)1