Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit da5208c

Browse files
committed
Further improve Java16 support
1 parent 17e456c commit da5208c

File tree

5 files changed

+43
-23
lines changed
  • simplixcore-common/simplixcore-common-api/src/main/java/dev/simplix/core/common/libloader
  • simplixcore-minecraft

5 files changed

+43
-23
lines changed

simplixcore-common/simplixcore-common-api/src/main/java/dev/simplix/core/common/libloader/SimplixClassLoader.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ public Class<?> loadClass(String name, boolean resolve, boolean useParent)
4545
throws ClassNotFoundException {
4646
try {
4747
return super.loadClass(name, resolve);
48-
} catch (ClassNotFoundException ignored) {
48+
} catch (Throwable ignored) {
4949
if (parentLoader != null && useParent) {
5050
try {
5151
return parentLoader.loadClass(name);
52-
} catch (ClassNotFoundException ignored1) {
53-
52+
} catch (Throwable ignored1) {
5453
}
5554
}
5655
}

simplixcore-minecraft/simplixcore-minecraft-bungeecord/simplixcore-minecraft-bungeecord-plugin/src/main/java/dev/simplix/core/minecraft/bungeecord/plugin/libloader/PluginClassLoaderFabricator.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
import java.lang.reflect.Method;
99
import java.net.URL;
1010
import java.net.URLClassLoader;
11-
import java.util.Arrays;
1211
import java.util.Set;
1312
import java.util.function.Function;
1413
import lombok.NonNull;
1514
import lombok.extern.slf4j.Slf4j;
1615
import net.md_5.bungee.api.ProxyServer;
17-
import net.md_5.bungee.api.plugin.Plugin;
1816
import net.md_5.bungee.api.plugin.PluginDescription;
1917

2018
@Slf4j
@@ -50,16 +48,21 @@ public ClassLoader apply(@NonNull File file) {
5048
PluginDescription pluginDescription = new PluginDescription();
5149
pluginDescription.setName("SimplixBridge");
5250

53-
final ClassLoader pluginClassloader = ProxyServer
51+
final ClassLoader simplixPluginClassLoader = ProxyServer
5452
.getInstance()
5553
.getPluginManager()
5654
.getPlugin("SimplixCore")
5755
.getClass()
5856
.getClassLoader();
5957

60-
final Method loadClass0 = pluginClassloader
58+
final Method loadClass0 = simplixPluginClassLoader
6159
.getClass()
62-
.getDeclaredMethod("loadClass0", String.class, boolean.class, boolean.class, boolean.class);
60+
.getDeclaredMethod(
61+
"loadClass0",
62+
String.class,
63+
boolean.class,
64+
boolean.class,
65+
boolean.class);
6366

6467
loadClass0.setAccessible(true);
6568

@@ -68,8 +71,8 @@ public ClassLoader apply(@NonNull File file) {
6871
@Override
6972
public Class<?> loadClass(String name) throws ClassNotFoundException {
7073
try {
71-
return (Class<?>) loadClass0.invoke(pluginClassloader, name, false, true, false);
72-
} catch (IllegalAccessException | InvocationTargetException e) {
74+
return (Class<?>) loadClass0.invoke(simplixPluginClassLoader, name, false, true, false);
75+
} catch (IllegalAccessException | InvocationTargetException reflectiveOperationException) {
7376
throw new ClassNotFoundException(name);
7477
}
7578
}

simplixcore-minecraft/simplixcore-minecraft-spigot/simplixcore-minecraft-spigot-implementation/src/main/java/dev/simplix/core/minecraft/spigot/util/ReflectionUtil.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ public final class ReflectionUtil {
3232

3333
public static Class<?> getClass(String classname) throws ClassNotFoundException {
3434
String path = classname
35-
.replace("{nms}", "net.minecraft.server." + serverVersion())
36-
.replace("{obc}", "org.bukkit.craftbukkit." + serverVersion())
37-
.replace("{nm}", "net.minecraft." + serverVersion());
35+
.replace(
36+
"{nm}",
37+
"net.minecraft" + (
38+
Bukkit.getBukkitVersion().startsWith("1.17")
39+
? ""
40+
: "." + serverVersion()))
41+
.replace(
42+
"{nms}",
43+
"net.minecraft.server" + (
44+
Bukkit.getBukkitVersion().startsWith("1.17")
45+
? ""
46+
: "." + serverVersion()))
47+
.replace("{obc}", "org.bukkit.craftbukkit." + serverVersion());
3848
Class<?> out = CACHED_CLASSES.get(path);
3949
if (out == null) {
4050
out = Class.forName(path);

simplixcore-minecraft/simplixcore-minecraft-spigot/simplixcore-minecraft-spigot-plugin/src/main/java/dev/simplix/core/minecraft/spigot/plugin/libloader/PluginClassLoaderFabricator.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
import dev.simplix.core.common.updater.Version;
66
import dev.simplix.core.minecraft.spigot.plugin.SimplixPlugin;
77
import java.io.File;
8-
import java.lang.reflect.Constructor;
9-
import java.lang.reflect.Field;
10-
import java.lang.reflect.Method;
11-
import java.lang.reflect.Modifier;
8+
import java.lang.reflect.*;
129
import java.net.URI;
1310
import java.net.URL;
1411
import java.net.URLClassLoader;
@@ -31,7 +28,7 @@
3128
public final class PluginClassLoaderFabricator implements Function<File, ClassLoader> {
3229

3330
private static ClassLoader cachedResult;
34-
private final Version javaVersion = Version.parse(System.getProperty("os.version"));
31+
private final Version javaVersion = Version.parse(System.getProperty("java.version"));
3532

3633
private void unfinalize(@NonNull Field loadersField)
3734
throws NoSuchFieldException, IllegalAccessException {
@@ -42,7 +39,6 @@ private void unfinalize(@NonNull Field loadersField)
4239

4340
@Override
4441
public ClassLoader apply(@NonNull File file) {
45-
4642
// We don't to create a SimplixClassLoader twice if on 1.16 & Java16
4743
if (cachedResult != null) {
4844
return cachedResult;
@@ -79,6 +75,7 @@ public ClassLoader apply(@NonNull File file) {
7975
out = (ClassLoader) pluginClassloader;
8076
} catch (Throwable throwable) {
8177
if (javaVersion.olderThen(Version.parse("16.0.0"))) {
78+
log.info("Used Java11 fabricator");
8279
// Spigot 1.16 or newer using old reflection
8380
Class<?> classLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
8481
Constructor<?> constructor = classLoaderClass.getDeclaredConstructor(
@@ -100,6 +97,7 @@ public ClassLoader apply(@NonNull File file) {
10097
);
10198
out = (ClassLoader) pluginClassloader;
10299
} else {
100+
log.info("Used Java16 fabricator");
103101
// Spigot 1.16 newer - Compatible with Java16
104102
Class<?> classLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
105103
Constructor<?> constructor = classLoaderClass.getDeclaredConstructor(
@@ -127,18 +125,28 @@ public ClassLoader apply(@NonNull File file) {
127125
boolean.class,
128126
boolean.class);
129127

128+
loadClass0.setAccessible(true);
129+
130130
ClassLoader parentLoader = new URLClassLoader(new URL[]{
131131
}) {
132132
@Override
133133
public Class<?> loadClass(String name) throws ClassNotFoundException {
134134
try {
135-
return (Class<?>) loadClass0.invoke(
136-
plugin.getPluginLoader(),
135+
final Object invoke = loadClass0.invoke(
136+
simplixCoreClassLoader,
137137
name,
138138
false,
139139
true,
140140
false);
141-
} catch (Exception exception) {
141+
return (Class<?>) invoke;
142+
} catch (IllegalAccessException reflectiveOperationException) {
143+
reflectiveOperationException.printStackTrace();
144+
throw new ClassNotFoundException(name);
145+
} catch (InvocationTargetException invocationTargetException) {
146+
if (invocationTargetException.getCause() instanceof ClassNotFoundException) {
147+
throw (ClassNotFoundException) invocationTargetException.getCause();
148+
}
149+
invocationTargetException.printStackTrace();
142150
throw new ClassNotFoundException(name);
143151
}
144152
}

simplixcore-minecraft/simplixcore-minecraft-spigot/simplixcore-minecraft-spigot-plugin/src/main/resources/fakeplugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ authors: [ SimplixSoftworks ]
44

55
main: dev.simplix.core.minecraft.spigot.fake.FakeJavaPlugin
66

7-
api-version: 1.16
7+
api-version: 1.16

0 commit comments

Comments
 (0)