cfr icon indicating copy to clipboard operation
cfr copied to clipboard

Character literals decompiled as number literals

Open nbauma109 opened this issue 4 years ago • 3 comments

CFR version

CFR 0.152-SNAPSHOT (050e735)

Compiler

javac 1.8.0_252

Description

Character literals decompiled as number literals which makes the code non-readable. For example '.' and '/' decompiled as 46 and 47.

Example

package test.test;

public class TestChar {

    private static final String TEST = "src/Test.java";

    public static void main(final String[] args) {
        System.out.println(TEST.substring(TEST.indexOf('/') + 1, TEST.indexOf('.')));
    }

}

Result

package test.test;

public class TestChar {
    private static final String TEST = "src/Test.java";

    public static void main(String[] arrstring) {
        System.out.println(TEST.substring(TEST.indexOf(47) + 1, TEST.indexOf(46)));
    }
}

nbauma109 avatar Apr 18 '21 09:04 nbauma109

Hey - yes, I can see that's annoying ... the pain here is - this is actually behaving technically as it should..... because the indexOf method doesn't take a char... it takes an int...

    public int indexOf(int ch) {
        return indexOf(ch, 0);
    }

(I've no idea why, it's probably some odd historical thing, or to do with the fact that char type is too small for unicode characters...).

Edit : Ah yes, it's definitely because of unicode....

        if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
            // handle most cases here (ch is a BMP code point or a
            // negative value (invalid code point))

leibnitz27 avatar May 13 '21 17:05 leibnitz27

Probably possible to hack together a special case for it, as I can see that it's liable to be a common place to put char constants, but not so much a bug as a ..... 'java should have better chars' ;)

leibnitz27 avatar May 13 '21 17:05 leibnitz27

See also https://github.com/nbauma109/jd-core-v0/blob/da9279904055a5a67fe35e95bda61f4723e71e56/src/main/java/jd/core/process/analyzer/classfile/visitor/SetConstantTypeInStringIndexOfMethodsVisitor.java#L190

nbauma109 avatar Apr 02 '23 08:04 nbauma109