xref: /aosp_15_r20/external/dagger2/java/dagger/internal/codegen/ProcessingEnvironmentModule.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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 androidx.room.compiler.processing.XFiler;
20 import androidx.room.compiler.processing.XMessager;
21 import androidx.room.compiler.processing.XProcessingEnv;
22 import androidx.room.compiler.processing.compat.XConverters;
23 import com.google.googlejavaformat.java.filer.FormattingFiler;
24 import dagger.Binds;
25 import dagger.Module;
26 import dagger.Provides;
27 import dagger.Reusable;
28 import dagger.internal.codegen.compileroption.CompilerOptions;
29 import dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions;
30 import dagger.internal.codegen.compileroption.ProcessingOptions;
31 import java.util.Map;
32 
33 /** Bindings that depend on the {@link XProcessingEnv}. */
34 @Module
35 interface ProcessingEnvironmentModule {
36   @Binds
37   @Reusable // to avoid parsing options more than once
bindCompilerOptions( ProcessingEnvironmentCompilerOptions processingEnvironmentCompilerOptions)38   CompilerOptions bindCompilerOptions(
39       ProcessingEnvironmentCompilerOptions processingEnvironmentCompilerOptions);
40 
41   @Provides
42   @ProcessingOptions
processingOptions(XProcessingEnv xProcessingEnv)43   static Map<String, String> processingOptions(XProcessingEnv xProcessingEnv) {
44     return xProcessingEnv.getOptions();
45   }
46 
47   @Provides
messager(XProcessingEnv xProcessingEnv)48   static XMessager messager(XProcessingEnv xProcessingEnv) {
49     return xProcessingEnv.getMessager();
50   }
51 
52   @Provides
filer(CompilerOptions compilerOptions, XProcessingEnv xProcessingEnv)53   static XFiler filer(CompilerOptions compilerOptions, XProcessingEnv xProcessingEnv) {
54     return compilerOptions.headerCompilation() || !compilerOptions.formatGeneratedSource()
55         ? xProcessingEnv.getFiler()
56         : XConverters.toXProcessing(
57             new FormattingFiler(XConverters.toJavac(xProcessingEnv.getFiler())), xProcessingEnv);
58   }
59 }
60