Search in sources :

Example 1 with PsiMethodMember

use of com.intellij.codeInsight.generation.PsiMethodMember in project intellij-community by JetBrains.

the class CreateSubclassAction method chooseAndImplement.

protected static void chooseAndImplement(PsiClass psiClass, Project project, @NotNull PsiClass targetClass, Editor editor) {
    boolean hasNonTrivialConstructor = false;
    final PsiMethod[] constructors = psiClass.getConstructors();
    for (PsiMethod constructor : constructors) {
        if (constructor.getParameterList().getParametersCount() > 0) {
            hasNonTrivialConstructor = true;
            break;
        }
    }
    if (hasNonTrivialConstructor) {
        final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(psiClass, targetClass, PsiSubstitutor.EMPTY);
        final List<PsiMethodMember> baseConstructors = new ArrayList<>();
        for (PsiMethod baseConstr : constructors) {
            if (PsiUtil.isAccessible(project, baseConstr, targetClass, targetClass)) {
                baseConstructors.add(new PsiMethodMember(baseConstr, substitutor));
            }
        }
        final int offset = editor.getCaretModel().getOffset();
        CreateConstructorMatchingSuperFix.chooseConstructor2Delegate(project, editor, substitutor, baseConstructors, constructors, targetClass);
        editor.getCaretModel().moveToOffset(offset);
    }
    OverrideImplementUtil.chooseAndImplementMethods(project, editor, targetClass);
}
Also used : PsiMethodMember(com.intellij.codeInsight.generation.PsiMethodMember) ArrayList(java.util.ArrayList)

Example 2 with PsiMethodMember

use of com.intellij.codeInsight.generation.PsiMethodMember in project intellij-community by JetBrains.

the class ChangeParameterClassFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final PsiClass myClass = (PsiClass) startElement;
    if (!FileModificationService.getInstance().prepareFileForWrite(file))
        return;
    ApplicationManager.getApplication().runWriteAction(() -> invokeImpl(myClass));
    final Editor editor1 = CodeInsightUtil.positionCursorAtLBrace(project, myClass.getContainingFile(), myClass);
    if (editor1 == null)
        return;
    final Collection<CandidateInfo> toImplement = OverrideImplementExploreUtil.getMethodsToOverrideImplement(myClass, true);
    if (!toImplement.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            ApplicationManager.getApplication().runWriteAction(() -> {
                Collection<PsiMethodMember> members = ContainerUtil.map2List(toImplement, s -> new PsiMethodMember(s));
                OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor1, myClass, members, false);
            });
        } else {
            //SCR 12599
            editor1.getCaretModel().moveToOffset(myClass.getTextRange().getStartOffset());
            OverrideImplementUtil.chooseAndImplementMethods(project, editor1, myClass);
        }
    }
    UndoUtil.markPsiFileForUndo(file);
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) PsiMethodMember(com.intellij.codeInsight.generation.PsiMethodMember) PsiClass(com.intellij.psi.PsiClass) Editor(com.intellij.openapi.editor.Editor)

Example 3 with PsiMethodMember

use of com.intellij.codeInsight.generation.PsiMethodMember in project intellij-community by JetBrains.

