<commit_before>package com.jarvis.cache.script;

import com.jarvis.cache.CacheUtil;
import lombok.extern.slf4j.Slf4j;

import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 解析JavaScript表达式
 *
 * @author jiayu.qiu
 */
@Slf4j
public class JavaScriptParser extends AbstractScriptParser {

    private final ScriptEngineManager manager = new ScriptEngineManager();

    private final ConcurrentHashMap<String, CompiledScript> expCache = new ConcurrentHashMap<String, CompiledScript>();

    private final StringBuffer funcs = new StringBuffer();

    private static int versionCode;

    /**
     * 如果使用的是JDK大于1.8版本的，则用 nashorn，否则用javascript
     */
    private final ScriptEngine engine;

    static {
        String javaVersion = System.getProperty("java.version");
        int ind = 0;
        int cnt = 2;
        for (int i = 0; i < cnt; i++) {
            ind = javaVersion.indexOf(".", ind);
            ind++;
        }
        javaVersion = javaVersion.substring(0, ind);
        javaVersion = javaVersion.replaceAll("\\.", "");
        versionCode = Integer.parseInt(javaVersion);
    }

    public JavaScriptParser() {
        engine = manager.getEngineByName(versionCode >= 18 ? "nashorn" : "javascript");
        try {
            addFunction(HASH, CacheUtil.class.getDeclaredMethod("getUniqueHashStr", new Class[]{Object.class}));
            addFunction(EMPTY, CacheUtil.class.getDeclaredMethod("isEmpty", new Class[]{Object.class}));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }<commit_msg>update jdk11弃用Nashorn JavaScript引擎<commit_after>package com.jarvis.cache.script;

import com.jarvis.cache.CacheUtil;
import lombok.extern.slf4j.Slf4j;

import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 解析JavaScript表达式
 *
 * @author jiayu.qiu
 */
@Slf4j
public class JavaScriptParser extends AbstractScriptParser {

    private final ScriptEngineManager manager = new ScriptEngineManager();

    private final ConcurrentHashMap<String, CompiledScript> expCache = new ConcurrentHashMap<String, CompiledScript>();

    private final StringBuffer funcs = new StringBuffer();

    private static int versionCode;

    private final ScriptEngine engine;

    public JavaScriptParser() {
        engine = manager.getEngineByName("javascript");
        try {
            addFunction(HASH, CacheUtil.class.getDeclaredMethod("getUniqueHashStr", new Class[]{Object.class}));
            addFunction(EMPTY, CacheUtil.class.getDeclaredMethod("isEmpty", new Class[]{Object.class}));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
{'fraction_non_alphanumeric': 0.10823909531502424, 'fraction_numerical': 0.005169628432956382, 'mean_word_length': 3.690909090909091, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '6539004', 'n_tokens_mistral': 899, 'n_tokens_neox': 898, 'n_words': 189}