-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for jakarta providers. These can be used interchangeably with their native and javax.inject counterparts.
- Loading branch information
Showing
26 changed files
with
1,066 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
core/src/com/google/inject/internal/BoundJeeProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (C) 2022 Google Inc. | ||
* | ||
* 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 com.google.inject.internal; | ||
|
||
import com.google.inject.Key; | ||
import com.google.inject.internal.InjectorImpl.JitLimitation; | ||
import com.google.inject.spi.Dependency; | ||
import jakarta.inject.Provider; | ||
|
||
/** | ||
* Delegates to a custom factory which is also bound in the injector. | ||
*/ | ||
final class BoundJeeProviderFactory<T> extends JeeProviderInternalFactory<T> implements | ||
CreationListener { | ||
|
||
final Key<? extends Provider<? extends T>> providerKey; | ||
private final ProvisionListenerStackCallback<T> provisionCallback; | ||
private final InjectorImpl injector; | ||
private InternalFactory<? extends Provider<? extends T>> providerFactory; | ||
|
||
BoundJeeProviderFactory( | ||
InjectorImpl injector, | ||
Key<? extends Provider<? extends T>> providerKey, | ||
Object source, | ||
ProvisionListenerStackCallback<T> provisionCallback) { | ||
super(source); | ||
this.provisionCallback = provisionCallback; | ||
this.injector = injector; | ||
this.providerKey = providerKey; | ||
} | ||
|
||
@Override | ||
public void notify(Errors errors) { | ||
try { | ||
providerFactory = | ||
injector.getInternalFactory( | ||
providerKey, errors.withSource(source), JitLimitation.NEW_OR_EXISTING_JIT); | ||
} catch (ErrorsException e) { | ||
errors.merge(e.getErrors()); | ||
} | ||
} | ||
|
||
@Override | ||
public T get(InternalContext context, Dependency<?> dependency, boolean linked) | ||
throws InternalProvisionException { | ||
try { | ||
Provider<? extends T> provider = providerFactory.get(context, dependency, true); | ||
return circularGet(provider, context, dependency, provisionCallback); | ||
} catch (InternalProvisionException ipe) { | ||
throw ipe.addSource(providerKey); | ||
} | ||
} | ||
|
||
@Override | ||
protected T provision( | ||
Provider<? extends T> provider, | ||
Dependency<?> dependency, | ||
ConstructionContext<T> constructionContext) | ||
throws InternalProvisionException { | ||
try { | ||
return super.provision(provider, dependency, constructionContext); | ||
} catch (RuntimeException userException) { | ||
throw InternalProvisionException.errorInProvider(userException); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return providerKey.toString(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
core/src/com/google/inject/internal/InternalFactoryToJeeInitializableAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (C) 2022 Google Inc. | ||
* | ||
* 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 com.google.inject.internal; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.inject.spi.Dependency; | ||
import com.google.inject.spi.ProviderInstanceBinding; | ||
|
||
/** | ||
* Adapts {@link ProviderInstanceBinding} providers, ensuring circular proxies fail (or proxy) | ||
* properly. | ||
*/ | ||
final class InternalFactoryToJeeInitializableAdapter<T> extends JeeProviderInternalFactory<T> { | ||
|
||
private final ProvisionListenerStackCallback<T> provisionCallback; | ||
private final Initializable<? extends jakarta.inject.Provider<? extends T>> initializable; | ||
|
||
public InternalFactoryToJeeInitializableAdapter( | ||
Initializable<? extends jakarta.inject.Provider<? extends T>> initializable, | ||
Object source, | ||
ProvisionListenerStackCallback<T> provisionCallback) { | ||
super(source); | ||
this.provisionCallback = provisionCallback; | ||
this.initializable = checkNotNull(initializable, "provider"); | ||
} | ||
|
||
@Override | ||
public T get(InternalContext context, Dependency<?> dependency, boolean linked) | ||
throws InternalProvisionException { | ||
return circularGet(initializable.get(), context, dependency, provisionCallback); | ||
} | ||
|
||
@Override | ||
protected T provision( | ||
jakarta.inject.Provider<? extends T> provider, | ||
Dependency<?> dependency, | ||
ConstructionContext<T> constructionContext) | ||
throws InternalProvisionException { | ||
try { | ||
return super.provision(provider, dependency, constructionContext); | ||
} catch (RuntimeException userException) { | ||
throw InternalProvisionException.errorInProvider(userException).addSource(source); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return initializable.toString(); | ||
} | ||
} |
Oops, something went wrong.