question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Statements placed before this() or super() call when using switch expression as argument

See original GitHub issue

CFR version

0.150-SNAPSHOT (commit cf242d2)

Compiler

javac 14

Description

When using a switch expression (Java 14 feature) as argument for the call of this() or super(), statements are placed before the constructor call when decompiled which makes the code invalid.

Source:

class SwitchConstructorTest {
    SwitchConstructorTest(int i) { }
    
    SwitchConstructorTest(String s) {
        // Also happens for super();
        this(switch (s) {
           case "a" -> 1;
           default -> 2;
        });
    }
    
    SwitchConstructorTest(Byte b) {
        this(switch(b) {
           default -> 1; 
        });
    }
}

Decompiled output:

class SwitchConstructorTest {
    SwitchConstructorTest(int n) {
    }

    SwitchConstructorTest(String string) {
        // Invalid, no other statement must appear before call to this()
        int n = switch (string) {
            case "a" -> 1;
            default -> 2;
        };
        this(n);
    }

    SwitchConstructorTest(Byte by) {
        // Invalid, no other statement must appear before call to this()
        switch (by) {
            default: 
        }
        this(1);
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
leibnitz27commented, Apr 13, 2020
1reaction
Marcono1234commented, Mar 28, 2020

This was fixed for the first example where there is no fall-through to the default case by the fix for #129. However, the second example (fall-through to default case) still occurs, see also #133.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Switch Statement in C/C++ - GeeksforGeeks
The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on...
Read more >
How to use Switch case Statement in Java with Example - Xperti
The statement or variable used as a switch expression must be a constant value otherwise it would not be valid. In the case...
Read more >
Kotlin when: A switch with Superpowers - SuperKotlin
The when expression in Kotlin is much better than a switch statement: it can be used with arbitrary conditions, as a statement or...
Read more >
Write Conditional Statement Using SWITCH in DAX and ...
Replacing the expression with TRUE, and the value of that with a conditional expression means that you get the same output, but this...
Read more >
Expression inside switch case statement - Stack Overflow
The solution where the switch expression is set to true works not because true is a constant but because equality with the case...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found