@Nullable
 private static String getCannotRefactorMessage(PsiMember member) {
   if (member == null) {
     return RefactoringBundle.message("locate.caret.inside.a.method");
   }
   if (member instanceof PsiMethod) {
     if (((PsiMethod) member).isConstructor()) {
       return RefactoringBundle.message("replace.with.method.call.does.not.work.for.constructors");
     }
     final PsiCodeBlock body = ((PsiMethod) member).getBody();
     if (body == null) {
       return RefactoringBundle.message("method.does.not.have.a.body", member.getName());
     }
     final PsiStatement[] statements = body.getStatements();
     if (statements.length == 0) {
       return RefactoringBundle.message("method.has.an.empty.body", member.getName());
     }
   } else if (member instanceof PsiField) {
     final PsiField field = (PsiField) member;
     if (field.getInitializer() == null) {
       return "Field " + member.getName() + " doesn't have initializer";
     }
     final PsiClass containingClass = field.getContainingClass();
     if (!field.hasModifierProperty(PsiModifier.FINAL)
         || !field.hasModifierProperty(PsiModifier.STATIC)
         || containingClass == null
         || containingClass.getQualifiedName() == null) {
       return "Replace Duplicates works with constants only";
     }
   } else {
     return "Caret should be inside method or constant";
   }
   return null;
 }
 @Override
 public void visitAnonymousClass(@NotNull PsiAnonymousClass anonymousClass) {
   if (anonymousClass instanceof PsiEnumConstantInitializer) {
     return;
   }
   final PsiMember containingMember =
       PsiTreeUtil.getParentOfType(anonymousClass, PsiMember.class);
   if (containingMember == null || containingMember.hasModifierProperty(PsiModifier.STATIC)) {
     return;
   }
   final PsiJavaCodeReferenceElement reference = anonymousClass.getBaseClassReference();
   if (reference.resolve() == null) {
     // don't warn on broken code
     return;
   }
   final PsiClass containingClass = PsiTreeUtil.getParentOfType(anonymousClass, PsiClass.class);
   if (containingClass == null) {
     return;
   }
   if (containingClass.getContainingClass() != null
       && !containingClass.hasModifierProperty(PsiModifier.STATIC)) {
     // strictly speaking can be named static inner class but not when part of the current
     // containing class
     return;
   }
   final InnerClassReferenceVisitor visitor = new InnerClassReferenceVisitor(anonymousClass);
   anonymousClass.accept(visitor);
   if (!visitor.canInnerClassBeStatic()) {
     return;
   }
   if (hasReferenceToLocalClass(anonymousClass)) {
     return;
   }
   registerClassError(anonymousClass);
 }
 @Override
 public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
   if (FileTypeUtils.isInServerPageFile(expression)) {
     // disable for jsp files IDEADEV-12957
     return;
   }
   super.visitReferenceExpression(expression);
   if (expression.getQualifierExpression() == null) {
     return;
   }
   final PsiElement referenceNameElement = expression.getReferenceNameElement();
   if (referenceNameElement == null) {
     return;
   }
   final PsiElement containingClass = getContainingContextClass(expression);
   if (containingClass == null) {
     return;
   }
   final PsiElement element = expression.resolve();
   if (!(element instanceof PsiMethod || element instanceof PsiField)) {
     return;
   }
   final PsiMember member = (PsiMember) element;
   if (!member.hasModifierProperty(PsiModifier.PRIVATE)) {
     return;
   }
   final PsiClass memberClass = ClassUtils.getContainingClass(member);
   if (memberClass == null) {
     return;
   }
   if (memberClass.equals(containingClass)) {
     return;
   }
   registerError(referenceNameElement, memberClass, member);
 }
  private boolean isAccessible(final PsiElement element) {
    if (!myCheckAccess) return true;
    if (!(element instanceof PsiMember)) return true;

    PsiMember member = (PsiMember) element;
    return JavaPsiFacade.getInstance(element.getProject())
        .getResolveHelper()
        .isAccessible(
            member, member.getModifierList(), myElement, myQualifierClass, myDeclarationHolder);
  }
  @Nullable
  public static String getMemberQualifiedName(PsiMember member) {
    if (member instanceof PsiClass) {
      return ((PsiClass) member).getQualifiedName();
    }

    PsiClass containingClass = member.getContainingClass();
    if (containingClass == null) return null;
    String className = containingClass.getQualifiedName();
    if (className == null) return null;
    return className + "." + member.getName();
  }
