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

CFR may perform incorrect modifications to the type of generic class variables #338

Open xxh160 opened 1 year ago

xxh160 commented 1 year ago

Hi! I am doing some research based on CFR, and I found a bug related to CFR's incorrect modifications to the type of generic class variables.

I have tested this issue with several code samples, and here is an example:

class Demo1<T extends Character, N> {
    public final Short foo(T t1) {
        final Boolean b = false;
        T t2 = t1;
        T t3 = (T) new Character('x');
        N n = (N) new String("");
        return foo(((b) ? t3 : t2));
    }
}

After decompiling with CFR, the type of t3 and n were modified:

/*
 * Decompiled with CFR 0.153-SNAPSHOT (24c7433-dirty).
 */
class Demo1<T extends Character, N> {
    Demo1() {
    }
    public final Short foo(T t) {
        Boolean bl = false;
        T t2 = t;
        Character c = new Character('x');
        String string = new String("");
        return this.foo(bl != false ? c : t2);
    }
}

When I tried to compile the decompiled code, a compilation error occurred:

./Demo1.java:13: error: incompatible types: bad type in conditional expression
        return this.foo(bl != false ? c : t2);
                                      ^
    Character cannot be converted to T
  where T is a type-variable:
    T extends Character declared in class Demo1
Note: ./Demo1.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

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 the source files and the decompilation results and I hope they will be helpful: demo.zip.