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();
}
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();
}
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());
}
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());
}
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());
}
Aggregations