示例#6
0
 @Override
 public boolean isAbstractWhenDisabled(MemberInfo member) {
   PsiClass currentSuperClass = getSuperClass();
   if (currentSuperClass == null) return false;
   if (currentSuperClass.isInterface()) {
     final PsiMember psiMember = member.getMember();
     if (psiMember instanceof PsiMethod) {
       return !psiMember.hasModifierProperty(PsiModifier.STATIC);
     }
   }
   return false;
 }
 private static boolean inLambdaInsideClassInitialization(
     @Nullable PsiClass containingClass, PsiClass aClass) {
   PsiMember member = aClass;
   while (member != null) {
     if (member.getContainingClass() == containingClass) {
       return member instanceof PsiField
           || member instanceof PsiMethod && ((PsiMethod) member).isConstructor()
           || member instanceof PsiClassInitializer;
     }
     member = PsiTreeUtil.getParentOfType(member, PsiMember.class, true);
   }
   return false;
 }
 @Override
 public int checkForProblems(@NotNull MemberInfo member) {
   if (member.isChecked()) return OK;
   PsiClass currentSuperClass = getSuperClass();
   if (currentSuperClass != null && currentSuperClass.isInterface()) {
     PsiMember element = member.getMember();
     if (element.hasModifierProperty(PsiModifier.STATIC)) {
       return super.checkForProblems(member);
     }
     return OK;
   } else {
     return super.checkForProblems(member);
   }
 }
 @Override
 @NotNull
 protected UsageInfo[] findUsages() {
   final List<UsageInfo> result = new ArrayList<>();
   for (MemberInfo memberInfo : myMembersToMove) {
     final PsiMember member = memberInfo.getMember();
     if (member.hasModifierProperty(PsiModifier.STATIC)) {
       for (PsiReference reference : ReferencesSearch.search(member)) {
         result.add(new UsageInfo(reference));
       }
     }
   }
   return result.isEmpty() ? UsageInfo.EMPTY_ARRAY : result.toArray(new UsageInfo[result.size()]);
 }
 private static void shortenWithStaticImports(
     PsiElement affectedElement, int startOffset, int endOffset) {
   final int elementOffset = affectedElement.getTextOffset();
   final int finalStartOffset = startOffset + elementOffset;
   final int finalEndOffset = endOffset + elementOffset;
   final List<PsiReferenceExpression> references = new ArrayList<PsiReferenceExpression>();
   final JavaRecursiveElementVisitor collector =
       new JavaRecursiveElementVisitor() {
         @Override
         public void visitReferenceExpression(PsiReferenceExpression expression) {
           final int offset = expression.getTextOffset();
           if (offset > finalEndOffset) {
             return;
           }
           super.visitReferenceExpression(expression);
           if (offset + expression.getTextLength() < finalStartOffset)
             if (expression.getQualifierExpression() == null) {
               return;
             }
           references.add(expression);
         }
       };
   affectedElement.accept(collector);
   for (PsiReferenceExpression expression : references) {
     final PsiElement target = expression.resolve();
     if (!(target instanceof PsiMember)) {
       continue;
     }
     final PsiMember member = (PsiMember) target;
     final PsiClass containingClass = member.getContainingClass();
     if (containingClass == null) {
       continue;
     }
     final String className = containingClass.getQualifiedName();
     if (className == null) {
       continue;
     }
     final String name = member.getName();
     if (name == null) {
       continue;
     }
     if (ImportUtils.addStaticImport(className, name, expression)) {
       final PsiExpression qualifierExpression = expression.getQualifierExpression();
       if (qualifierExpression != null) {
         qualifierExpression.delete();
       }
     }
   }
 }
  @NotNull
  @Override
  protected UsageInfo[] findUsages() {
    final List<UsageInfo> result = new ArrayList<UsageInfo>();
    for (GrMemberInfo info : myMembersToMove) {
      final PsiMember member = info.getMember();
      if (member.hasModifierProperty(PsiModifier.STATIC)) {
        for (PsiReference reference : ReferencesSearch.search(member)) {
          result.add(new UsageInfo(reference));
        }
      }
    }

    return DefaultGroovyMethods.asType(result, UsageInfo[].class);
  }
 public static PsiReferenceExpression qualifyReference(
     PsiReferenceExpression referenceExpression,
     final PsiMember member,
     @Nullable final PsiClass qualifyingClass)
     throws IncorrectOperationException {
   PsiManager manager = referenceExpression.getManager();
   PsiMethodCallExpression methodCallExpression =
       PsiTreeUtil.getParentOfType(referenceExpression, PsiMethodCallExpression.class, true);
   while ((methodCallExpression) != null) {
     if (HighlightUtil.isSuperOrThisMethodCall(methodCallExpression)) {
       return referenceExpression;
     }
     methodCallExpression =
         PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression.class, true);
   }
   PsiReferenceExpression expressionFromText;
   final PsiElementFactory factory =
       JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
   if (qualifyingClass == null) {
     PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
     final PsiClass containingClass = member.getContainingClass();
     if (parentClass != null
         && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
       while (parentClass != null
           && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
         parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true);
       }
       LOG.assertTrue(parentClass != null);
       expressionFromText =
           (PsiReferenceExpression)
               factory.createExpressionFromText("A.this." + member.getName(), null);
       ((PsiThisExpression) expressionFromText.getQualifierExpression())
           .getQualifier()
           .replace(factory.createClassReferenceElement(parentClass));
     } else {
       expressionFromText =
           (PsiReferenceExpression)
               factory.createExpressionFromText("this." + member.getName(), null);
     }
   } else {
     expressionFromText =
         (PsiReferenceExpression) factory.createExpressionFromText("A." + member.getName(), null);
     expressionFromText.setQualifierExpression(factory.createReferenceExpression(qualifyingClass));
   }
   CodeStyleManager codeStyleManager = manager.getCodeStyleManager();
   expressionFromText = (PsiReferenceExpression) codeStyleManager.reformat(expressionFromText);
   return (PsiReferenceExpression) referenceExpression.replace(expressionFromText);
 }
