mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2025-02-06 04:05:28 +01:00
Better FileUtil#findClass
Issue #727 's CME roots come from FileUtil. This can be due to the previous implementation having 2 lists linking each other. This is removing 1 of the lists and handling on-the-go while also cleaning up the code a bit. Oh, and for the CloudExpansionManager change: I put it on the wrong spot the first time :P so yea
This commit is contained in:
parent
e0b18faf1d
commit
f1de7d058a
@ -104,6 +104,10 @@ public final class CloudExpansionManager {
|
|||||||
|
|
||||||
public void kill() {
|
public void kill() {
|
||||||
clean();
|
clean();
|
||||||
|
ASYNC_EXECUTOR.shutdown();
|
||||||
|
try {
|
||||||
|
ASYNC_EXECUTOR.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
|
||||||
|
} catch (InterruptedException ignored) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@ -167,10 +171,6 @@ public final class CloudExpansionManager {
|
|||||||
|
|
||||||
await.values().forEach(future -> future.cancel(true));
|
await.values().forEach(future -> future.cancel(true));
|
||||||
await.clear();
|
await.clear();
|
||||||
ASYNC_EXECUTOR.shutdown();
|
|
||||||
try {
|
|
||||||
ASYNC_EXECUTOR.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
|
|
||||||
} catch (InterruptedException ignored) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fetch(final boolean allowUnverified) {
|
public void fetch(final boolean allowUnverified) {
|
||||||
|
@ -28,9 +28,11 @@ import java.io.IOException;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Enumeration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarInputStream;
|
import java.util.jar.JarFile;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
|
||||||
public class FileUtil {
|
public class FileUtil {
|
||||||
|
|
||||||
@ -41,37 +43,34 @@ public class FileUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final URL jar = file.toURI().toURL();
|
JarFile jar = new JarFile(file);
|
||||||
final URLClassLoader loader = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader());
|
Enumeration<? extends ZipEntry> entries = jar.entries();
|
||||||
final List<String> matches = new ArrayList<>();
|
List<Class<? extends T>> classes = new ArrayList<>();
|
||||||
final List<Class<? extends T>> classes = new ArrayList<>();
|
try (URLClassLoader loader =
|
||||||
|
new URLClassLoader(new URL[] {file.toURI().toURL()}, clazz.getClassLoader())) {
|
||||||
try (final JarInputStream stream = new JarInputStream(jar.openStream())) {
|
while (entries.hasMoreElements()) {
|
||||||
JarEntry entry;
|
ZipEntry zip = entries.nextElement();
|
||||||
while ((entry = stream.getNextJarEntry()) != null) {
|
JarEntry entry = jar.getJarEntry(zip.getName());
|
||||||
final String name = entry.getName();
|
if (entry == null) {
|
||||||
if (name.isEmpty() || !name.endsWith(".class")) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
String name = entry.getName();
|
||||||
matches.add(name.substring(0, name.lastIndexOf('.')).replace('/', '.'));
|
if (!name.endsWith(".class")) {
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
for (final String match : matches) {
|
name = name.substring(0, name.indexOf('.')).replace('/', '.');
|
||||||
try {
|
try {
|
||||||
final Class<?> loaded = loader.loadClass(match);
|
Class<?> loaded = loader.loadClass(name);
|
||||||
if (clazz.isAssignableFrom(loaded)) {
|
if (clazz.isAssignableFrom(loaded)) {
|
||||||
classes.add(loaded.asSubclass(clazz));
|
classes.add(loaded.asSubclass(clazz));
|
||||||
}
|
}
|
||||||
} catch (final NoClassDefFoundError ignored) {
|
} catch (NoClassDefFoundError ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (classes.isEmpty()) {
|
if (classes.isEmpty()) {
|
||||||
loader.close();
|
return null;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
return classes.get(0);
|
return classes.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user