Kapt3: Fix literal expressions for byte and short

This commit is contained in:
Yan Zhulanow
2016-11-18 22:32:03 +03:00
committed by Yan Zhulanow
parent de0e4ac340
commit d246bd8cff
3 changed files with 21 additions and 14 deletions
@@ -203,11 +203,8 @@ class ClassFileToSourceStubConverter(
val value = field.value
val initializer = when (value) {
is Char -> treeMaker.Literal(TypeTag.CHAR, value.toInt())
is Byte, is Boolean, is Short, is Int, is Long, is Float, is Double, is String -> treeMaker.Literal(value)
else -> if (isFinal(field.access)) convertLiteralExpression(getDefaultValue(type)) else null
}
val initializer = convertValueOfPrimitiveTypeOrString(value)
?: if (isFinal(field.access)) convertLiteralExpression(getDefaultValue(type)) else null
return treeMaker.VarDef(modifiers, name, typeExpression, initializer)
}
@@ -346,11 +343,21 @@ class ClassFileToSourceStubConverter(
return treeMaker.Annotation(name, values)
}
private fun convertValueOfPrimitiveTypeOrString(value: Any?): JCExpression? {
return when (value) {
is Char -> treeMaker.Literal(TypeTag.CHAR, value.toInt())
is Byte -> treeMaker.TypeCast(treeMaker.TypeIdent(TypeTag.BYTE), treeMaker.Literal(TypeTag.INT, value.toInt()))
is Short -> treeMaker.TypeCast(treeMaker.TypeIdent(TypeTag.SHORT), treeMaker.Literal(TypeTag.INT, value.toInt()))
is Boolean, is Int, is Long, is Float, is Double, is String -> treeMaker.Literal(value)
else -> null
}
}
private fun convertLiteralExpression(value: Any?): JCExpression {
convertValueOfPrimitiveTypeOrString(value)?.let { return it }
return when (value) {
null -> treeMaker.Literal(TypeTag.BOT, null)
is Char -> treeMaker.Literal(TypeTag.CHAR, value.toInt())
is Byte, is Boolean, is Short, is Int, is Long, is Float, is Double, is String -> treeMaker.Literal(value)
is ByteArray -> treeMaker.NewArray(null, JavacList.nil(), mapJList(value.asIterable()) { convertLiteralExpression(it) })
is BooleanArray -> treeMaker.NewArray(null, JavacList.nil(), mapJList(value.asIterable()) { convertLiteralExpression(it) })
@@ -375,10 +382,10 @@ class ClassFileToSourceStubConverter(
}
private fun getDefaultValue(type: Type): Any? = when (type) {
Type.BYTE_TYPE -> 0.toByte()
Type.BYTE_TYPE -> 0
Type.BOOLEAN_TYPE -> false
Type.CHAR_TYPE -> '\u0000'
Type.SHORT_TYPE -> 0.toShort()
Type.SHORT_TYPE -> 0
Type.INT_TYPE -> 0
Type.LONG_TYPE -> 0L
Type.FLOAT_TYPE -> 0.0F
+2 -2
View File
@@ -1,6 +1,6 @@
public final class JvmStaticTest {
private final byte three = "3";
private final byte three = (byte)3;
private final char d = 'D';
private static final int one = 1;
private static final int two = 2;
@@ -8,7 +8,7 @@ public final class JvmStaticTest {
public static final JvmStaticTest.Companion Companion = null;
public final byte getThree() {
return "0";
return 0;
}
public final char getD() {
+3 -3
View File
@@ -6,9 +6,9 @@ public final class PrimitiveTypes {
public static final int intMinValue = -2147483648;
public static final int intMaxValue = 2147483647;
public static final int intHex = -1;
public static final byte byte0 = "0";
public static final byte byte50 = "50";
public static final short short5 = "5";
public static final byte byte0 = (byte)0;
public static final byte byte50 = (byte)50;
public static final short short5 = (short)5;
public static final char charC = 'C';
public static final char char0 = '\u0000';
public static final char char10 = '\n';