Search in sources :

Example 1 with TypeMigrationRules

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

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

the class VariableTypeFromCallFix method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
    final TypeMigrationRules rules = new TypeMigrationRules();
    rules.setBoundScope(PsiSearchHelper.SERVICE.getInstance(project).getUseScope(myVar));
    TypeMigrationProcessor.runHighlightingTypeMigration(project, editor, rules, myVar, myExpressionType);
}
Also used : TypeMigrationRules(com.intellij.refactoring.typeMigration.TypeMigrationRules)

Example 3 with TypeMigrationRules

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

the class MethodReturnTypeFix method changeClassTypeArgument.

private static boolean changeClassTypeArgument(PsiMethod myMethod, Project project, PsiType superReturnType, PsiClass superClass, Editor editor, PsiType returnType) {
    if (superClass == null || !superClass.hasTypeParameters())
        return true;
    final PsiClass superReturnTypeClass = PsiUtil.resolveClassInType(superReturnType);
    if (superReturnTypeClass == null || !(superReturnTypeClass instanceof PsiTypeParameter || superReturnTypeClass.hasTypeParameters()))
        return true;
    final PsiClass derivedClass = myMethod.getContainingClass();
    if (derivedClass == null)
        return true;
    final PsiReferenceParameterList referenceParameterList = findTypeArgumentsList(superClass, derivedClass);
    if (referenceParameterList == null)
        return true;
    final PsiElement resolve = ((PsiJavaCodeReferenceElement) referenceParameterList.getParent()).resolve();
    if (!(resolve instanceof PsiClass))
        return true;
    final PsiClass baseClass = (PsiClass) resolve;
    if (returnType instanceof PsiPrimitiveType) {
        returnType = ((PsiPrimitiveType) returnType).getBoxedType(derivedClass);
    }
    final PsiSubstitutor superClassSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, baseClass, PsiSubstitutor.EMPTY);
    final PsiType superReturnTypeInBaseClassType = superClassSubstitutor.substitute(superReturnType);
    final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(project).getResolveHelper();
    final PsiSubstitutor psiSubstitutor = resolveHelper.inferTypeArguments(PsiTypesUtil.filterUnusedTypeParameters(superReturnTypeInBaseClassType, baseClass.getTypeParameters()), new PsiType[] { superReturnTypeInBaseClassType }, new PsiType[] { returnType }, PsiUtil.getLanguageLevel(superClass));
    final TypeMigrationRules rules = new TypeMigrationRules();
    final PsiSubstitutor compoundSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, derivedClass, PsiSubstitutor.EMPTY).putAll(psiSubstitutor);
    rules.setBoundScope(new LocalSearchScope(derivedClass));
    TypeMigrationProcessor.runHighlightingTypeMigration(project, editor, rules, referenceParameterList, JavaPsiFacade.getElementFactory(project).createType(baseClass, compoundSubstitutor));
    return false;
}
Also used : TypeMigrationRules(com.intellij.refactoring.typeMigration.TypeMigrationRules) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)

Example 4 with TypeMigrationRules

use of com.intellij.refactoring.typeMigration.TypeMigrationRules 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 5 with TypeMigrationRules

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

