• 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.MoreTypes.isType;
20 
21 import com.google.auto.common.MoreTypes;
22 import com.google.common.collect.ImmutableSet;
23 import dagger.Lazy;
24 import dagger.MembersInjector;
25 import dagger.producers.Produced;
26 import dagger.producers.Producer;
27 import java.util.Set;
28 import javax.inject.Provider;
29 import javax.lang.model.type.TypeMirror;
30 
31 /**
32  * A collection of utility methods for dealing with Dagger framework types. A framework type is any
33  * type that the framework itself defines.
34  */
35 public final class FrameworkTypes {
36   private static final ImmutableSet<Class<?>> PROVISION_TYPES =
37       ImmutableSet.of(Provider.class, Lazy.class, MembersInjector.class);
38 
39   // NOTE(beder): ListenableFuture is not considered a producer framework type because it is not
40   // defined by the framework, so we can't treat it specially in ordinary Dagger.
41   private static final ImmutableSet<Class<?>> PRODUCTION_TYPES =
42       ImmutableSet.of(Produced.class, Producer.class);
43 
44   /** Returns true if the type represents a producer-related framework type. */
isProducerType(TypeMirror type)45   public static boolean isProducerType(TypeMirror type) {
46     return isType(type) && typeIsOneOf(PRODUCTION_TYPES, type);
47   }
48 
49   /** Returns true if the type represents a framework type. */
isFrameworkType(TypeMirror type)50   public static boolean isFrameworkType(TypeMirror type) {
51     return isType(type)
52         && (typeIsOneOf(PROVISION_TYPES, type)
53             || typeIsOneOf(PRODUCTION_TYPES, type));
54   }
55 
typeIsOneOf(Set<Class<?>> classes, TypeMirror type)56   private static boolean typeIsOneOf(Set<Class<?>> classes, TypeMirror type) {
57     for (Class<?> clazz : classes) {
58       if (MoreTypes.isTypeOf(clazz, type)) {
59         return true;
60       }
61     }
62     return false;
63   }
64 
FrameworkTypes()65   private FrameworkTypes() {}
66 }
67