use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeVariableExprImports_wildcardType.
@Test
public void writeVariableExprImports_wildcardType() {
TypeNode wildcardListType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(TypeNode.WILDCARD_REFERENCE)).build());
// Constructs `List<?> x`.
Variable variable = Variable.builder().setName("x").setType(wildcardListType).build();
VariableExpr variableExpr = VariableExpr.builder().setIsDecl(true).setVariable(variable).build();
variableExpr.accept(writerVisitor);
assertEquals("import java.util.List;\n\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeNewObjectExprImports_withArgs.
@Test
public void writeNewObjectExprImports_withArgs() {
// [Constructing] `new FileOutputStream(File file)` and the argument needs to be imported.
ConcreteReference fileOutputStreamRef = ConcreteReference.withClazz(FileOutputStream.class);
ConcreteReference fileRef = ConcreteReference.withClazz(File.class);
Variable fileVar = Variable.builder().setName("file").setType(TypeNode.withReference(fileRef)).build();
VariableExpr fileExpr = VariableExpr.builder().setVariable(fileVar).build();
NewObjectExpr newObjectExpr = NewObjectExpr.builder().setType(TypeNode.withReference(fileOutputStreamRef)).setArguments(Arrays.asList(fileExpr)).build();
newObjectExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import java.io.File;\n", "import java.io.FileOutputStream;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeVariableExprImports_reference.
@Test
public void writeVariableExprImports_reference() {
Variable variable = Variable.builder().setName("expr").setType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))).build();
VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).build();
Variable subVariable = Variable.builder().setName("assignExpr").setType(TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class))).build();
variableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(variableExpr).build();
variableExpr.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.Variable in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeVariableExprImports_nestedReference.
@Test
public void writeVariableExprImports_nestedReference() {
Variable variable = Variable.builder().setName("expr").setType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))).build();
VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).build();
Variable subVariable = Variable.builder().setName("assignExpr").setType(TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class))).build();
variableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(variableExpr).build();
subVariable = Variable.builder().setName("anotherExpr").setType(TypeNode.withReference(ConcreteReference.withClazz(VariableExpr.class))).build();
variableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(variableExpr).build();
variableExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import com.google.api.generator.engine.ast.Expr;\n", "import com.google.api.generator.engine.ast.VariableExpr;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Variable 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();
}
Aggregations