<commit_before>package io.github.jdaapplications.guildbot.executor.executable;

import io.github.jdaapplications.guildbot.GuildBot;
import io.github.jdaapplications.guildbot.executor.Engine;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import org.apache.commons.collections4.map.LazyMap;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.hjson.JsonObject;

/**
 * @author <NAME>
 */
public class Method extends Executable
{
    protected final Map<Engine, String> executableScripts;
    protected final String name;
    protected final List<Pair<String, ? extends Class<?>>> params;
    protected final String proxyScript;
    protected final Class<?> type;

    public Method(final GuildBot guildBot, final JsonObject config, final String name, final String script)
    {
        super(guildBot, config, script);
        this.name = name;

        try
        {
            this.type = ClassUtils.getClass(config.getString("type", "void"));
        }
        catch (ClassNotFoundException e)
        {
            throw new RuntimeException(e);
        }

        final JsonObject paramList = (JsonObject) config.get("params");

        this.params = Collections.unmodifiableList(paramList == null 
                ? Collections.emptyList() 
                : paramList.names().stream()
                    .map(k -> {
                        try
                        {
                            return Pair.of(k, ClassUtils.getClass(paramList.get(k).asString()));
                        }
                        catch (ClassNotFoundException e)
                        {
                            throw new RuntimeException(e);
                        }
                    })
                    .collect(Collectors.toList()));

        this.executableScripts = Collections.unmodifiableMap(LazyMap.lazyMap(new HashMap<>(Engine.values().length), e -> e.getProxyMethod(name, this.type, this.params)));

        this.proxyScript = this.engine.getScript(this.getScript(), this.imports);
    }

    @SuppressWarnings("hiding")
    public String getExecutableScript(final Engine engine)
    {
        return this.executableScripts.get(engine);
    }

    public Map<Engine, String> getExecutableScripts()
    {
        return this.executableScripts;
    }

    public InvokeableMethod getInvokeableMethod(final ScriptContext context)
    {
        return args -> Method.this.invoke(context, args);
    }

    public String getName()
    {
        return this.name;
    }

    public List<Pair<String, ? extends Class<?>>> getParams()
    {
        return this.params;
    }

    public String getProxyScript()
    {
        return this.proxyScript;
    }

    public Class<?> getType()
    {
        return this.type;
    }

    protected Object invoke(final ScriptContext context, final Object... args)
    {
        try
        {
            final ScriptEngine scriptEngine = this.engine.newScriptEngine();
            scriptEngine.getContext().getBindings(ScriptContext.ENGINE_SCOPE).putAll(context.getBindings(ScriptContext.ENGINE_SCOPE));

            if (args != null)
                for (int i = 0; i < this.params.size(); i++)
                    scriptEngine.put(this.params.get(i).getKey(), args[i]);

            final Future<?> future = this.guildBot.getThreadPool().submit(() -> scriptEngine.eval(this.proxyScript));

            final Object result = future.get(this.config.getInt("timeout", this.guildBot.getConfig().getInt("timeout", 5)), TimeUnit.SECONDS);

            return this.type == Void.TYPE ? null : result;
        }
        catch (InterruptedException | TimeoutException e)
        {
            throw new RuntimeException("The execution of method \"" + this.name + "\" timed out\n" + this.proxyScript, e);
        }
        catch (final Exception e)
        {
            throw new RuntimeException("The execution of method \"" + this.name + "\" threw an error\n" + this.proxyScript, e);
        }
    }

    public interface InvokeableMethod
    {
        Object invoke(final Object... args);
    }
}
<commit_msg>Fixed calling groovy methods from js commands<commit_after>package io.github.jdaapplications.guildbot.executor.executable;

import io.github.jdaapplications.guildbot.GuildBot;
import io.github.jdaapplications.guildbot.executor.Engine;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import org.apache.commons.collections4.map.LazyMap;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.hjson.JsonObject;

/**
 * @author <NAME>
 */
public class Method extends Executable
{
    protected final Map<Engine, String> executableScripts;
    protected final String name;
    protected final List<Pair<String, ? extends Class<?>>> params;
    protected final String proxyScript;
    protected final Class<?> type;

    public Method(final GuildBot guildBot, final JsonObject config, final String name, final String script)
    {
        super(guildBot, config, script);
        this.name = name;

        try
        {
            this.type = ClassUtils.getClass(config.getString("type", "void"));
        }
        catch (ClassNotFoundException e)
        {
            throw new RuntimeException(e);
        }

        final JsonObject paramList = (JsonObject) config.get("params");

        this.params = Collections.unmodifiableList(paramList == null 
                ? Collections.emptyList() 
                : paramList.names().stream()
                    .map(k -> {
                        try
                        {
                            return Pair.of(k, ClassUtils.getClass(paramList.get(k).asString()));
                        }
                        catch (ClassNotFoundException e)
                        {
                            throw new RuntimeException(e);
                        }
                    })
                    .collect(Collectors.toList()));

        this.executableScripts = Collections.unmodifiableMap(LazyMap.lazyMap(new HashMap<>(Engine.values().length), e -> e.getProxyMethod(name, this.type, this.params)));

        this.proxyScript = this.engine.getScript(this.getScript(), this.imports);
    }

    @SuppressWarnings("hiding")
    public String getExecutableScript(final Engine engine)
    {
        return this.executableScripts.get(engine);
    }

    public Map<Engine, String> getExecutableScripts()
    {
        return this.executableScripts;
    }

    public InvokeableMethod getInvokeableMethod(final ScriptContext context)
    {
        return args -> Method.this.invoke(context, args);
    }

    public String getName()
    {
        return this.name;
    }

    public List<Pair<String, ? extends Class<?>>> getParams()
    {
        return this.params;
    }

    public String getProxyScript()
    {
        return this.proxyScript;
    }

    public Class<?> getType()
    {
        return this.type;
    }

    protected Object invoke(final ScriptContext context, final Object... args)
    {
        try
        {
            final ScriptEngine scriptEngine = this.engine.newScriptEngine();
            scriptEngine.getContext().getBindings(ScriptContext.ENGINE_SCOPE).putAll(context.getBindings(ScriptContext.ENGINE_SCOPE));

            if (args != null)
                for (int i = 0; i < this.params.size(); i++)
                    scriptEngine.put(this.params.get(i).getKey(), args[i]);

            final Future<?> future = this.guildBot.getThreadPool().submit(() -> scriptEngine.eval(this.proxyScript));

            final Object result = future.get(this.config.getInt("timeout", this.guildBot.getConfig().getInt("timeout", 5)), TimeUnit.SECONDS);

            return this.type == Void.TYPE ? null : result;
        }
        catch (InterruptedException | TimeoutException e)
        {
            throw new RuntimeException("The execution of method \"" + this.name + "\" timed out\n" + this.proxyScript, e);
        }
        catch (final Exception e)
        {
            throw new RuntimeException("The execution of method \"" + this.name + "\" threw an error\n" + this.proxyScript, e);
        }
    }

    @FunctionalInterface
    public static interface InvokeableMethod
    {
        Object invoke(final Object... args);
    }
}

{'fraction_non_alphanumeric': 0.1093608340888486, 'fraction_numerical': 0.0011332728921124207, 'mean_word_length': 2.3314458286145716, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5631959', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2350, 'n_words': 508}