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

Use method references in lambda where possible. #213

Closed GraxCode closed 3 years ago

GraxCode commented 3 years ago
public class InterfaceTest {

    public static void main(String[] args) {
        new InterfaceTest().start();
    }

    public void start() {
        invokeInterface(this::printMyShit);
    }

    private void printMyShit(String text, String text2) {
        System.out.println(text + " " + text2);
    }

    private void invokeInterface(ItfPrint t) {
        t.print("hello", "world");
    }

    public interface ItfPrint {
        void print(String one, String two);
    }
}

is decompiled to


public class InterfaceTest {
    public static void main(String[] args) {
        new InterfaceTest().start();
    }

    public void start() {
        this.invokeInterface((arg_0, arg_1) -> this.printMyShit(arg_0, arg_1));
    }

    private void printMyShit(String text, String text2) {
        System.out.println(String.valueOf(text) + " " + text2);
    }

    private void invokeInterface(InterfaceTest$ItfPrint t) {
        t.print("hello", "world");
    }
}

You can see the problem, imagine more args like in this sample:

   public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        if (this.shadowSize <= 0) {
            return;
        }
        HiDPIUtils.paintAtScale1x((Graphics2D)((Graphics2D)g), (int)x, (int)y, (int)width, (int)height, (arg_0, arg_1, arg_2, arg_3, arg_4, arg_5) -> this.paintImpl(arg_0, arg_1, arg_2, arg_3, arg_4, arg_5));
    }

Original code:

    @Override
    public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
        if( shadowSize <= 0 )
            return;

        HiDPIUtils.paintAtScale1x( (Graphics2D) g, x, y, width, height, this::paintImpl );
    }
Marcono1234 commented 3 years ago

Is this about CFR not using method references (e.g. this::printMyShit)? Maybe it would be good then to rephrase the title of the issue to reflect that.

leibnitz27 commented 3 years ago

Yeah - I had a ridiculously paranoid check for method reference safety on instance methods. I've loosened it to just stupidly paranoid.