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

Commit 4164d22

Browse files
Merge pull request #15 from Simplix-Softworks/java16
Java16
2 parents d7a87bc + cacb54c commit 4164d22

File tree

8 files changed

+284
-61
lines changed

8 files changed

+284
-61
lines changed

simplixcore-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<dependency>
2525
<groupId>com.google.inject</groupId>
2626
<artifactId>guice</artifactId>
27-
<version>4.1.0</version>
27+
<version>5.0.1</version>
2828
</dependency>
2929
<dependency>
3030
<groupId>com.google.inject.extensions</groupId>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package dev.simplix.core.common.libloader;
2+
3+
import java.net.URL;
4+
import java.net.URLClassLoader;
5+
import java.net.URLStreamHandlerFactory;
6+
7+
public class SimplixClassLoader extends URLClassLoader {
8+
9+
private final ClassLoader parentLoader;
10+
11+
public SimplixClassLoader(URL[] urls, ClassLoader parent) {
12+
super(urls);
13+
this.parentLoader = parent;
14+
}
15+
16+
public SimplixClassLoader(URL[] urls) {
17+
super(urls);
18+
this.parentLoader = null;
19+
}
20+
21+
public SimplixClassLoader(
22+
URL[] urls,
23+
ClassLoader parent,
24+
URLStreamHandlerFactory factory) {
25+
super(urls, null, factory);
26+
this.parentLoader = parent;
27+
}
28+
29+
@Override
30+
public void addURL(URL url) {
31+
super.addURL(url);
32+
}
33+
34+
@Override
35+
public Class<?> loadClass(String name) throws ClassNotFoundException {
36+
return loadClass(name, false);
37+
}
38+
39+
@Override
40+
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
41+
return loadClass(name, resolve, true);
42+
}
43+
44+
public Class<?> loadClass(String name, boolean resolve, boolean useParent)
45+
throws ClassNotFoundException {
46+
try {
47+
return super.loadClass(name, resolve);
48+
} catch (Throwable ignored) {
49+
if (parentLoader != null && useParent) {
50+
try {
51+
return parentLoader.loadClass(name);
52+
} catch (Throwable ignored1) {
53+
}
54+
}
55+
}
56+
throw new ClassNotFoundException(name);
57+
}
58+
}

simplixcore-common/simplixcore-common-implementation/src/main/java/dev/simplix/core/common/libloader/SimpleLibraryLoader.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.gson.GsonBuilder;
55
import dev.simplix.core.common.aop.SimplixApplication;
66
import dev.simplix.core.common.inject.SimplixInstaller;
7+
import dev.simplix.core.common.updater.Version;
78
import java.io.File;
89
import java.io.IOException;
910
import java.io.InputStream;
@@ -22,19 +23,16 @@
2223

