• 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 dagger.internal.codegen.xprocessing.XTypes.isTypeOf;
20 
21 import androidx.room.compiler.processing.XType;
22 import com.google.common.collect.ImmutableSet;
23 import com.squareup.javapoet.ClassName;
24 import dagger.internal.codegen.javapoet.TypeNames;
25 import java.util.Set;
26 
27 /**
28  * A collection of utility methods for dealing with Dagger framework types. A framework type is any
29  * type that the framework itself defines.
30  */
31 public final class FrameworkTypes {
32   // TODO(erichang): Add the Jakarta Provider here
33   private static final ImmutableSet<ClassName> PROVISION_TYPES =
34       ImmutableSet.of(
35           TypeNames.PROVIDER,
36           TypeNames.JAKARTA_PROVIDER,
37           TypeNames.LAZY,
38           TypeNames.MEMBERS_INJECTOR);
39 
40   // NOTE(beder): ListenableFuture is not considered a producer framework type because it is not
41   // defined by the framework, so we can't treat it specially in ordinary Dagger.
42   private static final ImmutableSet<ClassName> PRODUCTION_TYPES =
43       ImmutableSet.of(TypeNames.PRODUCED, TypeNames.PRODUCER);
44 
45   private static final ImmutableSet<ClassName> ALL_FRAMEWORK_TYPES =
46       ImmutableSet.<ClassName>builder().addAll(PROVISION_TYPES).addAll(PRODUCTION_TYPES).build();
47 
48   public static final ImmutableSet<ClassName> SET_VALUE_FRAMEWORK_TYPES =
49       ImmutableSet.of(TypeNames.PRODUCED);
50 
51   public static final ImmutableSet<ClassName> MAP_VALUE_FRAMEWORK_TYPES =
52       ImmutableSet.of(
53           TypeNames.PRODUCED,
54           TypeNames.PRODUCER,
55           TypeNames.PROVIDER,
56           TypeNames.JAKARTA_PROVIDER);
57 
58   // This is a set of types that are disallowed from use, but also aren't framework types in the
59   // sense that they aren't supported. Like we shouldn't try to unwrap these if we see them, though
60   // we shouldn't see them at all if they are correctly caught in validation.
61   private static final ImmutableSet<ClassName> DISALLOWED_TYPES =
62       ImmutableSet.of(TypeNames.DAGGER_PROVIDER);
63 
64   /** Returns true if the type represents a producer-related framework type. */
isProducerType(XType type)65   public static boolean isProducerType(XType type) {
66     return typeIsOneOf(PRODUCTION_TYPES, type);
67   }
68 
69   /** Returns true if the type represents a framework type. */
isFrameworkType(XType type)70   public static boolean isFrameworkType(XType type) {
71     return typeIsOneOf(ALL_FRAMEWORK_TYPES, type);
72   }
73 
isSetValueFrameworkType(XType type)74   public static boolean isSetValueFrameworkType(XType type) {
75     return typeIsOneOf(SET_VALUE_FRAMEWORK_TYPES, type);
76   }
77 
isMapValueFrameworkType(XType type)78   public static boolean isMapValueFrameworkType(XType type) {
79     return typeIsOneOf(MAP_VALUE_FRAMEWORK_TYPES, type);
80   }
81 
isDisallowedType(XType type)82   public static boolean isDisallowedType(XType type) {
83     return typeIsOneOf(DISALLOWED_TYPES, type);
84   }
85 
typeIsOneOf(Set<ClassName> classNames, XType type)86   private static boolean typeIsOneOf(Set<ClassName> classNames, XType type) {
87     return classNames.stream().anyMatch(className -> isTypeOf(type, className));
88   }
89 
FrameworkTypes()90   private FrameworkTypes() {}
91 }
92