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 static com.google.common.base.Preconditions.checkNotNull; 20 21 import com.google.googlejavaformat.java.filer.FormattingFiler; 22 import dagger.Module; 23 import dagger.Provides; 24 import dagger.Reusable; 25 import dagger.internal.codegen.langmodel.DaggerElements; 26 import java.util.Map; 27 import java.util.Optional; 28 import javax.annotation.processing.Filer; 29 import javax.annotation.processing.Messager; 30 import javax.annotation.processing.ProcessingEnvironment; 31 import javax.lang.model.SourceVersion; 32 import javax.lang.model.util.Types; 33 34 /** Bindings that depend on the {@link ProcessingEnvironment}. */ 35 @Module 36 final class ProcessingEnvironmentModule { 37 38 private final ProcessingEnvironment processingEnvironment; 39 ProcessingEnvironmentModule(ProcessingEnvironment processingEnvironment)40 ProcessingEnvironmentModule(ProcessingEnvironment processingEnvironment) { 41 this.processingEnvironment = checkNotNull(processingEnvironment); 42 } 43 44 @Provides 45 @ProcessingOptions processingOptions()46 Map<String, String> processingOptions() { 47 return processingEnvironment.getOptions(); 48 } 49 50 @Provides messager()51 Messager messager() { 52 return processingEnvironment.getMessager(); 53 } 54 55 @Provides filer(CompilerOptions compilerOptions)56 Filer filer(CompilerOptions compilerOptions) { 57 if (compilerOptions.headerCompilation() || !compilerOptions.formatGeneratedSource()) { 58 return processingEnvironment.getFiler(); 59 } else { 60 return new FormattingFiler(processingEnvironment.getFiler()); 61 } 62 } 63 64 @Provides types()65 Types types() { 66 return processingEnvironment.getTypeUtils(); 67 } 68 69 @Provides sourceVersion()70 SourceVersion sourceVersion() { 71 return processingEnvironment.getSourceVersion(); 72 } 73 74 @Provides daggerElements()75 DaggerElements daggerElements() { 76 return new DaggerElements(processingEnvironment); 77 } 78 79 @Provides 80 @Reusable // to avoid parsing options more than once compilerOptions()81 CompilerOptions compilerOptions() { 82 return ProcessingEnvironmentCompilerOptions.create(processingEnvironment); 83 } 84 85 @Provides daggerStatisticsRecorder()86 Optional<DaggerStatisticsRecorder> daggerStatisticsRecorder() { 87 return Optional.empty(); 88 } 89 } 90