use of com.google.api.generator.engine.ast.ConcreteReference 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.ConcreteReference 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.ConcreteReference in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnonymousClassExpr_basic.
@Test
public void writeAnonymousClassExpr_basic() {
ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
TypeNode type = TypeNode.withReference(ref);
AssignmentExpr assignmentExpr = createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN);
ExprStatement statement = ExprStatement.withExpr(assignmentExpr);
MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("run").setIsOverride(true).setBody(Arrays.asList(statement)).build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).build();
anonymousClassExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("new Runnable() {\n", "@Override\n", "public void run() {\n", "boolean foobar = false;\n}\n\n}"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.ConcreteReference in project gapic-generator-java by googleapis.
the class JavaWriterVisitor method visit.
@Override
public void visit(ConcreteReference reference) {
if (reference.isWildcard()) {
buffer.append(QUESTION_MARK);
if (reference.wildcardUpperBound() != null) {
// Handle the upper bound.
buffer.append(SPACE);
buffer.append(EXTENDS);
buffer.append(SPACE);
reference.wildcardUpperBound().accept(this);
}
return;
}
String pakkage = reference.pakkage();
String shortName = reference.name();
if (reference.useFullName() || importWriterVisitor.collidesWithImport(pakkage, shortName)) {
buffer.append(pakkage);
buffer.append(DOT);
}
if (reference.hasEnclosingClass() && !reference.isStaticImport()) {
buffer.append(String.join(DOT, reference.enclosingClassNames()));
buffer.append(DOT);
}
buffer.append(reference.simpleName());
if (!reference.generics().isEmpty()) {
buffer.append(LEFT_ANGLE);
for (int i = 0; i < reference.generics().size(); i++) {
Reference r = reference.generics().get(i);
r.accept(this);
if (i < reference.generics().size() - 1) {
buffer.append(COMMA);
buffer.append(SPACE);
}
}
buffer.append(RIGHT_ANGLE);
}
}
use of com.google.api.generator.engine.ast.ConcreteReference in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeNewObjectExprImports_genericsAndVariableArgs.
@Test
public void writeNewObjectExprImports_genericsAndVariableArgs() {
// [Constructing] `new HashMap<List<String>, Integer>>(int initialCapacity, float loadFactor)`
ConcreteReference listRef = ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class))).build();
ConcreteReference mapRef = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(listRef, ConcreteReference.withClazz(Integer.class))).build();
TypeNode type = TypeNode.withReference(mapRef);
Variable initialCapacity = Variable.builder().setName("initialCapacity").setType(TypeNode.INT).build();
VariableExpr initCapacityExpr = VariableExpr.builder().setVariable(initialCapacity).build();
Variable loadFactor = Variable.builder().setName("loadFactor").setType(TypeNode.FLOAT).build();
VariableExpr loadFactorExpr = VariableExpr.builder().setVariable(loadFactor).build();
NewObjectExpr newObjectExpr = NewObjectExpr.builder().setIsGeneric(true).setType(type).setArguments(Arrays.asList(initCapacityExpr, loadFactorExpr)).build();
newObjectExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import java.util.HashMap;\n", "import java.util.List;\n\n"), writerVisitor.write());
}
Aggregations