示例#13
0
  @NotNull
  public static SearchScope getMemberUseScope(@NotNull PsiMember member) {
    PsiFile file = member.getContainingFile();
    PsiElement topElement = file == null ? member : file;
    Project project = topElement.getProject();
    final GlobalSearchScope maximalUseScope =
        ResolveScopeManager.getInstance(project).getUseScope(topElement);
    if (isInServerPage(file)) return maximalUseScope;

    PsiClass aClass = member.getContainingClass();
    if (aClass instanceof PsiAnonymousClass
        && !(aClass instanceof PsiEnumConstantInitializer
            && member instanceof PsiMethod
            && member.hasModifierProperty(PsiModifier.PUBLIC)
            && ((PsiMethod) member).findSuperMethods().length > 0)) {
      // member from anonymous class can be called from outside the class
      PsiElement methodCallExpr =
          PsiUtil.isLanguageLevel8OrHigher(aClass)
              ? PsiTreeUtil.getTopmostParentOfType(aClass, PsiStatement.class)
              : PsiTreeUtil.getParentOfType(aClass, PsiMethodCallExpression.class);
      return new LocalSearchScope(methodCallExpr != null ? methodCallExpr : aClass);
    }

    PsiModifierList modifierList = member.getModifierList();
    int accessLevel =
        modifierList == null ? PsiUtil.ACCESS_LEVEL_PUBLIC : PsiUtil.getAccessLevel(modifierList);
    if (accessLevel == PsiUtil.ACCESS_LEVEL_PUBLIC
        || accessLevel == PsiUtil.ACCESS_LEVEL_PROTECTED) {
      return maximalUseScope; // class use scope doesn't matter, since another very visible class
      // can inherit from aClass
    }
    if (accessLevel == PsiUtil.ACCESS_LEVEL_PRIVATE) {
      PsiClass topClass = PsiUtil.getTopLevelClass(member);
      return topClass != null
          ? new LocalSearchScope(topClass)
          : file == null ? maximalUseScope : new LocalSearchScope(file);
    }
    if (file instanceof PsiJavaFile) {
      PsiPackage aPackage =
          JavaPsiFacade.getInstance(project).findPackage(((PsiJavaFile) file).getPackageName());
      if (aPackage != null) {
        SearchScope scope = PackageScope.packageScope(aPackage, false);
        return scope.intersectWith(maximalUseScope);
      }
    }
    return maximalUseScope;
  }
 protected void visitClassMemberReferenceElement(
     PsiMember classMember, PsiJavaCodeReferenceElement classMemberReference) {
   if (classMember.hasModifierProperty(PsiModifier.STATIC)) {
     if (!myMembersToMove.contains(classMember)
         && RefactoringHierarchyUtil.isMemberBetween(
             myTargetSuperClass, mySourceClass, classMember)) {
       myReferences.add(classMemberReference);
       myReferees.add(classMember);
       myRefereeClasses.add(classMember.getContainingClass());
     } else if (myMembersToMove.contains(classMember)
         || myMembersAfterMove.contains(classMember)) {
       myReferences.add(classMemberReference);
       myReferees.add(classMember);
       myRefereeClasses.add(myTargetSuperClass);
     }
   }
 }
