+ @IgnoreClasses 注解

This commit is contained in:
坏黑 2018-12-05 15:36:05 +08:00
parent 45f60aee9d
commit 881cb2c4a9
3 changed files with 54 additions and 29 deletions

View File

@ -24,6 +24,10 @@ import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.InputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.net.InetAddress;
import java.util.*;
@ -186,7 +190,13 @@ public class TabooLibLoader implements Listener {
if (TabooLib.isTabooLib(plugin) || TabooLib.isDependTabooLib(plugin)) {
try {
long time = System.currentTimeMillis();
List<Class> classes = FileUtils.getClasses(plugin);
List<Class> classes;
IgnoreClasses annotation = plugin.getClass().getAnnotation(IgnoreClasses.class);
if (annotation != null) {
classes = FileUtils.getClasses(plugin, annotation.value());
} else {
classes = FileUtils.getClasses(plugin);
}
TabooLib.debug("Saved " + classes.size() + " classes (" + plugin.getName() + ") (" + (System.currentTimeMillis() - time) + "ms)");
pluginClasses.put(plugin.getName(), classes);
} catch (Exception ignored) {
@ -265,4 +275,12 @@ public class TabooLibLoader implements Listener {
default void unload(Plugin plugin, Class<?> cancelClass) {
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreClasses {
String[] value();
}
}

View File

@ -81,7 +81,8 @@ public class TInjectLoader implements TabooLibLoader.Loader {
public void preLoad(Plugin plugin, Class<?> pluginClass) {
for (Field declaredField : pluginClass.getDeclaredFields()) {
TInject annotation = declaredField.getAnnotation(TInject.class);
if (annotation == null) {
// 是否为主类类型
if (annotation == null || !declaredField.getType().equals(plugin.getClass())) {
continue;
}
Object instance = null;
@ -95,18 +96,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
continue;
}
}
if (declaredField.getType().equals(plugin.getClass())) {
try {
declaredField.setAccessible(true);
injectTypes.get(Plugin.class).run(plugin, declaredField, annotation.value(), instance);
TabooLib.debug(declaredField.getName() + " injected. (" + declaredField.getType().getName() + ")");
} catch (Throwable e) {
TLogger.getGlobalLogger().error(declaredField.getName() + " inject failed: " + e.getMessage() + " (" + declaredField.getType().getName() + ")");
if (e.getMessage() == null) {
e.printStackTrace();
}
}
}
inject(plugin, declaredField, instance, annotation, injectTypes.get(Plugin.class));
}
}
@ -129,19 +119,23 @@ public class TInjectLoader implements TabooLibLoader.Loader {
}
}
TInjectTask tInjectTask = injectTypes.get(declaredField.getType());
if (tInjectTask == null) {
if (tInjectTask != null) {
inject(plugin, declaredField, instance, annotation, tInjectTask);
} else {
TLogger.getGlobalLogger().error(declaredField.getName() + " is an invalid inject type. (" + declaredField.getType().getName() + ")");
continue;
}
try {
declaredField.setAccessible(true);
tInjectTask.run(plugin, declaredField, annotation.value(), instance);
TabooLib.debug(declaredField.getName() + " injected. (" + declaredField.getType().getName() + ")");
} catch (Throwable e) {
TLogger.getGlobalLogger().error(declaredField.getName() + " inject failed: " + e.getMessage() + " (" + declaredField.getType().getName() + ")");
if (e.getMessage() == null) {
e.printStackTrace();
}
}
}
public void inject(Plugin plugin, Field field, Object instance, TInject annotation, TInjectTask injectTask) {
try {
field.setAccessible(true);
injectTask.run(plugin, field, annotation.value(), instance);
TabooLib.debug(field.getName() + " injected. (" + field.getType().getName() + ")");
} catch (Throwable e) {
TLogger.getGlobalLogger().error(field.getName() + " inject failed: " + e.getMessage() + " (" + field.getType().getName() + ")");
if (e.getMessage() == null) {
e.printStackTrace();
}
}
}

View File

@ -14,6 +14,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
@ -90,9 +91,19 @@ public class FileUtils {
/**
* 获取插件所有类
*
* @return {@link List<Class>}
* @param plugin 插件
*/
public static List<Class> getClasses(Plugin plugin) {
return getClasses(plugin, new String[0]);
}
/**
* 获取插件所有类
*
* @param plugin 插件
* @param ignore 忽略包名
*/
public static List<Class> getClasses(Plugin plugin, String[] ignore) {
List<Class> classes = new CopyOnWriteArrayList<>();
URL url = plugin.getClass().getProtectionDomain().getCodeSource().getLocation();
try {
@ -105,7 +116,9 @@ public class FileUtils {
new JarFile(src).stream().filter(entry -> entry.getName().endsWith(".class")).forEach(entry -> {
String className = entry.getName().replace('/', '.').substring(0, entry.getName().length() - 6);
try {
classes.add(Class.forName(className, false, plugin.getClass().getClassLoader()));
if (Arrays.stream(ignore).noneMatch(className::startsWith)) {
classes.add(Class.forName(className, false, plugin.getClass().getClassLoader()));
}
} catch (Throwable ignored) {
}
});
@ -142,10 +155,10 @@ public class FileUtils {
* @param file 文件
*/
public static void inputStreamToFile(InputStream inputStream, File file) {
try (FileOutputStream fos = new FileOutputStream(file) ; BufferedOutputStream bos = new BufferedOutputStream(fos)) {
try (FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos)) {
byte[] buf = new byte[1024];
int len;
while((len = inputStream.read(buf)) > 0) {
while ((len = inputStream.read(buf)) > 0) {
bos.write(buf, 0, len);
}
bos.flush();