Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Nov 28, 2024
1 parent 6a9a2ad commit afe2c77
Showing 1 changed file with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExecutionContext>() {
@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<ExecutionContext> getJavaToCsharpVisitor() {
return new JavaVisitor<>() {


};
}
}

0 comments on commit afe2c77

Please sign in to comment.