blob: 2c104f5ba175c5888702f2a23f86f157b87106a6 [file] [log] [blame]
dpbe1ed0452018-04-24 09:28:20 -07001/*
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
17package dagger.internal.codegen;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import com.google.googlejavaformat.java.filer.FormattingFiler;
22import dagger.Binds;
23import dagger.Module;
24import dagger.Provides;
25import dagger.Reusable;
26import dagger.internal.codegen.ProcessingEnvironmentModule.ElementsModule;
27import java.util.Map;
28import javax.annotation.processing.Filer;
29import javax.annotation.processing.Messager;
30import javax.annotation.processing.ProcessingEnvironment;
31import javax.lang.model.SourceVersion;
32import javax.lang.model.util.Elements;
33import javax.lang.model.util.Types;
34
35/** Bindings that depend on the {@link ProcessingEnvironment}. */
36@Module(includes = ElementsModule.class)
37final 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
ronshapiro6814a212018-09-13 15:05:41 -070057 Filer filer(CompilerOptions compilerOptions) {
58 if (compilerOptions.headerCompilation() || !compilerOptions.formatGeneratedSource()) {
dpbe1ed0452018-04-24 09:28:20 -070059 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}