Search in sources :

Example 26 with Reference

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());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 27 with Reference

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());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Example 28 with Reference

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());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 29 with Reference

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());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) Descriptor(com.google.protobuf.Descriptors.Descriptor) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.Test)

Aggregations

Reference (com.google.api.generator.engine.ast.Reference)29 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)27 TypeNode (com.google.api.generator.engine.ast.TypeNode)21 VaporReference (com.google.api.generator.engine.ast.VaporReference)21 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)17 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)13 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)13 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)12 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)12 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)11 Expr (com.google.api.generator.engine.ast.Expr)11 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)11 Statement (com.google.api.generator.engine.ast.Statement)11 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)11 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)10 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)10 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)10 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)10 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)10