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);
}
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);
}
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();
}
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");
}
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);
}
}
Aggregations