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());
}
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());
}
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)");
}
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());
}
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());
}
Aggregations