use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeMethodDefinitionImports_templatedMixedNamesAndTypes.
@Test
public void writeMethodDefinitionImports_templatedMixedNamesAndTypes() {
Reference mapRef = ConcreteReference.withClazz(Map.class);
List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("K", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).build(), VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("T", "V")).build());
TypeNode returnType = TypeNode.withReference(mapRef);
MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(returnType).setTemplateNames(Arrays.asList("T", "K", "V")).setReturnTemplateNames(Arrays.asList("K", "V")).setArguments(arguments).setReturnExpr(MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(returnType).build()).build();
methodDefinition.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import java.util.Map;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeAnonymousClassExprImports.
@Test
public void writeAnonymousClassExprImports() {
// [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] HashMap<String, Integer> map;
ConcreteReference mapRef = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Integer.class))).build();
VariableExpr mapExpr = VariableExpr.builder().setVariable(Variable.builder().setName("map").setType(TypeNode.withReference(mapRef)).build()).setIsDecl(true).build();
ExprStatement exprStatement = ExprStatement.withExpr(mapExpr);
// [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)).setReturnExpr(returnArg).setName("apply").build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).setStatements(Arrays.asList(exprStatement)).build();
anonymousClassExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.MethodDefinition;\n", "import com.google.common.base.Function;\n", "import java.io.IOException;\n", "import java.util.HashMap;\n", "import java.util.List;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createOverrideCreateBookMethod.
private MethodDefinition createOverrideCreateBookMethod(VaporReference classRef, Expr bookKindDefaultExpr) {
VariableExpr thisBookKindVariableExpr = createVarExprFromRefVarExpr(bookKindVar, ValueExpr.withValue(ThisObjectValue.withType(TypeNode.withReference(classRef))));
VariableExpr thisSeriesNumVariableExpr = createVarExprFromRefVarExpr(seriesNumVar, ValueExpr.withValue(ThisObjectValue.withType(TypeNode.withReference(classRef))));
AssignmentExpr thisSeriesNumAssignExpr = createAssignmentExpr(thisSeriesNumVariableExpr, VariableExpr.withVariable(seriesNumVar));
AssignmentExpr thisBookKindAssignmentExpr = createAssignmentExpr(thisBookKindVariableExpr, bookKindDefaultExpr);
MethodDefinition overrideCreateBook = MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.OVERRIDE)).setArguments(Arrays.asList(createVarDeclExpr(seriesNumVar), createVarDeclExpr(bookKindVar))).setReturnType(TypeNode.VOID).setName("createBook").setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(thisSeriesNumAssignExpr), ExprStatement.withExpr(thisBookKindAssignmentExpr))).build();
return overrideCreateBook;
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createNestedClassShelf.
private ClassDefinition createNestedClassShelf() {
VariableExpr shelfNameDel = createVarPublicDeclExpr(shelfNameVar);
VariableExpr seriesNumDel = createVarPublicDeclExpr(seriesNumVar);
VariableExpr shelfServiceNameDel = createVarPublicDeclExpr(shelfServiceNameVar);
AssignmentExpr shelfServiceNameAssignmentExpr = createAssignmentExpr(shelfServiceNameDel, VariableExpr.withVariable(createVarFromType(TypeNode.STRING, "serviceName")));
MethodDefinition nestedShelfClassCtor = createNestedShelfClassCtor();
return ClassDefinition.builder().setIsNested(true).setMethods(Arrays.asList(nestedShelfClassCtor)).setStatements(Arrays.asList(ExprStatement.withExpr(shelfNameDel), ExprStatement.withExpr(seriesNumDel), ExprStatement.withExpr(shelfServiceNameAssignmentExpr))).setName("Shelf").setScope(ScopeNode.PUBLIC).build();
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method validJavaClass.
@Test
public void validJavaClass() {
// Create outer class variableDecls.
// [code] private static final String serviceName = "LibraryServiceStub";
VariableExpr serviceName = createServiceNameVarExpr();
AssignmentExpr serviceNameDel = createAssignmentExpr(serviceName, ValueExpr.withValue(StringObjectValue.withValue("LibraryServiceStub")));
// [code] protected List<Shelf> shelfList;
VariableExpr shelfListExpr = createShelfListVarExpr();
// [code] public static HashMap<String, Shelf> shelfMap;
VariableExpr shelfMapExpr = createShelfMapVarExpr();
// Create the LibraryServiceStub constructor
MethodDefinition libraryServiceCtor = createLibServiceCtor();
// Create nested class Shelf.
ClassDefinition nestedClassShelf = createNestedClassShelf();
// Create nested abstract class Book.
ClassDefinition nestedClassBook = createNestedClassBook();
// Create nested class Novel.
ClassDefinition nestedClassNovel = createNestedClassNovel();
// Create method `addShelf`
MethodDefinition addShelfMethod = createAddShelfMethod();
// Create method `updateShelfMap`
MethodDefinition updateShelfMap = createUpdateShelfMap();
// Create method `printShelfListToFile`
MethodDefinition printShelfListToFile = createPrintShelfListToFile();
// Create method `addBooksContainsNovel`
MethodDefinition addBooksContainsNovel = createAddBooksContainsNovel();
// Create private method `addBookToShelf`
MethodDefinition addBookToShelf = createAddBookToShelf();
// Create outer class LibraryServiceStub
ClassDefinition libraryServiceStubClass = ClassDefinition.builder().setFileHeader(Arrays.asList(createFileHeader())).setHeaderCommentStatements(Arrays.asList(createOuterClassJavaDocComment())).setPackageString("com.google.example.library.core").setAnnotations(Arrays.asList(AnnotationNode.withSuppressWarnings("all"), AnnotationNode.DEPRECATED, AnnotationNode.OVERRIDE)).setImplementsTypes(Arrays.asList(TypeNode.withReference(libraryServiceRef))).setExtendsType(TypeNode.withReference(stubRef)).setScope(ScopeNode.PUBLIC).setStatements(Arrays.asList(ExprStatement.withExpr(serviceNameDel), ExprStatement.withExpr(shelfListExpr), ExprStatement.withExpr(shelfMapExpr))).setMethods(Arrays.asList(libraryServiceCtor, addShelfMethod, updateShelfMap, printShelfListToFile, addBooksContainsNovel, addBookToShelf)).setNestedClasses(Arrays.asList(nestedClassShelf, nestedClassBook, nestedClassNovel)).setName("LibraryServiceStub").build();
JavaWriterVisitor javaWriterVisitor = new JavaWriterVisitor();
libraryServiceStubClass.accept(javaWriterVisitor);
Utils.saveCodegenToFile(this.getClass(), "JavaCodeGeneratorTest.golden", javaWriterVisitor.write());
Path goldenFilePath = Paths.get(GOLDENFILES_DIRECTORY, "JavaCodeGeneratorTest.golden");
Assert.assertCodeEquals(goldenFilePath, javaWriterVisitor.write());
}
Aggregations