use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeMethodDefinitionImports_templatedMixedNamesAndTypes.
@Test
public void writeMethodDefinitionImports_templatedMixedNamesAndTypes() {
Reference mapRef = ConcreteReference.withClazz(Map.class);
List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("K", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).build(), VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("T", "V")).build());
TypeNode returnType = TypeNode.withReference(mapRef);
MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(returnType).setTemplateNames(Arrays.asList("T", "K", "V")).setReturnTemplateNames(Arrays.asList("K", "V")).setArguments(arguments).setReturnExpr(MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(returnType).build()).build();
methodDefinition.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import java.util.Map;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeAnonymousClassExprImports.
@Test
public void writeAnonymousClassExprImports() {
// [Constructing] Function<List<IOException>, MethodDefinition>
ConcreteReference exceptionListRef = ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(IOException.class))).build();
ConcreteReference methodDefinitionRef = ConcreteReference.withClazz(MethodDefinition.class);
ConcreteReference ref = ConcreteReference.builder().setClazz(Function.class).setGenerics(Arrays.asList(exceptionListRef, methodDefinitionRef)).build();
TypeNode type = TypeNode.withReference(ref);
// [Constructing] HashMap<String, Integer> map;
ConcreteReference mapRef = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Integer.class))).build();
VariableExpr mapExpr = VariableExpr.builder().setVariable(Variable.builder().setName("map").setType(TypeNode.withReference(mapRef)).build()).setIsDecl(true).build();
ExprStatement exprStatement = ExprStatement.withExpr(mapExpr);
// [Constructing] an input argument whose type is `List<IOException>`
VariableExpr arg = VariableExpr.builder().setVariable(Variable.builder().setName("arg").setType(TypeNode.withReference(exceptionListRef)).build()).setIsDecl(true).build();
// [Constructing] a return variable expression whose type is `MethodDefinition`
VariableExpr returnArg = VariableExpr.builder().setVariable(Variable.builder().setName("returnArg").setType(TypeNode.withReference(methodDefinitionRef)).build()).build();
MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.withReference(methodDefinitionRef)).setArguments(Arrays.asList(arg)).setReturnExpr(returnArg).setName("apply").build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).setStatements(Arrays.asList(exprStatement)).build();
anonymousClassExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.MethodDefinition;\n", "import com.google.common.base.Function;\n", "import java.io.IOException;\n", "import java.util.HashMap;\n", "import java.util.List;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeReferenceConstructorExprImports_basic.
@Test
public void writeReferenceConstructorExprImports_basic() {
VaporReference ref = VaporReference.builder().setName("Parent").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
ReferenceConstructorExpr referenceConstructorExpr = ReferenceConstructorExpr.superBuilder().setType(classType).build();
referenceConstructorExpr.accept(writerVisitor);
assertEquals("import com.google.example.v1.Parent;\n\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class Message method findAndUnwrapPaginatedRepeatedField.
/**
* Returns the first list repeated field in a message, unwrapped from its list type.
*/
@Nullable
public Field findAndUnwrapPaginatedRepeatedField() {
for (Field field : fields()) {
if (field.isMap()) {
List<Reference> repeatedGenericMapRefs = field.type().reference().generics();
TypeNode paginatedType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.Entry.class).setGenerics(Arrays.asList(repeatedGenericMapRefs.get(0), repeatedGenericMapRefs.get(1))).build());
return field.toBuilder().setType(paginatedType).build();
}
}
for (Field field : fields()) {
if (field.isRepeated() && !field.isMap()) {
Reference repeatedGenericRef = field.type().reference().generics().get(0);
return field.toBuilder().setType(TypeNode.withReference(repeatedGenericRef)).build();
}
}
return null;
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class MethodSignatureParserTest method flattenMethodSignatures_manyToOne.
@Test
public void flattenMethodSignatures_manyToOne() {
String fooName = "fooName";
String anInt = "anInt";
TypeNode fooTypeOne = TypeNode.withReference(VaporReference.builder().setName("FooName").setPakkage("com.google.foobar").build());
TypeNode fooTypeTwo = TypeNode.withReference(VaporReference.builder().setName("FooTwoName").setPakkage("com.google.foobar").build());
List<String> argumentNames = Arrays.asList(fooName, anInt);
BiFunction<String, TypeNode, MethodArgument> methodArgFn = (name, type) -> MethodArgument.builder().setName(name).setType(type).setField(Field.builder().setName(name).setType(type).build()).build();
List<MethodArgument> fooArgs = Arrays.asList(TypeNode.STRING, fooTypeOne, fooTypeTwo).stream().map(t -> methodArgFn.apply(fooName, t)).collect(Collectors.toList());
Map<String, List<MethodArgument>> argumentNameToOverloads = new HashMap<>();
argumentNameToOverloads.put(fooName, fooArgs);
argumentNameToOverloads.put(anInt, Arrays.asList(methodArgFn.apply(anInt, TypeNode.INT)));
List<List<MethodArgument>> flattenedSignatures = MethodSignatureParser.flattenMethodSignatureVariants(argumentNames, argumentNameToOverloads);
assertEquals(3, flattenedSignatures.size());
assertTrue(containsTypes(flattenedSignatures, Arrays.asList(TypeNode.STRING, TypeNode.INT)));
assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeOne, TypeNode.INT)));
assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeTwo, TypeNode.INT)));
}
Aggregations