示例#15
0
 @Override
 public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
   if (referenceFound) {
     return;
   }
   super.visitReferenceElement(reference);
   if (reference.isQualified()) {
     return;
   }
   final PsiElement target = reference.resolve();
   for (PsiMember member : members) {
     if (member.equals(target)) {
       referenceFound = true;
       return;
     }
   }
 }
示例#16
0
 @Nullable
 public static String getPropertyName(final PsiMember member) {
   if (member instanceof PsiMethod) {
     return getPropertyName((PsiMethod) member);
   } else if (member instanceof PsiField) {
     return member.getName();
   } else return null;
 }
示例#17
0
 public static boolean isStaticallyImported(
     @NotNull PsiMember member, @NotNull PsiElement context) {
   final PsiClass memberClass = member.getContainingClass();
   if (memberClass == null) {
     return false;
   }
   final PsiClass containingClass = PsiTreeUtil.getParentOfType(context, PsiClass.class);
   if (InheritanceUtil.isInheritorOrSelf(containingClass, memberClass, true)) {
     return false;
   }
   final PsiFile psiFile = context.getContainingFile();
   if (!(psiFile instanceof PsiJavaFile)) {
     return false;
   }
   final PsiJavaFile javaFile = (PsiJavaFile) psiFile;
   final PsiImportList importList = javaFile.getImportList();
   if (importList == null) {
     return false;
   }
   final String memberName = member.getName();
   if (memberName == null) {
     return false;
   }
   final PsiImportStatementBase existingImportStatement =
       importList.findSingleImportStatement(memberName);
   if (existingImportStatement instanceof PsiImportStaticStatement) {
     final PsiClass importClass =
         ((PsiImportStaticStatement) existingImportStatement).resolveTargetClass();
     if (InheritanceUtil.isInheritorOrSelf(importClass, memberClass, true)) {
       return true;
     }
   }
   final String memberClassName = memberClass.getQualifiedName();
   if (memberClassName == null) {
     return false;
   }
   final PsiImportStaticStatement onDemandImportStatement =
       findOnDemandImportStaticStatement(importList, memberClassName);
   if (onDemandImportStatement != null) {
     if (!hasOnDemandImportStaticConflict(memberClassName, memberName, context)) {
       return true;
     }
   }
   return false;
 }
  public static boolean isInExcludedPackage(
      @NotNull final PsiMember member, boolean allowInstanceInnerClasses) {
    final String name = PsiUtil.getMemberQualifiedName(member);
    if (name == null) return false;

    if (!member.hasModifierProperty(PsiModifier.STATIC)) {
      if (member instanceof PsiMethod || member instanceof PsiField) {
        return false;
      }
      if (allowInstanceInnerClasses
          && member instanceof PsiClass
          && member.getContainingClass() != null) {
        return false;
      }
    }

    return ProjectCodeInsightSettings.getSettings(member.getProject()).isExcluded(name);
  }
 @NotNull
 private static Map<String, List<Pair<PsiMember, PsiSubstitutor>>> generateMapByList(
     @NotNull final List<Pair<PsiMember, PsiSubstitutor>> list) {
   Map<String, List<Pair<PsiMember, PsiSubstitutor>>> map =
       new THashMap<String, List<Pair<PsiMember, PsiSubstitutor>>>();
   map.put(ALL, list);
   for (final Pair<PsiMember, PsiSubstitutor> info : list) {
     PsiMember element = info.getFirst();
     String currentName = element.getName();
     List<Pair<PsiMember, PsiSubstitutor>> listByName = map.get(currentName);
     if (listByName == null) {
       listByName = new ArrayList<Pair<PsiMember, PsiSubstitutor>>(1);
       map.put(currentName, listByName);
     }
     listByName.add(info);
   }
   return map;
 }
  private static boolean processInstanceMember(
      boolean shouldProcessInstance, @NotNull PsiMember member) {
    if (shouldProcessInstance) return true;

    if (member instanceof GrReflectedMethod) {
      return ((GrReflectedMethod) member).getBaseMethod().hasModifierProperty(PsiModifier.STATIC);
    } else {
      return member.hasModifierProperty(PsiModifier.STATIC);
    }
  }
  @Override
  public InspectionGadgetsFix buildFix(Object... infos) {
    final PsiClass aClass = (PsiClass) infos[0];
    final String className = aClass.getName();
    if (infos.length == 1) {
      return new MakePackagePrivateFix(className, true);
    }
    final PsiMember member = (PsiMember) infos[1];
    @NonNls final String memberName;
    if (member instanceof PsiMethod) {
      final PsiMethod method = (PsiMethod) member;
      if (method.isConstructor()) {}

      memberName = member.getName() + "()";
    } else {
      memberName = member.getName();
    }
    @NonNls final String elementName = className + '.' + memberName;
    return new MakePackagePrivateFix(elementName, false);
  }
 public static Set<String> getAllLookupStrings(@NotNull PsiMember member) {
   Set<String> allLookupStrings = ContainerUtil.newLinkedHashSet();
   String name = member.getName();
   allLookupStrings.add(name);
   PsiClass containingClass = member.getContainingClass();
   while (containingClass != null) {
     final String className = containingClass.getName();
     if (className == null) {
       break;
     }
     name = className + "." + name;
     allLookupStrings.add(name);
     final PsiElement parent = containingClass.getParent();
     if (!(parent instanceof PsiClass)) {
       break;
     }
     containingClass = (PsiClass) parent;
   }
   return allLookupStrings;
 }
 private static boolean referenceExpressionsAreEquivalent(
     PsiReferenceExpression referenceExpression1, PsiReferenceExpression referenceExpression2) {
   final PsiElement element1 = referenceExpression1.resolve();
   final PsiElement element2 = referenceExpression2.resolve();
   if (element1 != null) {
     if (!element1.equals(element2)) {
       return false;
     }
   } else {
     return element2 == null;
   }
   if (element1 instanceof PsiMember) {
     final PsiMember member1 = (PsiMember) element1;
     if (member1.hasModifierProperty(PsiModifier.STATIC)) {
       return true;
     } else if (member1 instanceof PsiClass) {
       return true;
     }
   } else {
     return true;
   }
   final PsiExpression qualifier1 = referenceExpression1.getQualifierExpression();
   final PsiExpression qualifier2 = referenceExpression2.getQualifierExpression();
   if (qualifier1 != null
       && !(qualifier1 instanceof PsiThisExpression || qualifier1 instanceof PsiSuperExpression)) {
     if (qualifier2 == null) {
       return false;
     } else if (!expressionsAreEquivalent(qualifier1, qualifier2)) {
       return false;
     }
   } else {
     if (qualifier2 != null
         && !(qualifier2 instanceof PsiThisExpression
             || qualifier2 instanceof PsiSuperExpression)) {
       return false;
     }
   }
   final String text1 = referenceExpression1.getText();
   final String text2 = referenceExpression2.getText();
   return text1.equals(text2);
 }
