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

Mismatch in parameters of before/after not recognized #107

Open
Bananeweizen opened this issue Aug 19, 2024 · 1 comment · May be fixed by #108
Open

Mismatch in parameters of before/after not recognized #107

Bananeweizen opened this issue Aug 19, 2024 · 1 comment · May be fixed by #108
Labels
bug Something isn't working

Comments

@Bananeweizen
Copy link
Contributor

What is the smallest, simplest way to reproduce the problem?

Due to copy/pasting an existing recipe first I accidentally created an after method with more arguments than the before method:

    static final class Reproducer {
        @BeforeTemplate
        BufferedReader before(Path path, Charset charset) throws IOException {
            return new BufferedReader(
                    new InputStreamReader(new BufferedInputStream(Files.newInputStream(path)), charset));
        }

        @AfterTemplate
        BufferedReader after(Path path, String content, Charset charset) throws IOException {
            // the second argument "content" does not exist in the before template
            return Files.newBufferedReader(path, charset);
        }
    }

What did you expect to see?

It would be good to raise an explicit error that the @AfterTemplate method doesn't have the same number (and types and order...) of arguments like the @BeforeTemplate method.

What is the full stack trace of any errors you encountered?

The generated code doesn't compile, therefore the error doesn't go unnoticed. But users might still be confused about the exact reason.

FilesRulesRecipes.java:[480,131] incompatible types: <nulltype> cannot be converted to int

grafik

Are you interested in contributing a fix to OpenRewrite?

Yes.

Side note: The above recipe (and multiple variants of it for other nestings of readers/streams) would be a valid recipe for rewrite-java probably. I just stumbled over this in real code yesterday.

@Bananeweizen Bananeweizen added the bug Something isn't working label Aug 19, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Aug 19, 2024
@timtebeek
Copy link
Contributor

Agree with both points above: Best to explicitly warn when after requires more parameters than any of the before templates.

And yes would make a neat addition to have that simplification as an official recipe; I'm thinking rewrite-migrate-java would fit best, as some of those methods are only available in Java 8+.

Bananeweizen pushed a commit to Bananeweizen/rewrite-templating that referenced this issue Aug 26, 2024
Make sure that all templates belonging to a recipe have the same number, order and type for all the method arguments. This avoids accidental copy/paste errors which otherwise can lead to compiling recipe code, but wrong results at runtime.

Fixes openrewrite#107.
@Bananeweizen Bananeweizen linked a pull request Aug 26, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

Successfully merging a pull request may close this issue.

2 participants