use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAssignmentExpr_basicValue.
@Test
public void writeAssignmentExpr_basicValue() {
Variable variable = Variable.builder().setName("x").setType(TypeNode.INT).build();
VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).setIsDecl(true).build();
Value value = PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build();
Expr valueExpr = ValueExpr.builder().setValue(value).build();
AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
assignExpr.accept(writerVisitor);
assertEquals("int x = 3", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeVariableExprImport_wildcardTypeWithUpperBound.
@Test
public void writeVariableExprImport_wildcardTypeWithUpperBound() {
TypeNode wildcardListType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.wildcardWithUpperBound(ConcreteReference.withClazz(Expr.class)))).build());
// Constructs `List<? extends Expr> x`.
Variable variable = Variable.builder().setName("x").setType(wildcardListType).build();
VariableExpr variableExpr = VariableExpr.builder().setIsDecl(true).setVariable(variable).build();
variableExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.Expr;\n", "import java.util.List;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createUpdateShelfMap.
private MethodDefinition createUpdateShelfMap() {
ConcreteReference nonexistentShelfExceptionRef = ConcreteReference.builder().setClazz(Exception.class).build();
Variable shelfVar = createVarFromVaporRef(shelfClassRef, "newShelf");
VariableExpr shelfNameFromNewShelfObject = fieldFromShelfObjectExpr(shelfVar, createVarFromType(TypeNode.STRING, "shelfName"));
MethodInvocationExpr mapContainsKeyExpr = methodExprWithRefArgAndReturn(shelfMapVar, Arrays.asList(shelfNameFromNewShelfObject));
MethodInvocationExpr putShelfToMapExpr = methodExprWithRefAndArg(shelfMapVar, "put", Arrays.asList(shelfNameFromNewShelfObject, VariableExpr.withVariable(shelfVar)));
ThrowExpr throwExpr = ThrowExpr.builder().setMessageExpr("Updating shelf is not existing in the map").setType(TypeNode.withReference(nonexistentShelfExceptionRef)).build();
IfStatement updateShelfMapIfElseBlock = IfStatement.builder().setConditionExpr(mapContainsKeyExpr).setBody(Arrays.asList(ExprStatement.withExpr(putShelfToMapExpr))).setElseBody(Arrays.asList(ExprStatement.withExpr(throwExpr))).build();
return MethodDefinition.builder().setName("updateShelfMap").setThrowsExceptions(Arrays.asList(TypeNode.withReference(nonexistentShelfExceptionRef))).setReturnType(TypeNode.VOID).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(updateShelfMapIfElseBlock)).setArguments(Arrays.asList(VariableExpr.builder().setVariable(shelfVar).setIsDecl(true).build())).build();
}
use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createAddShelfMethod.
private MethodDefinition createAddShelfMethod() {
Variable nameVar = createVarFromType(TypeNode.STRING, "name");
Variable seriesDoubleNumVar = createVarFromType(TypeNode.DOUBLE, "seriesDoubleNum");
CastExpr seriesNumDoubleToIntExpr = CastExpr.builder().setExpr(VariableExpr.withVariable(seriesDoubleNumVar)).setType(TypeNode.INT).build();
AssignmentExpr castSeriesNumExpr = createAssignmentExpr(createVarDeclExpr(seriesNumVar), seriesNumDoubleToIntExpr);
ReturnExpr maxValueReturnExpr = createReturnExpr("Series number equals to max int value.");
ReturnExpr duplicateKeyReturnExpr = createReturnExpr("Shelf is already existing in the map.");
// TODO: update the condition from `condition` to `seriesNum == MAX_VALUE`
IfStatement maxValueCheck = IfStatement.builder().setConditionExpr(VariableExpr.withVariable(createVarFromType(TypeNode.BOOLEAN, "condition"))).setBody(Arrays.asList(ExprStatement.withExpr(maxValueReturnExpr))).build();
NewObjectExpr newShelfExpr = createNewObjectExprWithArg(nameVar);
MethodInvocationExpr addShelfToListExpr = methodExprWithRefAndArg(shelfListVar, "add", Arrays.asList(newShelfExpr));
MethodInvocationExpr putShelfToMapExpr = methodExprWithRefAndArg(shelfMapVar, "put", Arrays.asList(VariableExpr.withVariable(nameVar), newShelfExpr));
MethodInvocationExpr mapContainsKey = methodExprWithRefArgAndReturn(shelfMapVar, Arrays.asList(VariableExpr.withVariable(nameVar)));
IfStatement mapContainsKeyCheck = IfStatement.builder().setConditionExpr(mapContainsKey).setBody(Arrays.asList(ExprStatement.withExpr(duplicateKeyReturnExpr))).build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.OVERRIDE)).setName("addShelf").setReturnType(TypeNode.STRING).setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("Shelf added."))).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(castSeriesNumExpr), maxValueCheck, ExprStatement.withExpr(addShelfToListExpr), mapContainsKeyCheck, ExprStatement.withExpr(putShelfToMapExpr))).setArguments(Arrays.asList(createVarDeclExpr(nameVar), createVarDeclExpr(seriesDoubleNumVar))).build();
}
use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createAddBookToShelf.
private MethodDefinition createAddBookToShelf() {
Variable bookVar = createVarFromVaporRef(bookClassRef, "book");
AnonymousClassExpr anonymousBookClassExpr = createAnonymousClass();
AssignmentExpr createNewBook = createAssignmentExpr(createVarDeclExpr(bookVar), anonymousBookClassExpr);
return MethodDefinition.builder().setHeaderCommentStatements(Arrays.asList(createPreMethodLineComment("Private helper."))).setName("addBookToShelf").setReturnType(TypeNode.withReference(bookClassRef)).setArguments(Arrays.asList(createVarDeclExpr(bookKindVar), createVarDeclExpr(shelfVar))).setScope(ScopeNode.PRIVATE).setBody(Arrays.asList(ExprStatement.withExpr(createNewBook))).setReturnExpr(VariableExpr.withVariable(bookVar)).build();
}
Aggregations