@Override
 public void visitReferenceExpression(GrReferenceExpression expression) {
   super.visitReferenceExpression(expression);
   if (org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isSuperReference(
       expression.getQualifierExpression())) {
     PsiElement resolved = expression.resolve();
     if (resolved == null
         || resolved instanceof PsiMethod && shouldFixSuper((PsiMethod) resolved)) {
       mySupersToDelete.add(expression.getQualifierExpression());
     }
   } else if (org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isSuperReference(expression)) {
     mySupersToChangeToThis.add(expression);
   }
 }
  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;
    if (refExpr.getQualifierExpression() != null) return;

    final PsiElement replaced;
    if (member.hasModifierProperty(PsiModifier.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);
      }
    }
    JavaCodeStyleManager.getInstance(replaced.getProject()).shortenClassReferences(replaced);
  }
  @Nullable
  public static GrExpression getRuntimeQualifier(GrReferenceExpression refExpr) {
    GrExpression qualifier = refExpr.getQualifierExpression();
    if (qualifier != null) return qualifier;

    for (GrClosableBlock closure = PsiTreeUtil.getParentOfType(refExpr, GrClosableBlock.class);
        closure != null;
        closure = PsiTreeUtil.getParentOfType(closure, GrClosableBlock.class)) {

      PsiElement parent = closure.getParent();
      if (parent instanceof GrArgumentList) parent = parent.getParent();
      if (!(parent instanceof GrMethodCall)) continue;

      GrExpression funExpr = ((GrMethodCall) parent).getInvokedExpression();
      if (!(funExpr instanceof GrReferenceExpression)) return funExpr;

      final PsiElement resolved = ((GrReferenceExpression) funExpr).resolve();
      if (!(resolved instanceof PsiMethod)) return funExpr;

      if (resolved instanceof GrGdkMethod
          && isFromDGM((GrGdkMethod) resolved)
          && !GdkMethodUtil.isWithName(((GrGdkMethod) resolved).getStaticMethod().getName())) {
        continue;
      }

      qualifier = ((GrReferenceExpression) funExpr).getQualifierExpression();
      if (qualifier != null) return qualifier;
    }

    return null;
  }
 @Override
 public void visitReferenceExpression(GrReferenceExpression referenceExpression) {
   final PsiElement resolved = referenceExpression.resolve();
   if (resolved instanceof PsiVariable) {
     if (!isStaticFinalField((PsiVariable) resolved)) {
       if (expr instanceof GrClosableBlock) {
         if (!PsiTreeUtil.isContextAncestor(scope, resolved, true)) {
           throw new GrRefactoringError(
               GroovyRefactoringBundle.message("closure.uses.external.variables"));
         }
       } else {
         throw new GrRefactoringError(
             RefactoringBundle.message("selected.expression.cannot.be.a.constant.initializer"));
       }
     }
   } else if (resolved instanceof PsiMethod
       && ((PsiMethod) resolved).getContainingClass() != null) {
     final GrExpression qualifier = referenceExpression.getQualifierExpression();
     if (qualifier == null
         || (qualifier instanceof GrReferenceExpression
             && ((GrReferenceExpression) qualifier).resolve() instanceof PsiClass)) {
       if (!((PsiMethod) resolved).hasModifierProperty(PsiModifier.STATIC)) {
         throw new GrRefactoringError(
             RefactoringBundle.message("selected.expression.cannot.be.a.constant.initializer"));
       }
     }
   }
 }
 @Nullable
 private static PsiType getTypeFromMapAccess(@NotNull GrReferenceExpression ref) {
   // map access
   GrExpression qualifier = ref.getQualifierExpression();
   if (qualifier instanceof GrReferenceExpression) {
     if (((GrReferenceExpression) qualifier).resolve() instanceof PsiClass) return null;
   }
   if (qualifier != null) {
     PsiType qType = qualifier.getType();
     if (qType instanceof PsiClassType) {
       PsiClassType.ClassResolveResult qResult = ((PsiClassType) qType).resolveGenerics();
       PsiClass clazz = qResult.getElement();
       if (clazz != null) {
         PsiClass mapClass =
             JavaPsiFacade.getInstance(ref.getProject())
                 .findClass(CommonClassNames.JAVA_UTIL_MAP, ref.getResolveScope());
         if (mapClass != null && mapClass.getTypeParameters().length == 2) {
           PsiSubstitutor substitutor =
               TypeConversionUtil.getClassSubstitutor(mapClass, clazz, qResult.getSubstitutor());
           if (substitutor != null) {
             PsiType substituted = substitutor.substitute(mapClass.getTypeParameters()[1]);
             if (substituted != null) {
               return PsiImplUtil.normalizeWildcardTypeByPosition(substituted, ref);
             }
           }
         }
       }
     }
   }
   return null;
 }
    @Override
    public void visitMethodCallExpression(GrMethodCallExpression grMethodCallExpression) {
      super.visitMethodCallExpression(grMethodCallExpression);
      final GrArgumentList args = grMethodCallExpression.getArgumentList();
      if (args.getExpressionArguments().length != 1) {
        return;
      }
      if (PsiImplUtil.hasNamedArguments(args)) {
        return;
      }
      final GrExpression methodExpression = grMethodCallExpression.getInvokedExpression();
      if (!(methodExpression instanceof GrReferenceExpression)) {
        return;
      }
      final GrReferenceExpression referenceExpression = (GrReferenceExpression) methodExpression;
      final String name = referenceExpression.getReferenceName();
      if (!"get".equals(name)) {
        return;
      }
      final GrExpression qualifier = referenceExpression.getQualifierExpression();

      if (qualifier == null || PsiUtil.isThisOrSuperRef(qualifier)) {
        return;
      }

      if (referenceExpression.getDotTokenType() == GroovyTokenTypes.mOPTIONAL_DOT) return;
      final PsiType type = qualifier.getType();
      if (!InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP)) {
        return;
      }
      registerMethodCallError(grMethodCallExpression);
    }
  private void fixReferencesToStatic(GroovyPsiElement classMember, Set<PsiMember> movedMembers)
      throws IncorrectOperationException {
    final StaticReferencesCollector collector = new StaticReferencesCollector(movedMembers);
    classMember.accept(collector);
    ArrayList<GrReferenceElement> refs = collector.getReferences();
    ArrayList<PsiElement> members = collector.getReferees();
    ArrayList<PsiClass> classes = collector.getRefereeClasses();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);

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

      if (namedElement instanceof PsiNamedElement) {
        GrReferenceExpression newRef =
            (GrReferenceExpression)
                factory.createExpressionFromText(
                    "a." + ((PsiNamedElement) namedElement).getName(), null);
        GrExpression qualifier = newRef.getQualifierExpression();
        assert qualifier != null;
        qualifier =
            (GrExpression)
                qualifier.replace(
                    factory.createReferenceExpressionFromText(aClass.getQualifiedName()));
        qualifier.putCopyableUserData(PRESERVE_QUALIFIER, ref.isQualified());
        PsiElement replaced = ref.replace(newRef);
        JavaCodeStyleManager.getInstance(myProject).shortenClassReferences(replaced);
      }
    }
  }
    public void visitAssignmentExpression(@NotNull GrAssignmentExpression assignment) {
      super.visitAssignmentExpression(assignment);

      final IElementType sign = assignment.getOperationTokenType();
      if (!sign.equals(GroovyTokenTypes.mASSIGN)) {
        return;
      }
      final GrExpression lhs = assignment.getLValue();
      final GrExpression rhs = assignment.getRValue();
      if (rhs == null) {
        return;
      }
      if (!(rhs instanceof GrReferenceExpression) || !(lhs instanceof GrReferenceExpression)) {
        return;
      }
      final GrReferenceExpression rhsReference = (GrReferenceExpression) rhs;
      final GrReferenceExpression lhsReference = (GrReferenceExpression) lhs;
      final GrExpression rhsQualifier = rhsReference.getQualifierExpression();
      final GrExpression lhsQualifier = lhsReference.getQualifierExpression();
      if (rhsQualifier != null || lhsQualifier != null) {
        if (!EquivalenceChecker.expressionsAreEquivalent(rhsQualifier, lhsQualifier)) {
          return;
        }
      }
      final String rhsName = rhsReference.getReferenceName();
      final String lhsName = lhsReference.getReferenceName();
      if (rhsName == null || lhsName == null) {
        return;
      }
      if (!rhsName.equals(lhsName)) {
        return;
      }
      final PsiElement rhsReferent = rhsReference.resolve();
      final PsiElement lhsReferent = lhsReference.resolve();
      if (rhsReferent == null || lhsReferent == null || !rhsReferent.equals(lhsReferent)) {
        return;
      }
      registerError(assignment);
    }
 @Override
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement referenceName = descriptor.getPsiElement();
   final GrReferenceExpression invokedExpression =
       (GrReferenceExpression) referenceName.getParent();
   final GrMethodCallExpression callExpression =
       (GrMethodCallExpression) invokedExpression.getParent();
   final GrArgumentList args = callExpression.getArgumentList();
   final GrExpression arg = args.getExpressionArguments()[0];
   replaceExpression(
       callExpression,
       invokedExpression.getQualifierExpression().getText() + '[' + arg.getText() + ']');
 }