2324
public class SimpleLibraryLoader implements LibraryLoader {
2425

26+
private final Version javaVersion;
2527
private final Logger log;
2628
private final Gson gson = new GsonBuilder().create();
2729
private final Set<File> files = new HashSet<>();
2830
private Method addMethod;
2931

3032
public SimpleLibraryLoader(@NonNull Logger log) {
3133
this.log = log;
32-
try {
33-
addMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
34-
addMethod.setAccessible(true);
35-
} catch (Exception exception) {
36-
log.error("Cannot initialize LibraryLoader", exception);
37-
}
34+
javaVersion = Version.parse(System.getProperty("java.version"));
35+
3836
}
3937

4038
@Override
@@ -59,6 +57,7 @@ public void loadLibrary(File file) {
5957
return;
6058
}
6159
log.info("[Simplix | LibLoader] Loaded library " + file.getName());
60+
addUrlToClassLoader((URLClassLoader) classLoader, file);
6261
checkAndLoadSimplixApplication(file, classLoader);
6362
} catch (Exception ex) {
6463
log.info("[Simplix | LibLoader] Unable to load library " + file.getName(), ex);
@@ -71,7 +70,14 @@ public void loadLibraryEncapsulated(@NonNull File file, @NotNull Class<?> owner)
7170
if (files.contains(file)) {
7271
return;
7372
}
74-
ClassLoader classLoader = owner.getClassLoader();
73+
ClassLoader classLoader;
74+
75+
if (javaVersion.olderThen(Version.parse("16.0.0"))) {
76+
classLoader = owner.getClassLoader();
77+
} else {
78+
classLoader = createClassLoader(file);
79+
}
80+
7581
if (!(classLoader instanceof URLClassLoader)) {
7682
log.warn("[Simplix | LibLoader] "
7783
+ owner.getSimpleName()
@@ -89,6 +95,7 @@ public void loadLibraryEncapsulated(@NonNull File file, @NotNull Class<?> owner)
8995
} catch (Exception ex) {
9096
log.info("[Simplix | LibLoader] Unable to load encapsulated library " + file.getName() +
9197
" for application " + owner.getSimpleName(), ex);
98+
9299
}
93100
}
94101

@@ -131,6 +138,16 @@ private void checkAndLoadSimplixApplication(@NonNull File file, ClassLoader clas
131138

132139
private void addUrlToClassLoader(URLClassLoader classLoader, File file)
133140
throws ReflectiveOperationException, MalformedURLException {
141+
142+
if (classLoader instanceof SimplixClassLoader) {
143+
((SimplixClassLoader) classLoader).addURL(file.toURI().toURL());
144+
return;
145+
}
146+
147+
if (addMethod == null) {
148+
addMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
149+
addMethod.setAccessible(true);
150+
}
134151
addMethod.invoke(classLoader, file.toURI().toURL());
135152
}
136153

simplixcore-minecraft/simplixcore-minecraft-bungeecord/simplixcore-minecraft-bungeecord-plugin/pom.xml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,24 @@
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

12-
<artifactId>simplixcore-minecraft-bungeecord-plugin</artifactId>
13-
<version>1.0.${build.number}</version>
14-
1512
<repositories>
1613
<repository>
17-
<id>BungeeCord</id>
18-
<url>https://oss.sonatype.org/content/groups/public/</url>
14+
<id>simplixsoft-public</id>
15+
<url>https://repo.simplix.dev/repository/simplixsoft-public/</url>
1916
</repository>
2017
</repositories>
2118

19+
<artifactId>simplixcore-minecraft-bungeecord-plugin</artifactId>
20+
<version>1.0.${build.number}</version>
21+
2222
<dependencies>
23+
<dependency>
24+
<groupId>dev.simplix</groupId>
25+
<artifactId>bungee</artifactId>
26+
<version>1.0.0</version>
27+
<scope>provided</scope>
28+
</dependency>
29+
2330
<!-- Simple logging facade for java -->
2431
<dependency>
2532
<groupId>dev.simplix.core</groupId>

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

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package dev.simplix.core.minecraft.bungeecord.plugin.libloader;
22

3+
import dev.simplix.core.common.libloader.SimplixClassLoader;
34
import java.io.File;
45
import java.lang.reflect.Constructor;
56
import java.lang.reflect.Field;
6-
import java.lang.reflect.Modifier;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.lang.reflect.Method;
9+
import java.net.URL;
10+
import java.net.URLClassLoader;
711
import java.util.Set;
812
import java.util.function.Function;
913
import lombok.NonNull;
@@ -14,16 +18,22 @@
1418
@Slf4j
1519
public class PluginClassLoaderFabricator implements Function<File, ClassLoader> {
1620

17-
private void unfinalize(@NonNull Field loadersField)
18-
throws NoSuchFieldException, IllegalAccessException {
19-
Field modifiersField = Field.class.getDeclaredField("modifiers");
20-
modifiersField.setAccessible(true);
21-
modifiersField.set(loadersField, loadersField.getModifiers() & ~Modifier.FINAL);
22-
}
21+
private static ClassLoader result = null;
22+
23+
// private void unfinalize(@NonNull Field loadersField)
24+
// throws NoSuchFieldException, IllegalAccessException {
25+
// Field modifiersField = Field.class.getDeclaredField("modifiers");
26+
// modifiersField.setAccessible(true);
27+
// modifiersField.set(loadersField, loadersField.getModifiers() & ~Modifier.FINAL);
28+
// }
2329

2430
@Override
2531
public ClassLoader apply(@NonNull File file) {
2632

33+
if (result != null) {
34+
return result;
35+
}
36+
2737
try {
2838
Class<?> classLoaderClass = Class.forName("net.md_5.bungee.api.plugin.PluginClassloader",
2939
false, ClassLoader.getSystemClassLoader());
@@ -35,28 +45,59 @@ public ClassLoader apply(@NonNull File file) {
3545
);
3646
constructor.setAccessible(true);
3747

48+
PluginDescription pluginDescription = new PluginDescription();
49+
pluginDescription.setName("SimplixBridge");
3850

51+
final ClassLoader simplixPluginClassLoader = ProxyServer
52+
.getInstance()
53+
.getPluginManager()
54+
.getPlugin("SimplixCore")
55+
.getClass()
56+
.getClassLoader();
3957

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

41-
PluginDescription pluginDescription = new PluginDescription();
67+
loadClass0.setAccessible(true);
4268

69+
ClassLoader parentLoader = new URLClassLoader(new URL[]{
70+
}) {
71+
@Override
72+
public Class<?> loadClass(String name) throws ClassNotFoundException {
73+
try {
74+
return (Class<?>) loadClass0.invoke(simplixPluginClassLoader, name, false, true, false);
75+
} catch (IllegalAccessException | InvocationTargetException reflectiveOperationException) {
76+
throw new ClassNotFoundException(name);
77+
}
78+
}
79+
};
80+
81+
final SimplixClassLoader simplixClassLoader = new SimplixClassLoader(
82+
new URL[0],
83+
parentLoader);
4384
Object loader = constructor.newInstance(
4485
ProxyServer.getInstance(),
4586
pluginDescription,
4687
file,
47-
ClassLoader.getSystemClassLoader()
88+
simplixClassLoader
4889
);
4990

5091
Field loadersField = classLoaderClass.getDeclaredField("allLoaders");
5192
loadersField.setAccessible(true);
52-
if (Modifier.isFinal(loadersField.getModifiers())) {
53-
unfinalize(loadersField);
54-
}
5593

5694
Set<Object> loaders = (Set<Object>) loadersField.get(null);
5795
loaders.add(loader);
58-
loadersField.set(null, loaders);
59-
return (ClassLoader) loader;
96+
// loadersField.set(null, loaders);
97+
98+
// ProxyServer.getInstance().getPluginManager().
99+
result = simplixClassLoader;
100+
return simplixClassLoader;
60101
} catch (Exception exception) {
61102
log.error("[Simplix | LibLoader] Cannot fabricate PluginClassLoader", exception);
62103
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,21 @@ 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);
49+
3950
if (out == null) {
4051
out = Class.forName(path);
4152
CACHED_CLASSES.put(path, out);

0 commit comments

Comments
 (0)