-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to extension system and add docs
- Loading branch information
1 parent
7744998
commit 7cb9fd5
Showing
31 changed files
with
295 additions
and
173 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
guice/src/main/java/com/github/kaktushose/jda/commands/guice/GuiceExtension.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,29 @@ | ||
package com.github.kaktushose.jda.commands.guice; | ||
|
||
import com.github.kaktushose.jda.commands.JDACommandsBuilder; | ||
import com.github.kaktushose.jda.commands.dispatching.instance.InstanceProvider; | ||
import com.github.kaktushose.jda.commands.extension.Extension; | ||
import com.github.kaktushose.jda.commands.guice.internal.GuiceInstanceProvider; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/// The implementation of [Extension] for using Google's [Guice] as an [InstanceProvider]. | ||
/// | ||
/// @see GuiceExtensionData | ||
public class GuiceExtension implements Extension<GuiceExtensionData> { | ||
|
||
@Override | ||
public void configure(@NotNull JDACommandsBuilder builder, GuiceExtensionData data) { | ||
Injector injector = data != null | ||
? data.providedInjector() | ||
: Guice.createInjector(); | ||
|
||
builder.instanceProvider(new GuiceInstanceProvider(injector, false)); | ||
} | ||
|
||
@Override | ||
public @NotNull Class<GuiceExtensionData> dataType() { | ||
return GuiceExtensionData.class; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
guice/src/main/java/com/github/kaktushose/jda/commands/guice/GuiceExtensionData.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,13 @@ | ||
package com.github.kaktushose.jda.commands.guice; | ||
|
||
import com.github.kaktushose.jda.commands.extension.Extension; | ||
import com.google.inject.Injector; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/// Custom [Extension.Data] to be used to configure this extension. | ||
/// @param providedInjector The [Injector] to be used instead of creating one | ||
public record GuiceExtensionData( | ||
@NotNull | ||
Injector providedInjector | ||
) implements Extension.Data { | ||
} |
11 changes: 0 additions & 11 deletions
11
guice/src/main/java/com/github/kaktushose/jda/commands/guice/GuiceInstantiatorData.java
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
...rc/main/java/com/github/kaktushose/jda/commands/guice/internal/GuiceInstanceProvider.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,32 @@ | ||
package com.github.kaktushose.jda.commands.guice.internal; | ||
|
||
import com.github.kaktushose.jda.commands.annotations.interactions.Interaction; | ||
import com.github.kaktushose.jda.commands.dispatching.instance.InstanceProvider; | ||
import com.google.inject.Injector; | ||
|
||
public class GuiceInstanceProvider implements InstanceProvider { | ||
|
||
private final Injector injector; | ||
private final boolean runtimeBound; | ||
|
||
public GuiceInstanceProvider(Injector injector, boolean runtimeBound) { | ||
this.injector = injector; | ||
this.runtimeBound = runtimeBound; | ||
} | ||
|
||
@Override | ||
public <T> T instance(Class<T> clazz, Context context) { | ||
if (!runtimeBound) { | ||
throw new UnsupportedOperationException("GuiceInstanceProvider must be used runtime bound!"); | ||
} | ||
|
||
return injector.getInstance(clazz); | ||
} | ||
|
||
/// Creates a new child injector with its own [JDACommandsModule] for each runtime. | ||
/// This has the effect, that each class annotated with [Interaction] will be treated as a runtime scoped singleton. | ||
@Override | ||
public InstanceProvider forRuntime(String id) { | ||
return new GuiceInstanceProvider(injector.createChildInjector(new JDACommandsModule()), true); | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
guice/src/main/java/com/github/kaktushose/jda/commands/guice/internal/GuiceInstantiator.java
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
...ain/java/com/github/kaktushose/jda/commands/guice/internal/GuiceInstantiatorProvider.java
This file was deleted.
Oops, something went wrong.
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
1 change: 0 additions & 1 deletion
1
...ces/com.github.kaktushose.jda.commands.dispatching.instantiation.spi.InstantiatorProvider
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...c/main/resources/META-INF/services/com.github.kaktushose.jda.commands.extension.Extension
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 @@ | ||
com.github.kaktushose.jda.commands.guice.GuiceExtension |
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
Oops, something went wrong.