1

I am using Guice's dependency injection at Java 16. It works fine at Java 11, but at Java 16, there has an error. Generating modules with the Guice's AbstractModule, there has an exception at getting external jar's class.

I already found the answer that's closest to the answer I want but I am using AbstractModule so I can't resolve it.

Unnamed module interaction with named module by ServiceLoader::load

I think naming modules will resolve this problem.

Here is my code:

package com.awooga.profiles;

import com.awooga.profiles.dao.PlayerProfilesDAO;
import com.awooga.profiles.dao.impl.PlayerProfilesDAOImpl;
import com.google.inject.*;
import lombok.SneakyThrows;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;

import javax.inject.Named;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Optional;

public class ProfilesPaperGuiModule extends AbstractModule {

    public static final String MYSQL_CONNECTION_STRING="MYSQL_CONNECTION_STRING";

    private final ProfilesPaperGuiPlugin plugin;

    // This is also dependency injection, but without any libraries/frameworks since we can't use those here yet.
    public ProfilesPaperGuiModule(ProfilesPaperGuiPlugin plugin) {
        this.plugin = plugin;
    }

    public Injector createInjector() {
        return Guice.createInjector(this);
    }

    @Override
    protected void configure() {
        // Here we tell Guice to use our plugin instance everytime we need it
        bind(ProfilesPaperGuiPlugin.class).toInstance(this.plugin);
        bind(Plugin.class).toInstance(this.plugin);

        bind(PlayerProfilesDAO.class).to(PlayerProfilesDAOImpl.class).in(Singleton.class);
        bind(ProfilesPlaceholderExpansion.class).in(Singleton.class);
        bind(ConfigurationDelegator.class).in(Singleton.class);
        bind(Configuration.class).to(ConfigurationDelegator.class);
    }

    //get instance at external jar
    @Provides
    @Singleton
    ProfilesPaperCoreSDK providePaperCoreSdk() {
        return ProfilesPaperCoreSDK.getInstance();
    }

    @Provides
    @Named(MYSQL_CONNECTION_STRING)
    String provideConnectionString() {
        return this.plugin.getConfig().getString("mysql.connector");
    }

    @SneakyThrows
    @Provides
    @Singleton
    Connection provideConnection(@Named(MYSQL_CONNECTION_STRING) String connStr) {
        return DriverManager.getConnection(connStr);
    }
}

And here is the error:

java.lang.ClassCastException: class com.awooga.profiles.ProfilesPaperCorePlugin cannot be cast to class com.awooga.profiles.ProfilesPaperCorePlugin (com.awooga.profiles.ProfilesPaperCorePlugin is in unnamed module of loader 'profiles-paper-core-1.0-SNAPSHOT-all.jar' @6b7ad2b3; com.awooga.profiles.ProfilesPaperCorePlugin is in unnamed module of loader 'myJar.jar' @547132f7)
       at myJar.jar//com.awooga.profiles.ProfilesPaperCoreSDK.getInstance(ProfilesPaperCoreSDK.java:29)
       at myJar.jar//com.awooga.profiles.ProfilesPaperGuiModule.providePaperCoreSdk(ProfilesPaperGuiModule.java:49)
       at myJar.jar//com.awooga.profiles.ProfilesPaperGuiModule$$FastClassByGuice$$2439651.GUICE$TRAMPOLINE(<generated>)
       at myJar.jar//com.awooga.profiles.ProfilesPaperGuiModule$$FastClassByGuice$$2439651.apply(<generated>)
       at myJar.jar//com.google.inject.internal.ProviderMethod$FastClassProviderMethod.doProvision(ProviderMethod.java:260)
       at myJar.jar//com.google.inject.internal.ProviderMethod.doProvision(ProviderMethod.java:171)
       at myJar.jar//com.google.inject.internal.InternalProviderInstanceBindingImpl$CyclicFactory.provision(InternalProviderInstanceBindingImpl.java:185)
       at myJar.jar//com.google.inject.internal.InternalProviderInstanceBindingImpl$CyclicFactory.get(InternalProviderInstanceBindingImpl.java:162)
       at myJar.jar//com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
       at myJar.jar//com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:169)
       at myJar.jar//com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
       at myJar.jar//com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:50)
       at myJar.jar//com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:146)
       at myJar.jar//com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:124)
       at myJar.jar//com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91)
       at myJar.jar//com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:296)
       at myJar.jar//com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:50)
       at myJar.jar//com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:146)
       at myJar.jar//com.google.inject.internal.MembersInjectorImpl.injectAndNotify(MembersInjectorImpl.java:101)
       at myJar.jar//com.google.inject.internal.Initializer$InjectableReference.get(Initializer.java:245)
       at myJar.jar//com.google.inject.internal.Initializer.injectAll(Initializer.java:140)
       at myJar.jar//com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:180)
       at myJar.jar//com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:113)
       at myJar.jar//com.google.inject.Guice.createInjector(Guice.java:87)
       at myJar.jar//com.google.inject.Guice.createInjector(Guice.java:69)
       at myJar.jar//com.google.inject.Guice.createInjector(Guice.java:59)
       at myJar.jar//com.awooga.profiles.ProfilesPaperGuiModule.createInjector(ProfilesPaperGuiModule.java:29)
       at myJar.jar//com.awooga.profiles.ProfilesPaperGuiPlugin.onEnable(ProfilesPaperGuiPlugin.java:44)
       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264)
       at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:370)
       at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:500)
       at org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugin(CraftServer.java:561)
       at org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugins(CraftServer.java:475)
       at net.minecraft.server.MinecraftServer.loadWorld(MinecraftServer.java:730)
       at net.minecraft.server.dedicated.DedicatedServer.init(DedicatedServer.java:317)
       at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1217)
       at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:319)
       at java.base/java.lang.Thread.run(Thread.java:831)
Elikill58
  • 4,050
  • 24
  • 23
  • 45
vjh0107
  • 107
  • 10

0 Answers0