private static boolean canRemoveQualifier(GrReferenceExpression refExpr) {
    try {
      GrExpression qualifier = refExpr.getQualifier();
      if (!(qualifier instanceof GrReferenceExpression)) return false;

      PsiElement qualifierRefElement = ((GrReferenceExpression)qualifier).resolve();
      if (!(qualifierRefElement instanceof PsiClass)) return false;

      PsiElement refElement = refExpr.resolve();
      if (refElement == null) return false;

      final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(refExpr.getProject());

      if (refExpr.getParent() instanceof GrMethodCallExpression methodCall) {
        GrMethodCallExpression newMethodCall =
          (GrMethodCallExpression)factory.createExpressionFromText(refExpr.getReferenceName() + "()", refExpr);
        newMethodCall.getArgumentList().replace(methodCall.getArgumentList());
        PsiElement newRefElement = ((GrReferenceExpression)newMethodCall.getInvokedExpression()).resolve();
        return refElement.equals(newRefElement);
      }
      else {
        GrReferenceExpression newRefExpr = (GrReferenceExpression)factory.createExpressionFromText(refExpr.getReferenceName(), refExpr);
        PsiElement newRefElement = newRefExpr.resolve();
        return refElement.equals(newRefElement);
      }
    }
    catch (IncorrectOperationException e) {
      return false;
    }
  }
private static boolean canRemoveQualifier(GrReferenceExpression refExpr)