示例#24
0
 @Nullable
 public static PsiField findPropertyFieldByMember(final PsiMember psiMember) {
   if (psiMember instanceof PsiField) {
     return (PsiField) psiMember;
   } else if (psiMember instanceof PsiMethod) {
     final PsiMethod psiMethod = (PsiMethod) psiMember;
     final PsiType returnType = psiMethod.getReturnType();
     if (returnType == null) return null;
     final PsiCodeBlock body = psiMethod.getBody();
     final PsiStatement[] statements = body == null ? null : body.getStatements();
     final PsiStatement statement =
         statements == null || statements.length != 1 ? null : statements[0];
     final PsiElement target;
     if (PsiType.VOID.equals(returnType)) {
       final PsiExpression expression =
           statement instanceof PsiExpressionStatement
               ? ((PsiExpressionStatement) statement).getExpression()
               : null;
       target =
           expression instanceof PsiAssignmentExpression
               ? ((PsiAssignmentExpression) expression).getLExpression()
               : null;
     } else {
       target =
           statement instanceof PsiReturnStatement
               ? ((PsiReturnStatement) statement).getReturnValue()
               : null;
     }
     final PsiElement resolved =
         target instanceof PsiReferenceExpression
             ? ((PsiReferenceExpression) target).resolve()
             : null;
     if (resolved instanceof PsiField) {
       final PsiField field = (PsiField) resolved;
       if (psiMember.getContainingClass() == field.getContainingClass()
           || psiMember.getContainingClass().isInheritor(field.getContainingClass(), true))
         return field;
     }
   }
   return null;
 }
 @Override
 protected void visitClassMemberReferenceElement(
     PsiMember classMember, PsiJavaCodeReferenceElement classMemberReference) {
   if (classMember != null && !willBeMoved(classMember, myMovedMembers)) {
     final PsiClass containingClass = classMember.getContainingClass();
     if (containingClass != null) {
       if (!PsiUtil.isAccessibleFromPackage(classMember, myTargetPackage)) {
         if (classMember.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
           myConflicts.putValue(
               myMember,
               RefactoringUIUtil.getDescription(classMember, true) + " won't be accessible");
         } else if (classMember.hasModifierProperty(PsiModifier.PROTECTED)
             && !mySubClass.isInheritor(containingClass, true)) {
           myConflicts.putValue(
               myMember,
               RefactoringUIUtil.getDescription(classMember, true) + " won't be accessible");
         }
       }
     }
   }
 }
  private static void qualify(PsiMember member, PsiElement renamed, String name) {
    if (!(renamed instanceof GrReferenceExpression)) return;

    final PsiClass clazz = member.getContainingClass();
    if (clazz == null) return;

    final GrReferenceExpression refExpr = (GrReferenceExpression) renamed;
    final PsiElement replaced;
    if (member.hasModifierProperty(GrModifier.STATIC)) {
      final GrReferenceExpression newRefExpr =
          GroovyPsiElementFactory.getInstance(member.getProject())
              .createReferenceExpressionFromText(clazz.getQualifiedName() + "." + name);
      replaced = refExpr.replace(newRefExpr);
    } else {
      final PsiClass containingClass = PsiTreeUtil.getParentOfType(renamed, PsiClass.class);
      if (member.getManager().areElementsEquivalent(containingClass, clazz)) {
        final GrReferenceExpression newRefExpr =
            GroovyPsiElementFactory.getInstance(member.getProject())
                .createReferenceExpressionFromText("this." + name);
        replaced = refExpr.replace(newRefExpr);
      } else {
        final GrReferenceExpression newRefExpr =
            GroovyPsiElementFactory.getInstance(member.getProject())
                .createReferenceExpressionFromText(clazz.getQualifiedName() + ".this." + name);
        replaced = refExpr.replace(newRefExpr);
      }
    }
    PsiUtil.shortenReferences((GroovyPsiElement) replaced);
  }
  public GrReferenceExpression bindToElementViaStaticImport(@NotNull PsiMember member) {
    if (getQualifier() != null) {
      throw new IncorrectOperationException("Reference has qualifier");
    }

    if (StringUtil.isEmpty(getReferenceName())) {
      throw new IncorrectOperationException("Reference has empty name");
    }

    PsiClass containingClass = member.getContainingClass();
    if (containingClass == null) {
      throw new IncorrectOperationException("Member has no containing class");
    }
    final PsiFile file = getContainingFile();
    if (file instanceof GroovyFile) {
      GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
      String text = "import static " + containingClass.getQualifiedName() + "." + member.getName();
      final GrImportStatement statement = factory.createImportStatementFromText(text);
      ((GroovyFile) file).addImport(statement);
    }
    return this;
  }
  private void processMethodsDuplicates() {
    ProgressManager.getInstance()
        .runProcessWithProgressSynchronously(
            () ->
                ApplicationManager.getApplication()
                    .runReadAction(
                        () -> {
                          if (!myTargetSuperClass.isValid()) return;
                          final Query<PsiClass> search =
                              ClassInheritorsSearch.search(myTargetSuperClass);
                          final Set<VirtualFile> hierarchyFiles = new HashSet<>();
                          for (PsiClass aClass : search) {
                            final PsiFile containingFile = aClass.getContainingFile();
                            if (containingFile != null) {
                              final VirtualFile virtualFile = containingFile.getVirtualFile();
                              if (virtualFile != null) {
                                hierarchyFiles.add(virtualFile);
                              }
                            }
                          }
                          final Set<PsiMember> methodsToSearchDuplicates = new HashSet<>();
                          for (PsiMember psiMember : myMembersAfterMove) {
                            if (psiMember instanceof PsiMethod
                                && psiMember.isValid()
                                && ((PsiMethod) psiMember).getBody() != null) {
                              methodsToSearchDuplicates.add(psiMember);
                            }
                          }

                          MethodDuplicatesHandler.invokeOnScope(
                              myProject,
                              methodsToSearchDuplicates,
                              new AnalysisScope(myProject, hierarchyFiles),
                              true);
                        }),
            MethodDuplicatesHandler.REFACTORING_NAME,
            true,
            myProject);
  }
 public static GrReferenceExpression qualifyReference(
     GrReferenceExpression referenceExpression,
     final PsiMember member,
     @Nullable final PsiClass qualifyingClass)
     throws IncorrectOperationException {
   PsiManager manager = referenceExpression.getManager();
   GrReferenceExpression expressionFromText;
   final GroovyPsiElementFactory factory =
       GroovyPsiElementFactory.getInstance(referenceExpression.getProject());
   if (qualifyingClass == null) {
     PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
     final PsiClass containingClass = member.getContainingClass();
     if (parentClass != null
         && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
       while (parentClass != null
           && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
         parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true);
       }
       LOG.assertTrue(parentClass != null);
       expressionFromText =
           factory.createReferenceExpressionFromText("A.this." + member.getName());
       //noinspection ConstantConditions
       ((GrReferenceExpression) expressionFromText.getQualifier())
           .getQualifier()
           .replace(factory.createReferenceElementForClass(parentClass));
     } else {
       expressionFromText =
           (GrReferenceExpression) factory.createExpressionFromText("this." + member.getName());
     }
   } else {
     expressionFromText =
         (GrReferenceExpression) factory.createExpressionFromText("A." + member.getName());
     expressionFromText.setQualifier(factory.createReferenceElementForClass(qualifyingClass));
   }
   CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
   expressionFromText = (GrReferenceExpression) codeStyleManager.reformat(expressionFromText);
   return (GrReferenceExpression) referenceExpression.replace(expressionFromText);
 }
 protected void visitClassMemberReferenceElement(
     PsiMember classMember, PsiJavaCodeReferenceElement classMemberReference) {
   if (classMember != null
       && RefactoringHierarchyUtil.isMemberBetween(mySuperClass, mySubclass, classMember)) {
     if (classMember.hasModifierProperty(PsiModifier.STATIC)
         && !willBeMoved(classMember, myMovedMembers)) {
       final boolean isAccessible;
       if (mySuperClass != null) {
         isAccessible = PsiUtil.isAccessible(classMember, mySuperClass, null);
       } else if (myTargetPackage != null) {
         isAccessible = PsiUtil.isAccessibleFromPackage(classMember, myTargetPackage);
       } else {
         isAccessible = 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);
       }
     }
   }
 }