use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createUpdateShelfMap.
private MethodDefinition createUpdateShelfMap() {
ConcreteReference nonexistentShelfExceptionRef = ConcreteReference.builder().setClazz(Exception.class).build();
Variable shelfVar = createVarFromVaporRef(shelfClassRef, "newShelf");
VariableExpr shelfNameFromNewShelfObject = fieldFromShelfObjectExpr(shelfVar, createVarFromType(TypeNode.STRING, "shelfName"));
MethodInvocationExpr mapContainsKeyExpr = methodExprWithRefArgAndReturn(shelfMapVar, Arrays.asList(shelfNameFromNewShelfObject));
MethodInvocationExpr putShelfToMapExpr = methodExprWithRefAndArg(shelfMapVar, "put", Arrays.asList(shelfNameFromNewShelfObject, VariableExpr.withVariable(shelfVar)));
ThrowExpr throwExpr = ThrowExpr.builder().setMessageExpr("Updating shelf is not existing in the map").setType(TypeNode.withReference(nonexistentShelfExceptionRef)).build();
IfStatement updateShelfMapIfElseBlock = IfStatement.builder().setConditionExpr(mapContainsKeyExpr).setBody(Arrays.asList(ExprStatement.withExpr(putShelfToMapExpr))).setElseBody(Arrays.asList(ExprStatement.withExpr(throwExpr))).build();
return MethodDefinition.builder().setName("updateShelfMap").setThrowsExceptions(Arrays.asList(TypeNode.withReference(nonexistentShelfExceptionRef))).setReturnType(TypeNode.VOID).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(updateShelfMapIfElseBlock)).setArguments(Arrays.asList(VariableExpr.builder().setVariable(shelfVar).setIsDecl(true).build())).build();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr 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();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeBlockStatement_static.
@Test
public void writeBlockStatement_static() {
TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.some.pakkage").build());
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setStaticReferenceType(someType).build();
BlockStatement blockStatement = BlockStatement.builder().setIsStatic(true).setBody(Arrays.asList(ExprStatement.withExpr(methodExpr), ExprStatement.withExpr(methodExpr))).build();
blockStatement.accept(writerVisitor);
assertEquals("static {\nSomeClass.foobar();\nSomeClass.foobar();\n}\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLogicalOperationExpr_logicalOr.
@Test
public void writeLogicalOperationExpr_logicalOr() {
VariableExpr lhsExpr = VariableExpr.withVariable(createVariable("isGood", TypeNode.BOOLEAN));
MethodInvocationExpr rhsExpr = MethodInvocationExpr.builder().setMethodName("isValid").setReturnType(TypeNode.BOOLEAN).build();
LogicalOperationExpr logicalOperationExpr = LogicalOperationExpr.logicalOrWithExprs(lhsExpr, rhsExpr);
logicalOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("isGood || isValid()");
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodInvocationExpr_genericWithArgs.
@Test
public void writeMethodInvocationExpr_genericWithArgs() {
Reference mapReference = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Integer.class))).build();
Reference outerMapReference = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(mapReference, mapReference)).build();
Variable variable = Variable.builder().setType(TypeNode.INT).setName("anArg").build();
VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Double.class), TypeNode.WILDCARD_REFERENCE, outerMapReference)).setArguments(Arrays.asList(varExpr, varExpr, varExpr)).setExprReferenceExpr(varExpr).setReturnType(TypeNode.STRING).build();
Variable lhsVariable = Variable.builder().setType(TypeNode.STRING).setName("someStr").build();
VariableExpr lhsVarExpr = VariableExpr.builder().setVariable(lhsVariable).setIsDecl(true).setIsFinal(true).build();
AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(lhsVarExpr).setValueExpr(methodExpr).build();
assignExpr.accept(writerVisitor);
assertEquals("final String someStr = anArg.<String, Double, ?, HashMap<HashMap<String, Integer>," + " HashMap<String, Integer>>>foobar(anArg, anArg, anArg)", writerVisitor.write());
}
Aggregations