private static void checkInterfaceTarget( MemberInfoBase<? extends GrMember>[] infos, MultiMap<PsiElement, String> conflictsList) { for (MemberInfoBase<? extends GrMember> info : infos) { GrMember member = info.getMember(); if (member instanceof PsiField || member instanceof PsiClass) { if (!member.hasModifierProperty(PsiModifier.STATIC) && !(member instanceof PsiClass && ((PsiClass) member).isInterface())) { String message = RefactoringBundle.message( "0.is.not.static.it.cannot.be.moved.to.the.interface", RefactoringUIUtil.getDescription(member, false)); message = CommonRefactoringUtil.capitalize(message); conflictsList.putValue(member, message); } } if (member instanceof PsiField && ((PsiField) member).getInitializer() == null) { String message = RefactoringBundle.message( "0.is.not.initialized.in.declaration.such.fields.are.not.allowed.in.interfaces", RefactoringUIUtil.getDescription(member, false)); conflictsList.putValue(member, CommonRefactoringUtil.capitalize(message)); } } }
private static boolean isPropertyAccessInStaticMethod(GrReferenceExpression referenceExpression) { if (referenceExpression.getParent() instanceof GrMethodCall || referenceExpression.getQualifier() != null) return false; GrMember context = PsiTreeUtil.getParentOfType( referenceExpression, GrMember.class, true, GrClosableBlock.class); return (context instanceof GrMethod || context instanceof GrClassInitializer) && context.hasModifierProperty(STATIC); }
private static void checkScriptField(AnnotationHolder holder, GrAnnotation annotation) { final PsiAnnotationOwner owner = annotation.getOwner(); final GrMember container = PsiTreeUtil.getParentOfType(((PsiElement) owner), GrMember.class); if (container != null) { if (container.getContainingClass() instanceof GroovyScriptClass) { holder.createErrorAnnotation( annotation, GroovyBundle.message("annotation.field.can.only.be.used.within.a.script.body")); } else { holder.createErrorAnnotation( annotation, GroovyBundle.message("annotation.field.can.only.be.used.within.a.script")); } } }
@Override protected void visitClassMemberReferenceElement(GrMember classMember, GrReferenceElement ref) { if (classMember != null && !willBeMoved(classMember, myMovedMembers)) { final PsiClass containingClass = classMember.getContainingClass(); if (containingClass != null && !PsiUtil.isAccessibleFromPackage(classMember, myTargetPackage) && (classMember.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) || classMember.hasModifierProperty(PsiModifier.PROTECTED) && !mySubClass.isInheritor(containingClass, true))) { myConflicts.putValue( myMember, RefactoringUIUtil.getDescription(classMember, true) + " won't be accessible"); } } }
@Override protected void visitClassMemberReferenceElement( GrMember classMember, GrReferenceElement classMemberReference) { if (classMember.hasModifierProperty( PsiModifier.STATIC) /*&& classMemberReference.isQualified()*/) { if (!myMovedMembers.contains(classMember) && RefactoringHierarchyUtil.isMemberBetween( myTargetSuperClass, mySourceClass, classMember)) { myReferences.add(classMemberReference); myReferees.add(classMember); myRefereeClasses.add(classMember.getContainingClass()); } else if (myMovedMembers.contains(classMember) || myMembersAfterMove.contains(classMember)) { myReferences.add(classMemberReference); myReferees.add(classMember); myRefereeClasses.add(myTargetSuperClass); } } }
@Override protected void visitClassMemberReferenceElement(GrMember classMember, GrReferenceElement ref) { if (classMember != null && RefactoringHierarchyUtil.isMemberBetween(mySuperClass, mySubclass, classMember)) { if (classMember.hasModifierProperty(PsiModifier.STATIC) && !willBeMoved(classMember, myMovedMembers)) { final boolean isAccessible = mySuperClass != null ? PsiUtil.isAccessible(classMember, mySuperClass, null) : myTargetPackage != null ? PsiUtil.isAccessibleFromPackage(classMember, myTargetPackage) : classMember.hasModifierProperty(PsiModifier.PUBLIC); if (!isAccessible) { String message = RefactoringBundle.message( "0.uses.1.which.is.not.accessible.from.the.superclass", RefactoringUIUtil.getDescription(myScope, false), RefactoringUIUtil.getDescription(classMember, true)); message = CommonRefactoringUtil.capitalize(message); myConflictsList.putValue(classMember, message); } return; } if (!myAbstractMethods.contains(classMember) && !willBeMoved(classMember, myMovedMembers)) { if (!existsInSuperClass(classMember)) { String message = RefactoringBundle.message( "0.uses.1.which.is.not.moved.to.the.superclass", RefactoringUIUtil.getDescription(myScope, false), RefactoringUIUtil.getDescription(classMember, true)); message = CommonRefactoringUtil.capitalize(message); myConflictsList.putValue(classMember, message); } } } }
private static void checkSuperclassMembers( PsiClass superClass, MemberInfoBase<? extends GrMember>[] infos, MultiMap<PsiElement, String> conflictsList) { for (MemberInfoBase<? extends GrMember> info : infos) { GrMember member = info.getMember(); boolean isConflict = false; if (member instanceof PsiField) { String name = member.getName(); isConflict = superClass.findFieldByName(name, false) != null; } else if (member instanceof PsiMethod) { PsiSubstitutor superSubstitutor = TypeConversionUtil.getSuperClassSubstitutor( superClass, member.getContainingClass(), PsiSubstitutor.EMPTY); MethodSignature signature = ((PsiMethod) member).getSignature(superSubstitutor); final PsiMethod superClassMethod = MethodSignatureUtil.findMethodBySignature(superClass, signature, false); isConflict = superClassMethod != null; } if (isConflict) { String message = RefactoringBundle.message( "0.already.contains.a.1", RefactoringUIUtil.getDescription(superClass, false), RefactoringUIUtil.getDescription(member, false)); message = CommonRefactoringUtil.capitalize(message); conflictsList.putValue(superClass, message); } if (member instanceof PsiMethod) { final PsiMethod method = (PsiMethod) member; final PsiModifierList modifierList = method.getModifierList(); if (!modifierList.hasModifierProperty(PsiModifier.PRIVATE)) { for (PsiClass subClass : ClassInheritorsSearch.search(superClass)) { if (method.getContainingClass() != subClass) { MethodSignature signature = ((PsiMethod) member) .getSignature( TypeConversionUtil.getSuperClassSubstitutor( superClass, subClass, PsiSubstitutor.EMPTY)); final PsiMethod wouldBeOverriden = MethodSignatureUtil.findMethodBySignature(subClass, signature, false); if (wouldBeOverriden != null && VisibilityUtil.compare( VisibilityUtil.getVisibilityModifier(wouldBeOverriden.getModifierList()), VisibilityUtil.getVisibilityModifier(modifierList)) > 0) { conflictsList.putValue( wouldBeOverriden, CommonRefactoringUtil.capitalize( RefactoringUIUtil.getDescription(method, true) + " in super class would clash with local method from " + RefactoringUIUtil.getDescription(subClass, true))); } } } } } } }
public void moveMembersToBase() throws IncorrectOperationException { final HashSet<PsiMember> movedMembers = ContainerUtil.newHashSet(); myMembersAfterMove = ContainerUtil.newHashSet(); // build aux sets for (GrMemberInfo info : myMembersToMove) { movedMembers.add(info.getMember()); } // correct private member visibility for (GrMemberInfo info : myMembersToMove) { if (info.getMember() instanceof PsiClass && info.getOverrides() != null) continue; setCorrectVisibility(movedMembers, info); GroovyChangeContextUtil.encodeContextInfo(info.getMember()); info.getMember().accept(new QualifiedThisSuperSearcher()); fixReferencesToStatic(info.getMember(), movedMembers); } final PsiSubstitutor substitutor = upDownSuperClassSubstitutor(); // do actual move for (GrMemberInfo info : myMembersToMove) { if (info.getMember() instanceof PsiMethod) { doMoveMethod(substitutor, info); } else if (info.getMember() instanceof GrField) { doMoveField(substitutor, info); } else if (info.getMember() instanceof PsiClass) { doMoveClass(substitutor, info); } } ExplicitSuperDeleter explicitSuperDeleter = new ExplicitSuperDeleter(); for (PsiMember member : myMembersAfterMove) { ((GrMember) member).accept(explicitSuperDeleter); } explicitSuperDeleter.fixSupers(); final QualifiedThisSuperAdjuster qualifiedThisSuperAdjuster = new QualifiedThisSuperAdjuster(); for (PsiMember member : myMembersAfterMove) { ((GrMember) member).accept(qualifiedThisSuperAdjuster); } for (PsiMember member : myMembersAfterMove) { GroovyChangeContextUtil.decodeContextInfo(member, null, null); } final JavaRefactoringListenerManagerImpl listenerManager = (JavaRefactoringListenerManagerImpl) JavaRefactoringListenerManager.getInstance(myProject); for (final PsiMember movedMember : myMembersAfterMove) { ((GroovyPsiElement) movedMember) .accept( new GroovyRecursiveElementVisitor() { @Override public void visitReferenceExpression(GrReferenceExpression referenceExpression) { if (processRef(referenceExpression)) return; super.visitReferenceExpression(referenceExpression); } @Override public void visitCodeReferenceElement(GrCodeReferenceElement refElement) { if (processRef(refElement)) return; super.visitCodeReferenceElement(refElement); } private boolean processRef( @NotNull GrReferenceElement<? extends GroovyPsiElement> refElement) { final PsiElement qualifier = refElement.getQualifier(); if (qualifier != null) { final Boolean preserveQualifier = qualifier.getCopyableUserData(PRESERVE_QUALIFIER); if (preserveQualifier != null && !preserveQualifier) { refElement.setQualifier(null); return true; } } return false; } }); listenerManager.fireMemberMoved(mySourceClass, movedMember); } }