Search in sources :

Example 11 with MemberInfo

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

the class ExtractSuperclassHandler method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
    if (elements.length != 1)
        return;
    myProject = project;
    mySubclass = (PsiClass) elements[0];
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, mySubclass))
        return;
    Editor editor = dataContext != null ? CommonDataKeys.EDITOR.getData(dataContext) : null;
    if (mySubclass.isInterface()) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("superclass.cannot.be.extracted.from.an.interface"));
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.EXTRACT_SUPERCLASS);
        return;
    }
    if (mySubclass.isEnum()) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("superclass.cannot.be.extracted.from.an.enum"));
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.EXTRACT_SUPERCLASS);
        return;
    }
    List<MemberInfo> memberInfos = MemberInfo.extractClassMembers(mySubclass, new MemberInfo.Filter<PsiMember>() {

        @Override
        public boolean includeMember(PsiMember element) {
            return true;
        }
    }, false);
    if (mySubclass instanceof PsiAnonymousClass) {
        memberInfos = ContainerUtil.filter(memberInfos, memberInfo -> !(memberInfo.getMember() instanceof PsiClass && memberInfo.getOverrides() != null));
    }
    final ExtractSuperclassDialog dialog = new ExtractSuperclassDialog(project, mySubclass, memberInfos, this);
    if (!dialog.showAndGet() || !dialog.isExtractSuperclass()) {
        return;
    }
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        final Runnable action = () -> doRefactoring(project, mySubclass, dialog);
        ApplicationManager.getApplication().runWriteAction(action);
    }, REFACTORING_NAME, null);
}
Also used : DescriptiveNameUtil(com.intellij.lang.findUsages.DescriptiveNameUtil) ArrayUtil(com.intellij.util.ArrayUtil) DataContext(com.intellij.openapi.actionSystem.DataContext) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy) LocalHistoryAction(com.intellij.history.LocalHistoryAction) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) ContainerUtil(com.intellij.util.containers.ContainerUtil) ScrollType(com.intellij.openapi.editor.ScrollType) Project(com.intellij.openapi.project.Project) PsiUtil(com.intellij.psi.util.PsiUtil) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) HelpID(com.intellij.refactoring.HelpID) PullUpConflictsUtil(com.intellij.refactoring.memberPullUp.PullUpConflictsUtil) ProgressManager(com.intellij.openapi.progress.ProgressManager) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ContextAwareActionHandler(com.intellij.lang.ContextAwareActionHandler) Editor(com.intellij.openapi.editor.Editor) ExtractClassUtil(com.intellij.refactoring.extractInterface.ExtractClassUtil) CommandProcessor(com.intellij.openapi.command.CommandProcessor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) List(java.util.List) ElementsHandler(com.intellij.refactoring.lang.ElementsHandler) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) LocalHistory(com.intellij.history.LocalHistory) NotNull(org.jetbrains.annotations.NotNull) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) Editor(com.intellij.openapi.editor.Editor)

Example 12 with MemberInfo

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

the class ExtractSuperclassHandler method doRefactoring.

// invoked inside Command and Atomic action
private static void doRefactoring(final Project project, final PsiClass subclass, final ExtractSuperclassDialog dialog) {
    final String superclassName = dialog.getExtractedSuperName();
    final PsiDirectory targetDirectory = dialog.getTargetDirectory();
    final MemberInfo[] selectedMemberInfos = ArrayUtil.toObjectArray(dialog.getSelectedMemberInfos(), MemberInfo.class);
    final DocCommentPolicy javaDocPolicy = new DocCommentPolicy(dialog.getDocCommentPolicy());
    LocalHistoryAction a = LocalHistory.getInstance().startAction(getCommandName(subclass, superclassName));
    try {
        final PsiClass superclass;
        try {
            superclass = ExtractSuperClassUtil.extractSuperClass(project, targetDirectory, superclassName, subclass, selectedMemberInfos, javaDocPolicy);
        } finally {
            a.finish();
        }
        // ask whether to search references to subclass and turn them into refs to superclass if possible
        ExtractClassUtil.suggestToTurnRefsToSuper(project, superclass, subclass);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) LocalHistoryAction(com.intellij.history.LocalHistoryAction) IncorrectOperationException(com.intellij.util.IncorrectOperationException) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy)

