Ejemplo n.º 1
0
 public static void deleteStatementTail(PsiElement container, @NotNull PsiElement statement) {
   PsiElement next = statement.getNextSibling();
   while (next != null) {
     final ASTNode node = next.getNode();
     final IElementType type = node.getElementType();
     if (type == mSEMI) {
       final PsiElement nnext = next.getNextSibling();
       container.deleteChildRange(next, next);
       next = nnext;
     } else if (type == mNLS || type == TokenType.WHITE_SPACE && next.getText().contains("\n")) {
       final String text = next.getText();
       final int first = text.indexOf("\n");
       final int second = text.indexOf("\n", first + 1);
       if (second < 0) {
         container.deleteChildRange(next, next);
         return;
       }
       final String substring = text.substring(second);
       container
           .getNode()
           .replaceChild(
               node,
               createSingleLeafElement(
                   type, substring, 0, substring.length(), null, container.getManager()));
       return;
     } else {
       break;
     }
   }
 }
Ejemplo n.º 2
0
  public static boolean isAssignableWithoutConversions(
      @Nullable PsiType lType, @Nullable PsiType rType, @NotNull PsiElement context) {
    if (lType == null || rType == null) return false;

    if (rType == PsiType.NULL) {
      return !(lType instanceof PsiPrimitiveType);
    }

    PsiManager manager = context.getManager();
    GlobalSearchScope scope = context.getResolveScope();

    if (rType instanceof GrTupleType && ((GrTupleType) rType).getComponentTypes().length == 0) {
      if (lType instanceof PsiArrayType
          || InheritanceUtil.isInheritor(lType, JAVA_UTIL_LIST)
          || InheritanceUtil.isInheritor(lType, JAVA_UTIL_SET)) {
        return true;
      }
    }

    if (isClassType(rType, GROOVY_LANG_GSTRING) && lType.equalsToText(JAVA_LANG_STRING)) {
      return true;
    }

    if (isNumericType(lType) && isNumericType(rType)) {
      lType = unboxPrimitiveTypeWrapper(lType);
      if (isClassType(lType, JAVA_MATH_BIG_DECIMAL)) lType = PsiType.DOUBLE;
      rType = unboxPrimitiveTypeWrapper(rType);
      if (isClassType(rType, JAVA_MATH_BIG_DECIMAL)) rType = PsiType.DOUBLE;
    } else {
      rType = boxPrimitiveType(rType, manager, scope);
      lType = boxPrimitiveType(lType, manager, scope);
    }

    if (rType instanceof GrMapType || rType instanceof GrTupleType) {
      Boolean result = isAssignableForNativeTypes(lType, (PsiClassType) rType, context);
      if (result != null && result.booleanValue()) return true;
    }

    if (TypeConversionUtil.isAssignable(lType, rType)) {
      return true;
    }

    return false;
  }
Ejemplo n.º 3
0
 @NotNull
 public static PsiClassType getJavaLangObject(@NotNull PsiElement context) {
   return PsiType.getJavaLangObject(context.getManager(), context.getResolveScope());
 }
Ejemplo n.º 4
0
  public static boolean isAssignable(
      @Nullable PsiType lType, @Nullable PsiType rType, @NotNull PsiElement context) {
    if (lType == null || rType == null) {
      return false;
    }

    if (rType instanceof PsiIntersectionType) {
      for (PsiType child : ((PsiIntersectionType) rType).getConjuncts()) {
        if (isAssignable(lType, child, context)) {
          return true;
        }
      }
      return false;
    }
    if (lType instanceof PsiIntersectionType) {
      for (PsiType child : ((PsiIntersectionType) lType).getConjuncts()) {
        if (!isAssignable(child, rType, context)) {
          return false;
        }
      }
      return true;
    }

    if (rType == PsiType.NULL) {
      return !(lType instanceof PsiPrimitiveType);
    }

    if (isNumericType(lType) && isNumericType(rType)) {
      return true;
    }

    if (isClassType(lType, JAVA_LANG_STRING)) {
      return true;
    }

    final PsiManager manager = context.getManager();
    final GlobalSearchScope scope = context.getResolveScope();

    if (lType instanceof PsiArrayType) {
      PsiType lComponentType = ((PsiArrayType) lType).getComponentType();
      PsiType rComponentType = ClosureParameterEnhancer.findTypeForIteration(rType, context);
      if (rComponentType != null && isAssignable(lComponentType, rComponentType, context)) {
        return true;
      }
    }

    if (unboxPrimitiveTypeWrapper(lType) == PsiType.CHAR
        && (isClassType(rType, JAVA_LANG_STRING) || isClassType(rType, GROOVY_LANG_GSTRING))) {
      return true;
    }

    if (isAssignableByMethodCallConversion(lType, rType, context)) return true;

    lType = boxPrimitiveType(lType, manager, scope);
    rType = boxPrimitiveType(rType, manager, scope);
    if (lType.isAssignableFrom(rType)) {
      return true;
    }

    if (context instanceof GroovyPsiElement) {
      for (GrTypeConverter converter : GrTypeConverter.EP_NAME.getExtensions()) {
        if (!converter.isAllowedInMethodCall()) {
          Boolean result = converter.isConvertible(lType, rType, (GroovyPsiElement) context);
          if (result != null) {
            return result;
          }
        }
      }
    }

    return false;
  }