KT-794 Support autoboxing primitive return types
This commit is contained in:
@@ -25,8 +25,9 @@ public class Converter {
|
||||
add("javax.annotation.Nonnull");
|
||||
}
|
||||
};
|
||||
private static Set<String> ourClassIdentifiers = new HashSet<String>();
|
||||
private static final Dispatcher ourDispatcher = new Dispatcher();
|
||||
@NotNull private static Set<String> ourClassIdentifiers = new HashSet<String>();
|
||||
@NotNull private static final Dispatcher ourDispatcher = new Dispatcher();
|
||||
@Nullable private static PsiType ourMethodReturnType = null;
|
||||
|
||||
public static void setClassIdentifiers(Set<String> identifiers) {
|
||||
ourClassIdentifiers = identifiers;
|
||||
@@ -36,6 +37,11 @@ public class Converter {
|
||||
return new HashSet<String>(ourClassIdentifiers);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiType getMethodReturnType() {
|
||||
return ourMethodReturnType;
|
||||
}
|
||||
|
||||
public static void clearClassIdentifiers() {
|
||||
ourClassIdentifiers.clear();
|
||||
}
|
||||
@@ -300,6 +306,8 @@ public class Converter {
|
||||
else
|
||||
ourDispatcher.setExpressionVisitor(new ExpressionVisitor());
|
||||
|
||||
ourMethodReturnType = method.getReturnType();
|
||||
|
||||
final IdentifierImpl identifier = new IdentifierImpl(method.getName());
|
||||
final Type returnType = typeToType(method.getReturnType(), isNotNull(method.getModifierList()));
|
||||
final Block body = blockToBlock(method.getBody(), notEmpty);
|
||||
|
||||
@@ -1,20 +1,27 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.util.AstUtil;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class ReturnStatement extends Statement {
|
||||
private final Expression myExpression;
|
||||
private String myConversion = EMPTY;
|
||||
|
||||
public ReturnStatement(Expression expression) {
|
||||
myExpression = expression;
|
||||
}
|
||||
|
||||
public ReturnStatement(final Expression expression, final String conversion) {
|
||||
this(expression);
|
||||
myConversion = conversion;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
return "return" + SPACE + myExpression.toKotlin();
|
||||
return "return" + SPACE + AstUtil.applyConversionForOneItem(myExpression.toKotlin(), myConversion);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package org.jetbrains.jet.j2k.util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.ast.Expression;
|
||||
import org.jetbrains.jet.j2k.ast.INode;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
@@ -53,15 +55,27 @@ public class AstUtil {
|
||||
return conversions;
|
||||
}
|
||||
|
||||
public static List<String> applyConversions(List<String> first, List<String> second) {
|
||||
@NotNull
|
||||
public static List<String> applyConversions(@NotNull List<String> first, @NotNull List<String> second) {
|
||||
List<String> result = new LinkedList<String>();
|
||||
assert first.size() == second.size() : "Lists must have the same size.";
|
||||
for (int i = 0; i < first.size(); i++) {
|
||||
if (second.get(i).isEmpty())
|
||||
result.add(first.get(i));
|
||||
else
|
||||
result.add("(" + first.get(i) + ")" + second.get(i));
|
||||
result.add(applyConversionForOneItem(first.get(i), second.get(i)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String applyConversionForOneItem(@NotNull String f, @NotNull String s) {
|
||||
if (s.isEmpty())
|
||||
return f;
|
||||
else
|
||||
return "(" + f + ")" + s;
|
||||
}
|
||||
|
||||
public static <T> T getOrElse(Map<T, T> map, T e, T orElse) {
|
||||
if (map.containsKey(e))
|
||||
return map.get(e);
|
||||
return orElse;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.j2k.Converter;
|
||||
import org.jetbrains.jet.j2k.ast.*;
|
||||
import org.jetbrains.jet.j2k.util.AstUtil;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -231,7 +232,9 @@ public class ExpressionVisitor extends StatementVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isConversionNeeded(final PsiType actual, final PsiType expected) {
|
||||
static boolean isConversionNeeded(@Nullable final PsiType actual,@Nullable final 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");
|
||||
@@ -242,20 +245,13 @@ public class ExpressionVisitor extends StatementVisitor {
|
||||
typeMap.put("java.lang.Character", "char");
|
||||
String expectedStr = expected.getCanonicalText();
|
||||
String actualStr = actual.getCanonicalText();
|
||||
boolean o1 = getOrElse(typeMap, actualStr, "").equals(expectedStr);
|
||||
boolean o2 = getOrElse(typeMap, expectedStr, "").equals(actualStr);
|
||||
boolean o1 = AstUtil.getOrElse(typeMap, actualStr, "").equals(expectedStr);
|
||||
boolean o2 = AstUtil.getOrElse(typeMap, expectedStr, "").equals(actualStr);
|
||||
return !actualStr.equals(expectedStr) && (!(o1 ^ o2));
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static <T> T getOrElse(Map<T, T> map, T e, T orElse) {
|
||||
if (map.containsKey(e))
|
||||
return map.get(e);
|
||||
return orElse;
|
||||
}
|
||||
|
||||
private static String getPrimitiveTypeConversion(String type) {
|
||||
@NotNull
|
||||
static String getPrimitiveTypeConversion(@NotNull String type) {
|
||||
Map<String, String> conversions = new HashMap<String, String>();
|
||||
conversions.put("byte", "byt");
|
||||
conversions.put("short", "sht");
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.j2k.Converter;
|
||||
import org.jetbrains.jet.j2k.ast.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -14,6 +15,8 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.j2k.Converter.*;
|
||||
import static org.jetbrains.jet.j2k.visitors.ExpressionVisitor.getPrimitiveTypeConversion;
|
||||
import static org.jetbrains.jet.j2k.visitors.ExpressionVisitor.isConversionNeeded;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
@@ -332,8 +335,16 @@ public class StatementVisitor extends ElementVisitor {
|
||||
@Override
|
||||
public void visitReturnStatement(PsiReturnStatement statement) {
|
||||
super.visitReturnStatement(statement);
|
||||
PsiExpression returnValue = statement.getReturnValue();
|
||||
String conversion = "";
|
||||
|
||||
PsiType methodReturnType = Converter.getMethodReturnType();
|
||||
if (returnValue != null && methodReturnType != null && isConversionNeeded(returnValue.getType(), methodReturnType)) {
|
||||
conversion = getPrimitiveTypeConversion(methodReturnType.getCanonicalText());
|
||||
}
|
||||
myResult = new ReturnStatement(
|
||||
expressionToExpression(statement.getReturnValue())
|
||||
expressionToExpression(returnValue),
|
||||
conversion
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package demo;
|
||||
|
||||
class Test {
|
||||
Integer getInteger(Integer i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
void test() {
|
||||
int i = getInteger(10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace demo
|
||||
open class Test() {
|
||||
open fun getInteger(i : Int?) : Int? {
|
||||
return i
|
||||
}
|
||||
open fun test() : Unit {
|
||||
var i : Int = getInteger(10)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class Test {
|
||||
int getInt() {
|
||||
byte b = 10;
|
||||
return b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
open class Test() {
|
||||
open fun getInt() : Int {
|
||||
var b : Byte = 10
|
||||
return (b).int
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user