コード例 #1
0
  protected boolean preprocessUsages(Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<PsiElement, String>();

    AnySameNameVariables anySameNameVariables = new AnySameNameVariables();
    myMethodToReplaceIn.accept(anySameNameVariables);
    final Pair<PsiElement, String> conflictPair = anySameNameVariables.getConflict();
    if (conflictPair != null) {
      conflicts.putValue(conflictPair.first, conflictPair.second);
    }

    if (!myGenerateDelegate) {
      detectAccessibilityConflicts(usagesIn, conflicts);
    }

    if (myParameterInitializer != null
        && !myMethodToReplaceIn.hasModifierProperty(PsiModifier.PRIVATE)) {
      final AnySupers anySupers = new AnySupers();
      myParameterInitializer.accept(anySupers);
      if (anySupers.isResult()) {
        for (UsageInfo usageInfo : usagesIn) {
          if (!(usageInfo.getElement() instanceof PsiMethod)
              && !(usageInfo instanceof InternalUsageInfo)) {
            if (!PsiTreeUtil.isAncestor(
                myMethodToReplaceIn.getContainingClass(), usageInfo.getElement(), false)) {
              conflicts.putValue(
                  myParameterInitializer,
                  RefactoringBundle.message(
                      "parameter.initializer.contains.0.but.not.all.calls.to.method.are.in.its.class",
                      CommonRefactoringUtil.htmlEmphasize(PsiKeyword.SUPER)));
              break;
            }
          }
        }
      }
    }

    for (IntroduceParameterMethodUsagesProcessor processor :
        IntroduceParameterMethodUsagesProcessor.EP_NAME.getExtensions()) {
      processor.findConflicts(this, refUsages.get(), conflicts);
    }

    myHasConflicts = !conflicts.isEmpty();
    return showConflicts(conflicts, usagesIn);
  }
コード例 #2
0
 private void detectAccessibilityConflicts(
     final UsageInfo[] usageArray, MultiMap<PsiElement, String> conflicts) {
   if (myParameterInitializer != null) {
     final ReferencedElementsCollector collector = new ReferencedElementsCollector();
     myParameterInitializer.accept(collector);
     final Set<PsiElement> result = collector.myResult;
     if (!result.isEmpty()) {
       for (final UsageInfo usageInfo : usageArray) {
         if (usageInfo instanceof ExternalUsageInfo
             && IntroduceParameterUtil.isMethodUsage(usageInfo)) {
           final PsiElement place = usageInfo.getElement();
           for (PsiElement element : result) {
             if (element instanceof PsiField
                 && myReplaceFieldsWithGetters
                     != IntroduceParameterRefactoring
                         .REPLACE_FIELDS_WITH_GETTERS_NONE) { // check getter access instead
               final PsiClass psiClass = ((PsiField) element).getContainingClass();
               LOG.assertTrue(psiClass != null);
               final PsiMethod method =
                   psiClass.findMethodBySignature(
                       PropertyUtil.generateGetterPrototype((PsiField) element), true);
               if (method != null) {
                 element = method;
               }
             }
             if (element instanceof PsiMember
                 && !JavaPsiFacade.getInstance(myProject)
                     .getResolveHelper()
                     .isAccessible((PsiMember) element, place, null)) {
               String message =
                   RefactoringBundle.message(
                       "0.is.not.accessible.from.1.value.for.introduced.parameter.in.that.method.call.will.be.incorrect",
                       RefactoringUIUtil.getDescription(element, true),
                       RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(place), true));
               conflicts.putValue(element, message);
             }
           }
         }
       }
     }
   }
 }
コード例 #3
0
  private PsiExpression replaceRefsToTargetVariable(final PsiExpression expression) {
    final PsiManager manager = expression.getManager();
    if (expression instanceof PsiReferenceExpression
        && ((PsiReferenceExpression) expression).isReferenceTo(myTargetVariable)) {
      return createThisExpr(manager);
    }

    expression.accept(
        new JavaRecursiveElementVisitor() {
          @Override
          public void visitReferenceExpression(PsiReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            if (expression.isReferenceTo(myTargetVariable)) {
              try {
                expression.replace(createThisExpr(manager));
              } catch (IncorrectOperationException e) {
                LOG.error(e);
              }
            }
          }
        });

    return expression;
  }