leibnitz27 / cfr

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

Fix ConstantLinks using wrong class for relinking fields #251

Closed Marcono1234 closed 3 years ago

Marcono1234 commented 3 years ago

Previously the current class was used as declaring class; that resulted in correct code if the current class is a nested class which does not extend the class declaring the field, or if the current class is anonymous (would result in 1.field).

Example code:

class RelinkingTest {
    public static final String CONSTANT = "test";

    Object o = new Object() {
        @Override
        public String toString() {
            return CONSTANT;
        }
    };

    class Nested {
        @Override
        public String toString() {
            return CONSTANT;
        }
    }
}

Decompiled output:

class RelinkingTest {
    public static final String CONSTANT = "test";
    Object o = new Object(){

        public String toString() {
            return _1.CONSTANT;
        }
    };

    RelinkingTest() {
    }

    class Nested {
        Nested() {
        }

        public String toString() {
            return Nested.CONSTANT;
        }
    }
}
leibnitz27 commented 3 years ago

Nice spot, thanks.