Search in sources :

Example 1 with InstanceofExpr

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

the class ImportWriterVisitorTest method writeInstanceofExprImports_basic.

@Test
public void writeInstanceofExprImports_basic() {
    TypeNode exprType = TypeNode.withReference(ConcreteReference.withClazz(Expr.class));
    TypeNode assignExprType = TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class));
    Variable variable = Variable.builder().setName("x").setType(assignExprType).build();
    VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).build();
    InstanceofExpr expr = InstanceofExpr.builder().setExpr(variableExpr).setCheckType(exprType).build();
    expr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import com.google.api.generator.engine.ast.Expr;\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) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) 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)

Example 2 with InstanceofExpr

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

the class JavaCodeGeneratorTest method createAddBooksContainsNovel.

private MethodDefinition createAddBooksContainsNovel() {
    ConcreteReference bookKindStackRef = ConcreteReference.builder().setClazz(Stack.class).setGenerics(Arrays.asList(bookKindRef)).build();
    Variable bookKindStackVar = createVarFromConcreteRef(bookKindStackRef, "stack");
    Variable containsNovelVar = createVarFromType(TypeNode.BOOLEAN, "containsNovel");
    Variable bookVar = createVarFromVaporRef(bookClassRef, "addedBook");
    TernaryExpr ternaryExpr = createTernaryExpr(containsNovelVar);
    AssignmentExpr setContainsNovelToFalse = createAssignmentExpr(createVarDeclExpr(containsNovelVar), ValueExpr.withValue(createBooleanValue("false")));
    MethodInvocationExpr stackIsEmpty = MethodInvocationExpr.builder().setMethodName("isEmpty").setExprReferenceExpr(VariableExpr.withVariable(bookKindStackVar)).setReturnType(TypeNode.BOOLEAN).build();
    MethodInvocationExpr stackPop = methodExprWithRef(bookKindStackVar, "pop");
    MethodInvocationExpr addBookToShelfMethod = MethodInvocationExpr.builder().setMethodName("addBookToShelf").setArguments(stackPop, VariableExpr.withVariable(shelfVar)).setReturnType(TypeNode.withReference(bookClassRef)).build();
    AssignmentExpr createNewAddedBook = createAssignmentExpr(createVarDeclExpr(bookVar), addBookToShelfMethod);
    InstanceofExpr addedBookIsNovelInstance = InstanceofExpr.builder().setExpr(VariableExpr.withVariable(bookVar)).setCheckType(TypeNode.withReference(novelClassRef)).build();
    AssignmentExpr setContainsNovelToTrue = createAssignmentExpr(VariableExpr.withVariable(containsNovelVar), ValueExpr.withValue(createBooleanValue("true")));
    IfStatement ifStatement = IfStatement.builder().setConditionExpr(addedBookIsNovelInstance).setBody(Arrays.asList(ExprStatement.withExpr(setContainsNovelToTrue))).build();
    // TODO: update the conditionExpr from `stack.isEmpty()` to `!stack.isEmpty()`
    WhileStatement whileStatement = WhileStatement.builder().setConditionExpr(stackIsEmpty).setBody(Arrays.asList(ExprStatement.withExpr(createNewAddedBook), ifStatement)).build();
    return MethodDefinition.builder().setHeaderCommentStatements(createPreMethodJavaDocComment()).setArguments(Arrays.asList(createVarDeclExpr(shelfVar), createVarDeclExpr(bookKindStackVar))).setName("addBooksContainsNovel").setReturnType(TypeNode.STRING).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(setContainsNovelToFalse), whileStatement)).setReturnExpr(ternaryExpr).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) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr)

Example 3 with InstanceofExpr

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

the class ServiceClientTestClassComposer method createRpcLroExceptionTestCatchBody.

