Search in sources :

Example 1 with ConcreteReference

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

the class MockServiceImplClassComposer method createGenericClientStreamingProtoMethodOverride.

private static MethodDefinition createGenericClientStreamingProtoMethodOverride(Method protoMethod, VariableExpr responseObserverVarExpr, VariableExpr localResponseVarExpr) {
    ConcreteReference streamObserverRef = ConcreteReference.withClazz(StreamObserver.class);
    TypeNode returnType = TypeNode.withReference(streamObserverRef.copyAndSetGenerics(Arrays.asList(protoMethod.inputType().reference())));
    VariableExpr requestObserverVarExpr = VariableExpr.withVariable(Variable.builder().setName("requestObserver").setType(returnType).build());
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(returnType).setName(JavaStyle.toLowerCamelCase(protoMethod.name())).setArguments(Arrays.asList(responseObserverVarExpr.toBuilder().setIsFinal(true).setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(requestObserverVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(createStreamObserverClassInstance(protoMethod, returnType, responseObserverVarExpr, localResponseVarExpr)).build()))).setReturnExpr(requestObserverVarExpr).build();
}
Also used : VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference)

Example 2 with ConcreteReference

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

the class MockServiceImplClassComposer method createGenericProtoMethodOverride.

private static MethodDefinition createGenericProtoMethodOverride(Method protoMethod) {
    ConcreteReference streamObserverRef = ConcreteReference.withClazz(StreamObserver.class);
    TypeNode objectType = TypeNode.withReference(javaObjectReference);
    VariableExpr localResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("response").setType(objectType).build());
    VariableExpr responseObserverVarExpr = VariableExpr.withVariable(Variable.builder().setName("responseObserver").setType(TypeNode.withReference(streamObserverRef.copyAndSetGenerics(Arrays.asList(protoMethod.outputType().reference())))).build());
    if (protoMethod.stream().equals(Stream.CLIENT) || protoMethod.stream().equals(Stream.BIDI)) {
        return createGenericClientStreamingProtoMethodOverride(protoMethod, responseObserverVarExpr, localResponseVarExpr);
    }
    VariableExpr requestArgVarExpr = VariableExpr.withVariable(Variable.builder().setName("request").setType(protoMethod.inputType()).build());
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName(JavaStyle.toLowerCamelCase(protoMethod.name())).setArguments(Arrays.asList(requestArgVarExpr.toBuilder().setIsDecl(true).build(), responseObserverVarExpr.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(localResponseVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setMethodName("poll").setExprReferenceExpr(responsesVarExpr).setReturnType(objectType).build()).build()), createHandleObjectStatement(protoMethod, requestArgVarExpr, responseObserverVarExpr, localResponseVarExpr))).build();
}
Also used : VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference)

Example 3 with ConcreteReference

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

the class JavaWriterVisitorTest method writeNewObjectExpr_basic.

@Test
public void writeNewObjectExpr_basic() {
    // isGeneric() is true, but generics() is empty.
    ConcreteReference ref = ConcreteReference.withClazz(List.class);
    TypeNode type = TypeNode.withReference(ref);
    NewObjectExpr newObjectExpr = NewObjectExpr.builder().setIsGeneric(true).setType(type).build();
    newObjectExpr.accept(writerVisitor);
    assertEquals("new List<>()", writerVisitor.write());
}
Also used : NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Test(org.junit.Test)

Example 4 with ConcreteReference

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

the class ImportWriterVisitorTest method writeNewObjectExprImports_withArgs.

@Test
public void writeNewObjectExprImports_withArgs() {
    // [Constructing] `new FileOutputStream(File file)` and the argument needs to be imported.
    ConcreteReference fileOutputStreamRef = ConcreteReference.withClazz(FileOutputStream.class);
    ConcreteReference fileRef = ConcreteReference.withClazz(File.class);
    Variable fileVar = Variable.builder().setName("file").setType(TypeNode.withReference(fileRef)).build();
    VariableExpr fileExpr = VariableExpr.builder().setVariable(fileVar).build();
    NewObjectExpr newObjectExpr = NewObjectExpr.builder().setType(TypeNode.withReference(fileOutputStreamRef)).setArguments(Arrays.asList(fileExpr)).build();
    newObjectExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import java.io.File;\n", "import java.io.FileOutputStream;\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) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Test(org.junit.Test)

Example 5 with ConcreteReference

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

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