Пример #1
0
  private void fixReferencesToStatic(PsiElement classMember) throws IncorrectOperationException {
    final StaticReferencesCollector collector = new StaticReferencesCollector();
    classMember.accept(collector);
    ArrayList<PsiJavaCodeReferenceElement> refs = collector.getReferences();
    ArrayList<PsiElement> members = collector.getReferees();
    ArrayList<PsiClass> classes = collector.getRefereeClasses();
    PsiElementFactory factory =
        JavaPsiFacade.getInstance(classMember.getProject()).getElementFactory();

    for (int i = 0; i < refs.size(); i++) {
      PsiJavaCodeReferenceElement ref = refs.get(i);
      PsiElement namedElement = members.get(i);
      PsiClass aClass = classes.get(i);

      if (namedElement instanceof PsiNamedElement) {
        PsiReferenceExpression newRef =
            (PsiReferenceExpression)
                factory.createExpressionFromText(
                    "a." + ((PsiNamedElement) namedElement).getName(), null);
        PsiExpression qualifierExpression = newRef.getQualifierExpression();
        assert qualifierExpression != null;
        qualifierExpression =
            (PsiExpression) qualifierExpression.replace(factory.createReferenceExpression(aClass));
        qualifierExpression.putCopyableUserData(PRESERVE_QUALIFIER, ref.isQualified());
        ref.replace(newRef);
      }
    }
  }
 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);
 }
  protected void changeExternalUsage(UsageInfo usage, PsiElementFactory factory)
      throws IncorrectOperationException {
    final PsiElement element = usage.getElement();
    if (!(element instanceof PsiReferenceExpression)) return;

    PsiReferenceExpression methodRef = (PsiReferenceExpression) element;
    PsiElement parent = methodRef.getParent();

    PsiExpression instanceRef;

    instanceRef = methodRef.getQualifierExpression();
    PsiElement newQualifier;

    final PsiClass memberClass = myMember.getContainingClass();
    if (instanceRef == null || instanceRef instanceof PsiSuperExpression) {
      PsiClass contextClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
      if (!InheritanceUtil.isInheritorOrSelf(contextClass, memberClass, true)) {
        instanceRef =
            factory.createExpressionFromText(memberClass.getQualifiedName() + ".this", null);
      } else {
        instanceRef = factory.createExpressionFromText("this", null);
      }
      newQualifier = null;
    } else {
      newQualifier = factory.createReferenceExpression(memberClass);
    }

    if (mySettings.getNewParametersNumber() > 1) {
      int copyingSafetyLevel = RefactoringUtil.verifySafeCopyExpression(instanceRef);
      if (copyingSafetyLevel == RefactoringUtil.EXPR_COPY_PROHIBITED) {
        String tempVar = RefactoringUtil.createTempVar(instanceRef, parent, true);
        instanceRef = factory.createExpressionFromText(tempVar, null);
      }
    }

    PsiElement anchor = null;
    PsiExpressionList argList = null;
    PsiExpression[] exprs = PsiExpression.EMPTY_ARRAY;
    if (parent instanceof PsiMethodCallExpression) {
      argList = ((PsiMethodCallExpression) parent).getArgumentList();
      exprs = argList.getExpressions();
      if (mySettings.isMakeClassParameter()) {
        if (exprs.length > 0) {
          anchor = argList.addBefore(instanceRef, exprs[0]);
        } else {
          anchor = argList.add(instanceRef);
        }
      }
    }

    if (mySettings.isMakeFieldParameters()) {
      List<Settings.FieldParameter> parameters = mySettings.getParameterOrderList();

      for (Settings.FieldParameter fieldParameter : parameters) {
        PsiReferenceExpression fieldRef;
        if (newQualifier != null) {
          fieldRef =
              (PsiReferenceExpression)
                  factory.createExpressionFromText("a." + fieldParameter.field.getName(), null);
          fieldRef.getQualifierExpression().replace(instanceRef);
        } else {
          fieldRef =
              (PsiReferenceExpression)
                  factory.createExpressionFromText(fieldParameter.field.getName(), null);
        }

        if (anchor != null) {
          anchor = argList.addAfter(fieldRef, anchor);
        } else if (argList != null) {
          if (exprs.length > 0) {
            anchor = argList.addBefore(fieldRef, exprs[0]);
          } else {
            anchor = argList.add(fieldRef);
          }
        }
      }
    }

    if (newQualifier != null) {
      methodRef.getQualifierExpression().replace(newQualifier);
    }
  }