• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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;
18 
19 import com.google.googlejavaformat.java.filer.FormattingFiler;
20 import dagger.Binds;
21 import dagger.Module;
22 import dagger.Provides;
23 import dagger.Reusable;
24 import dagger.internal.codegen.SpiModule.ProcessorClassLoader;
25 import dagger.internal.codegen.base.ClearableCache;
26 import dagger.internal.codegen.compileroption.CompilerOptions;
27 import dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions;
28 import dagger.internal.codegen.compileroption.ProcessingOptions;
29 import dagger.internal.codegen.langmodel.DaggerElements;
30 import dagger.multibindings.IntoSet;
31 import dagger.spi.BindingGraphPlugin;
32 import java.util.Map;
33 import javax.annotation.processing.Filer;
34 import javax.annotation.processing.Messager;
35 import javax.annotation.processing.ProcessingEnvironment;
36 import javax.inject.Singleton;
37 import javax.lang.model.SourceVersion;
38 import javax.lang.model.util.Types;
39 
40 /** Bindings that depend on the {@link ProcessingEnvironment}. */
41 @Module
42 interface ProcessingEnvironmentModule {
43   @Binds
44   @Reusable // to avoid parsing options more than once
bindCompilerOptions( ProcessingEnvironmentCompilerOptions processingEnvironmentCompilerOptions)45   CompilerOptions bindCompilerOptions(
46       ProcessingEnvironmentCompilerOptions processingEnvironmentCompilerOptions);
47 
48   @Provides
49   @ProcessingOptions
processingOptions(ProcessingEnvironment processingEnvironment)50   static Map<String, String> processingOptions(ProcessingEnvironment processingEnvironment) {
51     return processingEnvironment.getOptions();
52   }
53 
54   @Provides
messager(ProcessingEnvironment processingEnvironment)55   static Messager messager(ProcessingEnvironment processingEnvironment) {
56     return processingEnvironment.getMessager();
57   }
58 
59   @Provides
filer(CompilerOptions compilerOptions, ProcessingEnvironment processingEnvironment)60   static Filer filer(CompilerOptions compilerOptions, ProcessingEnvironment processingEnvironment) {
61     if (compilerOptions.headerCompilation() || !compilerOptions.formatGeneratedSource()) {
62       return processingEnvironment.getFiler();
63     } else {
64       return new FormattingFiler(processingEnvironment.getFiler());
65     }
66   }
67 
68   @Provides
types(ProcessingEnvironment processingEnvironment)69   static Types types(ProcessingEnvironment processingEnvironment) {
70     return processingEnvironment.getTypeUtils();
71   }
72 
73   @Provides
sourceVersion(ProcessingEnvironment processingEnvironment)74   static SourceVersion sourceVersion(ProcessingEnvironment processingEnvironment) {
75     return processingEnvironment.getSourceVersion();
76   }
77 
78   @Provides
79   @Singleton
daggerElements(ProcessingEnvironment processingEnvironment)80   static DaggerElements daggerElements(ProcessingEnvironment processingEnvironment) {
81     return new DaggerElements(processingEnvironment);
82   }
83 
84   @Binds
85   @IntoSet
daggerElementAsClearableCache(DaggerElements elements)86   ClearableCache daggerElementAsClearableCache(DaggerElements elements);
87 
88   @Provides
89   @ProcessorClassLoader
processorClassloader(ProcessingEnvironment processingEnvironment)90   static ClassLoader processorClassloader(ProcessingEnvironment processingEnvironment) {
91     return BindingGraphPlugin.class.getClassLoader();
92   }
93 }
94