Skip to content

Commit

Permalink
Fix issues related to InvUI#tryFindPlugin
Browse files Browse the repository at this point in the history
- Fixed an issue where tryFindPlugin would throw an exception if the paper plugin class loader class wasn't found
- Surrounded most logic in tryFindPlugin with try-catch to ensure the more informative exception in getPlugin to be thrown in case of failure
  • Loading branch information
NichtStudioCode committed Sep 22, 2023
1 parent 436f520 commit 39cc053
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public class ReflectionRegistry {
public static final int VERSION = getVersionNumber();

public static final String CRAFT_BUKKIT_PACKAGE_PATH = getCB();
public static final String BUKKIT_PACKAGE_PATH = "org.bukkit.";

// Classes
public static final Class<?> PLUGIN_CLASS_LOADER_CLASS = getBukkitClass("plugin.java.PluginClassLoader");
public static final Class<?> PLUGIN_CLASS_LOADER_CLASS = ReflectionUtils.getClass("org.bukkit.plugin.java.PluginClassLoader");
public static final Class<?> PAPER_PLUGIN_CLASS_LOADER_CLASS = getClassOrNull("io.papermc.paper.plugin.entrypoint.classloader.PaperPluginClassLoader");
public static final Class<?> CB_CRAFT_META_SKULL_CLASS = getCBClass("inventory.CraftMetaSkull");
public static final Class<?> CB_CRAFT_META_ITEM_CLASS = getCBClass("inventory.CraftMetaItem");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package xyz.xenondevs.inventoryaccess.util;

import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.xenondevs.inventoryaccess.version.InventoryAccessRevision;

import java.lang.reflect.Constructor;
Expand All @@ -10,7 +12,7 @@
@SuppressWarnings({"unchecked", "unused"})
public class ReflectionUtils {

protected static String getCB() {
protected static @NotNull String getCB() {
String path = Bukkit.getServer().getClass().getPackage().getName();
String version = path.substring(path.lastIndexOf(".") + 1);
return "org.bukkit.craftbukkit." + version + ".";
Expand All @@ -22,39 +24,35 @@ protected static int getVersionNumber() {
return Integer.parseInt(version.split("\\.")[1]);
}

public static <T> Class<T> getImplClass(String path) {
public static <T> @NotNull Class<T> getImplClass(@NotNull String path) {
try {
return (Class<T>) Class.forName("xyz.xenondevs.inventoryaccess." + InventoryAccessRevision.REQUIRED_REVISION.getPackageName() + "." + path);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}

public static <T> Class<T> getBukkitClass(String path) {
return getClass(ReflectionRegistry.BUKKIT_PACKAGE_PATH + path);
}

public static <T> Class<T> getCBClass(String path) {
public static <T> @NotNull Class<T> getCBClass(@NotNull String path) {
return getClass(ReflectionRegistry.CRAFT_BUKKIT_PACKAGE_PATH + path);
}

public static <T> Class<T> getClass(String path) {
public static <T> @NotNull Class<T> getClass(@NotNull String path) {
try {
return (Class<T>) Class.forName(path);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}

public static <T> Class<T> getClassOrNull(String path) {
public static <T> @Nullable Class<T> getClassOrNull(@NotNull String path) {
try {
return (Class<T>) Class.forName(path);
} catch (Throwable t) {
return null;
}
}

public static Field getField(Class<?> clazz, boolean declared, String name) {
public static @NotNull Field getField(@NotNull Class<?> clazz, boolean declared, @NotNull String name) {
try {
Field field = declared ? clazz.getDeclaredField(name) : clazz.getField(name);
if (declared) field.setAccessible(true);
Expand All @@ -64,7 +62,7 @@ public static Field getField(Class<?> clazz, boolean declared, String name) {
}
}

public static <T> Constructor<T> getConstructor(Class<T> clazz, boolean declared, Class<?>... parameterTypes) {
public static <T> @NotNull Constructor<T> getConstructor(@NotNull Class<T> clazz, boolean declared, @NotNull Class<?> @NotNull... parameterTypes) {
try {
Constructor<T> constructor = declared ? clazz.getDeclaredConstructor(parameterTypes) : clazz.getConstructor(parameterTypes);
if (declared) constructor.setAccessible(true);
Expand All @@ -74,23 +72,23 @@ public static <T> Constructor<T> getConstructor(Class<T> clazz, boolean declared
}
}

public static <T> T constructEmpty(Class<?> clazz) {
public static <T> @NotNull T constructEmpty(@NotNull Class<?> clazz) {
try {
return (T) getConstructor(clazz, true).newInstance();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}

public static <T> T construct(Constructor<T> constructor, Object... args) {
public static <T> @NotNull T construct(@NotNull Constructor<T> constructor, @Nullable Object @Nullable... args) {
try {
return constructor.newInstance(args);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}

public static Method getMethod(Class<?> clazz, boolean declared, String name, Class<?>... parameterTypes) {
public static @NotNull Method getMethod(@NotNull Class<?> clazz, boolean declared, @NotNull String name, @NotNull Class<?>@NotNull ... parameterTypes) {
try {
Method method = declared ? clazz.getDeclaredMethod(name, parameterTypes) : clazz.getMethod(name, parameterTypes);
if (declared) method.setAccessible(true);
Expand All @@ -100,7 +98,7 @@ public static Method getMethod(Class<?> clazz, boolean declared, String name, Cl
}
}

public static Method getMethodOrNull(Class<?> clazz, boolean declared, String name, Class<?>... parameterTypes) {
public static @Nullable Method getMethodOrNull(@NotNull Class<?> clazz, boolean declared, @NotNull String name, @NotNull Class<?> @NotNull... parameterTypes) {
try {
Method method = declared ? clazz.getDeclaredMethod(name, parameterTypes) : clazz.getMethod(name, parameterTypes);
if (declared) method.setAccessible(true);
Expand All @@ -110,15 +108,15 @@ public static Method getMethodOrNull(Class<?> clazz, boolean declared, String na
}
}

public static <T> T invokeMethod(Method method, Object obj, Object... args) {
public static <T> T invokeMethod(@NotNull Method method, @Nullable Object obj, @Nullable Object @Nullable... args) {
try {
return (T) method.invoke(obj, args);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}

public static void setFieldValue(Field field, Object obj, Object value) {
public static void setFieldValue(@NotNull Field field, @Nullable Object obj, @Nullable Object value) {
try {
field.set(obj, value);
} catch (Throwable t) {
Expand All @@ -127,7 +125,7 @@ public static void setFieldValue(Field field, Object obj, Object value) {
}

@SuppressWarnings("unchecked")
public static <T> T getFieldValue(Field field, Object obj) {
public static <T> @Nullable T getFieldValue(@NotNull Field field, @Nullable Object obj) {
try {
return (T) field.get(obj);
} catch (Throwable t) {
Expand Down
16 changes: 9 additions & 7 deletions invui-core/src/main/java/xyz/xenondevs/invui/InvUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.xenondevs.inventoryaccess.util.ReflectionRegistry;
import xyz.xenondevs.inventoryaccess.util.ReflectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import static xyz.xenondevs.inventoryaccess.util.ReflectionRegistry.PAPER_PLUGIN_CLASS_LOADER_GET_LOADED_JAVA_PLUGIN_METHOD;
import static xyz.xenondevs.inventoryaccess.util.ReflectionRegistry.PLUGIN_CLASS_LOADER_PLUGIN_FIELD;
import static xyz.xenondevs.inventoryaccess.util.ReflectionRegistry.*;

public class InvUI implements Listener {

Expand Down Expand Up @@ -45,10 +43,14 @@ private InvUI() {
private @Nullable Plugin tryFindPlugin() {
ClassLoader loader = getClass().getClassLoader();

if (ReflectionRegistry.PLUGIN_CLASS_LOADER_CLASS.isInstance(loader)) {
return ReflectionUtils.getFieldValue(PLUGIN_CLASS_LOADER_PLUGIN_FIELD, loader);
} else if (ReflectionRegistry.PAPER_PLUGIN_CLASS_LOADER_CLASS.isInstance(loader)) {
return ReflectionUtils.invokeMethod(PAPER_PLUGIN_CLASS_LOADER_GET_LOADED_JAVA_PLUGIN_METHOD, loader);
try {
if (PLUGIN_CLASS_LOADER_CLASS.isInstance(loader)) {
return ReflectionUtils.getFieldValue(PLUGIN_CLASS_LOADER_PLUGIN_FIELD, loader);
} else if (PAPER_PLUGIN_CLASS_LOADER_CLASS != null && PAPER_PLUGIN_CLASS_LOADER_CLASS.isInstance(loader)) {
return ReflectionUtils.invokeMethod(PAPER_PLUGIN_CLASS_LOADER_GET_LOADED_JAVA_PLUGIN_METHOD, loader);
}
} catch (Throwable t) {
t.printStackTrace();
}

return null;
Expand Down

0 comments on commit 39cc053

Please sign in to comment.