• 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 com.squareup.javapoet.AnnotationSpec;
20 import com.squareup.javapoet.ClassName;
21 import com.squareup.javapoet.JavaFile;
22 import com.squareup.javapoet.TypeSpec;
23 
24 /**
25  * An object that generates one top-level type.
26  */
27 abstract class SourceGenerator {
28 
29   private final String packageName;
30 
SourceGenerator(String packageName)31   protected SourceGenerator(String packageName) {
32     this.packageName = packageName;
33   }
34 
javaFile()35   public JavaFile javaFile() {
36     return JavaFile.builder(packageName, createType()).build();
37   }
38 
39   /**
40    * Creates the type to write.
41    */
createType()42   protected abstract TypeSpec createType();
43 
44   /** Class names and annotation specs for types in the {@link dagger} package. */
45   protected static final class Dagger {
Dagger()46     private Dagger() {}
47 
binds()48     static AnnotationSpec binds() {
49       return AnnotationSpec.builder(ClassName.get("dagger", "Binds")).build();
50     }
51 
intoSet()52     static AnnotationSpec intoSet() {
53       return AnnotationSpec.builder(ClassName.get("dagger.multibindings", "IntoSet")).build();
54     }
55 
provides()56     static AnnotationSpec provides() {
57       return AnnotationSpec.builder(ClassName.get("dagger", "Provides")).build();
58     }
59 
60     /** A {@code @dagger.Module} annotation that includes the given module classes. */
module(ClassName... includedModules)61     static AnnotationSpec module(ClassName... includedModules) {
62       AnnotationSpec.Builder module = AnnotationSpec.builder(ClassName.get("dagger", "Module"));
63       for (ClassName includedModule : includedModules) {
64         module.addMember("includes", "$T.class", includedModule);
65       }
66       return module.build();
67     }
68 
69     /** Class names and annotation specs for types in the {@link dagger.grpc} package. */
70     protected static final class GrpcServer {
GrpcServer()71       private GrpcServer() {}
72 
73       static final ClassName PROXY_SERVER_CALL_HANDLER =
74           ClassName.get("dagger.grpc.server", "ProxyServerCallHandler");
75 
76       static final ClassName GRPC_CALL_METADATA_MODULE =
77           ClassName.get("dagger.grpc.server", "GrpcCallMetadataModule");
78 
79       static final ClassName SERVICE_DEFINITION_FACTORY =
80           PROXY_SERVER_CALL_HANDLER.nestedClass("ServiceDefinitionFactory");
81     }
82   }
83 
84   /** Class names and annotation specs for types in the {@link io.grpc} package. */
85   protected static final class IoGrpc {
IoGrpc()86     private IoGrpc() {}
87 
88     static final ClassName BINDABLE_SERVICE = ClassName.get("io.grpc", "BindableService");
89     static final ClassName METADATA = ClassName.get("io.grpc", "Metadata");
90     static final ClassName METHOD_DESCRIPTOR = ClassName.get("io.grpc", "MethodDescriptor");
91     static final ClassName SERVER_INTERCEPTOR =
92         ClassName.get("io.grpc", "ServerInterceptor");
93     static final ClassName SERVER_INTERCEPTORS =
94         ClassName.get("io.grpc", "ServerInterceptors");
95     static final ClassName SERVER_SERVICE_DEFINITION =
96         ClassName.get("io.grpc", "ServerServiceDefinition");
97   }
98 
99   /** Class names and annotation specs for types in the {@link javax.inject} package. */
100   protected static final class JavaxInject {
JavaxInject()101     private JavaxInject() {}
102 
inject()103     static AnnotationSpec inject() {
104       return AnnotationSpec.builder(ClassName.get("javax.inject", "Inject")).build();
105     }
106 
singleton()107     static AnnotationSpec singleton() {
108       return AnnotationSpec.builder(ClassName.get("javax.inject", "Singleton")).build();
109     }
110   }
111 }
112