@NotNull public List<String> createConversions(@NotNull PsiCallExpression expression) { PsiExpressionList argumentList = expression.getArgumentList(); PsiExpression[] arguments = argumentList != null ? argumentList.getExpressions() : new PsiExpression[] {}; List<String> conversions = new LinkedList<String>(); //noinspection UnusedDeclaration for (PsiExpression a : arguments) { conversions.add(""); } PsiMethod resolve = expression.resolveMethod(); if (resolve != null) { List<PsiType> expectedTypes = new LinkedList<PsiType>(); List<PsiType> actualTypes = new LinkedList<PsiType>(); for (PsiParameter p : resolve.getParameterList().getParameters()) expectedTypes.add(p.getType()); for (PsiExpression e : arguments) actualTypes.add(e.getType()); if (conversions.size() == actualTypes.size() && actualTypes.size() == expectedTypes.size()) { for (int i = 0; i < actualTypes.size(); i++) conversions.set(i, createConversionForExpression(arguments[i], expectedTypes.get(i))); } } return conversions; }
@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; }
@NotNull public Expression expressionToExpression(@Nullable PsiExpression e) { if (e == null) return Expression.EMPTY_EXPRESSION; ExpressionVisitor expressionVisitor = dispatcher.getExpressionVisitor(); e.accept(expressionVisitor); return expressionVisitor.getResult(); }
@NotNull public List<String> createConversions( @NotNull PsiPolyadicExpression expression, PsiType expectedType) { PsiExpression[] arguments = expression.getOperands(); int length = arguments.length; List<String> conversions = new LinkedList<String>(); List<PsiType> expectedTypes = Collections.nCopies(length, expectedType); List<PsiType> actualTypes = new LinkedList<PsiType>(); for (PsiExpression e : arguments) actualTypes.add(e.getType()); assert actualTypes.size() == expectedTypes.size() : "The type list must have the same length"; for (int i = 0; i < actualTypes.size(); i++) conversions.add(i, createConversionForExpression(arguments[i], expectedTypes.get(i))); return conversions; }