J2K: convert hex Java literal to hex Kotlin literal

This commit is contained in:
Yan Zhulanow
2015-12-04 01:03:46 +09:00
parent bd9b2f9f06
commit 4cd2ff040d
3 changed files with 25 additions and 3 deletions
@@ -297,8 +297,18 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
text = text.replace("l", "L")
}
fun isHexLiteral(text: String) = text.startsWith("0x") || text.startsWith("0X")
fun isLongField(element: PsiElement): Boolean {
val fieldType = (element as? PsiVariable)?.type ?: return false
return when (fieldType) {
is PsiPrimitiveType -> fieldType.canonicalText == "long"
else -> PsiPrimitiveType.getUnboxedType(fieldType)?.canonicalText == "long"
}
}
if (typeStr == "int") {
text = if (value != null) value.toString() else text
val toIntIsNeeded = value != null && value.toString().toInt() < 0 && !isLongField(expression.parent)
text = if (value != null && !isHexLiteral(text)) value.toString() else text + (if (toIntIsNeeded) ".toInt()" else "")
}
}
+6
View File
@@ -3,5 +3,11 @@ class Test {
void test() {
int i1 = 0x21;
Int i2 = 0x33;
int i3 = 0xffffffff;
int i4 = 0XCCCC;
int i5 = 0x6fffffff;
long l1 = 0xffffffff;
Long l2 = 0xffffffffl;
long l3 = 0xffffffffffL;
}
}
+8 -2
View File
@@ -1,6 +1,12 @@
internal class Test {
fun test() {
val i1 = 33
val i2 = 51
val i1 = 0x21
val i2 = 0x33
val i3 = 0xffffffff.toInt()
val i4 = 0XCCCC
val i5 = 0x6fffffff
val l1: Long = 0xffffffff
val l2 = 0xffffffffL
val l3 = 0xffffffffffL
}
}