Example 13 with MemberInfo

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

the class ExtractClassDialog method preselectOneTypeEnumConstants.

private void preselectOneTypeEnumConstants() {
    if (enumConstants.isEmpty()) {
        MemberInfo selected = null;
        for (MemberInfo info : memberInfo) {
            if (info.isChecked()) {
                selected = info;
                break;
            }
        }
        if (selected != null && isConstantField(selected.getMember())) {
            enumConstants.add(selected);
            selected.setToAbstract(true);
        }
    }
    for (MemberInfo info : memberInfo) {
        final PsiMember member = info.getMember();
        if (isConstantField(member)) {
            if (enumConstants.isEmpty() || ((PsiField) enumConstants.get(0).getMember()).getType().equals(((PsiField) member).getType())) {
                if (!enumConstants.contains(info))
                    enumConstants.add(info);
                info.setToAbstract(true);
            }
        }
    }
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo)

Example 14 with MemberInfo

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

the class ExtractClassDialog method createCenterPanel.

protected JComponent createCenterPanel() {
    final JPanel panel = new JPanel(new BorderLayout());
    final MemberSelectionTable table = new MemberSelectionTable(memberInfo, "As enum") {

        @Nullable
        @Override
        protected Object getAbstractColumnValue(MemberInfo memberInfo) {
            if (isExtractAsEnum()) {
                final PsiMember member = memberInfo.getMember();
                if (isConstantField(member)) {
                    return Boolean.valueOf(enumConstants.contains(memberInfo));
                }
            }
            return null;
        }

        @Override
        protected boolean isAbstractColumnEditable(int rowIndex) {
            final MemberInfo info = memberInfo.get(rowIndex);
            if (info.isChecked()) {
                final PsiMember member = info.getMember();
                if (isConstantField(member)) {
                    if (enumConstants.isEmpty())
                        return true;
                    final MemberInfo currentEnumConstant = enumConstants.get(0);
                    if (((PsiField) currentEnumConstant.getMember()).getType().equals(((PsiField) member).getType()))
                        return true;
                }
            }
            return false;
        }
    };
    table.setMemberInfoModel(new DelegatingMemberInfoModel<PsiMember, MemberInfo>(table.getMemberInfoModel()) {

        @Override
        public int checkForProblems(@NotNull final MemberInfo member) {
            final PsiMember cause = getCause(member);
            if (member.isChecked() && cause != null)
                return ERROR;
            if (!member.isChecked() && cause != null)
                return WARNING;
            return OK;
        }

        @Override
        public String getTooltipText(final MemberInfo member) {
            final PsiMember cause = getCause(member);
            if (cause != null) {
                final String presentation = SymbolPresentationUtil.getSymbolPresentableText(cause);
                if (member.isChecked()) {
                    return "Depends on " + presentation + " from " + sourceClass.getName();
                } else {
                    final String className = getClassName();
                    return "Depends on " + presentation + " from new class" + (className.length() > 0 ? ": " + className : "");
                }
            }
            return null;
        }

        private PsiMember getCause(final MemberInfo member) {
            PsiMember cause = myMember2CauseMap.get(member);
            if (cause != null)
                return cause;
            final BackpointerUsageVisitor visitor;
            if (member.isChecked()) {
                visitor = new BackpointerUsageVisitor(getFieldsToExtract(), getClassesToExtract(), getMethodsToExtract(), sourceClass);
            } else {
                visitor = new BackpointerUsageVisitor(getMembersToExtract(false, PsiField.class), getMembersToExtract(false, PsiClass.class), getMembersToExtract(false, PsiMethod.class), sourceClass, false);
            }
            member.getMember().accept(visitor);
            cause = visitor.getCause();
            myMember2CauseMap.put(member, cause);
            return cause;
        }
    });
    final MemberSelectionPanelBase<PsiMember, MemberInfo, MemberSelectionTable> memberSelectionPanel = new MemberSelectionPanelBase<>(RefactorJBundle.message("members.to.extract.label"), table);
    panel.add(memberSelectionPanel, BorderLayout.CENTER);
    table.addMemberInfoChangeListener(this);
    extractAsEnum.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (extractAsEnum.isSelected()) {
                preselectOneTypeEnumConstants();
            }
            table.repaint();
        }
    });
    createInner.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final boolean isCreateInner = createInner.isSelected();
            packageTextField.setEnabled(!isCreateInner);
            myDestinationFolderComboBox.setEnabled(!isCreateInner);
        }
    });
    myGenerateAccessorsCb = new JCheckBox("Generate accessors");
    myGenerateAccessorsCb.setMnemonic('G');
    panel.add(myGenerateAccessorsCb, BorderLayout.SOUTH);
    panel.add(myVisibilityPanel, BorderLayout.EAST);
    return panel;
}
Also used : ActionEvent(java.awt.event.ActionEvent) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) ActionListener(java.awt.event.ActionListener)

