use of com.intellij.codeInsight.generation.PsiMethodMember in project intellij-community by JetBrains.
the class InitializeFinalFieldInConstructorFix method toPsiMethodArray.
@NotNull
private static PsiMethod[] toPsiMethodArray(@NotNull List<PsiMethodMember> methodMembers) {
final PsiMethod[] result = new PsiMethod[methodMembers.size()];
int i = 0;
for (PsiMethodMember methodMember : methodMembers) {
result[i++] = methodMember.getElement();
}
return result;
}
use of com.intellij.codeInsight.generation.PsiMethodMember in project intellij-community by JetBrains.
the class CreateConstructorParameterFromFieldFix method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
if (!FileModificationService.getInstance().prepareFileForWrite(file))
return;
PsiMethod[] constructors = myClass.getConstructors();
if (constructors.length == 0) {
final AddDefaultConstructorFix defaultConstructorFix = new AddDefaultConstructorFix(myClass);
ApplicationManager.getApplication().runWriteAction(() -> defaultConstructorFix.invoke(project, editor, file));
constructors = myClass.getConstructors();
}
Arrays.sort(constructors, new Comparator<PsiMethod>() {
@Override
public int compare(PsiMethod c1, PsiMethod c2) {
final PsiMethod cc1 = RefactoringUtil.getChainedConstructor(c1);
final PsiMethod cc2 = RefactoringUtil.getChainedConstructor(c2);
if (cc1 == c2)
return 1;
if (cc2 == c1)
return -1;
if (cc1 == null) {
return cc2 == null ? 0 : compare(c1, cc2);
} else {
return cc2 == null ? compare(cc1, c2) : compare(cc1, cc2);
}
}
});
final List<SmartPsiElementPointer<PsiElement>> cleanupElements = new ArrayList<>();
final List<PsiMethod> filtered = filterConstructorsIfFieldAlreadyAssigned(constructors, getField());
if (filtered.size() > 1) {
final PsiMethodMember[] members = new PsiMethodMember[filtered.size()];
int i = 0;
for (PsiMethod constructor : filtered) {
members[i++] = new PsiMethodMember(constructor);
}
final List<PsiMethodMember> elements;
if (ApplicationManager.getApplication().isUnitTestMode()) {
elements = Arrays.asList(members);
} else {
final MemberChooser<PsiMethodMember> chooser = new MemberChooser<>(members, false, true, project);
chooser.setTitle("Choose Constructors to Add Parameter to");
chooser.show();
elements = chooser.getSelectedElements();
if (elements == null)
return;
}
for (PsiMethodMember member : elements) {
if (!addParameterToConstructor(project, file, editor, member.getElement(), new PsiField[] { getField() }, cleanupElements)) {
break;
}
}
} else if (!filtered.isEmpty()) {
final Collection<SmartPsiElementPointer<PsiField>> fieldsToFix = getFieldsToFix();
try {
final PsiMethod constructor = filtered.get(0);
final LinkedHashSet<PsiField> fields = new LinkedHashSet<>();
getFieldsToFix().add(myField);
for (SmartPsiElementPointer<PsiField> elementPointer : fieldsToFix) {
final PsiField field = elementPointer.getElement();
if (field != null && isAvailable(field) && filterConstructorsIfFieldAlreadyAssigned(new PsiMethod[] { constructor }, field).contains(constructor)) {
fields.add(field);
}
}
if (filtered.size() == constructors.length && fields.size() > 1 && !ApplicationManager.getApplication().isUnitTestMode()) {
PsiFieldMember[] members = new PsiFieldMember[fields.size()];
int i = 0;
for (PsiField field : fields) {
members[i++] = new PsiFieldMember(field);
}
MemberChooser<PsiElementClassMember> chooser = new MemberChooser<>(members, false, true, project);
chooser.setTitle("Choose Fields to Generate Constructor Parameters for");
chooser.show();
if (chooser.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
final List<PsiElementClassMember> selectedElements = chooser.getSelectedElements();
if (selectedElements == null)
return;
fields.clear();
for (PsiElementClassMember member : selectedElements) {
fields.add((PsiField) member.getElement());
}
}
addParameterToConstructor(project, file, editor, constructor, filtered.size() == constructors.length ? fields.toArray(new PsiField[fields.size()]) : new PsiField[] { getField() }, cleanupElements);
} finally {
fieldsToFix.clear();
}
}
GlobalInspectionContextBase.cleanupElements(project, null, cleanupElements);
}
use of com.intellij.codeInsight.generation.PsiMethodMember in project intellij-community by JetBrains.
the class GroovyOverrideImplementUtil method chooseAndOverrideOrImplementMethods.
public static void chooseAndOverrideOrImplementMethods(@NotNull Project project, @NotNull final Editor editor, @NotNull final GrTypeDefinition aClass, boolean toImplement) {
LOG.assertTrue(aClass.isValid());
ApplicationManager.getApplication().assertReadAccessAllowed();
Collection<CandidateInfo> candidates = GroovyOverrideImplementExploreUtil.getMethodsToOverrideImplement(aClass, toImplement);
Collection<CandidateInfo> secondary = toImplement || aClass.isInterface() ? ContainerUtil.<CandidateInfo>newArrayList() : GroovyOverrideImplementExploreUtil.getMethodsToOverrideImplement(aClass, true);
if (toImplement) {
for (Iterator<CandidateInfo> iterator = candidates.iterator(); iterator.hasNext(); ) {
CandidateInfo candidate = iterator.next();
PsiElement element = candidate.getElement();
if (element instanceof GrMethod) {
GrMethod method = (GrMethod) element;
if (GrTraitUtil.isTrait(method.getContainingClass()) && !GrTraitUtil.isMethodAbstract(method)) {
iterator.remove();
secondary.add(candidate);
}
}
}
}
final MemberChooser<PsiMethodMember> chooser = OverrideImplementUtil.showOverrideImplementChooser(editor, aClass, toImplement, candidates, secondary);
if (chooser == null)
return;
final List<PsiMethodMember> selectedElements = chooser.getSelectedElements();
if (selectedElements == null || selectedElements.isEmpty())
return;
LOG.assertTrue(aClass.isValid());
new WriteCommandAction(project, aClass.getContainingFile()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor, aClass, selectedElements, chooser.isCopyJavadoc(), chooser.isInsertOverrideAnnotation());
}
}.execute();
}
Aggregations