Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a dagger.internal.Provider in order to in the future help with adding support for jakarta.inject.Provider. #4103

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion java/dagger/internal/AbstractMapFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.util.LinkedHashMap;
import java.util.Map;
import javax.inject.Provider;

/**
* An {@code abstract} {@link Factory} implementation used to implement {@link Map} bindings.
Expand Down
37 changes: 30 additions & 7 deletions java/dagger/internal/DelegateFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
package dagger.internal;

import static dagger.internal.Preconditions.checkNotNull;

import javax.inject.Provider;
import static dagger.internal.Providers.asDaggerProvider;

/**
* A DelegateFactory that is used to stitch Provider/Lazy indirection based dependency cycles.
*
*
* @since 2.0.1
*/
public final class DelegateFactory<T> implements Factory<T> {
Expand All @@ -43,19 +42,44 @@ public void setDelegatedProvider(Provider<T> delegate) {
setDelegate(this, delegate);
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public void setDelegatedProvider(javax.inject.Provider<T> delegate) {
setDelegatedProvider(asDaggerProvider(delegate));
}

/**
* Sets {@code delegateFactory}'s delegate provider to {@code delegate}.
*
* <p>{@code delegateFactory} must be an instance of {@link DelegateFactory}, otherwise this
* method will throw a {@link ClassCastException}.
*/
public static <T> void setDelegate(Provider<T> delegateFactory, Provider<T> delegate) {
checkNotNull(delegate);
DelegateFactory<T> asDelegateFactory = (DelegateFactory<T>) delegateFactory;
if (asDelegateFactory.delegate != null) {
setDelegateInternal(asDelegateFactory, delegate);
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public static <T> void setDelegate(
javax.inject.Provider<T> delegateFactory, javax.inject.Provider<T> delegate) {
DelegateFactory<T> asDelegateFactory = (DelegateFactory<T>) delegateFactory;
setDelegateInternal(asDelegateFactory, asDaggerProvider(delegate));
}

private static <T> void setDelegateInternal(
DelegateFactory<T> delegateFactory, Provider<T> delegate) {
checkNotNull(delegate);
if (delegateFactory.delegate != null) {
throw new IllegalStateException();
}
asDelegateFactory.delegate = delegate;
delegateFactory.delegate = delegate;
}

/**
Expand All @@ -67,4 +91,3 @@ Provider<T> getDelegate() {
return checkNotNull(delegate);
}
}

24 changes: 22 additions & 2 deletions java/dagger/internal/DoubleCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package dagger.internal;

import static dagger.internal.Preconditions.checkNotNull;
import static dagger.internal.Providers.asDaggerProvider;

import dagger.Lazy;
import javax.inject.Provider;

/**
* A {@link Lazy} and {@link Provider} implementation that memoizes the value returned from a
Expand Down Expand Up @@ -73,7 +73,8 @@ private static Object reentrantCheck(Object currentInstance, Object newInstance)
/** Returns a {@link Provider} that caches the value from the given delegate provider. */
// This method is declared this way instead of "<T> Provider<T> provider(Provider<T> delegate)"
// to work around an Eclipse type inference bug: https://github.com/google/dagger/issues/949.
public static <P extends Provider<T>, T> Provider<T> provider(P delegate) {
public static <P extends dagger.internal.Provider<T>, T> dagger.internal.Provider<T> provider(
P delegate) {
checkNotNull(delegate);
if (delegate instanceof DoubleCheck) {
/* This should be a rare case, but if we have a scoped @Binds that delegates to a scoped
Expand All @@ -83,6 +84,16 @@ public static <P extends Provider<T>, T> Provider<T> provider(P delegate) {
return new DoubleCheck<T>(delegate);
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public static <P extends javax.inject.Provider<T>, T> javax.inject.Provider<T> provider(
P delegate) {
return provider(asDaggerProvider(delegate));
}

/** Returns a {@link Lazy} that caches the value from the given provider. */
// This method is declared this way instead of "<T> Lazy<T> lazy(Provider<T> delegate)"
// to work around an Eclipse type inference bug: https://github.com/google/dagger/issues/949.
Expand All @@ -99,4 +110,13 @@ public static <P extends Provider<T>, T> Lazy<T> lazy(P provider) {
}
return new DoubleCheck<T>(checkNotNull(provider));
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public static <P extends javax.inject.Provider<T>, T> Lazy<T> lazy(P provider) {
return lazy(asDaggerProvider(provider));
}
}
1 change: 0 additions & 1 deletion java/dagger/internal/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import dagger.Provides;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Scope;

/**
Expand Down
20 changes: 19 additions & 1 deletion java/dagger/internal/MapFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package dagger.internal;

import static dagger.internal.DaggerCollections.newLinkedHashMapWithExpectedSize;
import static dagger.internal.Providers.asDaggerProvider;
import static java.util.Collections.unmodifiableMap;

import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Provider;

/**
* A {@link Factory} implementation used to implement {@link Map} bindings. This factory returns a
Expand Down Expand Up @@ -72,12 +72,30 @@ public Builder<K, V> put(K key, Provider<V> providerOfValue) {
return this;
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public Builder<K, V> put(K key, javax.inject.Provider<V> providerOfValue) {
return put(key, asDaggerProvider(providerOfValue));
}

@Override
public Builder<K, V> putAll(Provider<Map<K, V>> mapFactory) {
super.putAll(mapFactory);
return this;
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public Builder<K, V> putAll(javax.inject.Provider<Map<K, V>> mapFactory) {
return putAll(asDaggerProvider(mapFactory));
}

/** Returns a new {@link MapProviderFactory}. */
public MapFactory<K, V> build() {
return new MapFactory<>(map);
Expand Down
36 changes: 35 additions & 1 deletion java/dagger/internal/MapProviderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package dagger.internal;

import static dagger.internal.DaggerCollections.newLinkedHashMapWithExpectedSize;
import static dagger.internal.Providers.asDaggerProvider;

import dagger.Lazy;
import java.util.Collections;
import java.util.Map;
import javax.inject.Provider;

/**
* A {@link Factory} implementation used to implement {@link Map} bindings. This factory returns a
Expand Down Expand Up @@ -57,12 +60,43 @@ public Builder<K, V> put(K key, Provider<V> providerOfValue) {
return this;
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public Builder<K, V> put(K key, javax.inject.Provider<V> providerOfValue) {
return put(key, asDaggerProvider(providerOfValue));
}

@Override
public Builder<K, V> putAll(Provider<Map<K, Provider<V>>> mapProviderFactory) {
super.putAll(mapProviderFactory);
return this;
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public Builder<K, V> putAll(
final javax.inject.Provider<Map<K, javax.inject.Provider<V>>> mapProviderFactory) {
return putAll(new Provider<Map<K, Provider<V>>>() {
@Override public Map<K, Provider<V>> get() {
Map<K, javax.inject.Provider<V>> javaxMap = mapProviderFactory.get();
if (javaxMap.isEmpty()) {
return Collections.emptyMap();
}
Map<K, Provider<V>> daggerMap = newLinkedHashMapWithExpectedSize(javaxMap.size());
for (Map.Entry<K, javax.inject.Provider<V>> e : javaxMap.entrySet()) {
daggerMap.put(e.getKey(), asDaggerProvider(e.getValue()));
}
return Collections.unmodifiableMap(daggerMap);
}
});
}

/** Returns a new {@link MapProviderFactory}. */
public MapProviderFactory<K, V> build() {
return new MapProviderFactory<>(map);
Expand Down
25 changes: 25 additions & 0 deletions java/dagger/internal/Provider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2023 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dagger.internal;

/**
* Internal Provider interface to make support for {@code javax.inject.Provider} and
* {@code jakarta.inject.Provider} easier. Do not use outside of Dagger implementation code.
*/
// TODO(erichang): Make this also extend the Jakarta Provider
public interface Provider<T> extends javax.inject.Provider<T> {
}
11 changes: 10 additions & 1 deletion java/dagger/internal/ProviderOfLazy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package dagger.internal;

import static dagger.internal.Preconditions.checkNotNull;
import static dagger.internal.Providers.asDaggerProvider;

import dagger.Lazy;
import javax.inject.Provider;

/**
* A {@link Provider} of {@link Lazy} instances that each delegate to a given {@link Provider}.
Expand Down Expand Up @@ -51,4 +51,13 @@ public Lazy<T> get() {
public static <T> Provider<Lazy<T>> create(Provider<T> provider) {
return new ProviderOfLazy<T>(checkNotNull(provider));
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public static <T> Provider<Lazy<T>> create(javax.inject.Provider<T> provider) {
return create(asDaggerProvider(provider));
}
}
35 changes: 35 additions & 0 deletions java/dagger/internal/Providers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2023 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dagger.internal;

import static dagger.internal.Preconditions.checkNotNull;

/** Helper class for utility functions dealing with Providers. */
public final class Providers {

/** Converts a javax provider to a Dagger internal provider. */
public static <T> Provider<T> asDaggerProvider(final javax.inject.Provider<T> provider) {
checkNotNull(provider);
return new Provider<T>() {
@Override public T get() {
return provider.get();
}
};
}

private Providers() {}
}
21 changes: 20 additions & 1 deletion java/dagger/internal/SetFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import static dagger.internal.DaggerCollections.newHashSetWithExpectedSize;
import static dagger.internal.DaggerCollections.presizedList;
import static dagger.internal.Preconditions.checkNotNull;
import static dagger.internal.Providers.asDaggerProvider;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.inject.Provider;

/**
* A {@link Factory} implementation used to implement {@link Set} bindings. This factory always
Expand Down Expand Up @@ -73,6 +73,15 @@ public Builder<T> addProvider(Provider<? extends T> individualProvider) {
return this;
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public Builder<T> addProvider(javax.inject.Provider<? extends T> individualProvider) {
return addProvider(asDaggerProvider(individualProvider));
}

@SuppressWarnings("unchecked")
public Builder<T> addCollectionProvider(
Provider<? extends Collection<? extends T>> collectionProvider) {
Expand All @@ -81,6 +90,16 @@ public Builder<T> addCollectionProvider(
return this;
}

/**
* Legacy javax version of the method to support libraries compiled with an older version of
* Dagger. Do not use directly.
*/
@Deprecated
public Builder<T> addCollectionProvider(
javax.inject.Provider<? extends Collection<? extends T>> collectionProvider) {
return addCollectionProvider(asDaggerProvider(collectionProvider));
}

public SetFactory<T> build() {
assert !hasDuplicates(individualProviders)
: "Codegen error? Duplicates in the provider list";
Expand Down
Loading
Loading