task: use lambdas for scheduled method invokes
This commit is contained in:
@@ -38,6 +38,11 @@ import com.google.inject.Injector;
|
|||||||
import com.google.inject.Key;
|
import com.google.inject.Key;
|
||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
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.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -57,8 +62,6 @@ import javax.inject.Singleton;
|
|||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.runelite.client.events.SessionClose;
|
|
||||||
import net.runelite.client.events.SessionOpen;
|
|
||||||
import net.runelite.client.RuneLite;
|
import net.runelite.client.RuneLite;
|
||||||
import net.runelite.client.config.Config;
|
import net.runelite.client.config.Config;
|
||||||
import net.runelite.client.config.ConfigManager;
|
import net.runelite.client.config.ConfigManager;
|
||||||
@@ -66,11 +69,14 @@ import net.runelite.client.config.RuneLiteConfig;
|
|||||||
import net.runelite.client.eventbus.EventBus;
|
import net.runelite.client.eventbus.EventBus;
|
||||||
import net.runelite.client.eventbus.Subscribe;
|
import net.runelite.client.eventbus.Subscribe;
|
||||||
import net.runelite.client.events.PluginChanged;
|
import net.runelite.client.events.PluginChanged;
|
||||||
|
import net.runelite.client.events.SessionClose;
|
||||||
|
import net.runelite.client.events.SessionOpen;
|
||||||
import net.runelite.client.task.Schedule;
|
import net.runelite.client.task.Schedule;
|
||||||
import net.runelite.client.task.ScheduledMethod;
|
import net.runelite.client.task.ScheduledMethod;
|
||||||
import net.runelite.client.task.Scheduler;
|
import net.runelite.client.task.Scheduler;
|
||||||
import net.runelite.client.ui.SplashScreen;
|
import net.runelite.client.ui.SplashScreen;
|
||||||
import net.runelite.client.util.GameEventManager;
|
import net.runelite.client.util.GameEventManager;
|
||||||
|
import net.runelite.client.util.ReflectUtil;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -501,7 +507,30 @@ public class PluginManager
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScheduledMethod scheduledMethod = new ScheduledMethod(schedule, method, plugin);
|
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);
|
log.debug("Scheduled task {}", scheduledMethod);
|
||||||
|
|
||||||
scheduler.addScheduledMethod(scheduledMethod);
|
scheduler.addScheduledMethod(scheduledMethod);
|
||||||
|
|||||||
@@ -26,49 +26,22 @@ package net.runelite.client.task;
|
|||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@ToString
|
||||||
|
@Getter
|
||||||
public class ScheduledMethod
|
public class ScheduledMethod
|
||||||
{
|
{
|
||||||
private final Schedule schedule;
|
private final Schedule schedule;
|
||||||
private final Method method;
|
private final Method method;
|
||||||
private final Object object;
|
private final Object object;
|
||||||
|
@EqualsAndHashCode.Exclude
|
||||||
|
private final Runnable lambda;
|
||||||
|
@Setter
|
||||||
private Instant last = Instant.now();
|
private Instant last = Instant.now();
|
||||||
|
|
||||||
public ScheduledMethod(Schedule schedule, Method method, Object object)
|
|
||||||
{
|
|
||||||
this.schedule = schedule;
|
|
||||||
this.method = method;
|
|
||||||
this.object = object;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString()
|
|
||||||
{
|
|
||||||
return "ScheduledMethod{" + "schedule=" + schedule + ", method=" + method + ", object=" + object + '}';
|
|
||||||
}
|
|
||||||
|
|
||||||
public Schedule getSchedule()
|
|
||||||
{
|
|
||||||
return schedule;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Method getMethod()
|
|
||||||
{
|
|
||||||
return method;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getObject()
|
|
||||||
{
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Instant getLast()
|
|
||||||
{
|
|
||||||
return last;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLast(Instant last)
|
|
||||||
{
|
|
||||||
this.last = last;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,11 +93,18 @@ public class Scheduler
|
|||||||
|
|
||||||
private void run(ScheduledMethod scheduledMethod)
|
private void run(ScheduledMethod scheduledMethod)
|
||||||
{
|
{
|
||||||
Method method = scheduledMethod.getMethod();
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
method.invoke(scheduledMethod.getObject());
|
Runnable lambda = scheduledMethod.getLambda();
|
||||||
|
if (lambda != null)
|
||||||
|
{
|
||||||
|
lambda.run();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Method method = scheduledMethod.getMethod();
|
||||||
|
method.invoke(scheduledMethod.getObject());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
|
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user