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.base.ClearableCache; 29 import dagger.internal.codegen.compileroption.CompilerOptions; 30 import dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions; 31 import dagger.internal.codegen.compileroption.ProcessingOptions; 32 import dagger.internal.codegen.langmodel.DaggerElements; 33 import dagger.internal.codegen.langmodel.DaggerTypes; 34 import dagger.multibindings.IntoSet; 35 import java.util.Map; 36 import javax.inject.Singleton; 37 import javax.lang.model.SourceVersion; 38 39 /** Bindings that depend on the {@link XProcessingEnv}. */ 40 @Module 41 interface ProcessingEnvironmentModule { 42 @Binds 43 @Reusable // to avoid parsing options more than once bindCompilerOptions( ProcessingEnvironmentCompilerOptions processingEnvironmentCompilerOptions)44 CompilerOptions bindCompilerOptions( 45 ProcessingEnvironmentCompilerOptions processingEnvironmentCompilerOptions); 46 47 @Provides 48 @ProcessingOptions processingOptions(XProcessingEnv xProcessingEnv)49 static Map<String, String> processingOptions(XProcessingEnv xProcessingEnv) { 50 return xProcessingEnv.getOptions(); 51 } 52 53 @Provides messager(XProcessingEnv xProcessingEnv)54 static XMessager messager(XProcessingEnv xProcessingEnv) { 55 return xProcessingEnv.getMessager(); 56 } 57 58 @Provides filer(CompilerOptions compilerOptions, XProcessingEnv xProcessingEnv)59 static XFiler filer(CompilerOptions compilerOptions, XProcessingEnv xProcessingEnv) { 60 return compilerOptions.headerCompilation() || !compilerOptions.formatGeneratedSource() 61 ? xProcessingEnv.getFiler() 62 : XConverters.toXProcessing( 63 new FormattingFiler(XConverters.toJavac(xProcessingEnv.getFiler())), xProcessingEnv); 64 } 65 66 @Provides sourceVersion(XProcessingEnv xProcessingEnv)67 static SourceVersion sourceVersion(XProcessingEnv xProcessingEnv) { 68 return XConverters.toJavac(xProcessingEnv).getSourceVersion(); 69 } 70 71 @Provides 72 @Singleton daggerElements(XProcessingEnv xProcessingEnv)73 static DaggerElements daggerElements(XProcessingEnv xProcessingEnv) { 74 return new DaggerElements( 75 XConverters.toJavac(xProcessingEnv).getElementUtils(), 76 XConverters.toJavac(xProcessingEnv).getTypeUtils()); 77 } 78 79 @Provides 80 @Singleton daggerTypes(XProcessingEnv xProcessingEnv, DaggerElements elements)81 static DaggerTypes daggerTypes(XProcessingEnv xProcessingEnv, DaggerElements elements) { 82 return new DaggerTypes(XConverters.toJavac(xProcessingEnv).getTypeUtils(), elements); 83 } 84 85 @Binds 86 @IntoSet daggerElementAsClearableCache(DaggerElements elements)87 ClearableCache daggerElementAsClearableCache(DaggerElements elements); 88 } 89