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