You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
staticfinalclassReproducer {
@BeforeTemplateBufferedReaderbefore(Pathpath, Charsetcharset) throwsIOException {
returnnewBufferedReader(
newInputStreamReader(newBufferedInputStream(Files.newInputStream(path)), charset));
}
@AfterTemplateBufferedReaderafter(Pathpath, Stringcontent, Charsetcharset) throwsIOException {
// the second argument "content" does not exist in the before templatereturnFiles.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
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.
The text was updated successfully, but these errors were encountered:
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.
Fixesopenrewrite#107.
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:
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.
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.
The text was updated successfully, but these errors were encountered: