Skip to content

Commit bdf12b0

Browse files
authored
fix(ast): update import generation to cover annotation parameters (#1229)
This change is cherry-picked from an AST fix made in the spring branch (https://togithub.com/googleapis/gapic-generator-java/pull/1208). It fixes `ImportWriter` to account for types introduced by annotation parameters, which as enabled in #1012.
1 parent 86a560d commit bdf12b0

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

gapic-generator-java/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java

+3
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ public void visit(ScopeNode scope) {
166166
@Override
167167
public void visit(AnnotationNode annotation) {
168168
annotation.type().accept(this);
169+
if (annotation.descriptionExprs() != null) {
170+
expressions(annotation.descriptionExprs());
171+
}
169172
}
170173

171174
@Override

gapic-generator-java/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.io.FileNotFoundException;
6363
import java.io.FileOutputStream;
6464
import java.io.IOException;
65+
import java.lang.annotation.Repeatable;
6566
import java.util.ArrayList;
6667
import java.util.Arrays;
6768
import java.util.HashMap;
@@ -390,6 +391,43 @@ public void writeVariableExprImports_withAnnotations() {
390391
writerVisitor.write());
391392
}
392393

394+
@Test
395+
public void writeVariableExprImports_annotationsWithDescription() {
396+
Variable variable =
397+
Variable.builder()
398+
.setName("expr")
399+
.setType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class)))
400+
.build();
401+
402+
VariableExpr annotationDescription =
403+
VariableExpr.builder()
404+
.setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
405+
.setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(List.class)))
406+
.build();
407+
408+
// Constructs with annotation @Repeatable(List.class)
409+
VariableExpr variableExpr =
410+
VariableExpr.builder()
411+
.setVariable(variable)
412+
.setIsDecl(true)
413+
.setAnnotations(
414+
Arrays.asList(
415+
AnnotationNode.builder()
416+
.setType(
417+
TypeNode.withReference(ConcreteReference.withClazz(Repeatable.class)))
418+
.setDescription(annotationDescription)
419+
.build()))
420+
.build();
421+
422+
variableExpr.accept(writerVisitor);
423+
assertEquals(
424+
LineFormatter.lines(
425+
"import com.google.api.generator.engine.ast.Expr;\n",
426+
"import java.lang.annotation.Repeatable;\n",
427+
"import java.util.List;\n\n"),
428+
writerVisitor.write());
429+
}
430+
393431
@Test
394432
public void writeAnonymousClassExprImports() {
395433
// [Constructing] Function<List<IOException>, MethodDefinition>

0 commit comments

Comments
 (0)