我使用以下代码来实例化包中的每个类:
for (Class listenerClass : ClassGetter.getClassesForPackage(this, "cullan.listener")) {
Bukkit.getLogger().info(listenerClass.getSimpleName());
if (!listenerClass.getSimpleName().equals("EnchantmentListener")) {
try {
listenerClass.newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
这很好用,但是,当其中一个类被实例化时,我收到了这个错误:
[07:07:58] [Server thread/WARN]: java.lang.InstantiationException: cullan.listener.AgilityListener$1
[07:07:58] [Server thread/WARN]: at java.lang.Class.newInstance(Unknown Source)
[07:07:58] [Server thread/WARN]: at cullan.main.DCEnchants.loadEnchants(DCEnchants.java:62)
[07:07:58] [Server thread/WARN]: at cullan.main.DCEnchants.onEnable(DCEnchants.java:24)
[07:07:58] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321)
[07:07:58] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340)
[07:07:58] [Server thread/WARN]: at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405)
[07:07:58] [Server thread/WARN]: at net.skycraftmc.PluginManager.PluginManagerPlugin.onCommand(PluginManagerPlugin.java:287)
[07:07:58] [Server thread/WARN]: at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
[07:07:58] [Server thread/WARN]: at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
[07:07:58] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:646)
[07:07:58] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:632)
[07:07:58] [Server thread/WARN]: at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:409)
[07:07:58] [Server thread/WARN]: at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:373)
[07:07:58] [Server thread/WARN]: at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:709)
[07:07:58] [Server thread/WARN]: at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:612)
[07:07:58] [Server thread/WARN]: at java.lang.Thread.run(Unknown Source)
[07:07:58] [Server thread/WARN]: Caused by: java.lang.NoSuchMethodException: cullan.listener.AgilityListener$1.<init>()
[07:07:58] [Server thread/WARN]: at java.lang.Class.getConstructor0(Unknown Source)
这是抛出错误的类:
package cullan.listener;
import cullan.type.EnchantmentRarity;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import java.util.ArrayList;
import java.util.List;
public class AgilityListener extends EnchantmentListener{
private List<Player> players = new ArrayList<>();
public AgilityListener() {
super(Enchantment.getByName("Agility"), EnchantmentRarity.VERY_RARE);
startTimer();
}
@EventHandler
public void onQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
if (players.contains(player)) {
players.remove(player);
}
}
private void startTimer() {
final PotionEffect effect = mainConfig.getAgilityEffect();
instance.getServer().getScheduler().scheduleSyncRepeatingTask(instance, new Runnable() {
@Override
public void run() {
for (Player player : instance.getServer().getOnlinePlayers()) {
ItemStack boots = player.getInventory().getBoots();
if (boots != null && boots.containsEnchantment(enchantment)) {
player.addPotionEffect(effect);
players.add(player);
} else if (players.contains(player)) {
player.removePotionEffect(effect.getType());
players.remove(player);
}
}
}
}, 10L, 10L);
}
}
如果删除startTimer()方法,则不会发生错误。为什么会这样?
答案 0 :(得分:3)
问题是你没有尝试构造AgilityListener
的实例 - 你正试图构造一个在AgilityListener.startTimer
中创建的匿名内部类的实例。这就是堆栈跟踪的原因:
Caused by: java.lang.NoSuchMethodException: cullan.listener.AgilityListener$1.<init>()
“$ 1”表示它是一个匿名的内部类。
如果您只想过滤掉所有嵌套/内部类,可以扩展对类名的检查:
if (!listenerClass.getSimpleName().equals("EnchantmentListener") &&
!listenerClass.getSimpleName().contains("$"))
或者:
!listenerClass.isAnonymousClass()
等。您还可以检查该类是否具有公共无参数构造函数,并且可以将其分配给EnchantmentListener
。