From 03da5e386816ef0a4c48f5cf3a5348554301bf8d Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 30 Mar 2011 20:51:19 +0200 Subject: [PATCH] Move inter-type members to classes and aspects --- .../moveMembers/MoveMembersProcessor.java | 46 +++++++++---------- .../util/RefactoringConflictsUtil.java | 38 +++++++-------- .../impl/source/resolve/FileContextUtil.java | 18 +++++--- 3 files changed, 52 insertions(+), 50 deletions(-) diff --git a/java/java-impl/src/com/intellij/refactoring/move/moveMembers/MoveMembersProcessor.java b/java/java-impl/src/com/intellij/refactoring/move/moveMembers/MoveMembersProcessor.java index 013d4b5fa3b7..5560118bd8f2 100644 --- a/java/java-impl/src/com/intellij/refactoring/move/moveMembers/MoveMembersProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/move/moveMembers/MoveMembersProcessor.java @@ -13,11 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/** - * created at Sep 11, 2001 - * @author Jeka - */ package com.intellij.refactoring.move.moveMembers; import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector; @@ -52,6 +47,10 @@ import org.jetbrains.annotations.NotNull; import java.util.*; +/** + * created at Sep 11, 2001 + * @author Jeka + */ public class MoveMembersProcessor extends BaseRefactoringProcessor { private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.move.moveMembers.MoveMembersProcessor"); @@ -60,7 +59,6 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { private final MoveCallback myMoveCallback; private String myNewVisibility; // "null" means "as is" private String myCommandName = MoveMembersImpl.REFACTORING_NAME; - private boolean myMakeEnumConstant; private MoveMembersOptions myOptions; public MoveMembersProcessor(Project project, MoveCallback moveCallback, MoveMembersOptions options) { @@ -79,17 +77,15 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { private void setOptions(MoveMembersOptions dialog) { myOptions = dialog; + PsiMember[] members = dialog.getSelectedMembers(); myMembersToMove.clear(); ContainerUtil.addAll(myMembersToMove, members); setCommandName(members); - final PsiManager manager = PsiManager.getInstance(myProject); - myTargetClass = - JavaPsiFacade.getInstance(manager.getProject()).findClass(dialog.getTargetClassName(), GlobalSearchScope.projectScope(myProject)); + myTargetClass = JavaPsiFacade.getInstance(myProject).findClass(dialog.getTargetClassName(), GlobalSearchScope.projectScope(myProject)); myNewVisibility = dialog.getMemberVisibility(); - myMakeEnumConstant = dialog.makeEnumConstant(); } private void setCommandName(final PsiMember[] members) { @@ -108,6 +104,7 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { myCommandName = commandName.toString(); } + @NotNull protected UsageViewDescriptor createUsageViewDescriptor(UsageInfo[] usages) { return new MoveMemberViewDescriptor(PsiUtilBase.toPsiElementArray(myMembersToMove)); } @@ -156,8 +153,8 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { protected void performRefactoring(final UsageInfo[] usages) { try { // correct references to moved members from the outside - PsiClass targetClass = JavaPsiFacade.getInstance(myProject) - .findClass(myOptions.getTargetClassName(), GlobalSearchScope.projectScope(myProject)); + PsiClass targetClass = JavaPsiFacade.getInstance(myProject).findClass(myOptions.getTargetClassName(), + GlobalSearchScope.projectScope(myProject)); if (targetClass == null) return; final Map anchors = new HashMap(); for (PsiMember member : myMembersToMove) { @@ -221,7 +218,7 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { private void fixModifierList(PsiMember newMember, final UsageInfo[] usages) throws IncorrectOperationException { PsiModifierList modifierList = newMember.getModifierList(); - if(myTargetClass.isInterface()) { + if (modifierList != null && myTargetClass.isInterface()) { modifierList.setModifierProperty(PsiModifier.PUBLIC, false); modifierList.setModifierProperty(PsiModifier.PROTECTED, false); modifierList.setModifierProperty(PsiModifier.PRIVATE, false); @@ -231,7 +228,7 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { return; } - if(myNewVisibility == null) return; + if (myNewVisibility == null) return; VisibilityUtil.fixVisibility(usages, newMember, myNewVisibility); } @@ -240,7 +237,7 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { final MultiMap conflicts = new MultiMap(); final UsageInfo[] usages = refUsages.get(); try { - addInaccessiblleConflicts(conflicts, usages); + addInaccessibleConflicts(conflicts, usages); } catch (IncorrectOperationException e) { LOG.error(e); @@ -250,7 +247,7 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { return showConflicts(conflicts, usages); } - private void addInaccessiblleConflicts(final MultiMap conflicts, final UsageInfo[] usages) throws IncorrectOperationException { + private void addInaccessibleConflicts(final MultiMap conflicts, final UsageInfo[] usages) throws IncorrectOperationException { String newVisibility = myNewVisibility; if (VisibilityUtil.ESCALATE_VISIBILITY.equals(newVisibility)) { //Still need to check for access object newVisibility = PsiModifier.PUBLIC; @@ -259,9 +256,9 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { Map modifierListCopies = new HashMap(); for (PsiMember member : myMembersToMove) { PsiModifierList copy = member.getModifierList(); - if (copy!=null) copy= (PsiModifierList)copy.copy(); + if (copy != null) copy = (PsiModifierList)copy.copy(); if (newVisibility != null) { - if (copy!=null) VisibilityUtil.setVisibility(copy, newVisibility); + if (copy != null) VisibilityUtil.setVisibility(copy, newVisibility); } modifierListCopies.put(member, copy); } @@ -336,9 +333,10 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { private static boolean hasMethod(PsiClass targetClass, PsiMethod method) { PsiMethod[] targetClassMethods = targetClass.getMethods(); - for (PsiMethod method1 : targetClassMethods) { - if (MethodSignatureUtil.areSignaturesEqual(method.getSignature(PsiSubstitutor.EMPTY), - method1.getSignature(PsiSubstitutor.EMPTY))) { + for (PsiMethod candidate : targetClassMethods) { + if (candidate != method && + MethodSignatureUtil.areSignaturesEqual(method.getSignature(PsiSubstitutor.EMPTY), + candidate.getSignature(PsiSubstitutor.EMPTY))) { return true; } } @@ -348,8 +346,9 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { private static boolean hasField(PsiClass targetClass, PsiField field) { String fieldName = field.getName(); PsiField[] targetClassFields = targetClass.getFields(); - for (PsiField targetClassField : targetClassFields) { - if (fieldName.equals(targetClassField.getName())) { + for (PsiField candidate : targetClassFields) { + if (candidate != field && + fieldName.equals(candidate.getName())) { return true; } } @@ -377,5 +376,4 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor { reference = element; } } - } diff --git a/java/java-impl/src/com/intellij/refactoring/util/RefactoringConflictsUtil.java b/java/java-impl/src/com/intellij/refactoring/util/RefactoringConflictsUtil.java index 9cca54530051..e8b6538af7cd 100644 --- a/java/java-impl/src/com/intellij/refactoring/util/RefactoringConflictsUtil.java +++ b/java/java-impl/src/com/intellij/refactoring/util/RefactoringConflictsUtil.java @@ -13,11 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * User: anna - * Date: 05-Oct-2009 - */ package com.intellij.refactoring.util; import com.intellij.openapi.module.Module; @@ -45,11 +40,16 @@ import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.Set; +/** + * @author anna + * Date: 05-Oct-2009 + */ public class RefactoringConflictsUtil { + private RefactoringConflictsUtil() { } public static void analyzeAccessibilityConflicts(@NotNull Set membersToMove, - @NotNull final PsiClass targetClass, - final MultiMap conflicts, String newVisibility) { + @NotNull final PsiClass targetClass, + final MultiMap conflicts, String newVisibility) { analyzeAccessibilityConflicts(membersToMove, targetClass, conflicts, newVisibility, targetClass, null); } @@ -65,11 +65,11 @@ public class RefactoringConflictsUtil { checkUsedElements(member, member, membersToMove, abstractMethods, targetClass, context, conflicts); PsiModifierList modifierList = member.getModifierList(); - if (modifierList!=null) modifierList= (PsiModifierList)modifierList.copy(); + if (modifierList != null) modifierList = (PsiModifierList)modifierList.copy(); if (newVisibility != null) { try { - if (modifierList!=null) VisibilityUtil.setVisibility(modifierList, newVisibility); + if (modifierList != null) VisibilityUtil.setVisibility(modifierList, newVisibility); } catch (IncorrectOperationException ex) { /* do nothing and hope for the best */ @@ -110,7 +110,7 @@ public class RefactoringConflictsUtil { if (abstractMethods != null) { moving.addAll(abstractMethods); } - if(scope instanceof PsiReferenceExpression) { + if (scope instanceof PsiReferenceExpression) { PsiReferenceExpression refExpr = (PsiReferenceExpression)scope; PsiElement refElement = refExpr.resolve(); if (refElement instanceof PsiMember) { @@ -147,19 +147,17 @@ public class RefactoringConflictsUtil { } } - PsiElement[] children = scope.getChildren(); - for (PsiElement child : children) { - if (!(child instanceof PsiWhiteSpace)) { - checkUsedElements(member, child, membersToMove, abstractMethods, targetClass, context, conflicts); - } + for (PsiElement child : scope.getChildren()) { + if (child instanceof PsiWhiteSpace || child instanceof PsiComment) continue; + checkUsedElements(member, child, membersToMove, abstractMethods, targetClass, context, conflicts); } } public static void checkAccessibility(PsiMember refMember, @NotNull PsiElement newContext, - PsiClass accessClass, - PsiMember member, - MultiMap conflicts) { + PsiClass accessClass, + PsiMember member, + MultiMap conflicts) { if (!PsiUtil.isAccessible(refMember, newContext, accessClass)) { String message = RefactoringBundle.message("0.is.1.and.will.not.be.accessible.from.2.in.the.target.class", RefactoringUIUtil.getDescription(refMember, true), @@ -167,7 +165,8 @@ public class RefactoringConflictsUtil { RefactoringUIUtil.getDescription(member, false)); message = CommonRefactoringUtil.capitalize(message); conflicts.putValue(refMember, message); - } else if (newContext instanceof PsiClass && refMember instanceof PsiField && refMember.getContainingClass() == member.getContainingClass()) { + } + else if (newContext instanceof PsiClass && refMember instanceof PsiField && refMember.getContainingClass() == member.getContainingClass()) { final PsiField fieldInSubClass = ((PsiClass)newContext).findFieldByName(refMember.getName(), false); if (fieldInSubClass != null) { conflicts.putValue(refMember, CommonRefactoringUtil.capitalize(RefactoringUIUtil.getDescription(fieldInSubClass, true) + @@ -253,6 +252,7 @@ public class RefactoringConflictsUtil { if (module != null) { final String message; final PsiElement referencedElement = moveRenameUsageInfo.getReferencedElement(); + assert referencedElement != null : moveRenameUsageInfo; if (module == targetModule && isInTestSources) { message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.production.of.module.2", CommonRefactoringUtil.capitalize( diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/resolve/FileContextUtil.java b/platform/lang-impl/src/com/intellij/psi/impl/source/resolve/FileContextUtil.java index a5da52c6d624..1b572bd3e5ad 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/resolve/FileContextUtil.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/resolve/FileContextUtil.java @@ -13,10 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * @author max - */ package com.intellij.psi.impl.source.resolve; import com.intellij.openapi.util.Key; @@ -24,24 +20,32 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.SmartPsiElementPointer; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +/** + * @author max + */ public class FileContextUtil { public static final Key INJECTED_IN_ELEMENT = Key.create("injectedIn"); + private FileContextUtil() { } + + @Nullable public static PsiElement getFileContext(PsiFile file) { SmartPsiElementPointer pointer = file.getUserData(INJECTED_IN_ELEMENT); return pointer == null ? null : pointer.getElement(); } - public static PsiFile getContextFile(@NotNull PsiElement element){ + @Nullable + public static PsiFile getContextFile(@NotNull PsiElement element) { if (!element.isValid()) return null; PsiFile file = element.getContainingFile(); if (file == null) return null; PsiElement context = file.getContext(); - if (context == null){ + if (context == null) { return file; } - else{ + else { return getContextFile(context); } }