the class ChangeClassParametersIntention method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
    final PsiTypeElement typeElement = PsiTreeUtil.getTopmostParentOfType(element, PsiTypeElement.class);
    final PsiReferenceParameterList parameterList = PsiTreeUtil.getParentOfType(typeElement, PsiReferenceParameterList.class);
    if (parameterList != null) {
        final PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
        if (aClass instanceof PsiAnonymousClass) {
            editor.getCaretModel().moveToOffset(aClass.getTextOffset());
            final PsiTypeElement[] typeElements = parameterList.getTypeParameterElements();
            final int changeIdx = ArrayUtil.find(typeElements, typeElement);
            final PsiClassType.ClassResolveResult result = ((PsiAnonymousClass) aClass).getBaseClassType().resolveGenerics();
            final PsiClass baseClass = result.getElement();
            LOG.assertTrue(baseClass != null);
            final PsiTypeParameter typeParameter = baseClass.getTypeParameters()[changeIdx];
            final TemplateBuilderImpl templateBuilder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(aClass);
            final String oldTypeText = typeElement.getText();
            final String varName = "param";
            templateBuilder.replaceElement(typeElement, varName, new TypeExpression(project, new PsiType[] { typeElement.getType() }), true);
            final Template template = templateBuilder.buildInlineTemplate();
            TemplateManager.getInstance(project).startTemplate(editor, template, false, null, new TemplateEditingAdapter() {

                private String myNewType;

                @Override
                public void beforeTemplateFinished(TemplateState state, Template template) {
                    final TextResult value = state.getVariableValue(varName);
                    myNewType = value != null ? value.getText() : "";
                    final int segmentsCount = state.getSegmentsCount();
                    final Document document = state.getEditor().getDocument();
                    ApplicationManager.getApplication().runWriteAction(() -> {
                        for (int i = 0; i < segmentsCount; i++) {
                            final TextRange segmentRange = state.getSegmentRange(i);
                            document.replaceString(segmentRange.getStartOffset(), segmentRange.getEndOffset(), oldTypeText);
                        }
                    });
                }

                @Override
                public void templateFinished(Template template, boolean brokenOff) {
                    if (!brokenOff) {
                        final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
                        try {
                            final PsiType targetParam = elementFactory.createTypeFromText(myNewType, aClass);
                            if (!(targetParam instanceof PsiClassType)) {
                                HintManager.getInstance().showErrorHint(editor, JavaErrorMessages.message("generics.type.argument.cannot.be.of.primitive.type"));
                                return;
                            }
                            final PsiClassType classType = (PsiClassType) targetParam;
                            final PsiClass target = classType.resolve();
                            if (target == null) {
                                HintManager.getInstance().showErrorHint(editor, JavaErrorMessages.message("cannot.resolve.symbol", classType.getPresentableText()));
                                return;
                            }
                            final TypeMigrationRules myRules = new TypeMigrationRules();
                            final PsiSubstitutor substitutor = result.getSubstitutor().put(typeParameter, targetParam);
                            final PsiType targetClassType = elementFactory.createType(baseClass, substitutor);
                            myRules.setBoundScope(new LocalSearchScope(aClass));
                            TypeMigrationProcessor.runHighlightingTypeMigration(project, editor, myRules, ((PsiAnonymousClass) aClass).getBaseClassReference().getParameterList(), targetClassType);
                        } catch (IncorrectOperationException e) {
                            HintManager.getInstance().showErrorHint(editor, "Incorrect type");
                        }
                    }
                }
            });
        }
    }
}
Also used : TypeMigrationRules(com.intellij.refactoring.typeMigration.TypeMigrationRules) Document(com.intellij.openapi.editor.Document) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) TypeExpression(com.intellij.codeInsight.intention.impl.TypeExpression) TextRange(com.intellij.openapi.util.TextRange) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

TypeMigrationRules (com.intellij.refactoring.typeMigration.TypeMigrationRules)7 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)3 TypeMigrationProcessor (com.intellij.refactoring.typeMigration.TypeMigrationProcessor)3 UsageInfo (com.intellij.usageView.UsageInfo)2 TypeExpression (com.intellij.codeInsight.intention.impl.TypeExpression)1 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)1 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 Document (com.intellij.openapi.editor.Document)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiImmediateClassType (com.intellij.psi.impl.source.PsiImmediateClassType)1 ReplaceStaticVariableAccess (com.intellij.refactoring.extractclass.usageInfo.ReplaceStaticVariableAccess)1 FixableUsageInfo (com.intellij.refactoring.util.FixableUsageInfo)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 PrintWriter (java.io.PrintWriter)1