• 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.compileroption.CompilerOptions;
26 import dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions;
27 import dagger.internal.codegen.compileroption.ProcessingOptions;
28 import dagger.internal.codegen.langmodel.DaggerElements;
29 import dagger.spi.BindingGraphPlugin;
30 import java.util.Map;
31 import javax.annotation.processing.Filer;
32 import javax.annotation.processing.Messager;
33 import javax.annotation.processing.ProcessingEnvironment;
34 import javax.lang.model.SourceVersion;
35 import javax.lang.model.util.Types;
36 
37 /** Bindings that depend on the {@link ProcessingEnvironment}. */
38 @Module
39 interface ProcessingEnvironmentModule {
40   @Binds
41   @Reusable // to avoid parsing options more than once
bindCompilerOptions( ProcessingEnvironmentCompilerOptions processingEnvironmentCompilerOptions)42   CompilerOptions bindCompilerOptions(
43       ProcessingEnvironmentCompilerOptions processingEnvironmentCompilerOptions);
44 
45   @Provides
46   @ProcessingOptions
processingOptions(ProcessingEnvironment processingEnvironment)47   static Map<String, String> processingOptions(ProcessingEnvironment processingEnvironment) {
48     return processingEnvironment.getOptions();
49   }
50 
51   @Provides
messager(ProcessingEnvironment processingEnvironment)52   static Messager messager(ProcessingEnvironment processingEnvironment) {
53     return processingEnvironment.getMessager();
54   }
55 
56   @Provides
filer(CompilerOptions compilerOptions, ProcessingEnvironment processingEnvironment)57   static Filer filer(CompilerOptions compilerOptions, ProcessingEnvironment processingEnvironment) {
58     if (compilerOptions.headerCompilation() || !compilerOptions.formatGeneratedSource()) {
59       return processingEnvironment.getFiler();
60     } else {
61       return new FormattingFiler(processingEnvironment.getFiler());
62     }
63   }
64 
65   @Provides
types(ProcessingEnvironment processingEnvironment)66   static Types types(ProcessingEnvironment processingEnvironment) {
67     return processingEnvironment.getTypeUtils();
68   }
69 
70   @Provides
sourceVersion(ProcessingEnvironment processingEnvironment)71   static SourceVersion sourceVersion(ProcessingEnvironment processingEnvironment) {
72     return processingEnvironment.getSourceVersion();
73   }
74 
75   @Provides
daggerElements(ProcessingEnvironment processingEnvironment)76   static DaggerElements daggerElements(ProcessingEnvironment processingEnvironment) {
77     return new DaggerElements(processingEnvironment);
78   }
79 
80   @Provides
81   @ProcessorClassLoader
processorClassloader(ProcessingEnvironment processingEnvironment)82   static ClassLoader processorClassloader(ProcessingEnvironment processingEnvironment) {
83     return BindingGraphPlugin.class.getClassLoader();
84   }
85 }
86