• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.validation;
18 
19 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
20 
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.collect.Maps;
23 import com.google.common.collect.Sets;
24 import dagger.internal.codegen.compileroption.ProcessingOptions;
25 import dagger.internal.codegen.langmodel.DaggerElements;
26 import dagger.internal.codegen.langmodel.DaggerTypes;
27 import dagger.spi.BindingGraphPlugin;
28 import java.util.Map;
29 import java.util.Set;
30 import javax.annotation.processing.Filer;
31 import javax.inject.Inject;
32 
33 /** Initializes {@link BindingGraphPlugin}s. */
34 public final class BindingGraphPlugins {
35   private final ImmutableSet<BindingGraphPlugin> plugins;
36   private final Filer filer;
37   private final DaggerTypes types;
38   private final DaggerElements elements;
39   private final Map<String, String> processingOptions;
40 
41   @Inject
BindingGraphPlugins( @alidation ImmutableSet<BindingGraphPlugin> validationPlugins, ImmutableSet<BindingGraphPlugin> externalPlugins, Filer filer, DaggerTypes types, DaggerElements elements, @ProcessingOptions Map<String, String> processingOptions)42   BindingGraphPlugins(
43       @Validation ImmutableSet<BindingGraphPlugin> validationPlugins,
44       ImmutableSet<BindingGraphPlugin> externalPlugins,
45       Filer filer,
46       DaggerTypes types,
47       DaggerElements elements,
48       @ProcessingOptions Map<String, String> processingOptions) {
49     this.plugins = Sets.union(validationPlugins, externalPlugins).immutableCopy();
50     this.filer = filer;
51     this.types = types;
52     this.elements = elements;
53     this.processingOptions = processingOptions;
54   }
55 
56   /** Returns {@link BindingGraphPlugin#supportedOptions()} from all the plugins. */
allSupportedOptions()57   public ImmutableSet<String> allSupportedOptions() {
58     return plugins.stream()
59         .flatMap(plugin -> plugin.supportedOptions().stream())
60         .collect(toImmutableSet());
61   }
62 
63   /** Initializes the plugins. */
64   // TODO(ronshapiro): Should we validate the uniqueness of plugin names?
initializePlugins()65   public void initializePlugins() {
66     plugins.forEach(this::initializePlugin);
67   }
68 
initializePlugin(BindingGraphPlugin plugin)69   private void initializePlugin(BindingGraphPlugin plugin) {
70     plugin.initFiler(filer);
71     plugin.initTypes(types);
72     plugin.initElements(elements);
73     Set<String> supportedOptions = plugin.supportedOptions();
74     if (!supportedOptions.isEmpty()) {
75       plugin.initOptions(Maps.filterKeys(processingOptions, supportedOptions::contains));
76     }
77   }
78 }
79