@Override
protected List<Statement> createRpcLroExceptionTestCatchBody(VariableExpr exceptionExpr, boolean isStreaming) {
    List<Expr> catchBodyExprs = new ArrayList<>();
    Expr testExpectedValueExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build()).setStaticReferenceType(FIXED_TYPESTORE.get("InvalidArgumentException")).build();
    Expr getCauseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(exceptionExpr).setMethodName("getCause").setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))).build();
    Expr testActualValueExpr = MethodInvocationExpr.builder().setExprReferenceExpr(getCauseExpr).setMethodName("getClass").build();
    if (isStreaming) {
        InstanceofExpr checkInstanceExpr = InstanceofExpr.builder().setExpr(getCauseExpr).setCheckType(FIXED_TYPESTORE.get("InvalidArgumentException")).build();
        catchBodyExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("assertTrue").setArguments(checkInstanceExpr).build());
    } else {
        // Constructs `Assert.assertEquals(InvalidArgumentException.class, e.getCaus().getClass());`.
        catchBodyExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("assertEquals").setArguments(testExpectedValueExpr, testActualValueExpr).build());
    }
    // Construct the apiException variable.
    VariableExpr apiExceptionVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("InvalidArgumentException")).setName("apiException").build());
    Expr castedCauseExpr = CastExpr.builder().setType(FIXED_TYPESTORE.get("InvalidArgumentException")).setExpr(getCauseExpr).build();
    catchBodyExprs.add(AssignmentExpr.builder().setVariableExpr(apiExceptionVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(castedCauseExpr).build());
    // Construct the last assert statement.
    testExpectedValueExpr = EnumRefExpr.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(StatusCode.Code.class).setIsStaticImport(false).build())).setName("INVALID_ARGUMENT").build();
    testActualValueExpr = MethodInvocationExpr.builder().setExprReferenceExpr(apiExceptionVarExpr).setMethodName("getStatusCode").build();
    testActualValueExpr = MethodInvocationExpr.builder().setExprReferenceExpr(testActualValueExpr).setMethodName("getCode").build();
    catchBodyExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("assertEquals").setArguments(testExpectedValueExpr, testActualValueExpr).build());
    return catchBodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
}
Also used : LocalChannelProvider(com.google.api.gax.grpc.testing.LocalChannelProvider) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) Variable(com.google.api.generator.engine.ast.Variable) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) GaxGrpcProperties(com.google.api.gax.grpc.GaxGrpcProperties) DefaultValueComposer(com.google.api.generator.gapic.composer.defaultvalue.DefaultValueComposer) Field(com.google.api.generator.gapic.model.Field) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Method(com.google.api.generator.gapic.model.Method) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) MockServiceHelper(com.google.api.gax.grpc.testing.MockServiceHelper) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Collectors(java.util.stream.Collectors) ClassNames(com.google.api.generator.gapic.composer.utils.ClassNames) List(java.util.List) GapicContext(com.google.api.generator.gapic.model.GapicContext) MockGrpcService(com.google.api.gax.grpc.testing.MockGrpcService) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) AbstractServiceClientTestClassComposer(com.google.api.generator.gapic.composer.common.AbstractServiceClientTestClassComposer) Function(java.util.function.Function) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) AbstractMessage(com.google.protobuf.AbstractMessage) CastExpr(com.google.api.generator.engine.ast.CastExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) ResourceName(com.google.api.generator.gapic.model.ResourceName) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Statement(com.google.api.generator.engine.ast.Statement) Service(com.google.api.generator.gapic.model.Service) TreeMap(java.util.TreeMap) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) StatusCode(com.google.api.gax.rpc.StatusCode) Message(com.google.api.generator.gapic.model.Message) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) 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) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) StatusCode(com.google.api.gax.rpc.StatusCode)

Example 4 with InstanceofExpr

use of com.google.api.generator.engine.ast.InstanceofExpr 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());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Aggregations

InstanceofExpr (com.google.api.generator.engine.ast.InstanceofExpr)4 Variable (com.google.api.generator.engine.ast.Variable)4 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)3 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)3 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)3 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)2 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)2 Expr (com.google.api.generator.engine.ast.Expr)2 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)2 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)2 TypeNode (com.google.api.generator.engine.ast.TypeNode)2 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)2 GaxGrpcProperties (com.google.api.gax.grpc.GaxGrpcProperties)1 LocalChannelProvider (com.google.api.gax.grpc.testing.LocalChannelProvider)1 MockGrpcService (com.google.api.gax.grpc.testing.MockGrpcService)1 MockServiceHelper (com.google.api.gax.grpc.testing.MockServiceHelper)1 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)1 StatusCode (com.google.api.gax.rpc.StatusCode)1 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)1 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)1