Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Spoon inserts incorrect break in enhanced switch block #5743

Closed
Mr-Pine opened this issue Apr 6, 2024 · 0 comments · Fixed by #5746
Closed

[Bug]: Spoon inserts incorrect break in enhanced switch block #5743

Mr-Pine opened this issue Apr 6, 2024 · 0 comments · Fixed by #5746
Labels

Comments

@Mr-Pine
Copy link
Contributor

Mr-Pine commented Apr 6, 2024

Describe the bug

As seen in the outputs below, spoon inserts a break statement at the end of blocks in an enhanced (arrow) switch that don't exist in the original source code
Note: With complianceVersion < 14 the bug doesn't appear. But those versions should not support enhanced switches at all, so...

Source code you are trying to analyze/transform

class Main {
    void main() {
        switch(0) {
            case 1 -> {
            }
        }
    }
}

Source code for your Spoon processing

import spoon.Launcher;
import spoon.SpoonException;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtType;
import spoon.support.compiler.VirtualFile;

import java.util.Collection;

public class Main {

    public static void main(String[] args) {
        CtClass<?> clazz = parseClass("""
                class Main {
                    void main() {
                        switch(0) {
                            case 1 -> {
                            }
                        }
                    }
                }
                """);
        System.out.println(clazz);
    }

    public static CtClass<?> parseClass(String code) {
        Launcher launcher = new Launcher();
        launcher.addInputResource(new VirtualFile(code));
        launcher.getEnvironment().setNoClasspath(true);
        launcher.getEnvironment().setAutoImports(true);
        launcher.getEnvironment().setComplianceLevel(21);
        Collection<CtType<?>> allTypes = launcher.buildModel().getAllTypes();
        try {
            return (CtClass<?>) allTypes.stream().findFirst().get();
        } catch (ClassCastException e) {
            throw new SpoonException("parseClass only considers classes (and not interfaces and enums). Please consider using a Launcher object for more advanced usage.");
        }
    }
}

Actual output

class Main {
    void main() {
        switch (0) {
            case 1 ->
                {
                    break;
                }
        }
    }
}

Expected output

class Main {
    void main() {
        switch (0) {
            case 1 ->
                {
                }
        }
    }
}

Spoon Version

11.0.0

JVM Version

21

What operating system are you using?

Linux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant