MultiverseCore#getService should not instantiate services.

This commit is contained in:
Jeremy Wood 2023-03-11 11:24:24 -05:00
parent f7bd538049
commit 49c23d31b0
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B
4 changed files with 22 additions and 50 deletions

View File

@ -116,7 +116,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
// Init all the other stuff
// TODO consider moving this into the AnchorManager constructor
getService(AnchorManager.class).loadAnchors();
serviceLocator.getService(AnchorManager.class).loadAnchors();
this.registerEvents();
this.registerCommands();
this.setUpLocales();
@ -173,7 +173,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
*/
private void registerCommands() {
// TODO add automatic command registration through hk2
var commandManager = getService(MVCommandManager.class);
var commandManager = serviceLocator.getService(MVCommandManager.class);
getAllServices(MultiverseCommand.class).forEach(commandManager::registerCommand);
}
@ -181,7 +181,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
* Register locales
*/
private void setUpLocales() {
var commandManager = getService(MVCommandManager.class);
var commandManager = serviceLocator.getService(MVCommandManager.class);
commandManager.usePerIssuerLocale(true, true);
commandManager.getLocales().addFileResClassLoader(this);
@ -193,7 +193,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
*/
private void registerDestinations() {
// TODO add automatic destination registration through hk2
var destinationsProvider = getService(DestinationsProvider.class);
var destinationsProvider = serviceLocator.getService(DestinationsProvider.class);
getAllServices(Destination.class).forEach(destinationsProvider::registerDestination);
}
@ -203,7 +203,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
private void setupMetrics() {
if (TestingMode.isDisabled()) {
// Load metrics
getService(MetricsConfigurator.class);
serviceLocator.getService(MetricsConfigurator.class);
}
}
@ -389,13 +389,16 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
*
* @param contractOrImpl The contract or concrete implementation to get the best instance of
* @param qualifiers The set of qualifiers that must match this service definition
* @return An instance of the contract or impl. May return null if there is no provider that provides the given
* implementation or contract
* @throws MultiException if there was an error during service creation
* @return An instance of the contract or impl if it is a service and is already instantiated, null otherwise
* @throws MultiException if there was an error during lookup
*/
@Nullable
public <T> T getService(@NotNull Class<T> contractOrImpl, Annotation... qualifiers) throws MultiException {
return serviceLocator.getService(contractOrImpl, qualifiers);
var handle = serviceLocator.getServiceHandle(contractOrImpl, qualifiers);
if (handle != null && handle.isActive()) {
return handle.getService();
}
return null;
}
/**

View File

@ -7,14 +7,11 @@
package com.onarandombox.MultiverseCore.api;
import org.jvnet.hk2.annotations.Contract;
/**
* Multiverse 2 Core API
* <p>
* This API contains a bunch of useful things you can get out of Multiverse in general!
*/
@Contract
public interface MVCore extends MVPlugin {
/**

View File

@ -7,12 +7,9 @@
package com.onarandombox.MultiverseCore.api;
import org.jvnet.hk2.annotations.Contract;
/**
* This interface is implemented by every official Multiverse-plugin.
*/
@Contract
public interface MVPlugin {
/**
* Gets the reference to MultiverseCore.

View File

@ -1,12 +1,10 @@
package org.mvplugins.multiverse.core.inject
import com.onarandombox.MultiverseCore.MultiverseCore
import com.onarandombox.MultiverseCore.anchor.AnchorManager
import com.onarandombox.MultiverseCore.api.BlockSafety
import com.onarandombox.MultiverseCore.api.Destination
import com.onarandombox.MultiverseCore.api.LocationManipulation
import com.onarandombox.MultiverseCore.api.MVCore
import com.onarandombox.MultiverseCore.api.MVPlugin
import com.onarandombox.MultiverseCore.api.MVConfig
import com.onarandombox.MultiverseCore.api.MVWorldManager
import com.onarandombox.MultiverseCore.api.SafeTTeleporter
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager
@ -26,44 +24,14 @@ import com.onarandombox.MultiverseCore.teleportation.SimpleSafeTTeleporter
import com.onarandombox.MultiverseCore.utils.MVPermissions
import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper
import com.onarandombox.MultiverseCore.world.SimpleMVWorldManager
import org.bukkit.Server
import org.bukkit.plugin.PluginManager
import org.junit.jupiter.api.Test
import org.mvplugins.multiverse.core.TestWithMockBukkit
import java.util.logging.Logger
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertSame
import kotlin.test.Test
import kotlin.test.assertNull
class InjectionTest : TestWithMockBukkit() {
@Test
fun `Server is available as a service`() {
assertNotNull(multiverseCore.getService(Server::class.java))
}
@Test
fun `PluginManager is available as a service`() {
assertNotNull(multiverseCore.getService(PluginManager::class.java))
}
@Test
fun `MultiverseCore is available as a service`() {
assertNotNull(multiverseCore.getService(MultiverseCore::class.java))
assertNotNull(multiverseCore.getService(MVCore::class.java))
assertNotNull(multiverseCore.getService(MVPlugin::class.java))
}
@Test
fun `MultiverseCore service is the same instance of MultiverseCore that the MockBukkit server creates`() {
assertSame(multiverseCore, multiverseCore.getService(MultiverseCore::class.java));
}
@Test
fun `Logger is available as a service`() {
assertNotNull(multiverseCore.getService(Logger::class.java));
}
@Test
fun `AnchorManager is available as a service`() {
assertNotNull(multiverseCore.getService(AnchorManager::class.java))
@ -166,4 +134,11 @@ class InjectionTest : TestWithMockBukkit() {
// TODO come up with a better way to test this like via actually testing the effect of using each destination
assertEquals(6, destinations.size)
}
@Test
fun `MVConfig is not available as a service`() {
// We need one test case for asking for non-services to make sure we don't accidentally make them available
// and that the getService method doesn't throw an exception
assertNull(multiverseCore.getService(MVConfig::class.java))
}
}