Java newInstance抛出错误,它不应该

时间:2017-07-18 23:46:45

标签: java bukkit

我目前正在为自己编写一个私有Java API,它使用Class来允许扩展API的某个部分(统计信息保留部分),而不会更改基础用法。我喜欢这种方法,但由于某种原因,每当API尝试执行theClass.newInstance()时,它都会抛出错误:

java.lang.IllegalAccessError: tried to access method me.duper51.DuperFramework.utils.player.StatsObject.<init>()V from class me.duper51.Skyrings.stats.SkyringsStats
    at me.duper51.Skyrings.stats.SkyringsStats.<init>(SkyringsStats.java:14) ~[?:?]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_131]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_131]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_131]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_131]
    at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_131]
    at me.duper51.DuperFramework.utils.player.PlaygroundPlayer.getStats(PlaygroundPlayer.java:71) ~[?:?]
    at me.duper51.Skyrings.listeners.GameListener.lambda$getReplacements$0(GameListener.java:65) ~[?:?]
    at me.duper51.DuperFramework.utils.scoreboard.SidebarOption.update(SidebarOption.java:44) ~[?:?]
    at me.duper51.DuperFramework.utils.scoreboard.SidebarOption.update(SidebarOption.java:53) ~[?:?]
    at me.duper51.DuperFramework.utils.scoreboard.SidebarManager.updateAll(SidebarManager.java:75) ~[?:?]
    at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]

其中me.duper51.Skyrings.stats.SkyringsStats是扩展类,me.duper51.DuperFramework.utils.player.StatsObject是基类。

扩展对象如下所示:

public class SkyringsStats extends StatsObject {
public SkyringsStats() {
    super();
}

private int kills = 0;
private int wins = 0;
private int totalPlays = 0;
private int deaths = 0;
public void incrementKills() {
    kills++;
}
public void incrementDeaths() {
    deaths++;
}
public void incrementTotalPlays() {
    totalPlays++;
}
public void incrementWins() {
    wins++;
}

public int getKills() {
    return kills;
}

public int getWins() {
    return kills;
}

public int getDeaths() {
    return kills;
}

public int getGamesPlayed() {
    return kills;
}

}

根据另一个答案的要求,这是javap -p me.duper51.DuperFramework.utils.player.StatsObject

的输出
Compiled from "StatsObject.java"
public class me.duper51.DuperFramework.utils.player.StatsObject {
  private java.util.UUID pUUID;
  public me.duper51.DuperFramework.utils.player.StatsObject();
  void setpUUID(java.util.UUID);
  public void commitChanges(me.duper51.DuperFramework.GamePlugin);
  private static void lambda$commitChanges$0();
}

2 个答案:

答案 0 :(得分:1)

StatsObject的默认构造函数是私有的或以其他方式无法访问,并且必须已经编译,因为此代码已编译。

答案 1 :(得分:0)

就我而言,我使用Jenkins作为构建此应用程序的方法。然后将JAR加载到另一个应用程序中。 DuperAPI软件包有多个版本,第一个要加载的JAR具有优势。这导致了差异并最终导致错误。我调整了着色系统,使其不包含上游API不需要的文件。