diff --git a/src/test/java/org/openrewrite/staticanalysis/CatchClauseOnlyRethrowsTest.java b/src/test/java/org/openrewrite/staticanalysis/CatchClauseOnlyRethrowsTest.java index ee40b3999..2d945878c 100755 --- a/src/test/java/org/openrewrite/staticanalysis/CatchClauseOnlyRethrowsTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/CatchClauseOnlyRethrowsTest.java @@ -345,42 +345,37 @@ void foo() throws IllegalAccessException { try { throw new IllegalAccessException(); } catch (Exception e) { - throw e; // C# can rethrow the caught exception implicitly and so the `e` Identifier is removed by the inline visitor below + throw e; // `e` is removed below } } - }""" - , spec -> spec.beforeRecipe(compUnit -> { + } + """, + spec -> spec.beforeRecipe(compUnit -> { + // C# can rethrow the caught exception implicitly and so the `e` Identifier is removed by the inline visitor below Cs.CompilationUnit cSharpCompUnit = (Cs.CompilationUnit) new JavaVisitor() { - @Override - public J visitThrow(J.Throw thrown, ExecutionContext ctx) { - if (thrown.getException() instanceof J.Identifier) { - return thrown.withException(new J.Empty(Tree.randomId(), Space.EMPTY, Markers.EMPTY)); - } - return thrown; - } - }.visit(JavaToCsharp.compilationUnit(compUnit), new InMemoryExecutionContext()); - - assertThat(cSharpCompUnit.getMembers().get(0)).isInstanceOf(J.ClassDeclaration.class); - assertThat(((J.ClassDeclaration)cSharpCompUnit.getMembers().get(0)).getBody().getStatements().get(0)).isInstanceOf(J.MethodDeclaration.class); - J.MethodDeclaration md = (J.MethodDeclaration) ((J.ClassDeclaration)cSharpCompUnit.getMembers().get(0)).getBody().getStatements().get(0); - assertThat(md.getBody().getStatements().get(0)).isInstanceOf(J.Try.class); - - Cs.CompilationUnit output = (Cs.CompilationUnit) new CatchClauseOnlyRethrows().getVisitor().visit(cSharpCompUnit, new InMemoryExecutionContext()); - - assertThat(output.getMembers().get(0)).isInstanceOf(J.ClassDeclaration.class); - assertThat(((J.ClassDeclaration)output.getMembers().get(0)).getBody().getStatements().get(0)).isInstanceOf(J.MethodDeclaration.class); - md = (J.MethodDeclaration) ((J.ClassDeclaration)output.getMembers().get(0)).getBody().getStatements().get(0); - assertThat(md.getBody().getStatements().get(0)).isInstanceOf(J.Throw.class); - } + @Override + public J visitThrow(J.Throw thrown, ExecutionContext ctx) { + if (thrown.getException() instanceof J.Identifier) { + return thrown.withException(new J.Empty(Tree.randomId(), Space.EMPTY, Markers.EMPTY)); + } + return thrown; + } + }.visit(JavaToCsharp.compilationUnit(compUnit), new InMemoryExecutionContext()); + + Cs.CompilationUnit after = (Cs.CompilationUnit) new CatchClauseOnlyRethrows().getVisitor() + .visit(cSharpCompUnit, new InMemoryExecutionContext()); + + var classA = (J.ClassDeclaration) after.getMembers().get(0); + var methodFoo = (J.MethodDeclaration) classA.getBody().getStatements().get(0); + var firstStatement = methodFoo.getBody().getStatements().get(0); + assertThat(firstStatement) + .isInstanceOf(J.Throw.class) // The `try/catch` block removed + .extracting(s -> ((J.Throw) s).getException()) + .isInstanceOf(J.NewClass.class); + } ) ) ); } - public JavaVisitor getJavaToCsharpVisitor() { - return new JavaVisitor<>() { - - - }; - } }