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