Search in sources :

Example 16 with MemberInfo

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

the class InheritanceToDelegationDialog method createCenterPanel.

protected JComponent createCenterPanel() {
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridy = 0;
    gbc.gridx = 0;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridwidth = 1;
    gbc.insets = JBUI.insets(4, 0, 4, 4);
    myMemberSelectionPanel = new MemberSelectionPanel(RefactoringBundle.message("delegate.members"), Collections.<MemberInfo>emptyList(), null);
    panel.add(myMemberSelectionPanel, gbc);
    MyMemberInfoModel memberInfoModel = new InheritanceToDelegationDialog.MyMemberInfoModel();
    myMemberSelectionPanel.getTable().setMemberInfoModel(memberInfoModel);
    myMemberSelectionPanel.getTable().addMemberInfoChangeListener(memberInfoModel);
    gbc.gridy++;
    gbc.insets = JBUI.insets(4, 8, 0, 8);
    gbc.weighty = 0.0;
    myCbGenerateGetter = new JCheckBox(RefactoringBundle.message("generate.getter.for.delegated.component"));
    myCbGenerateGetter.setFocusable(false);
    panel.add(myCbGenerateGetter, gbc);
    myCbGenerateGetter.setSelected(JavaRefactoringSettings.getInstance().INHERITANCE_TO_DELEGATION_DELEGATE_OTHER);
    updateTargetClass();
    return panel;
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) MemberSelectionPanel(com.intellij.refactoring.ui.MemberSelectionPanel)

Example 17 with MemberInfo

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

the class InlineSuperClassRefactoringProcessor method findUsages.

