• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.hilt.android;
18 
19 import android.content.Context;
20 import dagger.hilt.EntryPoints;
21 import dagger.hilt.internal.GeneratedComponentManagerHolder;
22 import dagger.hilt.internal.Preconditions;
23 import dagger.hilt.internal.TestSingletonComponentManager;
24 import dagger.internal.Beta;
25 import java.lang.annotation.Annotation;
26 import javax.annotation.Nonnull;
27 
28 /** Static utility methods for accessing entry points annotated with {@link EarlyEntryPoint}. */
29 @Beta
30 public final class EarlyEntryPoints {
31 
32   /**
33    * Returns the early entry point interface given a component manager holder. Note that this
34    * performs an unsafe cast and so callers should be sure that the given component/component
35    * manager matches the early entry point interface that is given.
36    *
37    * @param applicationContext The application context.
38    * @param entryPoint The interface marked with {@link EarlyEntryPoint}. The {@link
39    *     dagger.hilt.InstallIn} annotation on this entry point should match the component argument
40    *     above.
41    */
42   // Note that the input is not statically declared to be a Component or ComponentManager to make
43   // this method easier to use, since most code will use this with an Application or Context type.
44   @Nonnull
get(Context applicationContext, Class<T> entryPoint)45   public static <T> T get(Context applicationContext, Class<T> entryPoint) {
46     applicationContext = applicationContext.getApplicationContext();
47     Preconditions.checkState(
48         applicationContext instanceof GeneratedComponentManagerHolder,
49         "Expected application context to implement GeneratedComponentManagerHolder. "
50             + "Check that you're passing in an application context that uses Hilt.");
51     Object componentManager =
52         ((GeneratedComponentManagerHolder) applicationContext).componentManager();
53     if (componentManager instanceof TestSingletonComponentManager) {
54       Preconditions.checkState(
55           hasAnnotationReflection(entryPoint, EarlyEntryPoint.class),
56           "%s should be called with EntryPoints.get() rather than EarlyEntryPoints.get()",
57           entryPoint.getCanonicalName());
58       Object earlyComponent =
59           ((TestSingletonComponentManager) componentManager).earlySingletonComponent();
60       return entryPoint.cast(earlyComponent);
61     }
62 
63     // @EarlyEntryPoint only has an effect in test environment, so if this is not a test we
64     // delegate to EntryPoints.
65     return EntryPoints.get(applicationContext, entryPoint);
66   }
67 
68   // Note: This method uses reflection but it should only be called in test environments.
hasAnnotationReflection( Class<?> clazz, Class<? extends Annotation> annotationClazz)69   private static boolean hasAnnotationReflection(
70       Class<?> clazz, Class<? extends Annotation> annotationClazz) {
71     for (Annotation annotation : clazz.getAnnotations()) {
72       if (annotation.annotationType().equals(annotationClazz)) {
73         return true;
74       }
75     }
76     return false;
77   }
78 
EarlyEntryPoints()79   private EarlyEntryPoints() {}
80 }
81