diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java b/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java index 48e7d3f..6a03bf4 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java @@ -877,7 +877,7 @@ public class ExprProcessor implements CodeConstants { boolean quote = cast && exprent.getPrecedence() >= FunctionExprent.getPrecedence(FunctionExprent.FUNCTION_CAST); // cast instead to 'byte' / 'short' when int constant is used as a value for 'Byte' / 'Short' - if (castNarrowing && exprent.type == Exprent.EXPRENT_CONST) { + if (castNarrowing && exprent.type == Exprent.EXPRENT_CONST && !((ConstExprent) exprent).isNull()) { if (leftType.equals(VarType.VARTYPE_BYTE_OBJ)) { leftType = VarType.VARTYPE_BYTE; } diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java index e77b680..cfd300d 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java @@ -247,6 +247,10 @@ public class ConstExprent extends Exprent { throw new RuntimeException("invalid constant type: " + constType); } + public boolean isNull() { + return CodeConstants.TYPE_NULL == constType.type; + } + private static String convertStringToJava(String value, boolean ascii) { char[] arr = value.toCharArray(); StringBuilder buffer = new StringBuilder(arr.length); diff --git a/test/org/jetbrains/java/decompiler/SingleClassesTest.java b/test/org/jetbrains/java/decompiler/SingleClassesTest.java index 2e6c819..11d393e 100644 --- a/test/org/jetbrains/java/decompiler/SingleClassesTest.java +++ b/test/org/jetbrains/java/decompiler/SingleClassesTest.java @@ -32,6 +32,7 @@ public class SingleClassesTest { fixture = null; } + @Test public void testPrimitiveNarrowing() { doTest("pkg/TestPrimitiveNarrowing"); } @Test public void testClassFields() { doTest("pkg/TestClassFields"); } @Test public void testInterfaceFields() { doTest("pkg/TestInterfaceFields"); } @Test public void testClassLambda() { doTest("pkg/TestClassLambda"); } diff --git a/testData/classes/pkg/TestPrimitiveNarrowing.class b/testData/classes/pkg/TestPrimitiveNarrowing.class new file mode 100644 index 0000000..dc7c0c5 Binary files /dev/null and b/testData/classes/pkg/TestPrimitiveNarrowing.class differ diff --git a/testData/results/TestPrimitiveNarrowing.dec b/testData/results/TestPrimitiveNarrowing.dec new file mode 100644 index 0000000..1e5d30c --- /dev/null +++ b/testData/results/TestPrimitiveNarrowing.dec @@ -0,0 +1,64 @@ +package pkg; + +class TestPrimitiveNarrowing { + TestPrimitiveNarrowing(Short value) { + }// 6 + + static void invocations() { + withInteger((Integer)null);// 9 + withShort((Short)null);// 10 + withByte((Byte)null);// 11 + new TestPrimitiveNarrowing((Short)null);// 12 + }// 13 + + static void withByte(Byte defaultValue) { + }// 16 + + static void withInteger(Integer defaultValue) { + }// 19 + + static void withShort(Short defaultValue) { + }// 22 +} + +class 'pkg/TestPrimitiveNarrowing' { + method ' (Ljava/lang/Short;)V' { + 4 4 + } + + method 'invocations ()V' { + 0 7 + 1 7 + 4 8 + 5 8 + 8 9 + 9 9 + 10 10 + 15 11 + } + + method 'withByte (Ljava/lang/Byte;)V' { + 0 14 + } + + method 'withInteger (Ljava/lang/Integer;)V' { + 0 17 + } + + method 'withShort (Ljava/lang/Short;)V' { + 0 20 + } +} + +Lines mapping: +6 <-> 5 +9 <-> 8 +10 <-> 9 +11 <-> 10 +12 <-> 11 +13 <-> 12 +16 <-> 15 +19 <-> 18 +22 <-> 21 +Not mapped: +5 diff --git a/testData/src/pkg/TestPrimitiveNarrowing.java b/testData/src/pkg/TestPrimitiveNarrowing.java new file mode 100644 index 0000000..672ceb8 --- /dev/null +++ b/testData/src/pkg/TestPrimitiveNarrowing.java @@ -0,0 +1,24 @@ +package pkg; + +class TestPrimitiveNarrowing { + + TestPrimitiveNarrowing(Short value) { + } + + static void invocations() { + withInteger(null); + withShort(null); + withByte(null); + new TestPrimitiveNarrowing(null); + } + + static void withByte(Byte defaultValue) { + } + + static void withInteger(Integer defaultValue) { + } + + static void withShort(Short defaultValue) { + } + +}