protected void findUsages(@NotNull final List<FixableUsageInfo> usages) {
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(myProject);
    final PsiElementFactory elementFactory = facade.getElementFactory();
    final PsiResolveHelper resolveHelper = facade.getResolveHelper();
    if (myCurrentInheritor != null) {
        myTargetClasses = new PsiClass[] { myCurrentInheritor };
    } else {
        Collection<PsiClass> inheritors = DirectClassInheritorsSearch.search(mySuperClass).findAll();
        myTargetClasses = inheritors.toArray(new PsiClass[inheritors.size()]);
    }
    if (myCurrentInheritor != null) {
        findUsagesInExtendsList(usages, myCurrentInheritor.getExtendsList());
        findUsagesInExtendsList(usages, myCurrentInheritor.getImplementsList());
    } else {
        ReferencesSearch.search(mySuperClass).forEach(reference -> {
            final PsiElement element = reference.getElement();
            if (element instanceof PsiJavaCodeReferenceElement) {
                final PsiImportStaticStatement staticImportStatement = PsiTreeUtil.getParentOfType(element, PsiImportStaticStatement.class);
                if (staticImportStatement != null) {
                    usages.add(new ReplaceStaticImportUsageInfo(staticImportStatement, myTargetClasses));
                } else {
                    final PsiImportStatement importStatement = PsiTreeUtil.getParentOfType(element, PsiImportStatement.class);
                    if (importStatement != null) {
                        usages.add(new RemoveImportUsageInfo(importStatement));
                    } else {
                        final PsiElement parent = element.getParent();
                        if (parent instanceof PsiReferenceList) {
                            final PsiElement pparent = parent.getParent();
                            if (pparent instanceof PsiClass) {
                                final PsiClass inheritor = (PsiClass) pparent;
                                if (parent.equals(inheritor.getExtendsList()) || parent.equals(inheritor.getImplementsList())) {
                                    usages.add(new ReplaceExtendsListUsageInfo((PsiJavaCodeReferenceElement) element, mySuperClass, inheritor));
                                }
                            }
                        } else {
                            final PsiClass targetClass = myTargetClasses[0];
                            final PsiClassType targetClassType = elementFactory.createType(targetClass, TypeConversionUtil.getSuperClassSubstitutor(mySuperClass, targetClass, PsiSubstitutor.EMPTY));
                            if (parent instanceof PsiTypeElement) {
                                final PsiType superClassType = ((PsiTypeElement) parent).getType();
                                PsiSubstitutor subst = getSuperClassSubstitutor(superClassType, targetClassType, resolveHelper, targetClass);
                                usages.add(new ReplaceWithSubtypeUsageInfo(((PsiTypeElement) parent), elementFactory.createType(targetClass, subst), myTargetClasses));
                            } else if (parent instanceof PsiNewExpression) {
                                final PsiClassType newType = elementFactory.createType(targetClass, getSuperClassSubstitutor(((PsiNewExpression) parent).getType(), targetClassType, resolveHelper, targetClass));
                                usages.add(new ReplaceConstructorUsageInfo(((PsiNewExpression) parent), newType, myTargetClasses));
                            } else if (parent instanceof PsiJavaCodeReferenceElement) {
                                usages.add(new ReplaceReferenceUsageInfo(((PsiJavaCodeReferenceElement) parent).getQualifier(), myTargetClasses));
                            }
                        }
                    }
                }
            }
            return true;
        });
    }
    for (PsiClass targetClass : myTargetClasses) {
        if (skipTargetClass(targetClass)) {
            continue;
        }
        for (MemberInfo memberInfo : myMemberInfos) {
            final PsiMember member = memberInfo.getMember();
            for (PsiReference reference : ReferencesSearch.search(member, member.getUseScope(), true)) {
                final PsiElement element = reference.getElement();
                if (element instanceof PsiReferenceExpression && ((PsiReferenceExpression) element).getQualifierExpression() instanceof PsiSuperExpression && PsiTreeUtil.isAncestor(targetClass, element, false)) {
                    usages.add(new RemoveQualifierUsageInfo((PsiReferenceExpression) element));
                }
            }
        }
        if (!mySuperClass.isInterface()) {
            final PsiMethod[] superConstructors = mySuperClass.getConstructors();
            for (PsiMethod constructor : targetClass.getConstructors()) {
                final PsiCodeBlock constrBody = constructor.getBody();
                if (constrBody != null) {
                    final PsiStatement[] statements = constrBody.getStatements();
                    if (statements.length > 0) {
                        final PsiStatement firstConstrStatement = statements[0];
                        if (firstConstrStatement instanceof PsiExpressionStatement) {
                            final PsiExpression expression = ((PsiExpressionStatement) firstConstrStatement).getExpression();
                            if (expression instanceof PsiMethodCallExpression) {
                                final PsiReferenceExpression methodExpression = ((PsiMethodCallExpression) expression).getMethodExpression();
                                if (methodExpression.getText().equals(PsiKeyword.SUPER)) {
                                    final PsiMethod superConstructor = ((PsiMethodCallExpression) expression).resolveMethod();
                                    if (superConstructor != null && superConstructor.getBody() != null) {
                                        usages.add(new InlineSuperCallUsageInfo((PsiMethodCallExpression) expression));
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
                //insert implicit call to super
                for (PsiMethod superConstructor : superConstructors) {
                    if (superConstructor.getParameterList().getParametersCount() == 0) {
                        final PsiExpression expression = JavaPsiFacade.getElementFactory(myProject).createExpressionFromText("super()", constructor);
                        usages.add(new InlineSuperCallUsageInfo((PsiMethodCallExpression) expression, constrBody));
                    }
                }
            }
            if (targetClass.getConstructors().length == 0) {
                //copy default constructor
                for (PsiMethod superConstructor : superConstructors) {
                    if (superConstructor.getParameterList().getParametersCount() == 0) {
                        usages.add(new CopyDefaultConstructorUsageInfo(targetClass, superConstructor));
                        break;
                    }
                }
            }
        }
    }
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo)

Example 18 with MemberInfo

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

the class InlineSuperClassRefactoringProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull final Ref<UsageInfo[]> refUsages) {
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final PushDownConflicts pushDownConflicts = new PushDownConflicts(mySuperClass, myMemberInfos, conflicts);
    for (PsiClass targetClass : myTargetClasses) {
        if (targetClass instanceof PsiAnonymousClass) {
            conflicts.putValue(targetClass, "Cannot inline into anonymous class.");
        } else if (PsiTreeUtil.isAncestor(mySuperClass, targetClass, false)) {
            conflicts.putValue(targetClass, "Cannot inline into the inner class. Move \'" + targetClass.getName() + "\' to upper level");
        } else {
            for (MemberInfo info : myMemberInfos) {
                final PsiMember member = info.getMember();
                pushDownConflicts.checkMemberPlacementInTargetClassConflict(targetClass, member);
            }
        //todo check accessibility conflicts
        }
    }
    if (myCurrentInheritor != null) {
        ReferencesSearch.search(myCurrentInheritor).forEach(reference -> {
            final PsiElement element = reference.getElement();
            if (element != null) {
                final PsiElement parent = element.getParent();
                if (parent instanceof PsiNewExpression) {
                    final PsiClass aClass = PsiUtil.resolveClassInType(getPlaceExpectedType(parent));
                    if (aClass == mySuperClass) {
                        conflicts.putValue(parent, "Instance of target type is passed to a place where super class is expected.");
                        return false;
                    }
                }
            }
            return true;
        });
    }
    checkConflicts(refUsages, conflicts);
    return showConflicts(conflicts, refUsages.get());
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) PushDownConflicts(com.intellij.refactoring.memberPushDown.PushDownConflicts) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo)

Example 19 with MemberInfo

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

the class ExtractSuperBaseProcessor method refreshElements.

protected void refreshElements(@NotNull PsiElement[] elements) {
    myClass = (PsiClass) elements[0];
    myTargetDirectory = (PsiDirectory) elements[1];
    for (int i = 0; i < myMemberInfos.length; i++) {
        final MemberInfo info = myMemberInfos[i];
        info.updateMember((PsiMember) elements[i + 2]);
    }
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo)

Example 20 with MemberInfo

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

the class PullAsAbstractUpFix method pullUp.

private static void pullUp(PsiMethod method, PsiClass containingClass, PsiClass baseClass) {
    if (!FileModificationService.getInstance().prepareFileForWrite(baseClass.getContainingFile()))
        return;
    final MemberInfo memberInfo = new MemberInfo(method);
    memberInfo.setChecked(true);
    memberInfo.setToAbstract(true);
    new PullUpProcessor(containingClass, baseClass, new MemberInfo[] { memberInfo }, new DocCommentPolicy(DocCommentPolicy.ASIS)).run();
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) PullUpProcessor(com.intellij.refactoring.memberPullUp.PullUpProcessor) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy)

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