• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.grpc.server.processor;
18 
19 import static com.squareup.javapoet.MethodSpec.methodBuilder;
20 import static com.squareup.javapoet.TypeSpec.classBuilder;
21 import static com.squareup.javapoet.WildcardTypeName.subtypeOf;
22 import static javax.lang.model.element.Modifier.FINAL;
23 import static javax.lang.model.element.Modifier.PUBLIC;
24 import static javax.lang.model.element.Modifier.STATIC;
25 
26 import com.squareup.javapoet.ClassName;
27 import com.squareup.javapoet.MethodSpec;
28 import com.squareup.javapoet.ParameterSpec;
29 import com.squareup.javapoet.ParameterizedTypeName;
30 import com.squareup.javapoet.TypeName;
31 import com.squareup.javapoet.TypeSpec;
32 import dagger.grpc.server.GrpcService;
33 import java.util.List;
34 
35 /**
36  * An object that generates the non-proxying service definition module for a {@link
37  * GrpcService}-annotated service implementation.
38  */
39 final class GrpcServiceModuleGenerator extends SourceGenerator {
40 
41   private static final TypeName LIST_OF_INTERCEPTORS = ParameterizedTypeName.get(
42       ClassName.get(List.class), subtypeOf(IoGrpc.SERVER_INTERCEPTOR));
43 
44   private final GrpcServiceModel grpcServiceModel;
45 
GrpcServiceModuleGenerator(GrpcServiceModel grpcServiceModel)46   GrpcServiceModuleGenerator(GrpcServiceModel grpcServiceModel) {
47     super(grpcServiceModel.packageName());
48     this.grpcServiceModel = grpcServiceModel;
49   }
50 
51   @Override
createType()52   protected TypeSpec createType() {
53     TypeSpec.Builder serviceModule =
54         classBuilder(grpcServiceModel.serviceModuleName)
55             .addJavadoc(
56                 "Install this module in the {@link $T @Singleton} server component\n",
57                 JavaxInject.singleton().type)
58             .addJavadoc(
59                 "or in the subcomponent that implements {@link $T}.\n",
60                 grpcServiceModel.serviceDefinitionTypeName);
61     grpcServiceModel.generatedAnnotation().ifPresent(serviceModule::addAnnotation);
62     return serviceModule
63         .addAnnotation(Dagger.module())
64         .addModifiers(PUBLIC, FINAL)
65         .addMethod(provideServiceDefinition())
66         .build();
67   }
68 
69   /**
70    * Returns the {@link dagger.Provides @Provides} method for the {@link
71    * io.grpc.ServerServiceDefinition} for the service.
72    */
provideServiceDefinition()73   private MethodSpec provideServiceDefinition() {
74     return methodBuilder("serviceDefinition")
75         .addAnnotation(Dagger.provides())
76         .addAnnotation(grpcServiceModel.forGrpcService())
77         .addModifiers(STATIC)
78         .returns(IoGrpc.SERVER_SERVICE_DEFINITION)
79         .addParameter(grpcServiceModel.serviceImplementationClassName, "implementation")
80         .addParameter(
81             ParameterSpec.builder(LIST_OF_INTERCEPTORS, "interceptors")
82                 .addAnnotation(grpcServiceModel.forGrpcService())
83                 .build())
84         .addStatement(
85             "$T serviceDefinition = implementation.bindService()", IoGrpc.SERVER_SERVICE_DEFINITION)
86         .addStatement(
87             "return $T.intercept(serviceDefinition, interceptors)", IoGrpc.SERVER_INTERCEPTORS)
88         .build();
89   }
90 }
91