Search in sources :

Example 76 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr 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());
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) IOException(java.io.IOException) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) Test(org.junit.Test)

Example 77 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr 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;
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 78 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr 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();
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 79 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr 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();
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) FileWriter(java.io.FileWriter) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) IOException(java.io.IOException) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ForStatement(com.google.api.generator.engine.ast.ForStatement) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference)

Example 80 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr 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());
}
Also used : Path(java.nio.file.Path) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) Test(org.junit.Test)

Aggregations

VariableExpr (com.google.api.generator.engine.ast.VariableExpr)217 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)137 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)132 TypeNode (com.google.api.generator.engine.ast.TypeNode)118 Expr (com.google.api.generator.engine.ast.Expr)114 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)107 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)91 Variable (com.google.api.generator.engine.ast.Variable)88 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)87 ArrayList (java.util.ArrayList)87 Test (org.junit.Test)82 Statement (com.google.api.generator.engine.ast.Statement)80 List (java.util.List)64 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)62 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)58 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)56 CastExpr (com.google.api.generator.engine.ast.CastExpr)53 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)53 Arrays (java.util.Arrays)48 IfStatement (com.google.api.generator.engine.ast.IfStatement)47