Example #1
0
  @NotNull
  private String createConversionForExpression(
      @Nullable PsiExpression expression, @NotNull PsiType expectedType) {
    String conversion = "";
    if (expression != null) {
      PsiType actualType = expression.getType();
      boolean isPrimitiveTypeOrNull =
          actualType == null || Node.PRIMITIVE_TYPES.contains(actualType.getCanonicalText());
      boolean isRef =
          (expression instanceof PsiReferenceExpression
                  && ((PsiReferenceExpression) expression).isQualified()
              || expression instanceof PsiMethodCallExpression);
      boolean containsQuestDot = expressionToExpression(expression).toKotlin().contains("?.");

      if (isPrimitiveTypeOrNull && isRef && containsQuestDot) {
        conversion += "!!";
      }

      if (actualType != null) {
        if (isConversionNeeded(actualType, expectedType)) {
          conversion += getPrimitiveTypeConversion(expectedType.getCanonicalText());
        }
      }
    }
    return conversion;
  }
Example #2
0
 @NotNull
 public Type typeToType(@Nullable PsiType type) {
   if (type == null) return Type.EMPTY_TYPE;
   TypeVisitor typeVisitor = new TypeVisitor(this);
   type.accept(typeVisitor);
   return typeVisitor.getResult();
 }
Example #3
0
 private static boolean isConversionNeeded(@Nullable PsiType actual, @Nullable PsiType expected) {
   if (actual == null || expected == null) {
     return false;
   }
   Map<String, String> typeMap = new HashMap<String, String>();
   typeMap.put(JAVA_LANG_BYTE, "byte");
   typeMap.put(JAVA_LANG_SHORT, "short");
   typeMap.put(JAVA_LANG_INTEGER, "int");
   typeMap.put(JAVA_LANG_LONG, "long");
   typeMap.put(JAVA_LANG_FLOAT, "float");
   typeMap.put(JAVA_LANG_DOUBLE, "double");
   typeMap.put(JAVA_LANG_CHARACTER, "char");
   String expectedStr = expected.getCanonicalText();
   String actualStr = actual.getCanonicalText();
   boolean o1 = AstUtil.getOrElse(typeMap, actualStr, "").equals(expectedStr);
   boolean o2 = AstUtil.getOrElse(typeMap, expectedStr, "").equals(actualStr);
   return !actualStr.equals(expectedStr) && (!(o1 ^ o2));
 }