• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.internal.codegen.javac;
18 
19 import androidx.room.compiler.processing.XMessager;
20 import androidx.room.compiler.processing.XProcessingEnv;
21 import androidx.room.compiler.processing.compat.XConverters;
22 import com.sun.tools.javac.model.JavacElements;
23 import com.sun.tools.javac.model.JavacTypes;
24 import com.sun.tools.javac.util.Context;
25 import dagger.Binds;
26 import dagger.Module;
27 import dagger.Provides;
28 import dagger.internal.codegen.binding.BindingGraphFactory;
29 import dagger.internal.codegen.binding.ComponentDescriptorFactory;
30 import dagger.internal.codegen.compileroption.CompilerOptions;
31 import dagger.internal.codegen.langmodel.DaggerElements;
32 import dagger.internal.codegen.langmodel.DaggerTypes;
33 import javax.annotation.processing.ProcessingEnvironment;
34 import javax.lang.model.util.Elements; // ALLOW_TYPES_ELEMENTS
35 import javax.lang.model.util.Types; // ALLOW_TYPES_ELEMENTS
36 
37 /**
38  * A module that provides a {@link BindingGraphFactory} and {@link ComponentDescriptorFactory} for
39  * use in {@code javac} plugins. Requires a binding for the {@code javac} {@link Context}.
40  */
41 @Module(includes = JavacPluginModule.BindsModule.class)
42 public final class JavacPluginModule {
43   @Module
44   interface BindsModule {
45     @Binds
compilerOptions(JavacPluginCompilerOptions compilerOptions)46     CompilerOptions compilerOptions(JavacPluginCompilerOptions compilerOptions);
47   }
48 
49   private final XProcessingEnv processingEnv;
50 
JavacPluginModule(Context context)51   public JavacPluginModule(Context context) {
52     this(JavacElements.instance(context), JavacTypes.instance(context));
53   }
54 
JavacPluginModule(Elements elements, Types types)55   public JavacPluginModule(Elements elements, Types types) {
56     this.processingEnv =
57         XProcessingEnv.create(new JavacPluginProcessingEnvironment(elements, types));
58   }
59 
60   @Provides
messager()61   XMessager messager() {
62     return processingEnv.getMessager();
63   }
64 
65   @Provides
daggerElements()66   DaggerElements daggerElements() {
67     ProcessingEnvironment env = XConverters.toJavac(processingEnv);
68     return new DaggerElements(env.getElementUtils(), env.getTypeUtils()); // ALLOW_TYPES_ELEMENTS
69   }
70 
71   @Provides
daggerTypes(DaggerElements elements)72   DaggerTypes daggerTypes(DaggerElements elements) {
73     ProcessingEnvironment env = XConverters.toJavac(processingEnv);
74     return new DaggerTypes(env.getTypeUtils(), elements); // ALLOW_TYPES_ELEMENTS
75   }
76 
77   @Provides
xProcessingEnv()78   XProcessingEnv xProcessingEnv() {
79     return processingEnv;
80   }
81 }
82