• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.base;
18 
19 import static androidx.room.compiler.processing.compat.XConverters.toJavac;
20 import static com.google.auto.common.MoreElements.isAnnotationPresent;
21 
22 import androidx.room.compiler.processing.XElement;
23 import dagger.multibindings.ElementsIntoSet;
24 import dagger.multibindings.IntoMap;
25 import dagger.multibindings.IntoSet;
26 import javax.lang.model.element.Element;
27 
28 /** Whether a binding or declaration is for a unique contribution or a map or set multibinding. */
29 public enum ContributionType {
30   /** Represents map bindings. */
31   MAP,
32   /** Represents set bindings. */
33   SET,
34   /** Represents set values bindings. */
35   SET_VALUES,
36   /** Represents a valid non-collection binding. */
37   UNIQUE,
38   ;
39 
40   /** An object that is associated with a {@link ContributionType}. */
41   public interface HasContributionType {
42 
43     /** The contribution type of this object. */
contributionType()44     ContributionType contributionType();
45   }
46 
47   /** {@code true} if this is for a multibinding. */
isMultibinding()48   public boolean isMultibinding() {
49     return !this.equals(UNIQUE);
50   }
51 
52   /**
53    * The contribution type from a binding element's annotations. Presumes a well-formed binding
54    * element (at most one of @IntoSet, @IntoMap, @ElementsIntoSet and @Provides.type). {@link
55    * dagger.internal.codegen.validation.BindingMethodValidator} and {@link
56    * dagger.internal.codegen.validation.BindsInstanceProcessingStep} validate correctness on their
57    * own.
58    */
fromBindingElement(XElement element)59   public static ContributionType fromBindingElement(XElement element) {
60     return fromBindingElement(toJavac(element));
61   }
62 
63   /**
64    * The contribution type from a binding element's annotations. Presumes a well-formed binding
65    * element (at most one of @IntoSet, @IntoMap, @ElementsIntoSet and @Provides.type). {@link
66    * dagger.internal.codegen.validation.BindingMethodValidator} and {@link
67    * dagger.internal.codegen.validation.BindsInstanceProcessingStep} validate correctness on their
68    * own.
69    */
fromBindingElement(Element element)70   public static ContributionType fromBindingElement(Element element) {
71     // TODO(bcorso): Replace these class references with ClassName.
72     if (isAnnotationPresent(element, IntoMap.class)) {
73       return ContributionType.MAP;
74     } else if (isAnnotationPresent(element, IntoSet.class)) {
75       return ContributionType.SET;
76     } else if (isAnnotationPresent(element, ElementsIntoSet.class)) {
77       return ContributionType.SET_VALUES;
78     }
79     return ContributionType.UNIQUE;
80   }
81 }
82