Search in sources :

Example 1 with ThisObjectValue

use of com.google.api.generator.engine.ast.ThisObjectValue 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)

Aggregations

AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)1 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)1 ThisObjectValue (com.google.api.generator.engine.ast.ThisObjectValue)1 TypeNode (com.google.api.generator.engine.ast.TypeNode)1 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)1 VaporReference (com.google.api.generator.engine.ast.VaporReference)1 Variable (com.google.api.generator.engine.ast.Variable)1 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)1 Test (org.junit.Test)1