use of com.intellij.refactoring.memberPullUp.PullUpProcessor in project intellij-community by JetBrains.
the class ExtractInterfaceHandler method extractInterface.
static PsiClass extractInterface(PsiDirectory targetDir, PsiClass aClass, String interfaceName, MemberInfo[] selectedMembers, DocCommentPolicy javaDocPolicy) throws IncorrectOperationException {
final Project project = aClass.getProject();
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(ExtractSuperClassUtil.REFACTORING_EXTRACT_SUPER_ID, ExtractSuperClassUtil.createBeforeData(aClass, selectedMembers));
final PsiClass anInterface = JavaDirectoryService.getInstance().createInterface(targetDir, interfaceName);
try {
PsiJavaCodeReferenceElement ref = ExtractSuperClassUtil.createExtendingReference(anInterface, aClass, selectedMembers);
final PsiReferenceList referenceList = aClass.isInterface() ? aClass.getExtendsList() : aClass.getImplementsList();
assert referenceList != null;
CodeStyleManager.getInstance(project).reformat(referenceList.add(ref));
PullUpProcessor pullUpHelper = new PullUpProcessor(aClass, anInterface, selectedMembers, javaDocPolicy);
pullUpHelper.moveMembersToBase();
return anInterface;
} finally {
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(ExtractSuperClassUtil.REFACTORING_EXTRACT_SUPER_ID, ExtractSuperClassUtil.createAfterData(anInterface));
}
}
use of com.intellij.refactoring.memberPullUp.PullUpProcessor in project intellij-community by JetBrains.
the class PullUpTest method doTest.
private void doTest(final boolean checkMembersMovedCount, String conflictMessage, RefactoringTestUtil.MemberDescriptor... membersToFind) {
final MultiMap<PsiElement, String> conflictsMap = new MultiMap<>();
configureByFile(BASE_PATH + getTestName(false) + ".java");
PsiElement elementAt = getFile().findElementAt(getEditor().getCaretModel().getOffset());
final PsiClass sourceClass = PsiTreeUtil.getParentOfType(elementAt, PsiClass.class);
assertNotNull(sourceClass);
PsiClass targetClass = sourceClass.getSuperClass();
assertNotNull(targetClass);
if (!targetClass.isWritable()) {
final PsiClass[] interfaces = sourceClass.getInterfaces();
assertEquals(1, interfaces.length);
assertTrue(interfaces[0].isWritable());
targetClass = interfaces[0];
}
final MemberInfo[] infos = RefactoringTestUtil.findMembers(sourceClass, membersToFind);
final int[] countMoved = { 0 };
final MoveMemberListener listener = (aClass, member) -> {
assertEquals(sourceClass, aClass);
countMoved[0]++;
};
JavaRefactoringListenerManager.getInstance(getProject()).addMoveMembersListener(listener);
final PsiDirectory targetDirectory = targetClass.getContainingFile().getContainingDirectory();
final PsiPackage targetPackage = targetDirectory != null ? JavaDirectoryService.getInstance().getPackage(targetDirectory) : null;
conflictsMap.putAllValues(PullUpConflictsUtil.checkConflicts(infos, sourceClass, targetClass, targetPackage, targetDirectory, psiMethod -> PullUpProcessor.checkedInterfacesContain(Arrays.asList(infos), psiMethod)));
final PullUpProcessor helper = new PullUpProcessor(sourceClass, targetClass, infos, new DocCommentPolicy(DocCommentPolicy.ASIS));
helper.run();
UIUtil.dispatchAllInvocationEvents();
JavaRefactoringListenerManager.getInstance(getProject()).removeMoveMembersListener(listener);
if (conflictMessage != null && conflictsMap.isEmpty()) {
fail("Conflict was not detected");
}
if (conflictMessage == null && !conflictsMap.isEmpty()) {
fail(conflictsMap.values().iterator().next());
}
if (conflictMessage != null && !IGNORE_CONFLICTS.equals(conflictMessage)) {
TreeSet<String> conflicts = new TreeSet<>(conflictsMap.values());
assertEquals(conflictMessage, conflicts.iterator().next());
return;
}
if (checkMembersMovedCount) {
assertEquals(countMoved[0], membersToFind.length);
}
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
use of com.intellij.refactoring.memberPullUp.PullUpProcessor in project intellij-community by JetBrains.
the class ExtractSuperClassUtil method extractSuperClass.
public static PsiClass extractSuperClass(final Project project, final PsiDirectory targetDirectory, final String superclassName, final PsiClass subclass, final MemberInfo[] selectedMemberInfos, final DocCommentPolicy javaDocPolicy) throws IncorrectOperationException {
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_EXTRACT_SUPER_ID, createBeforeData(subclass, selectedMemberInfos));
final PsiClass superclass = JavaDirectoryService.getInstance().createClass(targetDirectory, superclassName);
try {
final PsiModifierList superClassModifierList = superclass.getModifierList();
assert superClassModifierList != null;
superClassModifierList.setModifierProperty(PsiModifier.FINAL, false);
final PsiReferenceList subClassExtends = subclass.getExtendsList();
if (subClassExtends != null) {
copyPsiReferenceList(subClassExtends, superclass.getExtendsList());
} else if (subclass instanceof PsiAnonymousClass) {
PsiJavaCodeReferenceElement classReference = ((PsiAnonymousClass) subclass).getBaseClassReference();
PsiElement baseClass = classReference.resolve();
if (baseClass instanceof PsiClass && ((PsiClass) baseClass).isInterface()) {
superclass.getImplementsList().add(classReference);
} else {
superclass.getExtendsList().add(classReference);
}
}
// create constructors if neccesary
PsiMethod[] constructors = getCalledBaseConstructors(subclass);
if (constructors.length > 0) {
createConstructorsByPattern(project, superclass, constructors);
}
// clear original class' "extends" list
if (subClassExtends != null) {
clearPsiReferenceList(subclass.getExtendsList());
}
// make original class extend extracted superclass
PsiJavaCodeReferenceElement ref = createExtendingReference(superclass, subclass, selectedMemberInfos);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
if (subClassExtends != null) {
codeStyleManager.reformat(subclass.getExtendsList().add(ref));
} else if (subclass instanceof PsiAnonymousClass) {
codeStyleManager.reformat(((PsiAnonymousClass) subclass).getBaseClassReference().replace(ref));
}
PullUpProcessor pullUpHelper = new PullUpProcessor(subclass, superclass, selectedMemberInfos, javaDocPolicy);
pullUpHelper.moveMembersToBase();
pullUpHelper.moveFieldInitializations();
Collection<MethodSignature> toImplement = OverrideImplementExploreUtil.getMethodSignaturesToImplement(superclass);
if (!toImplement.isEmpty()) {
superClassModifierList.setModifierProperty(PsiModifier.ABSTRACT, true);
}
return superclass;
} finally {
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_EXTRACT_SUPER_ID, createAfterData(superclass));
}
}
use of com.intellij.refactoring.memberPullUp.PullUpProcessor 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();
}
use of com.intellij.refactoring.memberPullUp.PullUpProcessor in project intellij-community by JetBrains.
the class PullUpMultifileTest method doTest.
private void doTest(final String... conflicts) throws Exception {
final MultiMap<PsiElement, String> conflictsMap = new MultiMap<>();
doTest((rootDir, rootAfter) -> {
final PsiClass srcClass = myJavaFacade.findClass("a.A", GlobalSearchScope.allScope(myProject));
assertTrue("Source class not found", srcClass != null);
final PsiClass targetClass = myJavaFacade.findClass("b.B", GlobalSearchScope.allScope(myProject));
assertTrue("Target class not found", targetClass != null);
final PsiMethod[] methods = srcClass.getMethods();
assertTrue("No methods found", methods.length > 0);
final MemberInfo[] membersToMove = new MemberInfo[1];
final MemberInfo memberInfo = new MemberInfo(methods[0]);
memberInfo.setChecked(true);
membersToMove[0] = memberInfo;
final PsiDirectory targetDirectory = targetClass.getContainingFile().getContainingDirectory();
final PsiPackage targetPackage = targetDirectory != null ? JavaDirectoryService.getInstance().getPackage(targetDirectory) : null;
conflictsMap.putAllValues(PullUpConflictsUtil.checkConflicts(membersToMove, srcClass, targetClass, targetPackage, targetDirectory, psiMethod -> PullUpProcessor.checkedInterfacesContain(Arrays.asList(membersToMove), psiMethod)));
new PullUpProcessor(srcClass, targetClass, membersToMove, new DocCommentPolicy(DocCommentPolicy.ASIS)).run();
});
if (conflicts.length != 0 && conflictsMap.isEmpty()) {
fail("Conflict was not detected");
}
final HashSet<String> values = new HashSet<>(conflictsMap.values());
final HashSet<String> expected = new HashSet<>(Arrays.asList(conflicts));
assertEquals(expected.size(), values.size());
for (String value : values) {
if (!expected.contains(value)) {
fail("Conflict: " + value + " is unexpectedly reported");
}
}
}
Aggregations