+ update inject api

This commit is contained in:
坏黑 2019-10-26 01:16:08 +08:00
parent e3daaf0162
commit f04fcaa2b3
11 changed files with 174 additions and 197 deletions

View File

@ -9,16 +9,24 @@ import io.izzel.taboolib.util.Reflection;
*/
public class CompatKotlin {
public static boolean isCompanion(Class clazz) {
return clazz.getName().endsWith("$Companion");
public static boolean isCompanion(Class<?> pluginClass) {
return pluginClass.getName().endsWith("$Companion");
}
public static Object getCompanion(Class clazz) {
public static Object getCompanion(Class<?> pluginClass) {
try {
return Reflection.getValue(null, TabooLibAPI.getPluginBridge().getClass(clazz.getName().substring(0, clazz.getName().indexOf("$Companion"))), true, "Companion");
return Reflection.getValue(null, TabooLibAPI.getPluginBridge().getClass(pluginClass.getName().substring(0, pluginClass.getName().indexOf("$Companion"))), true, "Companion");
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public static Object getInstance(Class<?> pluginClass) {
try {
return Reflection.getValue(null, pluginClass, true, "INSTANCE");
} catch (Throwable ignored) {
}
return null;
}
}

View File

@ -1,12 +1,11 @@
package io.izzel.taboolib.module.db.local;
import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.module.locale.logger.TLogger;
import io.izzel.taboolib.module.inject.TInjectHelper;
import io.izzel.taboolib.util.Ref;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* @Author 坏黑
@ -21,21 +20,13 @@ public class LocalLoader implements TabooLibLoader.Loader {
if (annotation == null) {
continue;
}
Object instance = null;
// 如果是非静态类型
if (!Modifier.isStatic(field.getModifiers())) {
// 是否为主类
if (pluginClass.equals(plugin.getClass())) {
instance = plugin;
} else {
TLogger.getGlobalLogger().error(field.getName() + " is not a static field. (" + pluginClass.getName() + ")");
continue;
}
}
Ref.forcedAccess(field);
for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) {
try {
field.set(instance, Local.get(plugin.getName()).get(annotation.value()));
} catch (IllegalAccessException ignored) {
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}

View File

@ -32,9 +32,12 @@ public class PlayerContainerLoader implements Listener, TabooLibLoader.Loader {
continue;
}
field.setAccessible(true);
for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) {
try {
pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(field.get(pluginClass), annotation.uniqueId()));
} catch (IllegalAccessException ignored) {
pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(field.get(instance), annotation.uniqueId()));
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}

View File

@ -1,13 +1,10 @@
package io.izzel.taboolib.module.inject;
import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.compat.kotlin.CompatKotlin;
import io.izzel.taboolib.module.locale.logger.TLogger;
import org.bukkit.plugin.Plugin;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* @Author sky
@ -17,62 +14,45 @@ public class TFunctionLoader implements TabooLibLoader.Loader {
@Override
public void preLoad(Plugin plugin, Class<?> pluginClass) {
invokeMethods(pluginClass, TFunction.Load.class);
invokeMethods(plugin, pluginClass, TFunction.Load.class);
}
@Override
public void postLoad(Plugin plugin, Class<?> pluginClass) {
invokeMethods(pluginClass, true);
invokeMethods(pluginClass, TFunction.Init.class);
invokeMethods(plugin, pluginClass, true);
invokeMethods(plugin, pluginClass, TFunction.Init.class);
}
@Override
public void unload(Plugin plugin, Class<?> pluginClass) {
invokeMethods(pluginClass, false);
invokeMethods(pluginClass, TFunction.Cancel.class);
invokeMethods(plugin, pluginClass, false);
invokeMethods(plugin, pluginClass, TFunction.Cancel.class);
}
public void invokeMethods(Class<?> pluginClass, boolean enable) {
public void invokeMethods(Plugin plugin, Class<?> pluginClass, boolean enable) {
if (pluginClass.isAnnotationPresent(TFunction.class)) {
TFunction function = pluginClass.getAnnotation(TFunction.class);
try {
Object instance = null;
Method method = pluginClass.getDeclaredMethod(enable ? function.enable() : function.disable());
if (CompatKotlin.isCompanion(pluginClass)) {
instance = CompatKotlin.getCompanion(pluginClass);
if (instance == null) {
TLogger.getGlobalLogger().error(method.getName() + " required @JvmStatic.");
return;
}
} else if (!Modifier.isStatic(method.getModifiers())) {
TLogger.getGlobalLogger().error(method.getName() + " is not a static method.");
return;
}
method.setAccessible(true);
for (Object instance : TInjectHelper.getInstance(method, pluginClass, plugin)) {
try {
method.invoke(instance);
} catch (Throwable t) {
t.printStackTrace();
}
}
} catch (NoSuchMethodException ignore) {
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void invokeMethods(Class<?> pluginClass, Class<? extends Annotation> a) {
public void invokeMethods(Plugin plugin, Class<?> pluginClass, Class<? extends Annotation> a) {
for (Method declaredMethod : pluginClass.getDeclaredMethods()) {
if (declaredMethod.isAnnotationPresent(a)) {
try {
Object instance = null;
if (CompatKotlin.isCompanion(pluginClass)) {
instance = CompatKotlin.getCompanion(pluginClass);
if (instance == null) {
TLogger.getGlobalLogger().error(declaredMethod.getName() + " required @JvmStatic.");
return;
}
} else if (!Modifier.isStatic(declaredMethod.getModifiers())) {
TLogger.getGlobalLogger().error(declaredMethod.getName() + " is not a static method.");
return;
}
declaredMethod.setAccessible(true);
for (Object instance : TInjectHelper.getInstance(declaredMethod, pluginClass, plugin)) {
try {
declaredMethod.invoke(instance);
} catch (Throwable t) {
t.printStackTrace();
@ -80,4 +60,5 @@ public class TFunctionLoader implements TabooLibLoader.Loader {
}
}
}
}
}

View File

@ -2,14 +2,12 @@ package io.izzel.taboolib.module.inject;
import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.module.locale.logger.TLogger;
import io.izzel.taboolib.util.Ref;
import io.izzel.taboolib.util.Strings;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* @Author sky
@ -29,18 +27,8 @@ public class THookLoader implements TabooLibLoader.Loader {
if (Plugin.class.isAssignableFrom(declaredField.getType()) && declaredField.isAnnotationPresent(THook.class)) {
THook hook = declaredField.getAnnotation(THook.class);
if (Strings.nonEmpty(hook.plugin())) {
Object instance = null;
// 如果是非静态类型
if (!Modifier.isStatic(declaredField.getModifiers())) {
// 是否为主类
if (pluginClass.equals(plugin.getClass())) {
instance = plugin;
} else {
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + pluginClass.getName() + ")");
continue;
}
}
Ref.forcedAccess(declaredField);
for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) {
try {
declaredField.set(instance, Bukkit.getPluginManager().getPlugin(hook.plugin()));
} catch (Throwable t) {
@ -50,4 +38,5 @@ public class THookLoader implements TabooLibLoader.Loader {
}
}
}
}
}

View File

@ -2,12 +2,10 @@ package io.izzel.taboolib.module.inject;
import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.module.lite.SimpleVersionControl;
import io.izzel.taboolib.module.locale.logger.TLogger;
import io.izzel.taboolib.util.Ref;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* @Author sky
@ -22,22 +20,13 @@ public class TInjectAsm implements TabooLibLoader.Loader {
if (annotation == null || annotation.asm().isEmpty()) {
continue;
}
Object instance = null;
// 如果是非静态类型
if (!Modifier.isStatic(declaredField.getModifiers())) {
// 是否为主类
if (pluginClass.equals(plugin.getClass())) {
instance = plugin;
} else {
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + pluginClass.getName() + ")");
continue;
}
}
Ref.forcedAccess(declaredField);
for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) {
try {
declaredField.set(instance, SimpleVersionControl.createNMS(annotation.asm()).useCache().translate(plugin).newInstance());
} catch (Throwable t) {
TLogger.getGlobalLogger().warn("Cannot translate class \"" + declaredField.getType().getName() + "\": " + t.getMessage());
t.printStackTrace();
}
}
}
}

View File

@ -8,7 +8,7 @@ import io.izzel.taboolib.util.Reflection;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -54,27 +54,15 @@ public class TInjectCreator implements TabooLibLoader.Loader {
if (annotation == null || annotation.state() != state) {
continue;
}
ClassData classData = new ClassData(loadClass, declaredField.getType());
Object instance = null;
// 非静态类型
if (!Modifier.isStatic(declaredField.getModifiers())) {
// 在插件主类
if (loadClass.equals(plugin.getClass())) {
instance = plugin;
}
// 判断 pluginCLass 是否为 TInject 创建
else if (instanceMap.containsKey(classData)) {
instance = instanceMap.get(classData).getInstance();
} else {
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + loadClass.getName() + ")");
List<Object> instance = TInjectHelper.getInstance(declaredField, loadClass, plugin);
if (instance.isEmpty()) {
continue;
}
}
Ref.forcedAccess(declaredField);
try {
InstanceData instanceData = new InstanceData(declaredField.getType().newInstance(), annotation);
declaredField.set(instance, instanceData.getInstance());
instanceMap.put(classData, instanceData);
instanceMap.put(new ClassData(loadClass, declaredField.getType()), instanceData);
} catch (Throwable t) {
TLogger.getGlobalLogger().error(declaredField.getName() + " instantiation failed: " + t.getMessage());
}

View File

@ -1,5 +1,15 @@
package io.izzel.taboolib.module.inject;
import com.google.common.collect.Lists;
import io.izzel.taboolib.compat.kotlin.CompatKotlin;
import io.izzel.taboolib.module.locale.logger.TLogger;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
/**
* @Author sky
* @Since 2019-08-17 23:22
@ -23,4 +33,60 @@ public class TInjectHelper {
return inject.cancel();
}
}
public static List<Object> getInstance(Field field, Class<?> pluginClass, Plugin plugin) {
List<Object> instance = Lists.newArrayList();
// Static
if (Modifier.isStatic(field.getModifiers())) {
instance.add(null);
}
// No Static
else if (!Modifier.isStatic(field.getModifiers())) {
// Main
if (pluginClass.equals(plugin.getClass())) {
instance.add(plugin);
}
// TInject
else if (TInjectCreator.getInstanceMap().entrySet().stream().anyMatch(e -> e.getKey().getType().equals(pluginClass))) {
TInjectCreator.getInstanceMap().entrySet().stream().filter(e -> e.getKey().getType().equals(pluginClass)).forEach(i -> instance.add(i.getValue().getInstance()));
}
}
// Nothing
else if (instance.isEmpty()) {
TLogger.getGlobalLogger().error(field.getName() + " is not a static field. (" + pluginClass.getName() + ")");
}
return instance;
}
public static List<Object> getInstance(Method method, Class<?> pluginClass, Plugin plugin) {
List<Object> instance = Lists.newArrayList();
// Object
if (CompatKotlin.getInstance(pluginClass) != null) {
instance.add(CompatKotlin.getInstance(pluginClass));
}
// Companion Object
else if (CompatKotlin.isCompanion(pluginClass)) {
instance.add(CompatKotlin.getCompanion(pluginClass));
}
// Static
else if (Modifier.isStatic(method.getModifiers())) {
instance.add(null);
}
// No Static
else if (!Modifier.isStatic(method.getModifiers())) {
// Main
if (pluginClass.equals(plugin.getClass())) {
instance.add(plugin);
}
// TInject
else if (TInjectCreator.getInstanceMap().entrySet().stream().anyMatch(e -> e.getKey().getType().equals(pluginClass))) {
TInjectCreator.getInstanceMap().entrySet().stream().filter(e -> e.getKey().getType().equals(pluginClass)).forEach(i -> instance.add(i.getValue().getInstance()));
}
}
// Nothing
else if (instance.isEmpty()) {
TLogger.getGlobalLogger().error(method.getName() + " is not a static method. (" + pluginClass.getName() + ")");
}
return instance;
}
}

View File

@ -17,7 +17,6 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
/**
@ -135,22 +134,11 @@ 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 || !declaredField.getType().equals(plugin.getClass())) {
continue;
}
Object instance = null;
// 如果是非静态类型
if (!Modifier.isStatic(declaredField.getModifiers())) {
// 是否为主类
if (pluginClass.equals(plugin.getClass())) {
instance = plugin;
} else {
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + pluginClass.getName() + ")");
continue;
}
}
inject(plugin, declaredField, instance, annotation, injectTypes.get(Plugin.class), pluginClass);
Ref.forcedAccess(declaredField);
TInjectHelper.getInstance(declaredField, pluginClass, plugin).forEach(instance -> inject(plugin, declaredField, instance, annotation, injectTypes.get(Plugin.class), pluginClass));
}
}
@ -161,26 +149,15 @@ public class TInjectLoader implements TabooLibLoader.Loader {
if (annotation == null || declaredField.getType().equals(plugin.getClass())) {
continue;
}
Object instance = null;
// 如果是非静态类型
if (!Modifier.isStatic(declaredField.getModifiers())) {
// 是否为主类
if (pluginClass.equals(plugin.getClass())) {
instance = plugin;
} else {
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + pluginClass.getName() + ")");
continue;
}
}
Ref.forcedAccess(declaredField);
TInjectTask tInjectTask = injectTypes.get(declaredField.getType());
if (tInjectTask != null) {
inject(plugin, declaredField, instance, annotation, tInjectTask, pluginClass);
TInjectHelper.getInstance(declaredField, pluginClass, plugin).forEach(instance -> inject(plugin, declaredField, instance, annotation, tInjectTask, pluginClass));
}
}
}
public void inject(Plugin plugin, Field field, Object instance, TInject annotation, TInjectTask injectTask, Class pluginClass) {
Ref.forcedAccess(field);
try {
injectTask.run(plugin, field, annotation, pluginClass, instance);
TabooLibAPI.debug(field.getName() + " injected. (" + field.getType().getName() + ")");

View File

@ -4,13 +4,10 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.compat.kotlin.CompatKotlin;
import io.izzel.taboolib.module.locale.logger.TLogger;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
@ -44,42 +41,34 @@ public class TScheduleLoader implements TabooLibLoader.Loader {
if (annotation == null) {
continue;
}
Object[] instance = new Object[] {pluginClass.equals(plugin.getClass()) ? plugin : null};
if (CompatKotlin.isCompanion(pluginClass)) {
instance[0] = CompatKotlin.getCompanion(pluginClass);
if (instance[0] == null) {
TLogger.getGlobalLogger().error(method.getName() + " required @JvmStatic.");
return;
}
} else if (!Modifier.isStatic(method.getModifiers()) && instance[0] == null) {
TLogger.getGlobalLogger().error(method.getName() + " is not a static method.");
continue;
}
method.setAccessible(true);
if (plugin.equals(TabooLib.getPlugin())) {
run(plugin, new BukkitRunnable() {
@Override
public void run() {
for (Object i : TInjectHelper.getInstance(method, pluginClass, plugin)) {
try {
method.setAccessible(true);
method.invoke(instance[0]);
method.invoke(i);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}, annotation.delay(), annotation.period(), annotation.async());
} else {
schedules.computeIfAbsent(plugin.getName(), n -> Lists.newArrayList()).add(new TScheduleData(annotation, new BukkitRunnable() {
@Override
public void run() {
for (Object i : TInjectHelper.getInstance(method, pluginClass, plugin)) {
try {
method.setAccessible(true);
method.invoke(instance[0]);
method.invoke(i);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}));
}
}

View File

@ -1,13 +1,14 @@
package io.izzel.taboolib.module.packet;
import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.module.inject.TInjectHelper;
import io.izzel.taboolib.module.locale.logger.TLogger;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
/**
* @Author sky
@ -19,49 +20,44 @@ public class TPacketLoader implements TabooLibLoader.Loader {
public void activeLoad(Plugin plugin, Class<?> pluginClass) {
for (Method method : pluginClass.getDeclaredMethods()) {
if (method.isAnnotationPresent(TPacket.class)) {
Object instance = null;
// 如果是非静态类型
if (!Modifier.isStatic(method.getModifiers())) {
// 是否为主类
if (pluginClass.equals(plugin.getClass())) {
instance = plugin;
} else {
TLogger.getGlobalLogger().error(method.getName() + " is not a static method. (" + pluginClass.getName() + ")");
List<Object> instance = TInjectHelper.getInstance(method, pluginClass, plugin);
if (instance.isEmpty()) {
continue;
}
}
method.setAccessible(true);
TPacket packet = method.getAnnotation(TPacket.class);
boolean packetType = method.getReturnType().equals(Boolean.TYPE) || method.getReturnType().equals(Boolean.class);
// object type
if (Arrays.equals(method.getParameterTypes(), new Class[] {Player.class, Object.class})) {
Object finalInstance = instance;
for (Object i : instance) {
TPacketHandler.addListener(plugin, new TPacketListener() {
@Override
public boolean onSend(Player player, Object p) {
return eval(finalInstance, packet, TPacket.Type.SEND, packetType, method, player, p);
return eval(i, packet, TPacket.Type.SEND, packetType, method, player, p);
}
@Override
public boolean onReceive(Player player, Object p) {
return eval(finalInstance, packet, TPacket.Type.RECEIVE, packetType, method, player, p);
return eval(i, packet, TPacket.Type.RECEIVE, packetType, method, player, p);
}
});
}
}
// packet type
else if (Arrays.equals(method.getParameterTypes(), new Class[] {Player.class, Packet.class})) {
Object finalInstance1 = instance;
for (Object i : instance) {
TPacketHandler.addListener(plugin, new TPacketListener() {
@Override
public boolean onSend(Player player, Packet p) {
return eval(finalInstance1, packet, TPacket.Type.SEND, packetType, method, player, p);
return eval(i, packet, TPacket.Type.SEND, packetType, method, player, p);
}
@Override
public boolean onReceive(Player player, Packet p) {
return eval(finalInstance1, packet, TPacket.Type.RECEIVE, packetType, method, player, p);
return eval(i, packet, TPacket.Type.RECEIVE, packetType, method, player, p);
}
});
}
} else {
TLogger.getGlobalLogger().error(method.getName() + " is an invalid packet listener. (" + pluginClass.getName() + ")");
TLogger.getGlobalLogger().error("Usage: boolean fun(Player player, Object packet) { ... }");