-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support indirect dependencies and multibinder
- Loading branch information
Showing
7 changed files
with
241 additions
and
29 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
src/main/java/org/spongepowered/common/inject/plugin/BindingHelper.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,121 @@ | ||
/* | ||
* This file is part of Sponge, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.common.inject.plugin; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.inject.Binder; | ||
import com.google.inject.Binding; | ||
import com.google.inject.Injector; | ||
import com.google.inject.Key; | ||
import com.google.inject.spi.Element; | ||
import com.google.inject.spi.ElementSource; | ||
import com.google.inject.spi.ExposedBinding; | ||
import com.google.inject.spi.PrivateElements; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
|
||
final class BindingHelper { | ||
|
||
private final Binder binder; | ||
private final Map<Key<?>, Object> combinedKeys; | ||
|
||
BindingHelper(final Binder binder) { | ||
this.binder = binder; | ||
this.combinedKeys = new HashMap<>(); | ||
} | ||
|
||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
void bind() { | ||
for (final Map.Entry<Key<?>, Object> key : this.combinedKeys.entrySet()) { | ||
Object value = key.getValue(); | ||
if (value instanceof Set<?> set) { | ||
value = ImmutableSet.copyOf(set); | ||
} else if (value instanceof Iterable<?> iterable) { | ||
value = ImmutableList.copyOf(iterable); | ||
} | ||
this.binder.bind((Key) key.getKey()).toInstance(value); | ||
} | ||
} | ||
|
||
void bindFrom(final Injector fromInjector) { | ||
for (final Binding<?> binding : fromInjector.getBindings().values()) { | ||
if (!(binding.getSource() instanceof ElementSource)) { | ||
continue; | ||
} | ||
|
||
this.bind(fromInjector, binding); | ||
} | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
void bind(final Injector fromInjector, final Binding binding) { | ||
if (binding instanceof final ExposedBinding<?> exposedBinding) { | ||
final PrivateElements privateElements = exposedBinding.getPrivateElements(); | ||
for (final Element privateElement : privateElements.getElements()) { | ||
if (!(privateElement instanceof final Binding privateBinding) | ||
|| !privateElements.getExposedKeys().contains(privateBinding.getKey())) { | ||
continue; | ||
} | ||
|
||
this.bind(fromInjector, privateBinding); | ||
} | ||
|
||
return; | ||
} | ||
|
||
this.bind(fromInjector, binding.getKey()); | ||
} | ||
|
||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
private void bind(final Injector fromInjector, final Key key) { | ||
final Class<?> clazz = key.getTypeLiteral().getRawType(); | ||
if (Iterable.class.isAssignableFrom(clazz)) { | ||
final Collection destinationCollection; | ||
if (Set.class.isAssignableFrom(clazz)) { | ||
destinationCollection = this.getBindData(key, () -> new HashSet<>()); | ||
} else { | ||
destinationCollection = this.getBindData(key, () -> new ArrayList<>()); | ||
} | ||
final Iterable<Object> originalIterable = (Iterable<Object>) fromInjector.getInstance(key); | ||
for (final Object value : originalIterable) { | ||
destinationCollection.add(value); | ||
} | ||
} else { | ||
this.binder.bind(key).toProvider(() -> fromInjector.getInstance(key)); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private <T> T getBindData(final Key<T> key, final Supplier<T> newValueSupplier) { | ||
return (T) this.combinedKeys.computeIfAbsent(key, $ -> newValueSupplier.get()); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
...ugins/src/main/java/org/spongepowered/test/serviceloader/CombinableTestPluginService.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,28 @@ | ||
/* | ||
* This file is part of Sponge, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.test.serviceloader; | ||
|
||
public interface CombinableTestPluginService { | ||
} |
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
41 changes: 41 additions & 0 deletions
41
...ugins/src/main/java/org/spongepowered/test/serviceloader/SecondChildModuleTestPlugin.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,41 @@ | ||
/* | ||
* This file is part of Sponge, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.test.serviceloader; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.multibindings.Multibinder; | ||
import org.spongepowered.plugin.builtin.jvm.Plugin; | ||
|
||
@Plugin("secondchildmoduletestplugin") | ||
public final class SecondChildModuleTestPlugin implements CombinableTestPluginService { | ||
|
||
public static final class Module extends AbstractModule { | ||
|
||
@Override | ||
protected void configure() { | ||
Multibinder.newSetBinder(this.binder(), CombinableTestPluginService.class).addBinding().to(SecondChildModuleTestPlugin.class); | ||
} | ||
} | ||
} |
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