| dpb | e1ed045 | 2018-04-24 09:28:20 -0700 | [diff] [blame] | 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.Binds; |
| 23 | import dagger.Module; |
| 24 | import dagger.Provides; |
| 25 | import dagger.Reusable; |
| 26 | import dagger.internal.codegen.ProcessingEnvironmentModule.ElementsModule; |
| 27 | import java.util.Map; |
| 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.Elements; |
| 33 | import javax.lang.model.util.Types; |
| 34 | |
| 35 | /** Bindings that depend on the {@link ProcessingEnvironment}. */ |
| 36 | @Module(includes = ElementsModule.class) |
| 37 | final class ProcessingEnvironmentModule { |
| 38 | |
| 39 | private final ProcessingEnvironment processingEnvironment; |
| 40 | |
| 41 | ProcessingEnvironmentModule(ProcessingEnvironment processingEnvironment) { |
| 42 | this.processingEnvironment = checkNotNull(processingEnvironment); |
| 43 | } |
| 44 | |
| 45 | @Provides |
| 46 | @ProcessingOptions |
| 47 | Map<String, String> processingOptions() { |
| 48 | return processingEnvironment.getOptions(); |
| 49 | } |
| 50 | |
| 51 | @Provides |
| 52 | Messager messager() { |
| 53 | return processingEnvironment.getMessager(); |
| 54 | } |
| 55 | |
| 56 | @Provides |
| ronshapiro | 6814a21 | 2018-09-13 15:05:41 -0700 | [diff] [blame^] | 57 | Filer filer(CompilerOptions compilerOptions) { |
| 58 | if (compilerOptions.headerCompilation() || !compilerOptions.formatGeneratedSource()) { |
| dpb | e1ed045 | 2018-04-24 09:28:20 -0700 | [diff] [blame] | 59 | return processingEnvironment.getFiler(); |
| 60 | } else { |
| 61 | return new FormattingFiler(processingEnvironment.getFiler()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Provides |
| 66 | Types types() { |
| 67 | return processingEnvironment.getTypeUtils(); |
| 68 | } |
| 69 | |
| 70 | @Provides |
| 71 | SourceVersion sourceVersion() { |
| 72 | return processingEnvironment.getSourceVersion(); |
| 73 | } |
| 74 | |
| 75 | @Provides |
| 76 | DaggerElements daggerElements() { |
| 77 | return new DaggerElements(processingEnvironment); |
| 78 | } |
| 79 | |
| 80 | @Provides |
| 81 | @Reusable // to avoid parsing options more than once |
| 82 | CompilerOptions compilerOptions(DaggerElements elements) { |
| 83 | return CompilerOptions.create(processingEnvironment, elements); |
| 84 | } |
| 85 | |
| 86 | @Module |
| 87 | interface ElementsModule { |
| 88 | @Binds |
| 89 | Elements elements(DaggerElements daggerElements); |
| 90 | } |
| 91 | } |