Exemple #10
0
 public static int getPrecendence(GrExpression expression) {
   if (expression instanceof GrThisReferenceExpression
       || expression instanceof GrLiteral
       || expression instanceof GrSuperReferenceExpression) {
     return LITERAL_PRECEDENCE;
   }
   if (expression instanceof GrReferenceExpression) {
     final GrReferenceExpression referenceExpression = (GrReferenceExpression) expression;
     if (referenceExpression.getQualifierExpression() != null) {
       return METHOD_CALL_PRECEDENCE;
     } else {
       return LITERAL_PRECEDENCE;
     }
   }
   if (expression instanceof GrMethodCallExpression) {
     return METHOD_CALL_PRECEDENCE;
   }
   if (expression instanceof GrTypeCastExpression || expression instanceof GrNewExpression) {
     return TYPE_CAST_PRECEDENCE;
   }
   if (expression instanceof GrPostfixExpression) {
     return POSTFIX_PRECEDENCE;
   }
   if (expression instanceof GrUnaryExpression) {
     return PREFIX_PRECEDENCE;
   }
   if (expression instanceof GrBinaryExpression) {
     final GrBinaryExpression binaryExpression = (GrBinaryExpression) expression;
     final IElementType sign = binaryExpression.getOperationTokenType();
     if (sign != null) return precedenceForBinaryOperator(sign);
   }
   if (expression instanceof GrConditionalExpression) {
     return CONDITIONAL_PRECEDENCE;
   }
   if (expression instanceof GrAssignmentExpression) {
     return ASSIGNMENT_PRECEDENCE;
   }
   if (expression instanceof GrParenthesizedExpression) {
     return PARENTHESIZED_PRECEDENCE;
   }
   return -1;
 }
  @NotNull
  @Override
  public ThreeState shouldFocusLookup(@NotNull CompletionParameters parameters) {
    final PsiElement position = parameters.getPosition();

    if (position.getParent() instanceof GrReferenceElement
        && psiElement()
            .afterLeaf(psiElement().withText("(").withParent(GrForStatement.class))
            .accepts(position)) {
      return ThreeState.NO;
    }

    if (position.getParent() instanceof GrReferenceExpression) {
      final GrReferenceExpression ref = (GrReferenceExpression) position.getParent();
      final GrExpression qualifier = ref.getQualifierExpression();
      if (qualifier == null) {
        if (isPossibleClosureParameter(ref)) return ThreeState.NO;
        if (parameters.getOriginalFile().getUserData(GroovyShellAction.GROOVY_SHELL_FILE)
            == Boolean.TRUE) {
          return ThreeState.NO;
        }

        GrExpression runtimeQualifier = PsiImplUtil.getRuntimeQualifier(ref);
        if (runtimeQualifier != null && runtimeQualifier.getType() == null) {
          return ThreeState.NO;
        }

        return ThreeState.YES;
      }

      if (qualifier.getType() == null) {
        return ThreeState.NO;
      }
      return ThreeState.YES;
    }
    return ThreeState.UNSURE;
  }