Search in sources :

Example 1 with CastExpr

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

the class ResourceNameHelperClassComposer method createEqualsMethod.

private static MethodDefinition createEqualsMethod(ResourceName resourceName, List<List<String>> tokenHierarchies, TypeStore typeStore) {
    // Create method definition variables.
    Variable oVariable = Variable.builder().setType(TypeNode.withReference(javaObjectReference)).setName("o").build();
    VariableExpr argVarExpr = VariableExpr.builder().setIsDecl(false).setVariable(oVariable).build();
    TypeNode thisClassType = typeStore.get(getThisClassName(resourceName));
    ValueExpr thisValueExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
    ValueExpr trueValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build());
    // Create first if statement's return expression
    ReturnExpr returnTrueExpr = ReturnExpr.withExpr(trueValueExpr);
    // Create second if statement's condition expression
    RelationalOperationExpr oEqualsThisExpr = RelationalOperationExpr.equalToWithExprs(argVarExpr, thisValueExpr);
    RelationalOperationExpr oNotEqualsNullExpr = RelationalOperationExpr.notEqualToWithExprs(argVarExpr, ValueExpr.createNullExpr());
    MethodInvocationExpr getClassMethodInvocationExpr = MethodInvocationExpr.builder().setMethodName("getClass").build();
    RelationalOperationExpr getClassEqualsExpr = RelationalOperationExpr.equalToWithExprs(getClassMethodInvocationExpr, getClassMethodInvocationExpr.toBuilder().setExprReferenceExpr(argVarExpr).build());
    LogicalOperationExpr orLogicalExpr = LogicalOperationExpr.logicalOrWithExprs(oNotEqualsNullExpr, getClassEqualsExpr);
    // Create second if statement's body assignment expression.
    Variable thatVariable = Variable.builder().setName("that").setType(thisClassType).build();
    VariableExpr thatVariableExpr = VariableExpr.builder().setIsDecl(false).setVariable(thatVariable).build();
    CastExpr oCastExpr = CastExpr.builder().setExpr(argVarExpr).setType(thisClassType).build();
    AssignmentExpr thatAssignmentExpr = AssignmentExpr.builder().setVariableExpr(thatVariableExpr.toBuilder().setIsDecl(true).build()).setValueExpr(oCastExpr).build();
    // PubSub special-case handling - exclude _deleted-topic_.
    List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(ts -> !ts.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
    // Create return expression in the second if statement's body.
    Set<String> tokenSet = getTokenSet(processedTokenHierarchies);
    Iterator<String> itToken = tokenSet.iterator();
    Expr curTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
    while (itToken.hasNext()) {
        Expr nextTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
        curTokenExpr = LogicalOperationExpr.logicalAndWithExprs(curTokenExpr, nextTokenExpr);
    }
    ReturnExpr secondIfReturnExpr = ReturnExpr.withExpr(curTokenExpr);
    // Code: if (o == this) { return true;}
    IfStatement firstIfStatement = IfStatement.builder().setConditionExpr(oEqualsThisExpr).setBody(Arrays.asList(ExprStatement.withExpr(returnTrueExpr))).build();
    // Code: if (o != null || getClass() == o.getClass()) { FoobarName that = ((FoobarName) o);
    // return ..}
    IfStatement secondIfStatement = IfStatement.builder().setConditionExpr(orLogicalExpr).setBody(Arrays.asList(ExprStatement.withExpr(thatAssignmentExpr), ExprStatement.withExpr(secondIfReturnExpr))).build();
    // Create method's return expression.
    ValueExpr falseValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("false").build());
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setArguments(argVarExpr.toBuilder().setIsDecl(true).build()).setReturnType(TypeNode.BOOLEAN).setName("equals").setReturnExpr(falseValueExpr).setBody(Arrays.asList(firstIfStatement, secondIfStatement)).build();
}
Also used : Arrays(java.util.Arrays) Reference(com.google.api.generator.engine.ast.Reference) PathTemplate(com.google.api.pathtemplate.PathTemplate) ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) Variable(com.google.api.generator.engine.ast.Variable) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Generated(javax.annotation.Generated) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) CommentComposer(com.google.api.generator.gapic.composer.comment.CommentComposer) Map(java.util.Map) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) ImmutableMap(com.google.common.collect.ImmutableMap) ForStatement(com.google.api.generator.engine.ast.ForStatement) Set(java.util.Set) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ResourceNameConstants(com.google.api.generator.gapic.utils.ResourceNameConstants) List(java.util.List) GapicContext(com.google.api.generator.gapic.model.GapicContext) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) IfStatement(com.google.api.generator.engine.ast.IfStatement) BetaApi(com.google.api.core.BetaApi) TypeNode(com.google.api.generator.engine.ast.TypeNode) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) HashMap(java.util.HashMap) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) Function(java.util.function.Function) ArrayList(java.util.ArrayList) 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) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) ValidationException(com.google.api.pathtemplate.ValidationException) LinkedHashSet(java.util.LinkedHashSet) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Iterator(java.util.Iterator) CastExpr(com.google.api.generator.engine.ast.CastExpr) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) GapicClass(com.google.api.generator.gapic.model.GapicClass) ResourceName(com.google.api.generator.gapic.model.ResourceName) Statement(com.google.api.generator.engine.ast.Statement) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Variable(com.google.api.generator.engine.ast.Variable) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) IfStatement(com.google.api.generator.engine.ast.IfStatement) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) 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) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) List(java.util.List) ArrayList(java.util.ArrayList) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 2 with CastExpr

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

