use of com.google.api.generator.engine.ast.ConcreteReference 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.ConcreteReference in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnonymousClassExpr_generics.
@Test
public void writeAnonymousClassExpr_generics() {
// [Constructing] Function<List<IOException>, MethodDefinition>
ConcreteReference exceptionListRef = ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(IOException.class))).build();
ConcreteReference methodDefinitionRef = ConcreteReference.withClazz(MethodDefinition.class);
ConcreteReference ref = ConcreteReference.builder().setClazz(Function.class).setGenerics(Arrays.asList(exceptionListRef, methodDefinitionRef)).build();
TypeNode type = TypeNode.withReference(ref);
// [Constructing] an input argument whose type is `List<IOException>`
VariableExpr arg = VariableExpr.builder().setVariable(Variable.builder().setName("arg").setType(TypeNode.withReference(exceptionListRef)).build()).setIsDecl(true).build();
// [Constructing] a return variable expression whose type is `MethodDefinition`
VariableExpr returnArg = VariableExpr.builder().setVariable(Variable.builder().setName("returnArg").setType(TypeNode.withReference(methodDefinitionRef)).build()).build();
MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.withReference(methodDefinitionRef)).setArguments(Arrays.asList(arg)).setIsOverride(true).setReturnExpr(returnArg).setName("apply").build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).build();
anonymousClassExpr.accept(writerVisitor);
String expected = LineFormatter.lines("new Function<List<IOException>, MethodDefinition>() {\n", "@Override\n", "public MethodDefinition apply(List<IOException> arg) {\n", "return returnArg;\n", "}\n\n}");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.ConcreteReference 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.ConcreteReference in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnonymousClassExpr_withStatementsMethods.
@Test
public void writeAnonymousClassExpr_withStatementsMethods() {
ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
TypeNode type = TypeNode.withReference(ref);
// [Constructing] private static final String s = "foo";
Variable variable = createVariable("s", TypeNode.STRING);
VariableExpr variableExpr = VariableExpr.builder().setScope(ScopeNode.PRIVATE).setIsDecl(true).setIsFinal(true).setIsStatic(true).setVariable(variable).build();
ValueExpr valueExpr = ValueExpr.builder().setValue(StringObjectValue.withValue("foo")).build();
AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
ExprStatement exprStatement = ExprStatement.withExpr(assignmentExpr);
MethodDefinition methodDefinition = MethodDefinition.builder().setName("run").setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setStatements(Arrays.asList(exprStatement)).setMethods(Arrays.asList(methodDefinition)).build();
anonymousClassExpr.accept(writerVisitor);
String expected = LineFormatter.lines("new Runnable() {\n", "private static final String s = \"foo\";\n", "@Override\n", "public void run() {\n", "int x = 3;\n}\n\n}");
assertEquals(expected, writerVisitor.write());
}
Aggregations