• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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;
18 
19 import static dagger.internal.Preconditions.checkNotNull;
20 import static dagger.internal.Providers.asDaggerProvider;
21 
22 import org.jspecify.annotations.Nullable;
23 
24 /**
25  * A DelegateFactory that is used to stitch Provider/Lazy indirection based dependency cycles.
26  *
27  * @since 2.0.1
28  */
29 public final class DelegateFactory<T> implements Factory<T> {
30 
31   private @Nullable Provider<T> delegate;
32 
33   @Override
get()34   public T get() {
35     if (delegate == null) {
36       throw new IllegalStateException();
37     }
38     return delegate.get();
39   }
40 
41   // TODO(ronshapiro): remove this once we can reasonably expect generated code is no longer using
42   // this method
43   @Deprecated
setDelegatedProvider(Provider<T> delegate)44   public void setDelegatedProvider(Provider<T> delegate) {
45     setDelegate(this, delegate);
46   }
47 
48   /**
49    * Legacy javax version of the method to support libraries compiled with an older version of
50    * Dagger. Do not use directly.
51    */
52   @Deprecated
setDelegatedProvider(javax.inject.Provider<T> delegate)53   public void setDelegatedProvider(javax.inject.Provider<T> delegate) {
54     setDelegatedProvider(asDaggerProvider(delegate));
55   }
56 
57   /**
58    * Sets {@code delegateFactory}'s delegate provider to {@code delegate}.
59    *
60    * <p>{@code delegateFactory} must be an instance of {@link DelegateFactory}, otherwise this
61    * method will throw a {@link ClassCastException}.
62    */
setDelegate(Provider<T> delegateFactory, Provider<T> delegate)63   public static <T> void setDelegate(Provider<T> delegateFactory, Provider<T> delegate) {
64     DelegateFactory<T> asDelegateFactory = (DelegateFactory<T>) delegateFactory;
65     setDelegateInternal(asDelegateFactory, delegate);
66   }
67 
68   /**
69    * Legacy javax version of the method to support libraries compiled with an older version of
70    * Dagger. Do not use directly.
71    */
72   @Deprecated
setDelegate( javax.inject.Provider<T> delegateFactory, javax.inject.Provider<T> delegate)73   public static <T> void setDelegate(
74       javax.inject.Provider<T> delegateFactory, javax.inject.Provider<T> delegate) {
75     DelegateFactory<T> asDelegateFactory = (DelegateFactory<T>) delegateFactory;
76     setDelegateInternal(asDelegateFactory, asDaggerProvider(delegate));
77   }
78 
setDelegateInternal( DelegateFactory<T> delegateFactory, Provider<T> delegate)79   private static <T> void setDelegateInternal(
80       DelegateFactory<T> delegateFactory, Provider<T> delegate) {
81     checkNotNull(delegate);
82     if (delegateFactory.delegate != null) {
83       throw new IllegalStateException();
84     }
85     delegateFactory.delegate = delegate;
86   }
87 
88   /**
89    * Returns the factory's delegate.
90    *
91    * @throws NullPointerException if the delegate has not been set
92    */
getDelegate()93   Provider<T> getDelegate() {
94     return checkNotNull(delegate);
95   }
96 }
97