use of com.google.api.generator.engine.ast.NewObjectExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeNewObjectExprImports_basic.
@Test
public void writeNewObjectExprImports_basic() {
// [Constructing] `new ArrayList<>()`
NewObjectExpr newObjectExpr = NewObjectExpr.builder().setIsGeneric(true).setType(TypeNode.withReference(ConcreteReference.withClazz(ArrayList.class))).build();
newObjectExpr.accept(writerVisitor);
assertEquals("import java.util.ArrayList;\n\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.NewObjectExpr 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());
}
use of com.google.api.generator.engine.ast.NewObjectExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeNewObjectExprImports_methodExprArg.
@Test
public void writeNewObjectExprImports_methodExprArg() {
// [Constructing] `new IOException(message, cause(mapArg))` and `cause(mapArg)` is a method
// invocation with a `HashMap` argument.
TypeNode exceptionType = TypeNode.withReference(ConcreteReference.withClazz(IOException.class));
Variable message = Variable.builder().setName("message").setType(TypeNode.STRING).build();
TypeNode mapType = TypeNode.withReference(ConcreteReference.withClazz(HashMap.class));
VariableExpr mapExpr = VariableExpr.builder().setVariable(Variable.builder().setName("mapArg").setType(mapType).build()).build();
VariableExpr msgExpr = VariableExpr.builder().setVariable(message).build();
MethodInvocationExpr causeExpr = MethodInvocationExpr.builder().setMethodName("cause").setArguments(Arrays.asList(mapExpr)).setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))).build();
NewObjectExpr newObjectExpr = NewObjectExpr.builder().setType(exceptionType).setArguments(Arrays.asList(msgExpr, causeExpr)).build();
newObjectExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import java.io.IOException;\n", "import java.util.HashMap;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.NewObjectExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeNewObjectExpr_withGenericsAndArgs.
@Test
public void writeNewObjectExpr_withGenericsAndArgs() {
// isGeneric() is true and generics() is not empty.
ConcreteReference listRef = ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(Integer.class))).build();
ConcreteReference mapRef = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), listRef)).build();
TypeNode type = TypeNode.withReference(mapRef);
TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.generator.engine").build());
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(TypeNode.INT).setStaticReferenceType(someType).build();
Variable num = Variable.builder().setName("num").setType(TypeNode.FLOAT).build();
VariableExpr numExpr = VariableExpr.builder().setVariable(num).build();
NewObjectExpr newObjectExpr = NewObjectExpr.builder().setIsGeneric(true).setType(type).setArguments(Arrays.asList(methodExpr, numExpr)).build();
newObjectExpr.accept(writerVisitor);
assertEquals("new HashMap<String, List<Integer>>(SomeClass.foobar(), num)", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.NewObjectExpr in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createAddShelfMethod.
private MethodDefinition createAddShelfMethod() {
Variable nameVar = createVarFromType(TypeNode.STRING, "name");
Variable seriesDoubleNumVar = createVarFromType(TypeNode.DOUBLE, "seriesDoubleNum");
CastExpr seriesNumDoubleToIntExpr = CastExpr.builder().setExpr(VariableExpr.withVariable(seriesDoubleNumVar)).setType(TypeNode.INT).build();
AssignmentExpr castSeriesNumExpr = createAssignmentExpr(createVarDeclExpr(seriesNumVar), seriesNumDoubleToIntExpr);
ReturnExpr maxValueReturnExpr = createReturnExpr("Series number equals to max int value.");
ReturnExpr duplicateKeyReturnExpr = createReturnExpr("Shelf is already existing in the map.");
// TODO: update the condition from `condition` to `seriesNum == MAX_VALUE`
IfStatement maxValueCheck = IfStatement.builder().setConditionExpr(VariableExpr.withVariable(createVarFromType(TypeNode.BOOLEAN, "condition"))).setBody(Arrays.asList(ExprStatement.withExpr(maxValueReturnExpr))).build();
NewObjectExpr newShelfExpr = createNewObjectExprWithArg(nameVar);
MethodInvocationExpr addShelfToListExpr = methodExprWithRefAndArg(shelfListVar, "add", Arrays.asList(newShelfExpr));
MethodInvocationExpr putShelfToMapExpr = methodExprWithRefAndArg(shelfMapVar, "put", Arrays.asList(VariableExpr.withVariable(nameVar), newShelfExpr));
MethodInvocationExpr mapContainsKey = methodExprWithRefArgAndReturn(shelfMapVar, Arrays.asList(VariableExpr.withVariable(nameVar)));
IfStatement mapContainsKeyCheck = IfStatement.builder().setConditionExpr(mapContainsKey).setBody(Arrays.asList(ExprStatement.withExpr(duplicateKeyReturnExpr))).build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.OVERRIDE)).setName("addShelf").setReturnType(TypeNode.STRING).setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("Shelf added."))).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(castSeriesNumExpr), maxValueCheck, ExprStatement.withExpr(addShelfToListExpr), mapContainsKeyCheck, ExprStatement.withExpr(putShelfToMapExpr))).setArguments(Arrays.asList(createVarDeclExpr(nameVar), createVarDeclExpr(seriesDoubleNumVar))).build();
}
Aggregations