From b0ee012a53d649d78566440954738b73d643dd93 Mon Sep 17 00:00:00 2001 From: Emily Wang Date: Mon, 31 Oct 2022 14:30:40 -0400 Subject: [PATCH 1/3] Support annotations on VariableExpr in ImportWriterVisitor --- .../google/api/generator/engine/writer/ImportWriterVisitor.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java index 0967cea087..b3b2c9d997 100644 --- a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java @@ -183,6 +183,7 @@ public void visit(TernaryExpr ternaryExpr) { @Override public void visit(VariableExpr variableExpr) { variableExpr.variable().type().accept(this); + annotations(variableExpr.annotations()); if (variableExpr.exprReferenceExpr() != null) { variableExpr.exprReferenceExpr().accept(this); } From 2be0a0f8b127de2fd7946378a7cbaa43c6ba4ad6 Mon Sep 17 00:00:00 2001 From: Emily Wang Date: Mon, 31 Oct 2022 14:51:41 -0400 Subject: [PATCH 2/3] Add unit test for imports for annotations on VariableExpr --- .../writer/ImportWriterVisitorTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java index 07be47a996..04ece44b54 100644 --- a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java @@ -37,6 +37,7 @@ import com.google.api.generator.engine.ast.MethodInvocationExpr; import com.google.api.generator.engine.ast.NewObjectExpr; import com.google.api.generator.engine.ast.PackageInfoDefinition; +import com.google.api.generator.engine.ast.PrimitiveValue; import com.google.api.generator.engine.ast.Reference; import com.google.api.generator.engine.ast.ReferenceConstructorExpr; import com.google.api.generator.engine.ast.RelationalOperationExpr; @@ -363,6 +364,41 @@ public void writeVariableExprImports_nestedReference() { writerVisitor.write()); } + @Test + public void writeVariableExprImports_withAnnotations() { + Variable variable = + Variable.builder() + .setName("expr") + .setType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))) + .build(); + + TypeNode fakeAnnotationType = + TypeNode.withReference( + VaporReference.builder().setName("FakeAnnotation").setPakkage("com.foo.bar").build()); + + AnnotationNode annotation = + AnnotationNode.builder() + .setType(fakeAnnotationType) + .setDescription( + ValueExpr.withValue( + PrimitiveValue.builder().setValue("1").setType(TypeNode.INT).build())) + .build(); + + VariableExpr variableExpr = + VariableExpr.builder() + .setVariable(variable) + .setIsDecl(true) + .setAnnotations(Arrays.asList(annotation)) + .build(); + + variableExpr.accept(writerVisitor); + assertEquals( + LineFormatter.lines( + "import com.foo.bar.FakeAnnotation;\n", + "import com.google.api.generator.engine.ast.Expr;\n\n"), + writerVisitor.write()); + } + @Test public void writeAnonymousClassExprImports() { // [Constructing] Function, MethodDefinition> From 73d858fa932b410dc64cd0b225a714dd12bc3bca Mon Sep 17 00:00:00 2001 From: Emily Wang Date: Tue, 1 Nov 2022 10:52:08 -0400 Subject: [PATCH 3/3] Make unit test more concise --- .../writer/ImportWriterVisitorTest.java | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java index 04ece44b54..bd8d62afb7 100644 --- a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java @@ -37,7 +37,6 @@ import com.google.api.generator.engine.ast.MethodInvocationExpr; import com.google.api.generator.engine.ast.NewObjectExpr; import com.google.api.generator.engine.ast.PackageInfoDefinition; -import com.google.api.generator.engine.ast.PrimitiveValue; import com.google.api.generator.engine.ast.Reference; import com.google.api.generator.engine.ast.ReferenceConstructorExpr; import com.google.api.generator.engine.ast.RelationalOperationExpr; @@ -372,30 +371,21 @@ public void writeVariableExprImports_withAnnotations() { .setType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))) .build(); - TypeNode fakeAnnotationType = - TypeNode.withReference( - VaporReference.builder().setName("FakeAnnotation").setPakkage("com.foo.bar").build()); - - AnnotationNode annotation = - AnnotationNode.builder() - .setType(fakeAnnotationType) - .setDescription( - ValueExpr.withValue( - PrimitiveValue.builder().setValue("1").setType(TypeNode.INT).build())) - .build(); - VariableExpr variableExpr = VariableExpr.builder() .setVariable(variable) .setIsDecl(true) - .setAnnotations(Arrays.asList(annotation)) + .setAnnotations( + Arrays.asList( + AnnotationNode.withType( + TypeNode.withReference(ConcreteReference.withClazz(Generated.class))))) .build(); variableExpr.accept(writerVisitor); assertEquals( LineFormatter.lines( - "import com.foo.bar.FakeAnnotation;\n", - "import com.google.api.generator.engine.ast.Expr;\n\n"), + "import com.google.api.generator.engine.ast.Expr;\n", + "import javax.annotation.Generated;\n\n"), writerVisitor.write()); }