project: fix lambda things and add mixins and rename two fields (#12)

This commit is contained in:
ThatGamerBlue
2020-01-28 17:49:43 +00:00
committed by Kyle
parent ed5acca637
commit db1bfdfb47
12 changed files with 128 additions and 77 deletions

View File

@@ -83,7 +83,6 @@ import net.runelite.client.task.ScheduledMethod;
import net.runelite.client.task.Scheduler;
import net.runelite.client.ui.RuneLiteSplashScreen;
import net.runelite.client.util.GameEventManager;
import net.runelite.client.util.ReflectUtil;
@Singleton
@Slf4j
@@ -547,45 +546,10 @@ public class PluginManager
return plugins;
}
private void schedule(Plugin plugin)
public void schedule(Object plugin)
{
for (Method method : plugin.getClass().getMethods())
{
Schedule schedule = method.getAnnotation(Schedule.class);
if (schedule == null)
{
continue;
}
Runnable runnable = null;
try
{
final Class<?> clazz = method.getDeclaringClass();
final MethodHandles.Lookup caller = ReflectUtil.privateLookupIn(clazz);
final MethodType subscription = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
final MethodHandle target = caller.findVirtual(clazz, method.getName(), subscription);
final CallSite site = LambdaMetafactory.metafactory(
caller,
"run",
MethodType.methodType(Runnable.class, clazz),
subscription,
target,
subscription);
final MethodHandle factory = site.getTarget();
runnable = (Runnable) factory.bindTo(plugin).invokeExact();
}
catch (Throwable e)
{
log.warn("Unable to create lambda for method {}", method, e);
}
ScheduledMethod scheduledMethod = new ScheduledMethod(schedule, method, plugin, runnable);
log.debug("Scheduled task {}", scheduledMethod);
scheduler.addScheduledMethod(scheduledMethod);
}
// note to devs: this method will almost certainly merge conflict in the future, just apply the changes in the scheduler instead
scheduler.registerObject(plugin);
}
private void unschedule(Plugin plugin)

View File

@@ -24,6 +24,11 @@
*/
package net.runelite.client.task;
import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Duration;
@@ -62,15 +67,41 @@ public class Scheduler
public void registerObject(Object obj)
{
for (Method method : obj.getClass().getDeclaredMethods())
for (Method method : obj.getClass().getMethods())
{
Schedule schedule = method.getAnnotation(Schedule.class);
if (schedule == null)
{
continue;
}
ScheduledMethod scheduledMethod = new ScheduledMethod(schedule, method, obj);
Runnable runnable = null;
try
{
final Class<?> clazz = method.getDeclaringClass();
final MethodHandles.Lookup caller = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
final MethodType subscription = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
final MethodHandle target = caller.findVirtual(clazz, method.getName(), subscription);
final CallSite site = LambdaMetafactory.metafactory(
caller,
"run",
MethodType.methodType(Runnable.class, clazz),
subscription,
target,
subscription);
final MethodHandle factory = site.getTarget();
runnable = (Runnable) factory.bindTo(obj).invokeExact();
}
catch (Throwable e)
{
log.warn("Unable to create lambda for method {}", method, e);
}
ScheduledMethod scheduledMethod = new ScheduledMethod(schedule, method, obj, runnable);
log.debug("Scheduled task {}", scheduledMethod);
addScheduledMethod(scheduledMethod);
}
}