use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodDefinition_templatedReturnTypeAndArguments.
@Test
public void writeMethodDefinition_templatedReturnTypeAndArguments() {
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.STRING)).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("public <T, K, V> Map<K, V> close(Map<K, String> x, Map<T, V> y) {\n", "return foobar();\n", "}\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodInvocationExpr_genericWithArgs.
@Test
public void writeMethodInvocationExpr_genericWithArgs() {
Reference mapReference = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Integer.class))).build();
Reference outerMapReference = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(mapReference, mapReference)).build();
Variable variable = Variable.builder().setType(TypeNode.INT).setName("anArg").build();
VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Double.class), TypeNode.WILDCARD_REFERENCE, outerMapReference)).setArguments(Arrays.asList(varExpr, varExpr, varExpr)).setExprReferenceExpr(varExpr).setReturnType(TypeNode.STRING).build();
Variable lhsVariable = Variable.builder().setType(TypeNode.STRING).setName("someStr").build();
VariableExpr lhsVarExpr = VariableExpr.builder().setVariable(lhsVariable).setIsDecl(true).setIsFinal(true).build();
AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(lhsVarExpr).setValueExpr(methodExpr).build();
assignExpr.accept(writerVisitor);
assertEquals("final String someStr = anArg.<String, Double, ?, HashMap<HashMap<String, Integer>," + " HashMap<String, Integer>>>foobar(anArg, anArg, anArg)", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeTryCatchStatement_withResources.
@Test
public void writeTryCatchStatement_withResources() {
Reference exceptionReference = ConcreteReference.withClazz(IllegalArgumentException.class);
TypeNode type = TypeNode.withReference(exceptionReference);
VariableExpr variableExpr = VariableExpr.builder().setVariable(createVariable("e", type)).setIsDecl(true).build();
TryCatchStatement tryCatch = TryCatchStatement.builder().setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN)).setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("y", "4", TypeNode.INT)))).addCatch(variableExpr, Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("foobar", "123", TypeNode.INT)))).build();
tryCatch.accept(writerVisitor);
assertEquals(String.format("%s%s%s%s%s", "try (boolean aBool = false) {\n", "int y = 4;\n", "} catch (IllegalArgumentException e) {\n", "int foobar = 123;\n", "}\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.
the class TypeParserTest method parseMessageType_nested.
@Test
public void parseMessageType_nested() {
FileDescriptor fileDescriptor = NestedMessageProto.getDescriptor();
Descriptor messageDescriptor = fileDescriptor.getMessageTypes().get(0);
assertEquals("Outer", messageDescriptor.getName());
messageDescriptor = messageDescriptor.getNestedTypes().get(0);
assertEquals("Middle", messageDescriptor.getName());
messageDescriptor = messageDescriptor.getNestedTypes().get(0);
assertEquals("Inner", messageDescriptor.getName());
Reference reference = TypeParser.parseMessageReference(messageDescriptor);
assertEquals("com.google.testgapic.v1beta1.Outer.Middle.Inner", reference.fullName());
}
Aggregations