Search in sources :

Example 36 with Variable

use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeNewObjectExpr_withGenericsAndArgs.

@Test
public void writeNewObjectExpr_withGenericsAndArgs() {
    // isGeneric() is true and generics() is not empty.
    ConcreteReference listRef = ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(Integer.class))).build();
    ConcreteReference mapRef = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), listRef)).build();
    TypeNode type = TypeNode.withReference(mapRef);
    TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.generator.engine").build());
    MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(TypeNode.INT).setStaticReferenceType(someType).build();
    Variable num = Variable.builder().setName("num").setType(TypeNode.FLOAT).build();
    VariableExpr numExpr = VariableExpr.builder().setVariable(num).build();
    NewObjectExpr newObjectExpr = NewObjectExpr.builder().setIsGeneric(true).setType(type).setArguments(Arrays.asList(methodExpr, numExpr)).build();
    newObjectExpr.accept(writerVisitor);
    assertEquals("new HashMap<String, List<Integer>>(SomeClass.foobar(), num)", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Test(org.junit.Test)

Example 37 with Variable

use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeVariableExpr_wildcardType.

@Test
public void writeVariableExpr_wildcardType() {
    TypeNode wildcardListType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(TypeNode.WILDCARD_REFERENCE)).build());
    Variable variable = Variable.builder().setName("x").setType(wildcardListType).build();
    VariableExpr variableExpr = VariableExpr.builder().setIsDecl(true).setVariable(variable).build();
    variableExpr.accept(writerVisitor);
    assertEquals("List<?> x", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) List(java.util.List) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 38 with Variable

use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeThisObjectValue_accessFieldAndInvokeMethod.

@Test
public void writeThisObjectValue_accessFieldAndInvokeMethod() {
    VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
    TypeNode classType = TypeNode.withReference(ref);
    ThisObjectValue thisObjectValue = ThisObjectValue.withType(classType);
    ValueExpr thisValueExpr = ValueExpr.withValue(thisObjectValue);
    VariableExpr varExpr = VariableExpr.builder().setVariable(Variable.builder().setName("id").setType(TypeNode.STRING).build()).build();
    Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build();
    VariableExpr thisVariableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(thisValueExpr).build();
    MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(thisObjectValue)).setArguments(Arrays.asList(varExpr)).setReturnType(TypeNode.STRING).build();
    AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(thisVariableExpr).setValueExpr(methodExpr).build();
    assignmentExpr.accept(writerVisitor);
    assertThat(writerVisitor.write()).isEqualTo("this.name = this.getName(id)");
}
Also used : ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VaporReference(com.google.api.generator.engine.ast.VaporReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Example 39 with Variable

use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeVariableExpr_localFinalDecl.

@Test
public void writeVariableExpr_localFinalDecl() {
    Variable variable = Variable.builder().setName("x").setType(TypeNode.BOOLEAN).build();
    VariableExpr expr = VariableExpr.builder().setVariable(variable).setIsFinal(true).setIsDecl(true).build();
    expr.accept(writerVisitor);
    assertEquals("final boolean x", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 40 with Variable

use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeInstanceofExpr.

@Test
public void writeInstanceofExpr() {
    Variable variable = Variable.builder().setName("x").setType(TypeNode.STRING).build();
    VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).build();
    InstanceofExpr instanceofExpr = InstanceofExpr.builder().setCheckType(TypeNode.STRING).setExpr(variableExpr).build();
    instanceofExpr.accept(writerVisitor);
    assertEquals("x instanceof String", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Aggregations

Variable (com.google.api.generator.engine.ast.Variable)58 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)54 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)37 Test (org.junit.Test)35 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)34 TypeNode (com.google.api.generator.engine.ast.TypeNode)29 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)26 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)25 Expr (com.google.api.generator.engine.ast.Expr)24 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)20 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)19 CastExpr (com.google.api.generator.engine.ast.CastExpr)17 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)17 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)17 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)17 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)17 List (java.util.List)17 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)16 PrimitiveValue (com.google.api.generator.engine.ast.PrimitiveValue)16 ArrayList (java.util.ArrayList)16