+ @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.File;
import java.io.InputStream; 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.net.InetAddress;
import java.util.*; import java.util.*;
@ -186,7 +190,13 @@ public class TabooLibLoader implements Listener {
if (TabooLib.isTabooLib(plugin) || TabooLib.isDependTabooLib(plugin)) { if (TabooLib.isTabooLib(plugin) || TabooLib.isDependTabooLib(plugin)) {
try { try {
long time = System.currentTimeMillis(); 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)"); TabooLib.debug("Saved " + classes.size() + " classes (" + plugin.getName() + ") (" + (System.currentTimeMillis() - time) + "ms)");
pluginClasses.put(plugin.getName(), classes); pluginClasses.put(plugin.getName(), classes);
} catch (Exception ignored) { } catch (Exception ignored) {
@ -265,4 +275,12 @@ public class TabooLibLoader implements Listener {
default void unload(Plugin plugin, Class<?> cancelClass) { 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) { public void preLoad(Plugin plugin, Class<?> pluginClass) {
for (Field declaredField : pluginClass.getDeclaredFields()) { for (Field declaredField : pluginClass.getDeclaredFields()) {
TInject annotation = declaredField.getAnnotation(TInject.class); TInject annotation = declaredField.getAnnotation(TInject.class);
if (annotation == null) { // 是否为主类类型
if (annotation == null || !declaredField.getType().equals(plugin.getClass())) {
continue; continue;
} }
Object instance = null; Object instance = null;
@ -95,18 +96,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
continue; continue;
} }
} }
if (declaredField.getType().equals(plugin.getClass())) { inject(plugin, declaredField, instance, annotation, injectTypes.get(Plugin.class));
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();
}
}
}
} }
} }
@ -129,19 +119,23 @@ public class TInjectLoader implements TabooLibLoader.Loader {
} }
} }
TInjectTask tInjectTask = injectTypes.get(declaredField.getType()); 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() + ")"); 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() + ")"); public void inject(Plugin plugin, Field field, Object instance, TInject annotation, TInjectTask injectTask) {
} catch (Throwable e) { try {
TLogger.getGlobalLogger().error(declaredField.getName() + " inject failed: " + e.getMessage() + " (" + declaredField.getType().getName() + ")"); field.setAccessible(true);
if (e.getMessage() == null) { injectTask.run(plugin, field, annotation.value(), instance);
e.printStackTrace(); 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.net.URLConnection;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList; 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) { 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<>(); List<Class> classes = new CopyOnWriteArrayList<>();
URL url = plugin.getClass().getProtectionDomain().getCodeSource().getLocation(); URL url = plugin.getClass().getProtectionDomain().getCodeSource().getLocation();
try { try {
@ -105,7 +116,9 @@ public class FileUtils {
new JarFile(src).stream().filter(entry -> entry.getName().endsWith(".class")).forEach(entry -> { new JarFile(src).stream().filter(entry -> entry.getName().endsWith(".class")).forEach(entry -> {
String className = entry.getName().replace('/', '.').substring(0, entry.getName().length() - 6); String className = entry.getName().replace('/', '.').substring(0, entry.getName().length() - 6);
try { 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) { } catch (Throwable ignored) {
} }
}); });
@ -142,10 +155,10 @@ public class FileUtils {
* @param file 文件 * @param file 文件
*/ */
public static void inputStreamToFile(InputStream inputStream, File 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]; byte[] buf = new byte[1024];
int len; int len;
while((len = inputStream.read(buf)) > 0) { while ((len = inputStream.read(buf)) > 0) {
bos.write(buf, 0, len); bos.write(buf, 0, len);
} }
bos.flush(); bos.flush();