Search in sources :

Example 11 with TypeMigrationUsageInfo

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

the class TypeMigrationStatementProcessor method findConversionOrFail.

private void findConversionOrFail(PsiExpression expression, PsiExpression toFail, Pair<PsiType, PsiType> typePair) {
    final TypeConversionDescriptorBase conversion = myLabeler.getRules().findConversion(typePair.getFirst(), typePair.getSecond(), null, expression, myLabeler);
    if (conversion == null) {
        myLabeler.markFailedConversion(typePair, toFail);
    } else {
        myLabeler.setConversionMapping(expression, conversion);
        final PsiType psiType = myTypeEvaluator.evaluateType(expression);
        LOG.assertTrue(psiType != null, expression);
        myTypeEvaluator.setType(new TypeMigrationUsageInfo(expression), psiType);
    }
}
Also used : TypeMigrationUsageInfo(com.intellij.refactoring.typeMigration.usageInfo.TypeMigrationUsageInfo)

Example 12 with TypeMigrationUsageInfo

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

the class TypeMigrationStatementProcessor method visitReferenceExpression.

@Override
public void visitReferenceExpression(PsiReferenceExpression expression) {
    final PsiExpression qualifierExpression = expression.getQualifierExpression();
    if (qualifierExpression != null && qualifierExpression.isPhysical()) {
        qualifierExpression.accept(this);
        final TypeView qualifierView = new TypeView(qualifierExpression);
        if (qualifierView.isChanged()) {
            final PsiMember member = (PsiMember) expression.advancedResolve(false).getElement();
            if (member == null)
                return;
            final Pair<PsiType, PsiType> typePair = qualifierView.getTypePair();
            final TypeConversionDescriptorBase conversion = myLabeler.getRules().findConversion(typePair.getFirst(), typePair.getSecond(), member, expression, false, myLabeler);
            if (conversion == null) {
                myLabeler.markFailedConversion(typePair, qualifierExpression);
            } else {
                final PsiElement parent = Util.getEssentialParent(expression);
                final PsiType type = conversion.conversionType();
                if (parent instanceof PsiMethodCallExpression) {
                    myLabeler.setConversionMapping((PsiMethodCallExpression) parent, conversion);
                    myTypeEvaluator.setType(new TypeMigrationUsageInfo(parent), type != null ? type : myTypeEvaluator.evaluateType((PsiExpression) parent));
                } else {
                    myLabeler.setConversionMapping(expression, conversion);
                    myTypeEvaluator.setType(new TypeMigrationUsageInfo(expression), type != null ? type : myTypeEvaluator.evaluateType(expression));
                }
            }
        }
    }
}
Also used : TypeMigrationUsageInfo(com.intellij.refactoring.typeMigration.usageInfo.TypeMigrationUsageInfo)

Example 13 with TypeMigrationUsageInfo

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

the class TypeMigrationStatementProcessor method visitArrayAccessExpression.

@Override
public void visitArrayAccessExpression(final PsiArrayAccessExpression expression) {
    super.visitArrayAccessExpression(expression);
    final PsiExpression indexExpression = expression.getIndexExpression();
    if (indexExpression != null) {
        checkIndexExpression(indexExpression);
    }
    final TypeView typeView = new TypeView(expression.getArrayExpression());
    if (typeView.isChanged() && typeView.getType() instanceof PsiClassType) {
        final TypeConversionDescriptorBase conversion = myLabeler.getRules().findConversion(typeView.getTypePair().first, typeView.getType(), null, expression, false, myLabeler);
        if (conversion == null) {
            myLabeler.markFailedConversion(typeView.getTypePair(), expression);
        } else {
            myLabeler.setConversionMapping(expression, conversion);
            myTypeEvaluator.setType(new TypeMigrationUsageInfo(expression), myTypeEvaluator.evaluateType(expression));
        }
    }
}
Also used : TypeMigrationUsageInfo(com.intellij.refactoring.typeMigration.usageInfo.TypeMigrationUsageInfo)

Example 14 with TypeMigrationUsageInfo

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

the class MigrationNode method getChildren.

@NotNull
public Collection<? extends AbstractTreeNode> getChildren() {
    if (myCachedChildren == null) {
        myCachedChildren = new ArrayList<>();
        final PsiElement element = myInfo.getElement();
        if (element != null) {
            try {
                myLabeler.setRootAndMigrate(myInfo, myMigrationType, myLabeler.markRootUsages(element, myMigrationType));
            } catch (TypeMigrationLabeler.MigrateException e) {
            //skip warning
            }
            final HashSet<Pair<TypeMigrationUsageInfo, PsiType>> roots = myLabeler.getRootsTree().get(myInfo);
            if (roots != null) {
                for (Pair<TypeMigrationUsageInfo, PsiType> root : roots) {
                    final TypeMigrationUsageInfo info = root.getFirst();
                    if (myParents.contains(info))
                        continue;
                    final HashSet<TypeMigrationUsageInfo> parents = new HashSet<>(myParents);
                    parents.add(info);
                    final MigrationNode migrationNode = new MigrationNode(getProject(), info, root.getSecond(), myLabeler, parents, myProcessed);
                    if (myInfo.isExcluded()) {
                        info.setExcluded(true);
                    }
                    myCachedChildren.add(migrationNode);
                }
            }
        }
    }
    return myCachedChildren;
}
Also used : TypeMigrationLabeler(com.intellij.refactoring.typeMigration.TypeMigrationLabeler) PsiElement(com.intellij.psi.PsiElement) TypeMigrationUsageInfo(com.intellij.refactoring.typeMigration.usageInfo.TypeMigrationUsageInfo) Pair(com.intellij.openapi.util.Pair) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with TypeMigrationUsageInfo

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

