Closed thomaszub closed 1 year ago
I encountered the same problem on some of my projects. And I can confirm it's due to the switch expressions. Any ideas how to fix this or to work around it?
Hi @thomaszub, @wjglerum, @fprochazka thank you for reporting the issue!
Our switch parse tests, seem to work fine on java 8, 11, and 17. Do you have an example in the code where this occurs?
Hi @traceyyoshima,
this error occurs in JavaVisitor
. Modifying the test in JavaVisitorTest
triggers the error:
/*
* Copyright 2020 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java
import org.junit.jupiter.api.Test
import org.openrewrite.ExecutionContext
import org.openrewrite.java.tree.J
interface JavaVisitorTest : JavaRecipeTest {
@Suppress("RedundantThrows")
@Test
fun javaVisitorHandlesPaddedWithNullElem() = assertChanged(
recipe = toRecipe {
object : JavaIsoVisitor<ExecutionContext>() {
override fun visitMethodInvocation(
method: J.MethodInvocation,
p: ExecutionContext
): J.MethodInvocation? {
val mi = super.visitMethodInvocation(method, p)
if ("removeMethod" == mi.simpleName) {
return null
}
return mi
}
}
}.doNext(
toRecipe {
object : JavaIsoVisitor<ExecutionContext>() {
override fun visitMethodDeclaration(
method: J.MethodDeclaration,
p: ExecutionContext
): J.MethodDeclaration {
var md = super.visitMethodDeclaration(method, p)
if (md.simpleName == "allTheThings") {
md = md.withTemplate(JavaTemplate.builder({ cursor }, "Exception").build(),
md.coordinates.replaceThrows())
}
return md
}
}
}
),
before = """
class A {
void allTheThings() {
var val = switch("abc") {
case "a" -> 1;
case "b" -> 2;
default -> 0;
};
doSomething();
removeMethod();
}
void doSomething() {}
void removeMethod() {}
}
""",
after = """
class A {
void allTheThings() throws Exception {
doSomething();
}
void doSomething() {}
void removeMethod() {}
}
""",
cycles = 2,
expectedCyclesThatMakeChanges = 2
)
}
@traceyyoshima
Do you have an example in the code where this occurs?
That's something of a challenge, since the exceptions don't contain any messages and no debug info even with mvn -X
is printed, with information on what source file is the plugin processing, so I don't know how to get you the sample easily. I would have to start removing code randomly from the affected maven module and it's quite large.
How can one get the sample in situations like this?
Hi @thomaszub, @wjglerum, @fprochazka thank you for reporting the issue!
Our switch parse tests, seem to work fine on java 8, 11, and 17. Do you have an example in the code where this occurs?
I could try to extract a little sample from our codebase later today. I have a small project where we only use 1 or 2 switch expressions, so that should be fairly easy to do.
Hi @thomaszub @fprochazka @wjglerum, thank you all for responding so quickly -- The test Thomas shared reproduces the issue. I'll use what he provided to track down the minimal amount of code to cause the SOE and look at this today.
Hi @thomaszub @fprochazka @wjglerum, thank you all for responding so quickly -- The test Thomas shared reproduces the issue. I'll use what he provided to track down the minimal amount of code to cause the SOE and look at this today.
Wow that was quick! I was about to look into a reproducer, but you already managed to provide a fix. Any chance that I could try this out locally already with a new build or version of openrewrite?
You can consume snapshots of OpenRewrite via the sonatype snapshot repository:
https://oss.sonatype.org/content/repositories/snapshots/
We do not publish the recipe bom in snapshots, so you will need to explicitly set the various snapshot versions of the rewrite artifacts.
Hi,
parsing a switch expression leads to a
StackOverflowError
. Visiting the switch expression inJavaVisitor
in line 1043 callsgetType
ofJ.SwitchExpression
. This function creates a newJavaVisitor
which callsvisitSwitchExpression
during traversing.Kind regards Thomas