the class JavaWriterVisitorTest method writeCastExpr_nested.

@Test
public void writeCastExpr_nested() {
    Variable variable = Variable.builder().setType(TypeNode.STRING).setName("str").build();
    VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
    CastExpr castExpr = CastExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Object.class))).setExpr(varExpr).build();
    castExpr = CastExpr.builder().setType(TypeNode.STRING).setExpr(castExpr).build();
    castExpr.accept(writerVisitor);
    assertEquals("((String) ((Object) str))", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) CastExpr(com.google.api.generator.engine.ast.CastExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 3 with CastExpr

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

the class JavaWriterVisitorTest method writeCastExpr_methodInvocation.

@Test
public void writeCastExpr_methodInvocation() {
    TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.some.pakkage").build());
    MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setStaticReferenceType(someType).setReturnType(TypeNode.STRING).build();
    CastExpr castExpr = CastExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Object.class))).setExpr(methodExpr).build();
    castExpr.accept(writerVisitor);
    assertEquals("((Object) SomeClass.foobar())", writerVisitor.write());
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 4 with CastExpr

use of com.google.api.generator.engine.ast.CastExpr 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 5 with CastExpr

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

the class JavaWriterVisitorTest method writeCastExpr_basic.

@Test
public void writeCastExpr_basic() {
    Variable variable = Variable.builder().setType(TypeNode.STRING).setName("str").build();
    VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
    CastExpr castExpr = CastExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Object.class))).setExpr(varExpr).build();
    castExpr.accept(writerVisitor);
    assertEquals("((Object) str)", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) CastExpr(com.google.api.generator.engine.ast.CastExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Aggregations

CastExpr (com.google.api.generator.engine.ast.CastExpr)5 Variable (com.google.api.generator.engine.ast.Variable)4 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)3 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)3 Test (org.junit.Test)3 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)2 IfStatement (com.google.api.generator.engine.ast.IfStatement)2 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)2 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)2 TypeNode (com.google.api.generator.engine.ast.TypeNode)2 BetaApi (com.google.api.core.BetaApi)1 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)1 AssignmentOperationExpr (com.google.api.generator.engine.ast.AssignmentOperationExpr)1 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)1 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)1 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)1 Expr (com.google.api.generator.engine.ast.Expr)1 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)1 ForStatement (com.google.api.generator.engine.ast.ForStatement)1 JavaDocComment (com.google.api.generator.engine.ast.JavaDocComment)1