Example 15 with MemberInfo

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

the class InheritanceToDelegationDialog method doAction.

protected void doAction() {
    JavaRefactoringSettings.getInstance().INHERITANCE_TO_DELEGATION_DELEGATE_OTHER = myCbGenerateGetter.isSelected();
    final Collection<MemberInfo> selectedMemberInfos = getSelectedMemberInfos();
    final ArrayList<PsiClass> implementedInterfaces = new ArrayList<>();
    final ArrayList<PsiMethod> delegatedMethods = new ArrayList<>();
    for (MemberInfo memberInfo : selectedMemberInfos) {
        final PsiElement member = memberInfo.getMember();
        if (member instanceof PsiClass && Boolean.FALSE.equals(memberInfo.getOverrides())) {
            implementedInterfaces.add((PsiClass) member);
        } else if (member instanceof PsiMethod) {
            delegatedMethods.add((PsiMethod) member);
        }
    }
    invokeRefactoring(new InheritanceToDelegationProcessor(myProject, myClass, getSelectedTargetClass(), getFieldName(), getInnerClassName(), implementedInterfaces.toArray(new PsiClass[implementedInterfaces.size()]), delegatedMethods.toArray(new PsiMethod[delegatedMethods.size()]), isGenerateGetter(), isGenerateGetter()));
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) ArrayList(java.util.ArrayList)

Aggregations

MemberInfo (com.intellij.refactoring.util.classMembers.MemberInfo)45 DocCommentPolicy (com.intellij.refactoring.util.DocCommentPolicy)12 PsiMethod (com.intellij.psi.PsiMethod)6 MultiMap (com.intellij.util.containers.MultiMap)6 ArrayList (java.util.ArrayList)6 Project (com.intellij.openapi.project.Project)5 com.intellij.psi (com.intellij.psi)5 MemberSelectionPanel (com.intellij.refactoring.ui.MemberSelectionPanel)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)5 HashSet (java.util.HashSet)5 Editor (com.intellij.openapi.editor.Editor)4 PsiClass (com.intellij.psi.PsiClass)4 PullUpProcessor (com.intellij.refactoring.memberPullUp.PullUpProcessor)4 DialogWrapper (com.intellij.openapi.ui.DialogWrapper)3 PsiMember (com.intellij.psi.PsiMember)3 MemberSelectionTable (com.intellij.refactoring.ui.MemberSelectionTable)3 MemberInfoStorage (com.intellij.refactoring.util.classMembers.MemberInfoStorage)3 UsageInfo (com.intellij.usageView.UsageInfo)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3