IDEA-171459 fix type cast for int literals used as byte / short in decompiled code

master
Dmitry Cherniachenko 7 years ago committed by Egor.Ushakov
parent 4beda48cf8
commit cd1acfa266
  1. 2
      src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java
  2. 1
      test/org/jetbrains/java/decompiler/SingleClassesTest.java
  3. BIN
      testData/classes/pkg/TestPrimitives.class
  4. 8
      testData/results/MoreAnnotations.dec
  5. 221
      testData/results/TestPrimitives.dec
  6. 66
      testData/src/pkg/TestPrimitives.java

@ -876,7 +876,7 @@ public class ExprProcessor implements CodeConstants {
castAlways ||
(!leftType.isSuperset(rightType) && (rightType.equals(VarType.VARTYPE_OBJECT) || leftType.type != CodeConstants.TYPE_OBJECT)) ||
(castNull && rightType.type == CodeConstants.TYPE_NULL && !UNDEFINED_TYPE_STRING.equals(getTypeName(leftType))) ||
(isIntConstant(exprent) && rightType.isStrictSuperset(leftType));
(isIntConstant(exprent) && VarType.VARTYPE_INT.isStrictSuperset(leftType));
boolean quote = cast && exprent.getPrecedence() >= FunctionExprent.getPrecedence(FunctionExprent.FUNCTION_CAST);

@ -101,6 +101,7 @@ public class SingleClassesTest {
@Test public void testAnonymousParams() { doTest("pkg/TestAnonymousParams"); }
@Test public void testAccessReplace() { doTest("pkg/TestAccessReplace"); }
@Test public void testStringLiterals() { doTest("pkg/TestStringLiterals"); }
@Test public void testPrimitives() { doTest("pkg/TestPrimitives"); }
private void doTest(String testFile, String... companionFiles) {
ConsoleDecompiler decompiler = fixture.getDecompiler();

@ -33,11 +33,11 @@ public @interface MoreAnnotations {
String annotatedWithEmptyArrays = "";
@MoreAnnotations(
intArray = {1, 0, 2147483647, -2147483648},
byteArray = {1, 0, 127, -128, -1},
byteArray = {(byte)1, (byte)0, (byte)127, (byte)-128, (byte)-1},
floatArray = {1.0F, 0.0F, 3.4028235E38F, 1.4E-45F, 0.0F / 0.0, 1.0F / 0.0, -1.0F / 0.0},
doubleArray = {1.0D, 0.0D, 1.7976931348623157E308D, 4.9E-324D, 0.0D / 0.0, 1.0D / 0.0, -1.0D / 0.0},
booleanArray = {true, false},
shortArray = {1, 0, 32767, -32768, -1},
shortArray = {(short)1, (short)0, (short)32767, (short)-32768, (short)-1},
longArray = {1L, 0L, 9223372036854775807L, -9223372036854775808L},
charArray = {'a', '\n', '\u0001', '\u0000', '\uffff', '\u0000'},
enumArray = {MoreAnnotations.TestEnum.FirstValue, MoreAnnotations.TestEnum.SecondValue},
@ -73,7 +73,7 @@ public @interface MoreAnnotations {
int[] intArray() default {1, 0, 2147483647, -2147483648};
byte[] byteArray() default {1, 0, 127, -128, -1};
byte[] byteArray() default {(byte)1, (byte)0, (byte)127, (byte)-128, (byte)-1};
float[] floatArray() default {1.0F, 0.0F, 3.4028235E38F, 1.4E-45F, 0.0F / 0.0, 1.0F / 0.0, -1.0F / 0.0};
@ -81,7 +81,7 @@ public @interface MoreAnnotations {
boolean[] booleanArray() default {true, false};
short[] shortArray() default {1, 0, 32767, -32768, -1};
short[] shortArray() default {(short)1, (short)0, (short)32767, (short)-32768, (short)-1};
long[] longArray() default {1L, 0L, 9223372036854775807L, -9223372036854775808L};

@ -0,0 +1,221 @@
package pkg;
public class TestPrimitives {
public void printAll() {
this.printBoolean(true);// 6
this.printByte((byte)123);// 7
this.printShort((short)257);// 8
this.printInt(123);// 9
this.printLong(123L);// 10
this.printFloat(1.23F);// 11
this.printDouble(1.23D);// 12
this.printChar('Z');// 13
System.out.println(String.format("%b, %d, %d, %d", new Object[]{Boolean.valueOf(this.getBoolean()), Byte.valueOf(this.getByte()), Short.valueOf(this.getShort()), Integer.valueOf(this.getInt())}));// 15
}// 16
public void printBoolean(boolean b) {
System.out.println(String.format("%b", new Object[]{Boolean.valueOf(b)}));// 19
}// 20
public void printByte(byte b) {
System.out.println(String.format("%d", new Object[]{Byte.valueOf(b)}));// 23
}// 24
public void printShort(short s) {
System.out.println(String.format("%d", new Object[]{Short.valueOf(s)}));// 27
}// 28
public void printInt(int i) {
System.out.println(String.format("%d", new Object[]{Integer.valueOf(i)}));// 31
}// 32
public void printLong(long l) {
System.out.println(String.format("%d", new Object[]{Long.valueOf(l)}));// 35
}// 36
public void printFloat(float f) {
System.out.println(String.format("%f", new Object[]{Float.valueOf(f)}));// 39
}// 40
public void printDouble(double d) {
System.out.println(String.format("%f", new Object[]{Double.valueOf(d)}));// 43
}// 44
public void printChar(char c) {
System.out.println(String.format("%c", new Object[]{Character.valueOf(c)}));// 47
}// 48
public boolean getBoolean() {
return false;// 52
}
public byte getByte() {
return (byte)-128;// 56
}
public short getShort() {
return (short)-32768;// 60
}
public int getInt() {
return 42;// 64
}
}
class 'pkg/TestPrimitives' {
method 'printAll ()V' {
1 4
2 4
6 5
8 5
c 6
f 6
13 7
15 7
19 8
1c 8
20 9
22 9
26 10
29 10
2d 11
2f 11
32 12
35 12
3e 12
41 12
48 12
4b 12
52 12
55 12
5c 12
5f 12
63 12
66 12
69 13
}
method 'printBoolean (Z)V' {
0 16
3 16
c 16
10 16
13 16
16 17
}
method 'printByte (B)V' {
0 20
3 20
c 20
10 20
13 20
16 21
}
method 'printShort (S)V' {
0 24
3 24
c 24
10 24
13 24
16 25
}
method 'printInt (I)V' {
0 28
3 28
c 28
10 28
13 28
16 29
}
method 'printLong (J)V' {
0 32
3 32
c 32
10 32
13 32
16 33
}
method 'printFloat (F)V' {
0 36
3 36
c 36
10 36
13 36
16 37
}
method 'printDouble (D)V' {
0 40
3 40
c 40
10 40
13 40
16 41
}
method 'printChar (C)V' {
0 44
3 44
c 44
10 44
13 44
16 45
}
method 'getBoolean ()Z' {
0 48
1 48
}
method 'getByte ()B' {
0 52
2 52
}
method 'getShort ()S' {
0 56
3 56
}
method 'getInt ()I' {
0 60
2 60
}
}
Lines mapping:
6 <-> 5
7 <-> 6
8 <-> 7
9 <-> 8
10 <-> 9
11 <-> 10
12 <-> 11
13 <-> 12
15 <-> 13
16 <-> 14
19 <-> 17
20 <-> 18
23 <-> 21
24 <-> 22
27 <-> 25
28 <-> 26
31 <-> 29
32 <-> 30
35 <-> 33
36 <-> 34
39 <-> 37
40 <-> 38
43 <-> 41
44 <-> 42
47 <-> 45
48 <-> 46
52 <-> 49
56 <-> 53
60 <-> 57
64 <-> 61

@ -0,0 +1,66 @@
package pkg;
public class TestPrimitives {
public void printAll() {
printBoolean(true);
printByte((byte) 123);
printShort((short) 257);
printInt(123);
printLong(123L);
printFloat(1.23F);
printDouble(1.23);
printChar('Z');
System.out.println(String.format("%b, %d, %d, %d", getBoolean(), getByte(), getShort(), getInt()));
}
public void printBoolean(boolean b) {
System.out.println(String.format("%b", b));
}
public void printByte(byte b) {
System.out.println(String.format("%d", b));
}
public void printShort(short s) {
System.out.println(String.format("%d", s));
}
public void printInt(int i) {
System.out.println(String.format("%d", i));
}
public void printLong(long l) {
System.out.println(String.format("%d", l));
}
public void printFloat(float f) {
System.out.println(String.format("%f", f));
}
public void printDouble(double d) {
System.out.println(String.format("%f", d));
}
public void printChar(char c) {
System.out.println(String.format("%c", c));
}
public boolean getBoolean() {
return false;
}
public byte getByte() {
return (byte) 128;
}
public short getShort() {
return (short) 32768;
}
public int getInt() {
return 42;
}
}
Loading…
Cancel
Save