diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/template/AnnotationTemplateGenerator.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/template/AnnotationTemplateGenerator.java index 50345fd75da..0f44d7bcb69 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/template/AnnotationTemplateGenerator.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/template/AnnotationTemplateGenerator.java @@ -223,8 +223,10 @@ private void classDeclaration(StringBuilder before, J.ClassDeclaration parent, S classDeclaration(before, (J.ClassDeclaration) statement, templated, cursor); } } - c = c.withBody(null).withLeadingAnnotations(null).withPrefix(Space.EMPTY); - before.insert(0, c.printTrimmed(cursor).trim() + '{'); + c = c.withBody(J.Block.createEmptyBlock()).withLeadingAnnotations(null).withPrefix(Space.EMPTY); + String printed = c.printTrimmed(cursor); + int braceIndex = printed.lastIndexOf('{'); + before.insert(0, braceIndex == -1 ? printed + '{' : printed.substring(0, braceIndex + 1)); } private String variable(J.VariableDeclarations variable, Cursor cursor) {