在java中创建新的scala对象实例

时间:2014-03-08 19:17:15

标签: java class scala reflection scala-java-interop

我正在尝试创建一个系统,当用scala或java编写时,它将加载一个扩展Mod的类。我有类对象,我通过检查类是否具有@ScalaMod注释来检查类是否是用scala编写的。我用我编写的语言适配器处理不同的语言。但我一直收到这个错误。

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.6.0_65]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[?:1.6.0_65]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[?:1.6.0_65]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[?:1.6.0_65]
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:134) [launchwrapper-1.9.jar:?]
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.9.jar:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.6.0_65]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[?:1.6.0_65]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[?:1.6.0_65]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[?:1.6.0_65]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) [idea_rt.jar:?]
Caused by: java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method) ~[?:1.6.0_65]
    at java.lang.Class.forName(Class.java:249) ~[?:1.6.0_65]
    at net.acomputerdog.BlazeLoader.mod.language.IModLanguage$ScalaModLanguage.newInstance(IModLanguage.java:10) ~[IModLanguage$ScalaModLanguage.class:?]
    at net.acomputerdog.BlazeLoader.mod.ModList.load(ModList.java:62) ~[ModList.class:?]
    at net.acomputerdog.BlazeLoader.main.BlazeLoader.init(BlazeLoader.java:96) ~[BlazeLoader.class:?]
    at net.minecraft.client.Minecraft.run(Minecraft.java:781) ~[Minecraft.class:?]
    at net.minecraft.client.main.Main.main(Main.java:94) ~[Main.class:?]
    ... 11 more

这是语言适配器类:

public interface IModLanguage {
public Mod newInstance(Class<?> objectClass) throws Exception;

public static class ScalaModLanguage implements IModLanguage {
    public Mod newInstance(Class<?> objectClass) throws Exception{
        Class<?> scalaObject = Class.forName(objectClass.getName(), true, ScalaModLanguage.class.getClassLoader());
        return (Mod) scalaObject.getField("MODULE$").get(null);
    }
}

public static class JavaModLanguage implements IModLanguage {
    public Mod newInstance(Class<?> objectClass) throws Exception {
        return (Mod) objectClass.newInstance();
    }
}

这是scala Mod类:

    @ScalaMod
    object XplosionCoreBL extends Mod
{
    val xplosionModHandler: XplosionModHandler = new XplosionModHandler
    var newModVersions: ListBuffer[NewModVersionEntry] = new ListBuffer[NewModVersionEntry]()
    val xplosionConfig: ConfigHandler = new ConfigHandler(new File(ApiGeneral.configDir, "XplosionCore-BL.cfg"), this, "Everything deleted from this file will restore itself with its default value.")
    val logger: XplosionLogger = new XplosionLogger("XplosionCore-BL")

    override def getModId: String = "XplosionCore-BL"

    override def getModName: String = "XplosionCore-BL"
    override def getIntModVersion: Int = 0

    override def getStringModVersion: String = "1.0.0"

    override def isCompatibleWithBLVersion: Boolean = true

    override def getModDescription: String = "Core for all XplosionMods made with BlazeLoader."

    override def load
    {
        VersionCheckHandler.checkXplosionModsVersions();

        xplosionConfig.load();
    }

    override def start
    {
    }

    override def eventPlayerLogin(player: EntityPlayerMP)
    {
        EventHandler.onPlayerLogin(player)
    }

这是handeling代码:

Class<? extends Mod> cls = iterator.next();
        Mod mod = null;
        try {
            mod = getModLanguage(cls).newInstance(cls);

public static IModLanguage getModLanguage(Class<?> modClass) throws Exception {
    if (modClass.isAnnotationPresent(ScalaMod.class)) {
        return new IModLanguage.ScalaModLanguage();
    }
    else {
        return new IModLanguage.JavaModLanguage();
    }
}

0 个答案:

没有答案