leibnitz27 / cfr

This is the public repository for the CFR Java decompiler
https://www.benf.org/other/cfr
MIT License
1.93k stars 249 forks source link

Casting of primitive types may be decompiled as casting of corresponding wrapper classes under certain circumstances #341

Open xxh160 opened 1 year ago

xxh160 commented 1 year ago

Hi! I am doing some research based on CFR, and I found that under certain circumstances, casting of primitive types may be decompiled as casting of corresponding wrapper classes.

Here is an example:

class T1 {
    public T1(long l, Short s) {
    }
}

class T2 extends T1 {
    public T2(Short s, long l) {
        super((long) -38, (short) -37);
    }

}

class T3 extends T1 {
    public T3(T2 t2, Short s) {
        super((long) -38, (short) -37);
    }
}

class Demo<M extends String> extends T3 {
    public Demo() {
        super(new T2((short) -55, (long) 96), (short) -48);
    }
}

After being decompiled with CFR, the output is:

/*
 * Decompiled with CFR 0.153-SNAPSHOT (24c7433-dirty).
 */
class T1 {
    public T1(long l, Short s) {
    }
}
class T2
extends T1 {
    public T2(Short s, long l) {
        super(-38L, (short)-37);
    }
}
class T3
extends T1 {
    public T3(T2 t2, Short s) {
        super(-38L, (short)-37);
    }
}
class Demo<M extends String>
extends T3 {
    public Demo() {
        super(new T2((Short)-55, 96L), (Short)-48);
    }
}

The statement (short)-55 is converted into (Short)-55, and (short)-48 is also similar. It triggers some compilation errors:

./Demo.java:7: error: cannot find symbol
        super(new T2((Short)-55, 96L), (Short)-48);
                      ^
  symbol:   variable Short
  location: class Demo<M>
  where M is a type-variable:
    M extends String declared in class Demo
./Demo.java:7: error: illegal parenthesized expression
        super(new T2((Short)-55, 96L), (Short)-48);
                     ^
./Demo.java:7: error: cannot find symbol
        super(new T2((Short)-55, 96L), (Short)-48);
                                        ^
  symbol:   variable Short
  location: class Demo<M>
  where M is a type-variable:
    M extends String declared in class Demo
./Demo.java:7: error: illegal parenthesized expression
        super(new T2((Short)-55, 96L), (Short)-48);
                                       ^
4 errors

Would you please have a look at this? It is important for me! Thanks a lot!

CFR version: CFR 0.153-SNAPSHOT (24c7433-dirty). Javac version: openjdk 11.0.18 2023-01-17. I also attach relative files and I hope they will be helpful: demo.zip.