+ support kotlin companion object
This commit is contained in:
parent
29f68d36e0
commit
2899edd549
@ -0,0 +1,24 @@
|
|||||||
|
package io.izzel.taboolib.compat.kotlin;
|
||||||
|
|
||||||
|
import io.izzel.taboolib.TabooLibAPI;
|
||||||
|
import io.izzel.taboolib.util.Reflection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author sky
|
||||||
|
* @Since 2019-09-19 14:27
|
||||||
|
*/
|
||||||
|
public class CompatKotlin {
|
||||||
|
|
||||||
|
public static boolean isCompanion(Class clazz) {
|
||||||
|
return clazz.getName().endsWith("$Companion");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Object getCompanion(Class clazz) {
|
||||||
|
try {
|
||||||
|
return Reflection.getValue(null, TabooLibAPI.getPluginBridge().getClass(clazz.getName().substring(0, clazz.getName().indexOf("$Companion"))), true, "Companion");
|
||||||
|
} catch (Throwable t) {
|
||||||
|
t.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package io.izzel.taboolib.module.inject;
|
package io.izzel.taboolib.module.inject;
|
||||||
|
|
||||||
import io.izzel.taboolib.TabooLibLoader;
|
import io.izzel.taboolib.TabooLibLoader;
|
||||||
|
import io.izzel.taboolib.compat.kotlin.CompatKotlin;
|
||||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
@ -35,13 +36,20 @@ public class TFunctionLoader implements TabooLibLoader.Loader {
|
|||||||
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
||||||
TFunction function = pluginClass.getAnnotation(TFunction.class);
|
TFunction function = pluginClass.getAnnotation(TFunction.class);
|
||||||
try {
|
try {
|
||||||
|
Object instance = null;
|
||||||
Method method = pluginClass.getDeclaredMethod(enable ? function.enable() : function.disable());
|
Method method = pluginClass.getDeclaredMethod(enable ? function.enable() : function.disable());
|
||||||
if (!Modifier.isStatic(method.getModifiers())) {
|
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.");
|
TLogger.getGlobalLogger().error(method.getName() + " is not a static method.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
method.invoke(null);
|
method.invoke(instance);
|
||||||
} catch (NoSuchMethodException ignore) {
|
} catch (NoSuchMethodException ignore) {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -53,12 +61,19 @@ public class TFunctionLoader implements TabooLibLoader.Loader {
|
|||||||
for (Method declaredMethod : pluginClass.getDeclaredMethods()) {
|
for (Method declaredMethod : pluginClass.getDeclaredMethods()) {
|
||||||
if (declaredMethod.isAnnotationPresent(a)) {
|
if (declaredMethod.isAnnotationPresent(a)) {
|
||||||
try {
|
try {
|
||||||
if (!Modifier.isStatic(declaredMethod.getModifiers())) {
|
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.");
|
TLogger.getGlobalLogger().error(declaredMethod.getName() + " is not a static method.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
declaredMethod.setAccessible(true);
|
declaredMethod.setAccessible(true);
|
||||||
declaredMethod.invoke(null);
|
declaredMethod.invoke(instance);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
|
|||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import io.izzel.taboolib.TabooLib;
|
import io.izzel.taboolib.TabooLib;
|
||||||
import io.izzel.taboolib.TabooLibLoader;
|
import io.izzel.taboolib.TabooLibLoader;
|
||||||
|
import io.izzel.taboolib.compat.kotlin.CompatKotlin;
|
||||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
@ -43,36 +44,38 @@ public class TScheduleLoader implements TabooLibLoader.Loader {
|
|||||||
if (annotation == null) {
|
if (annotation == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Object instance = pluginClass.equals(plugin.getClass()) ? plugin : null;
|
Object[] instance = new Object[] {pluginClass.equals(plugin.getClass()) ? plugin : null};
|
||||||
// 如果是非静态类型
|
if (CompatKotlin.isCompanion(pluginClass)) {
|
||||||
if (!Modifier.isStatic(method.getModifiers()) && instance == null) {
|
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.");
|
TLogger.getGlobalLogger().error(method.getName() + " is not a static method.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
method.setAccessible(true);
|
|
||||||
// 如果是本插件
|
|
||||||
if (plugin.equals(TabooLib.getPlugin())) {
|
if (plugin.equals(TabooLib.getPlugin())) {
|
||||||
run(plugin, new BukkitRunnable() {
|
run(plugin, new BukkitRunnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
method.invoke(instance);
|
method.setAccessible(true);
|
||||||
|
method.invoke(instance[0]);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, annotation.delay(), annotation.period(), annotation.async());
|
}, annotation.delay(), annotation.period(), annotation.async());
|
||||||
}
|
} else {
|
||||||
// 其他插件则添加到列队
|
|
||||||
else {
|
|
||||||
schedules.computeIfAbsent(plugin.getName(), n -> Lists.newArrayList()).add(new TScheduleData(annotation, new BukkitRunnable() {
|
schedules.computeIfAbsent(plugin.getName(), n -> Lists.newArrayList()).add(new TScheduleData(annotation, new BukkitRunnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
method.invoke(instance);
|
method.setAccessible(true);
|
||||||
|
method.invoke(instance[0]);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user