2018-03-21 14:15:51 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* PlaceholderAPI
|
2019-06-21 18:30:12 +02:00
|
|
|
* Copyright (C) 2019 Ryan McCarthy
|
2018-03-21 14:15:51 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2018-03-21 00:04:14 +01:00
|
|
|
package me.clip.placeholderapi.util;
|
|
|
|
|
2019-06-21 18:30:12 +02:00
|
|
|
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
|
|
|
|
2018-03-21 00:04:14 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FilenameFilter;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLClassLoader;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.jar.JarEntry;
|
|
|
|
import java.util.jar.JarInputStream;
|
|
|
|
|
|
|
|
public class FileUtil {
|
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
public static List<Class<?>> getClasses(String folder, Class<?> type) {
|
|
|
|
return getClasses(folder, null, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<Class<?>> getClasses(String folder, String fileName, Class<?> type) {
|
|
|
|
List<Class<?>> list = new ArrayList<>();
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
try {
|
|
|
|
File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), folder);
|
|
|
|
if (!f.exists()) {
|
|
|
|
return list;
|
|
|
|
}
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
FilenameFilter fileNameFilter = (dir, name) -> {
|
|
|
|
if (fileName != null) {
|
|
|
|
return name.endsWith(".jar") && name.replace(".jar", "")
|
|
|
|
.equalsIgnoreCase(fileName.replace(".jar", ""));
|
|
|
|
}
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
return name.endsWith(".jar");
|
|
|
|
};
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
File[] jars = f.listFiles(fileNameFilter);
|
|
|
|
if (jars == null) {
|
|
|
|
return list;
|
|
|
|
}
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
for (File file : jars) {
|
|
|
|
list = gather(file.toURI().toURL(), list, type);
|
|
|
|
}
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
return list;
|
|
|
|
} catch (Throwable t) {
|
2018-03-21 00:04:14 +01:00
|
|
|
}
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
return null;
|
|
|
|
}
|
2018-03-21 00:04:14 +01:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
private static List<Class<?>> gather(URL jar, List<Class<?>> list, Class<?> clazz) {
|
|
|
|
if (list == null) {
|
|
|
|
list = new ArrayList<>();
|
|
|
|
}
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2019-06-10 20:22:05 +02:00
|
|
|
try (URLClassLoader cl = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader());
|
|
|
|
JarInputStream jis = new JarInputStream(jar.openStream())) {
|
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
while (true) {
|
|
|
|
JarEntry j = jis.getNextJarEntry();
|
|
|
|
if (j == null) {
|
|
|
|
break;
|
2018-03-21 00:04:14 +01:00
|
|
|
}
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
String name = j.getName();
|
|
|
|
if (name == null || name.isEmpty()) {
|
|
|
|
continue;
|
2018-03-21 00:04:14 +01:00
|
|
|
}
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
if (name.endsWith(".class")) {
|
|
|
|
name = name.replace("/", ".");
|
|
|
|
String cname = name.substring(0, name.lastIndexOf(".class"));
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
Class<?> c = cl.loadClass(cname);
|
|
|
|
if (clazz.isAssignableFrom(c)) {
|
|
|
|
list.add(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
2018-03-21 00:04:14 +01:00
|
|
|
}
|
2019-06-10 20:03:48 +02:00
|
|
|
|
2018-06-29 20:40:48 +02:00
|
|
|
return list;
|
|
|
|
}
|
2018-03-21 00:04:14 +01:00
|
|
|
}
|