1 /*
2  * Copyright (C) 2015 Google Inc.
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 package com.google.inject.daggeradapter;
17 
18 import com.google.common.collect.ImmutableSet;
19 import com.google.inject.Binder;
20 import com.google.inject.Key;
21 import com.google.inject.internal.UniqueAnnotations;
22 import com.google.inject.multibindings.Multibinder;
23 import com.google.inject.spi.InjectionPoint;
24 import com.google.inject.spi.ModuleAnnotatedMethodScanner;
25 import dagger.Provides;
26 import dagger.Provides.Type;
27 import dagger.multibindings.ElementsIntoSet;
28 import dagger.multibindings.IntoMap;
29 import dagger.multibindings.IntoSet;
30 import java.lang.annotation.Annotation;
31 import java.lang.reflect.Method;
32 import java.util.Set;
33 
34 /**
35  * A scanner to process provider methods on Dagger modules.
36  *
37  * @author cgruber@google.com (Christian Gruber)
38  */
39 final class DaggerMethodScanner extends ModuleAnnotatedMethodScanner {
40   static final DaggerMethodScanner INSTANCE = new DaggerMethodScanner();
41   private static final ImmutableSet<Class<Provides>> ANNOTATIONS =
42       ImmutableSet.of(dagger.Provides.class);
43 
44   @Override
annotationClasses()45   public Set<? extends Class<? extends Annotation>> annotationClasses() {
46     return ANNOTATIONS;
47   }
48 
49   @Override
prepareMethod( Binder binder, Annotation rawAnnotation, Key<T> key, InjectionPoint injectionPoint)50   public <T> Key<T> prepareMethod(
51       Binder binder, Annotation rawAnnotation, Key<T> key, InjectionPoint injectionPoint) {
52     Method providesMethod = (Method) injectionPoint.getMember();
53     Provides annotation = (Provides) rawAnnotation;
54     if (providesMethod.isAnnotationPresent(IntoSet.class)) {
55       return processSetBinding(binder, key);
56     } else if (providesMethod.isAnnotationPresent(ElementsIntoSet.class)) {
57       binder.addError("@ElementsIntoSet contributions are not suppored by Guice.", providesMethod);
58       return key;
59     } else if (providesMethod.isAnnotationPresent(IntoMap.class)) {
60       /* TODO(cgruber) implement map bindings */
61       binder.addError("Map bindings are not yet supported.");
62       return key;
63     }
64 
65     switch (annotation.type()) {
66       case UNIQUE:
67         return key;
68       case SET:
69         return processSetBinding(binder, key);
70       case SET_VALUES:
71         binder.addError(
72             Type.SET_VALUES.name() + " contributions are not supported by Guice.", providesMethod);
73         return key;
74       default:
75         binder.addError("Unknown @Provides type " + annotation.type() + ".", providesMethod);
76         return key;
77     }
78   }
79 
processSetBinding(Binder binder, Key<T> key)80   private static <T> Key<T> processSetBinding(Binder binder, Key<T> key) {
81     Annotation annotation = key.getAnnotation();
82     Multibinder<T> setBinder =
83         (annotation != null)
84             ? Multibinder.newSetBinder(binder, key.getTypeLiteral(), annotation)
85             : Multibinder.newSetBinder(binder, key.getTypeLiteral());
86     Key<T> newKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create());
87     setBinder.addBinding().to(newKey);
88     return newKey;
89   }
90 
DaggerMethodScanner()91   private DaggerMethodScanner() {}
92 }
93