Search in sources :

Example 41 with Variable

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());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) NullObjectValue(com.google.api.generator.engine.ast.NullObjectValue) Value(com.google.api.generator.engine.ast.Value) SuperObjectValue(com.google.api.generator.engine.ast.SuperObjectValue) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Example 42 with Variable

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());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) List(java.util.List) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 43 with Variable

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();
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) IOException(java.io.IOException)

Example 44 with Variable

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();
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 45 with Variable

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();
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr)

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