the class ImplementAbstractClassMethodsFix method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull final PsiElement startElement, @NotNull PsiElement endElement) {
    if (editor == null)
        return;
    PsiJavaCodeReferenceElement classReference = ((PsiNewExpression) startElement).getClassReference();
    if (classReference == null)
        return;
    final PsiClass psiClass = (PsiClass) classReference.resolve();
    if (psiClass == null)
        return;
    final MemberChooser<PsiMethodMember> chooser = chooseMethodsToImplement(editor, startElement, psiClass, false);
    if (chooser == null)
        return;
    final List<PsiMethodMember> selectedElements = chooser.getSelectedElements();
    if (selectedElements == null || selectedElements.isEmpty())
        return;
    new WriteCommandAction(project, file) {

        @Override
        protected void run(@NotNull final Result result) throws Throwable {
            PsiNewExpression newExpression = (PsiNewExpression) JavaPsiFacade.getElementFactory(project).createExpressionFromText(startElement.getText() + "{}", startElement);
            newExpression = (PsiNewExpression) startElement.replace(newExpression);
            final PsiClass psiClass = newExpression.getAnonymousClass();
            if (psiClass == null)
                return;
            Map<PsiClass, PsiSubstitutor> subst = new HashMap<>();
            for (PsiMethodMember selectedElement : selectedElements) {
                final PsiClass baseClass = selectedElement.getElement().getContainingClass();
                if (baseClass != null) {
                    PsiSubstitutor substitutor = subst.get(baseClass);
                    if (substitutor == null) {
                        substitutor = TypeConversionUtil.getSuperClassSubstitutor(baseClass, psiClass, PsiSubstitutor.EMPTY);
                        subst.put(baseClass, substitutor);
                    }
                    selectedElement.setSubstitutor(substitutor);
                }
            }
            OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor, psiClass, selectedElements, chooser.isCopyJavadoc(), chooser.isInsertOverrideAnnotation());
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PsiMethodMember(com.intellij.codeInsight.generation.PsiMethodMember) Result(com.intellij.openapi.application.Result) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with PsiMethodMember

use of com.intellij.codeInsight.generation.PsiMethodMember in project intellij-community by JetBrains.

the class OverrideImplement15Test method doTest.

private void doTest(boolean copyJavadoc, @Nullable Boolean toImplement) {
    String name = getTestName(false);
    configureByFile(BASE_DIR + "before" + name + ".java");
    int offset = getEditor().getCaretModel().getOffset();
    PsiElement context = getFile().findElementAt(offset);
    PsiClass psiClass = PsiTreeUtil.getParentOfType(context, PsiClass.class);
    assert psiClass != null;
    ApplicationManager.getApplication().runWriteAction(() -> {
        if (toImplement == null) {
            PsiClassType[] implement = psiClass.getImplementsListTypes();
            final PsiClass superClass = implement.length == 0 ? psiClass.getSuperClass() : implement[0].resolve();
            assert superClass != null;
            PsiMethod method = superClass.getMethods()[0];
            final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, psiClass, PsiSubstitutor.EMPTY);
            final List<PsiMethodMember> candidates = Collections.singletonList(new PsiMethodMember(method, OverrideImplementExploreUtil.correctSubstitutor(method, substitutor)));
            OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(getEditor(), psiClass, candidates, copyJavadoc, true);
        } else {
            OverrideImplementUtil.chooseAndOverrideOrImplementMethods(getProject(), getEditor(), psiClass, toImplement);
        }
    });
    checkResultByFile(BASE_DIR + "after" + name + ".java");
}
Also used : PsiMethodMember(com.intellij.codeInsight.generation.PsiMethodMember)

Example 5 with PsiMethodMember

use of com.intellij.codeInsight.generation.PsiMethodMember in project intellij-community by JetBrains.

the class ImplementMethodsFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final PsiElement myPsiElement = startElement;
    if (editor == null || !FileModificationService.getInstance().prepareFileForWrite(myPsiElement.getContainingFile()))
        return;
    if (myPsiElement instanceof PsiEnumConstant) {
        final boolean hasClassInitializer = ((PsiEnumConstant) myPsiElement).getInitializingClass() != null;
        final MemberChooser<PsiMethodMember> chooser = chooseMethodsToImplement(editor, startElement, ((PsiEnumConstant) myPsiElement).getContainingClass(), hasClassInitializer);
        if (chooser == null)
            return;
        final List<PsiMethodMember> selectedElements = chooser.getSelectedElements();
        if (selectedElements == null || selectedElements.isEmpty())
            return;
        new WriteCommandAction(project, file) {

            @Override
            protected void run(@NotNull final Result result) throws Throwable {
                final PsiClass psiClass = ((PsiEnumConstant) myPsiElement).getOrCreateInitializingClass();
                OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor, psiClass, selectedElements, chooser.isCopyJavadoc(), chooser.isInsertOverrideAnnotation());
            }
        }.execute();
    } else {
        OverrideImplementUtil.chooseAndImplementMethods(project, editor, (PsiClass) myPsiElement);
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiMethodMember(com.intellij.codeInsight.generation.PsiMethodMember) PsiClass(com.intellij.psi.PsiClass) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement) Result(com.intellij.openapi.application.Result)

Aggregations

PsiMethodMember (com.intellij.codeInsight.generation.PsiMethodMember)8 Result (com.intellij.openapi.application.Result)3 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 PsiClass (com.intellij.psi.PsiClass)2 CandidateInfo (com.intellij.psi.infos.CandidateInfo)2 PsiElementClassMember (com.intellij.codeInsight.generation.PsiElementClassMember)1 PsiFieldMember (com.intellij.codeInsight.generation.PsiFieldMember)1 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 MemberChooser (com.intellij.ide.util.MemberChooser)1 Editor (com.intellij.openapi.editor.Editor)1 PsiElement (com.intellij.psi.PsiElement)1 PsiEnumConstant (com.intellij.psi.PsiEnumConstant)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1