From 3aa31391bef59a73b5fc7131d3c40ac32400bd3a Mon Sep 17 00:00:00 2001 From: Jikoo Date: Thu, 27 Jan 2022 10:03:12 -0500 Subject: [PATCH] Add trace to InstantiationException Closes #59 --- .../lishid/openinv/util/InternalAccessor.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/plugin/src/main/java/com/lishid/openinv/util/InternalAccessor.java b/plugin/src/main/java/com/lishid/openinv/util/InternalAccessor.java index 090f5cf..399154a 100644 --- a/plugin/src/main/java/com/lishid/openinv/util/InternalAccessor.java +++ b/plugin/src/main/java/com/lishid/openinv/util/InternalAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011-2021 lishid. All rights reserved. + * Copyright (C) 2011-2022 lishid. All rights reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,11 +19,13 @@ package com.lishid.openinv.util; import com.lishid.openinv.internal.IAnySilentContainer; import com.lishid.openinv.internal.IPlayerDataManager; import com.lishid.openinv.internal.ISpecialEnderChest; +import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.ISpecialPlayerInventory; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; public class InternalAccessor { @@ -134,6 +136,23 @@ public class InternalAccessor { throw new IllegalArgumentException(message); } + private T createSpecialInventory( + @NotNull Class assignableClass, + @NotNull String className, + @NotNull Player player, + boolean online) throws InstantiationException { + if (!this.supported) { + throw new IllegalStateException(String.format("Unsupported server version %s!", this.version)); + } + try { + return this.createObject(assignableClass, className, player, online); + } catch (Exception original) { + InstantiationException exception = new InstantiationException(String.format("Unable to create a new %s", className)); + exception.initCause(original.fillInStackTrace()); + throw exception; + } + } + /** * Creates an instance of the IAnySilentContainer implementation for the current server version. * @@ -189,14 +208,7 @@ public class InternalAccessor { * @throws InstantiationException if the ISpecialEnderChest could not be instantiated */ public ISpecialEnderChest newSpecialEnderChest(final Player player, final boolean online) throws InstantiationException { - if (!this.supported) { - throw new IllegalStateException(String.format("Unsupported server version %s!", this.version)); - } - try { - return this.createObject(ISpecialEnderChest.class, "SpecialEnderChest", player, online); - } catch (Exception e) { - throw new InstantiationException(String.format("Unable to create a new ISpecialEnderChest: %s", e.getMessage())); - } + return this.createSpecialInventory(ISpecialEnderChest.class, "SpecialEnderChest", player, online); } /** @@ -208,14 +220,7 @@ public class InternalAccessor { * @throws InstantiationException if the ISpecialPlayerInventory could not be instantiated */ public ISpecialPlayerInventory newSpecialPlayerInventory(final Player player, final boolean online) throws InstantiationException { - if (!this.supported) { - throw new IllegalStateException(String.format("Unsupported server version %s!", this.version)); - } - try { - return this.createObject(ISpecialPlayerInventory.class, "SpecialPlayerInventory", player, online); - } catch (Exception e) { - throw new InstantiationException(String.format("Unable to create a new ISpecialPlayerInventory: %s", e.getMessage())); - } + return this.createSpecialInventory(ISpecialPlayerInventory.class, "SpecialPlayerInventory", player, online); } }