Skip to content

Commit

Permalink
fix(ast): update import generation to cover annotation parameters (#1229
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
emmileaf committed Jan 26, 2023
1 parent 86a560d commit bdf12b0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Expand Up @@ -166,6 +166,9 @@ public void visit(ScopeNode scope) {
@Override
public void visit(AnnotationNode annotation) {
annotation.type().accept(this);
if (annotation.descriptionExprs() != null) {
expressions(annotation.descriptionExprs());
}
}

@Override
Expand Down
Expand Up @@ -62,6 +62,7 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Repeatable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -390,6 +391,43 @@ public void writeVariableExprImports_withAnnotations() {
writerVisitor.write());
}

@Test
public void writeVariableExprImports_annotationsWithDescription() {
Variable variable =
Variable.builder()
.setName("expr")
.setType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class)))
.build();

VariableExpr annotationDescription =
VariableExpr.builder()
.setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(List.class)))
.build();

// Constructs with annotation @Repeatable(List.class)
VariableExpr variableExpr =
VariableExpr.builder()
.setVariable(variable)
.setIsDecl(true)
.setAnnotations(
Arrays.asList(
AnnotationNode.builder()
.setType(
TypeNode.withReference(ConcreteReference.withClazz(Repeatable.class)))
.setDescription(annotationDescription)
.build()))
.build();

variableExpr.accept(writerVisitor);
assertEquals(
LineFormatter.lines(
"import com.google.api.generator.engine.ast.Expr;\n",
"import java.lang.annotation.Repeatable;\n",
"import java.util.List;\n\n"),
writerVisitor.write());
}

@Test
public void writeAnonymousClassExprImports() {
// [Constructing] Function<List<IOException>, MethodDefinition>
Expand Down

0 comments on commit bdf12b0

Please sign in to comment.