From 2523d450777047531409e910bb931967256a43f0 Mon Sep 17 00:00:00 2001 From: Marcelo Boveto Shima Date: Mon, 3 Jun 2024 15:43:48 -0300 Subject: [PATCH] don't add annotations to inner classes. --- .../java/support/add-java-annotation.spec.ts | 22 +++++++++++++++++++ .../java/support/add-java-annotation.ts | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/generators/java/support/add-java-annotation.spec.ts b/generators/java/support/add-java-annotation.spec.ts index 585c63e80eea..b9ec5a9caa7f 100644 --- a/generators/java/support/add-java-annotation.spec.ts +++ b/generators/java/support/add-java-annotation.spec.ts @@ -102,6 +102,28 @@ import com.mycompany.myapp.Foo; @Foo(bar="baz") public class MyTest {}`); }); + + it('should not add the annotation to inner class', () => { + expect( + addJavaAnnotation( + `package com.mycompany.myapp; + +public class MyTest { + public static class Inner { + + } +}`, + { annotation: 'Foo' }, + ), + ).toBe(`package com.mycompany.myapp; + +@Foo +public class MyTest { + public static class Inner { + + } +}`); + }); }); }); }); diff --git a/generators/java/support/add-java-annotation.ts b/generators/java/support/add-java-annotation.ts index 6bf2caec5051..3d3061800ffa 100644 --- a/generators/java/support/add-java-annotation.ts +++ b/generators/java/support/add-java-annotation.ts @@ -53,7 +53,7 @@ const addJavaAnnotationToContent = (content: string, annotationDef: JavaAnnotati throw new Error(`Annotation already exists: ${annotation} replace is not implemented yet.`); } // add the annotation before class or interface - content = content.replace(/\n([a-w ]*(class|@?interface|enum) )/g, `\n@${annotationToAdd}\n$1`); + content = content.replace(/\n([a-w ]*(class|@?interface|enum) )/, `\n@${annotationToAdd}\n$1`); } return content; };