From 2ad739275ff53165f53796d0d2532d530549ff71 Mon Sep 17 00:00:00 2001 From: Skylot Date: Mon, 4 Aug 2014 22:07:10 +0400 Subject: [PATCH] core: handle special values for numbers --- .../main/java/jadx/core/codegen/TypeGen.java | 90 +++++++++++++++---- .../internal/arith/TestSpecialValues.java | 64 +++++++++++++ 2 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 jadx-core/src/test/java/jadx/tests/internal/arith/TestSpecialValues.java diff --git a/jadx-core/src/main/java/jadx/core/codegen/TypeGen.java b/jadx-core/src/main/java/jadx/core/codegen/TypeGen.java index 96cd5f6e1..cb1cf34b2 100644 --- a/jadx-core/src/main/java/jadx/core/codegen/TypeGen.java +++ b/jadx-core/src/main/java/jadx/core/codegen/TypeGen.java @@ -66,37 +66,91 @@ public class TypeGen { } public static String formatShort(short s) { - return "(short) " + wrapNegNum(s < 0, Short.toString(s)); + if (s == Short.MAX_VALUE) { + return "Short.MAX_VALUE"; + } + if (s == Short.MIN_VALUE) { + return "Short.MIN_VALUE"; + } + return "(short) " + Short.toString(s); } public static String formatByte(byte b) { - return "(byte) " + wrapNegNum(b < 0, Byte.toString(b)); + if (b == Byte.MAX_VALUE) { + return "Byte.MAX_VALUE"; + } + if (b == Byte.MIN_VALUE) { + return "Byte.MIN_VALUE"; + } + return "(byte) " + Byte.toString(b); } public static String formatInteger(int i) { - return wrapNegNum(i < 0, Integer.toString(i)); + if (i == Integer.MAX_VALUE) { + return "Integer.MAX_VALUE"; + } + if (i == Integer.MIN_VALUE) { + return "Integer.MIN_VALUE"; + } + return Integer.toString(i); + } + + public static String formatLong(long l) { + if (l == Long.MAX_VALUE) { + return "Long.MAX_VALUE"; + } + if (l == Long.MIN_VALUE) { + return "Long.MIN_VALUE"; + } + String str = Long.toString(l); + if (Math.abs(l) >= Integer.MAX_VALUE) { + str += "L"; + } + return str; } public static String formatDouble(double d) { - return wrapNegNum(d < 0, Double.toString(d) + "d"); + if (Double.isNaN(d)) { + return "Double.NaN"; + } + if (d == Double.NEGATIVE_INFINITY) { + return "Double.NEGATIVE_INFINITY"; + } + if (d == Double.POSITIVE_INFINITY) { + return "Double.POSITIVE_INFINITY"; + } + if (d == Double.MIN_VALUE) { + return "Double.MIN_VALUE"; + } + if (d == Double.MAX_VALUE) { + return "Double.MAX_VALUE"; + } + if (d == Double.MIN_NORMAL) { + return "Double.MIN_NORMAL"; + } + return Double.toString(d) + "d"; } public static String formatFloat(float f) { - return wrapNegNum(f < 0, Float.toString(f) + "f"); - } - - public static String formatLong(long lit) { - String l = Long.toString(lit); - if (lit == Long.MIN_VALUE || Math.abs(lit) >= Integer.MAX_VALUE) { - l += "L"; + if (Float.isNaN(f)) { + return "Float.NaN"; } - return wrapNegNum(lit < 0, l); + if (f == Float.NEGATIVE_INFINITY) { + return "Float.NEGATIVE_INFINITY"; + } + if (f == Float.POSITIVE_INFINITY) { + return "Float.POSITIVE_INFINITY"; + } + if (f == Float.MIN_VALUE) { + return "Float.MIN_VALUE"; + } + if (f == Float.MAX_VALUE) { + return "Float.MAX_VALUE"; + } + if (f == Float.MIN_NORMAL) { + return "Float.MIN_NORMAL"; + } + return Float.toString(f) + "f"; } - private static String wrapNegNum(boolean lz, String str) { -// if (lz) -// return "(" + str + ")"; -// else - return str; - } } diff --git a/jadx-core/src/test/java/jadx/tests/internal/arith/TestSpecialValues.java b/jadx-core/src/test/java/jadx/tests/internal/arith/TestSpecialValues.java new file mode 100644 index 000000000..ad57b469b --- /dev/null +++ b/jadx-core/src/test/java/jadx/tests/internal/arith/TestSpecialValues.java @@ -0,0 +1,64 @@ +package jadx.tests.internal.arith; + +import jadx.api.InternalJadxTest; +import jadx.core.dex.nodes.ClassNode; + +import org.junit.Test; + +import static jadx.tests.utils.JadxMatchers.containsOne; +import static org.junit.Assert.assertThat; + +public class TestSpecialValues extends InternalJadxTest { + + public static class TestCls { + + public void test() { + shorts(Short.MIN_VALUE, Short.MAX_VALUE); + bytes(Byte.MIN_VALUE, Byte.MAX_VALUE); + ints(Integer.MIN_VALUE, Integer.MAX_VALUE); + longs(Long.MIN_VALUE, Long.MAX_VALUE); + + floats(Float.NaN, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, + Float.MIN_VALUE, Float.MAX_VALUE, Float.MIN_NORMAL); + + doubles(Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, + Double.MIN_VALUE, Double.MAX_VALUE, Double.MIN_NORMAL); + } + + private void shorts(short... v) { + } + + private void bytes(byte... v) { + } + + private void ints(int... v) { + } + + private void longs(long... v) { + } + + private void floats(float... v) { + } + + private void doubles(double... v) { + } + } + + @Test + public void test() { + ClassNode cls = getClassNode(TestCls.class); + String code = cls.getCode().toString(); + System.out.println(code); + + assertThat(code, containsOne("Float.NaN, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, " + + "Float.MIN_VALUE, Float.MAX_VALUE, Float.MIN_NORMAL")); + + assertThat(code, containsOne("Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, " + + "Double.MIN_VALUE, Double.MAX_VALUE, Double.MIN_NORMAL")); + + assertThat(code, containsOne("Short.MIN_VALUE, Short.MAX_VALUE")); + assertThat(code, containsOne("Byte.MIN_VALUE, Byte.MAX_VALUE")); + assertThat(code, containsOne("Integer.MIN_VALUE, Integer.MAX_VALUE")); + assertThat(code, containsOne("Long.MIN_VALUE, Long.MAX_VALUE")); + } +}