Search in sources :

Example 6 with NewObjectExpr

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

the class ImportWriterVisitorTest method writeNewObjectExprImports_basic.

@Test
public void writeNewObjectExprImports_basic() {
    // [Constructing] `new ArrayList<>()`
    NewObjectExpr newObjectExpr = NewObjectExpr.builder().setIsGeneric(true).setType(TypeNode.withReference(ConcreteReference.withClazz(ArrayList.class))).build();
    newObjectExpr.accept(writerVisitor);
    assertEquals("import java.util.ArrayList;\n\n", writerVisitor.write());
}
Also used : NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with NewObjectExpr

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

Example 8 with NewObjectExpr

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

the class ImportWriterVisitorTest method writeNewObjectExprImports_methodExprArg.

@Test
public void writeNewObjectExprImports_methodExprArg() {
    // [Constructing] `new IOException(message, cause(mapArg))` and `cause(mapArg)` is a method
    // invocation with a `HashMap` argument.
    TypeNode exceptionType = TypeNode.withReference(ConcreteReference.withClazz(IOException.class));
    Variable message = Variable.builder().setName("message").setType(TypeNode.STRING).build();
    TypeNode mapType = TypeNode.withReference(ConcreteReference.withClazz(HashMap.class));
    VariableExpr mapExpr = VariableExpr.builder().setVariable(Variable.builder().setName("mapArg").setType(mapType).build()).build();
    VariableExpr msgExpr = VariableExpr.builder().setVariable(message).build();
    MethodInvocationExpr causeExpr = MethodInvocationExpr.builder().setMethodName("cause").setArguments(Arrays.asList(mapExpr)).setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))).build();
    NewObjectExpr newObjectExpr = NewObjectExpr.builder().setType(exceptionType).setArguments(Arrays.asList(msgExpr, causeExpr)).build();
    newObjectExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import java.io.IOException;\n", "import java.util.HashMap;\n\n"), writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) HashMap(java.util.HashMap) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with NewObjectExpr

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

Example 10 with NewObjectExpr

use of com.google.api.generator.engine.ast.NewObjectExpr 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();
}
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) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Aggregations

NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)12 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)9 TypeNode (com.google.api.generator.engine.ast.TypeNode)8 Variable (com.google.api.generator.engine.ast.Variable)8 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)7 Test (org.junit.Test)7 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)5 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)5 VaporReference (com.google.api.generator.engine.ast.VaporReference)3 ArrayList (java.util.ArrayList)3 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)2 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)2 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)2 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)2 Statement (com.google.api.generator.engine.ast.Statement)2 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)2 IOException (java.io.IOException)2 MonitoredResourceDescriptor (com.google.api.MonitoredResourceDescriptor)1 ApiFunction (com.google.api.core.ApiFunction)1 ApiFuture (com.google.api.core.ApiFuture)1