the class MigrationPanel method createToolbar.

private JComponent createToolbar() {
    final JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, JBUI.insets(5, 10, 5, 0), 0, 0);
    final JButton performButton = new JButton(RefactoringBundle.message("type.migration.migrate.button.text"));
    performButton.addActionListener(new ActionListener() {

        private void expandTree(MigrationNode migrationNode) {
            if (!migrationNode.getInfo().isExcluded() || migrationNode.areChildrenInitialized()) {
                //do not walk into excluded collapsed nodes: nothing to migrate can be found
                final Collection<? extends AbstractTreeNode> nodes = migrationNode.getChildren();
                for (final AbstractTreeNode node : nodes) {
                    ApplicationManager.getApplication().runReadAction(() -> expandTree((MigrationNode) node));
                }
            }
        }

        public void actionPerformed(final ActionEvent e) {
            final Object root = myRootsTree.getModel().getRoot();
            if (root instanceof DefaultMutableTreeNode) {
                final Object userObject = ((DefaultMutableTreeNode) root).getUserObject();
                if (userObject instanceof MigrationRootNode) {
                    ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
                        final HashSet<VirtualFile> files = new HashSet<>();
                        final TypeMigrationUsageInfo[] usages = ReadAction.compute(() -> {
                            final Collection<? extends AbstractTreeNode> children = ((MigrationRootNode) userObject).getChildren();
                            for (AbstractTreeNode child : children) {
                                expandTree((MigrationNode) child);
                            }
                            final TypeMigrationUsageInfo[] usages1 = myLabeler.getMigratedUsages();
                            for (TypeMigrationUsageInfo usage : usages1) {
                                if (!usage.isExcluded()) {
                                    final PsiElement element = usage.getElement();
                                    if (element != null) {
                                        files.add(element.getContainingFile().getVirtualFile());
                                    }
                                }
                            }
                            return usages1;
                        });
                        ApplicationManager.getApplication().invokeLater(() -> {
                            if (ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(VfsUtilCore.toVirtualFileArray(files)).hasReadonlyFiles()) {
                                return;
                            }
                            new WriteCommandAction(myProject) {

                                protected void run(@NotNull Result result) throws Throwable {
                                    TypeMigrationProcessor.change(usages, myLabeler, myProject);
                                }
                            }.execute();
                        }, myProject.getDisposed());
                    }, "Type Migration", false, myProject);
                }
            }
            UsageViewManager.getInstance(myProject).closeContent(myContent);
        }
    });
    panel.add(performButton, gc);
    final JButton closeButton = new JButton(CommonBundle.getCancelButtonText());
    closeButton.addActionListener(new ActionListener() {

        public void actionPerformed(final ActionEvent e) {
            UsageViewManager.getInstance(myProject).closeContent(myContent);
        }
    });
    panel.add(closeButton, gc);
    final JButton rerunButton = new JButton(RefactoringBundle.message("type.migration.rerun.button.text"));
    rerunButton.addActionListener(new ActionListener() {

        public void actionPerformed(final ActionEvent e) {
            UsageViewManager.getInstance(myProject).closeContent(myContent);
            final TypeMigrationDialog.MultipleElements dialog = new TypeMigrationDialog.MultipleElements(myProject, myInitialRoots, myLabeler.getMigrationRootTypeFunction(), myLabeler.getRules());
            dialog.show();
        }
    });
    panel.add(rerunButton, gc);
    final JButton helpButton = new JButton(CommonBundle.getHelpButtonText());
    helpButton.addActionListener(new ActionListener() {

        public void actionPerformed(final ActionEvent e) {
            HelpManager.getInstance().invokeHelp("reference.typeMigrationPreview");
        }
    });
    gc.weightx = 1;
    panel.add(helpButton, gc);
    return panel;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ActionEvent(java.awt.event.ActionEvent) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) NotNull(org.jetbrains.annotations.NotNull) TypeMigrationUsageInfo(com.intellij.refactoring.typeMigration.usageInfo.TypeMigrationUsageInfo) Result(com.intellij.openapi.application.Result) ActionListener(java.awt.event.ActionListener) Collection(java.util.Collection) HashSet(com.intellij.util.containers.HashSet)

Aggregations

TypeMigrationUsageInfo (com.intellij.refactoring.typeMigration.usageInfo.TypeMigrationUsageInfo)17 Pair (com.intellij.openapi.util.Pair)7 NotNull (org.jetbrains.annotations.NotNull)5 UsageInfo (com.intellij.usageView.UsageInfo)4 Logger (com.intellij.openapi.diagnostic.Logger)3 Comparing (com.intellij.openapi.util.Comparing)3 StringUtil (com.intellij.openapi.util.text.StringUtil)3 com.intellij.psi (com.intellij.psi)3 PsiImmediateClassType (com.intellij.psi.impl.source.PsiImmediateClassType)3 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)3 PsiUtil (com.intellij.psi.util.PsiUtil)3 TypeConversionUtil (com.intellij.psi.util.TypeConversionUtil)3 PsiExtendedTypeVisitor (com.intellij.refactoring.typeCook.deductive.PsiExtendedTypeVisitor)3 OverriddenUsageInfo (com.intellij.refactoring.typeMigration.usageInfo.OverriddenUsageInfo)3 OverriderUsageInfo (com.intellij.refactoring.typeMigration.usageInfo.OverriderUsageInfo)3 Nullable (org.jetbrains.annotations.Nullable)3 GenerateMembersUtil (com.intellij.codeInsight.generation.GenerateMembersUtil)2 GetterSetterPrototypeProvider (com.intellij.codeInsight.generation.GetterSetterPrototypeProvider)2 JavaLanguage (com.intellij.lang.java.JavaLanguage)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2