Search in sources :

Example 6 with ConcreteReference

use of com.google.api.generator.engine.ast.ConcreteReference 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 7 with ConcreteReference

use of com.google.api.generator.engine.ast.ConcreteReference in project gapic-generator-java by googleapis.

the class JavaCodeGeneratorTest method createAddBooksContainsNovel.

private MethodDefinition createAddBooksContainsNovel() {
    ConcreteReference bookKindStackRef = ConcreteReference.builder().setClazz(Stack.class).setGenerics(Arrays.asList(bookKindRef)).build();
    Variable bookKindStackVar = createVarFromConcreteRef(bookKindStackRef, "stack");
    Variable containsNovelVar = createVarFromType(TypeNode.BOOLEAN, "containsNovel");
    Variable bookVar = createVarFromVaporRef(bookClassRef, "addedBook");
    TernaryExpr ternaryExpr = createTernaryExpr(containsNovelVar);
    AssignmentExpr setContainsNovelToFalse = createAssignmentExpr(createVarDeclExpr(containsNovelVar), ValueExpr.withValue(createBooleanValue("false")));
    MethodInvocationExpr stackIsEmpty = MethodInvocationExpr.builder().setMethodName("isEmpty").setExprReferenceExpr(VariableExpr.withVariable(bookKindStackVar)).setReturnType(TypeNode.BOOLEAN).build();
    MethodInvocationExpr stackPop = methodExprWithRef(bookKindStackVar, "pop");
    MethodInvocationExpr addBookToShelfMethod = MethodInvocationExpr.builder().setMethodName("addBookToShelf").setArguments(stackPop, VariableExpr.withVariable(shelfVar)).setReturnType(TypeNode.withReference(bookClassRef)).build();
    AssignmentExpr createNewAddedBook = createAssignmentExpr(createVarDeclExpr(bookVar), addBookToShelfMethod);
    InstanceofExpr addedBookIsNovelInstance = InstanceofExpr.builder().setExpr(VariableExpr.withVariable(bookVar)).setCheckType(TypeNode.withReference(novelClassRef)).build();
    AssignmentExpr setContainsNovelToTrue = createAssignmentExpr(VariableExpr.withVariable(containsNovelVar), ValueExpr.withValue(createBooleanValue("true")));
    IfStatement ifStatement = IfStatement.builder().setConditionExpr(addedBookIsNovelInstance).setBody(Arrays.asList(ExprStatement.withExpr(setContainsNovelToTrue))).build();
    // TODO: update the conditionExpr from `stack.isEmpty()` to `!stack.isEmpty()`
    WhileStatement whileStatement = WhileStatement.builder().setConditionExpr(stackIsEmpty).setBody(Arrays.asList(ExprStatement.withExpr(createNewAddedBook), ifStatement)).build();
    return MethodDefinition.builder().setHeaderCommentStatements(createPreMethodJavaDocComment()).setArguments(Arrays.asList(createVarDeclExpr(shelfVar), createVarDeclExpr(bookKindStackVar))).setName("addBooksContainsNovel").setReturnType(TypeNode.STRING).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(setContainsNovelToFalse), whileStatement)).setReturnExpr(ternaryExpr).build();
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr)

Example 8 with ConcreteReference

use of com.google.api.generator.engine.ast.ConcreteReference in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeAnonymousClassExpr_basic.

@Test
public void writeAnonymousClassExpr_basic() {
    ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
    TypeNode type = TypeNode.withReference(ref);
    AssignmentExpr assignmentExpr = createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN);
    ExprStatement statement = ExprStatement.withExpr(assignmentExpr);
    MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("run").setIsOverride(true).setBody(Arrays.asList(statement)).build();
    AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).build();
    anonymousClassExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("new Runnable() {\n", "@Override\n", "public void run() {\n", "boolean foobar = false;\n}\n\n}"), writerVisitor.write());
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) Test(org.junit.Test)

Example 9 with ConcreteReference

use of com.google.api.generator.engine.ast.ConcreteReference in project gapic-generator-java by googleapis.

the class JavaWriterVisitor method visit.

@Override
public void visit(ConcreteReference reference) {
    if (reference.isWildcard()) {
        buffer.append(QUESTION_MARK);
        if (reference.wildcardUpperBound() != null) {
            // Handle the upper bound.
            buffer.append(SPACE);
            buffer.append(EXTENDS);
            buffer.append(SPACE);
            reference.wildcardUpperBound().accept(this);
        }
        return;
    }
    String pakkage = reference.pakkage();
    String shortName = reference.name();
    if (reference.useFullName() || importWriterVisitor.collidesWithImport(pakkage, shortName)) {
        buffer.append(pakkage);
        buffer.append(DOT);
    }
    if (reference.hasEnclosingClass() && !reference.isStaticImport()) {
        buffer.append(String.join(DOT, reference.enclosingClassNames()));
        buffer.append(DOT);
    }
    buffer.append(reference.simpleName());
    if (!reference.generics().isEmpty()) {
        buffer.append(LEFT_ANGLE);
        for (int i = 0; i < reference.generics().size(); i++) {
            Reference r = reference.generics().get(i);
            r.accept(this);
            if (i < reference.generics().size() - 1) {
                buffer.append(COMMA);
                buffer.append(SPACE);
            }
        }
        buffer.append(RIGHT_ANGLE);
    }
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference)

Example 10 with ConcreteReference

use of com.google.api.generator.engine.ast.ConcreteReference 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());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Test(org.junit.Test)

Aggregations

ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)14 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)10 TypeNode (com.google.api.generator.engine.ast.TypeNode)9 Test (org.junit.Test)8 Variable (com.google.api.generator.engine.ast.Variable)7 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)4 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)4 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)4 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)4 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)4 IOException (java.io.IOException)4 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)3 IfStatement (com.google.api.generator.engine.ast.IfStatement)2 ForStatement (com.google.api.generator.engine.ast.ForStatement)1 InstanceofExpr (com.google.api.generator.engine.ast.InstanceofExpr)1 Reference (com.google.api.generator.engine.ast.Reference)1 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)1 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)1 TryCatchStatement (com.google.api.generator.engine.ast.TryCatchStatement)1 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)1