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