• 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;
18 
19 import static com.google.auto.common.MoreElements.isAnnotationPresent;
20 
21 import dagger.Provides;
22 import dagger.multibindings.ElementsIntoSet;
23 import dagger.multibindings.IntoMap;
24 import dagger.multibindings.IntoSet;
25 import javax.lang.model.element.Element;
26 
27 /** Whether a binding or declaration is for a unique contribution or a map or set multibinding. */
28 enum ContributionType {
29   /** Represents map bindings. */
30   MAP,
31   /** Represents set bindings. */
32   SET,
33   /** Represents set values bindings. */
34   SET_VALUES,
35   /** Represents a valid non-collection binding. */
36   UNIQUE,
37   ;
38 
39   /** An object that is associated with a {@link ContributionType}. */
40   interface HasContributionType {
41 
42     /** The contribution type of this object. */
contributionType()43     ContributionType contributionType();
44   }
45 
46   /** {@code true} if this is for a multibinding. */
isMultibinding()47   boolean isMultibinding() {
48     return !this.equals(UNIQUE);
49   }
50 
51   /**
52    * The contribution type from a binding element's annotations. Presumes a well-formed binding
53    * element (at most one of @IntoSet, @IntoMap, @ElementsIntoSet and @Provides.type). {@link
54    * BindingMethodValidator} and {@link BindsInstanceProcessingStep} validate correctness on their
55    * own.
56    */
fromBindingElement(Element element)57   static ContributionType fromBindingElement(Element element) {
58     if (isAnnotationPresent(element, IntoMap.class)) {
59       return ContributionType.MAP;
60     } else if (isAnnotationPresent(element, IntoSet.class)) {
61       return ContributionType.SET;
62     } else if (isAnnotationPresent(element, ElementsIntoSet.class)) {
63       return ContributionType.SET_VALUES;
64     }
65     return ContributionType.UNIQUE;
66   }
67 }
68