use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeSuperObjectValueImports.
@Test
public void writeSuperObjectValueImports() {
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.examples.v1").build();
TypeNode typeNode = TypeNode.withReference(ref);
SuperObjectValue superObjectValue = SuperObjectValue.withType(typeNode);
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(superObjectValue)).setReturnType(TypeNode.STRING).build();
methodExpr.accept(writerVisitor);
assertEquals("import com.google.example.examples.v1.Student;\n\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeUnaryOperationExprImports_LogicalNot.
@Test
public void writeUnaryOperationExprImports_LogicalNot() {
MethodInvocationExpr expr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))).setMethodName("isEmpty").setReturnType(TypeNode.BOOLEAN).build();
UnaryOperationExpr unaryOperationExpr = UnaryOperationExpr.logicalNotWithExpr(expr);
unaryOperationExpr.accept(writerVisitor);
assertEquals("import com.google.api.generator.engine.ast.Expr;\n\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeUnaryOperationExprImports_PostIncrement.
@Test
public void writeUnaryOperationExprImports_PostIncrement() {
MethodInvocationExpr expr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))).setMethodName("getNumber").setReturnType(TypeNode.INT).build();
UnaryOperationExpr unaryOperationExpr = UnaryOperationExpr.postfixIncrementWithExpr(expr);
unaryOperationExpr.accept(writerVisitor);
assertEquals("import com.google.api.generator.engine.ast.Expr;\n\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createPrintShelfListToFile.
private MethodDefinition createPrintShelfListToFile() {
ConcreteReference stringBuilderRef = ConcreteReference.builder().setClazz(StringBuilder.class).build();
ConcreteReference fileWriterRef = ConcreteReference.builder().setClazz(FileWriter.class).build();
Variable stringBuilderVar = createVarFromConcreteRef(stringBuilderRef, "sb");
Variable fileNameVar = createVarFromType(TypeNode.STRING, "fileName");
Variable shelfObject = createVarFromVaporRef(shelfClassRef, "s");
Variable fileWriterVar = createVarFromConcreteRef(fileWriterRef, "fileWriter");
Variable ioException = createVarFromConcreteRef(ConcreteReference.withClazz(IOException.class), "e");
VariableExpr shelfNameFromShelfObject = fieldFromShelfObjectExpr(shelfObject, shelfNameVar);
VariableExpr seriesNumFromShelfObject = fieldFromShelfObjectExpr(shelfObject, seriesNumVar);
AssignmentExpr createStringBuilderExpr = createAssignmentExpr(createVarDeclExpr(stringBuilderVar), NewObjectExpr.withType(TypeNode.withReference(stringBuilderRef)));
AssignmentExpr createFileWriterExpr = createAssignmentExpr(createVarDeclExpr(fileWriterVar), NewObjectExpr.builder().setType(TypeNode.withReference(fileWriterRef)).setArguments(Arrays.asList(VariableExpr.withVariable(fileNameVar))).build());
MethodInvocationExpr appendShelfName = methodExprWithRefAndArg(stringBuilderVar, "append", Arrays.asList(shelfNameFromShelfObject));
MethodInvocationExpr appendSeriesNum = MethodInvocationExpr.builder().setMethodName("append").setExprReferenceExpr(appendShelfName).setArguments(seriesNumFromShelfObject).build();
MethodInvocationExpr stringBuilderToString = methodExprWithRef(stringBuilderVar, "toString");
MethodInvocationExpr writeToFileWriter = methodExprWithRefAndArg(fileNameVar, "write", Arrays.asList(stringBuilderToString));
MethodInvocationExpr closeFileWriter = methodExprWithRef(fileNameVar, "close");
MethodInvocationExpr printError = methodExprWithRef(ioException, "printStackTrace");
ForStatement loopShelfList = ForStatement.builder().setLocalVariableExpr(createVarDeclExpr(shelfObject)).setCollectionExpr(VariableExpr.withVariable(shelfListVar)).setBody(Arrays.asList(ExprStatement.withExpr(appendSeriesNum))).build();
TryCatchStatement tryCatchStatement = TryCatchStatement.builder().setTryBody(Arrays.asList(ExprStatement.withExpr(createFileWriterExpr), loopShelfList, ExprStatement.withExpr(writeToFileWriter), ExprStatement.withExpr(closeFileWriter))).addCatch(createVarDeclExpr(ioException), Arrays.asList(ExprStatement.withExpr(printError))).build();
return MethodDefinition.builder().setName("printShelfListToFile").setReturnType(TypeNode.VOID).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(createStringBuilderExpr), tryCatchStatement)).setArguments(Arrays.asList(createVarDeclExpr(fileNameVar))).build();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr 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();
}
Aggregations