1 | /*- | |
2 | * #%L | |
3 | * io.earcam.utilitarian.site.search.offline | |
4 | * %% | |
5 | * Copyright (C) 2017 earcam | |
6 | * %% | |
7 | * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT) | |
8 | * | |
9 | * You <b>must</b> choose to accept, in full - any individual or combination of | |
10 | * the following licenses: | |
11 | * <ul> | |
12 | * <li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li> | |
13 | * <li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li> | |
14 | * <li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li> | |
15 | * <li><a href="https://opensource.org/licenses/MIT">MIT</a></li> | |
16 | * </ul> | |
17 | * #L% | |
18 | */ | |
19 | package io.earcam.utilitarian.site.search.offline; | |
20 | ||
21 | import static java.nio.charset.StandardCharsets.UTF_8; | |
22 | import static java.util.ServiceLoader.load; | |
23 | import static java.util.stream.StreamSupport.stream; | |
24 | ||
25 | import java.io.InputStream; | |
26 | import java.io.InputStreamReader; | |
27 | import java.nio.charset.Charset; | |
28 | import java.util.Optional; | |
29 | ||
30 | import javax.script.Invocable; | |
31 | import javax.script.ScriptEngine; | |
32 | import javax.script.ScriptEngineFactory; | |
33 | import javax.script.ScriptException; | |
34 | ||
35 | import io.earcam.unexceptional.Exceptional; | |
36 | ||
37 | final class Javascript { | |
38 | ||
39 | private static final String ECMA_SCRIPT = "ECMAScript"; | |
40 | private static final Charset CHARSET = UTF_8; | |
41 | ||
42 | ||
43 | private Javascript() | |
44 | { | |
45 | throw new IllegalStateException("nope"); | |
46 | } | |
47 | ||
48 | ||
49 | public static Invocable createJavascriptEngine(InputStream... scripts) | |
50 | { | |
51 |
1
1. createJavascriptEngine : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Javascript::createJavascriptEngine to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return createScriptEngine(ECMA_SCRIPT, scripts); |
52 | } | |
53 | ||
54 | ||
55 | public static Invocable createScriptEngine(String language, InputStream... scripts) | |
56 | { | |
57 |
1
1. lambda$createScriptEngine$0 : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Javascript::lambda$createScriptEngine$0 to ( if (x != null) null else throw new RuntimeException ) → KILLED |
ScriptEngine engine = specified().orElseGet(() -> spi(language)); |
58 | ||
59 | for(InputStream script : scripts) { | |
60 | try { | |
61 | engine.eval(new InputStreamReader(script, CHARSET)); | |
62 | } catch(ScriptException e) { | |
63 | throw new ScriptRuntimeException(e); | |
64 | } | |
65 | } | |
66 |
1
1. createScriptEngine : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Javascript::createScriptEngine to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return (Invocable) engine; |
67 | } | |
68 | ||
69 | ||
70 | private static Optional<ScriptEngine> specified() | |
71 | { | |
72 | String specified = System.getProperty(Resources.PROPERTY_USE_SCRIPT_ENGINE, ""); | |
73 | ||
74 |
2
1. specified : negated conditional → KILLED 2. specified : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Javascript::specified to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return ("".equals(specified)) ? Optional.empty() : Exceptional.apply(Javascript::loadSpecified, specified); |
75 | } | |
76 | ||
77 | ||
78 | private static Optional<ScriptEngine> loadSpecified(String engineType) throws ReflectiveOperationException | |
79 | { | |
80 | Class<?> specific = Javascript.class.getClassLoader().loadClass(engineType); | |
81 | ScriptEngineFactory factory = (ScriptEngineFactory) specific.newInstance(); | |
82 |
1
1. loadSpecified : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Javascript::loadSpecified to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Optional.of(factory.getScriptEngine()); |
83 | } | |
84 | ||
85 | ||
86 | private static ScriptEngine spi(String language) | |
87 | { | |
88 |
1
1. spi : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Javascript::spi to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return stream(load(ScriptEngineFactory.class).spliterator(), false) |
89 |
1
1. lambda$spi$1 : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
.filter(f -> f.getLanguageName().equals(language)) |
90 | .findAny() | |
91 | .orElseThrow(ScriptRuntimeException::engineNotFound) | |
92 | .getScriptEngine(); | |
93 | } | |
94 | ||
95 | ||
96 | public static Object invokeFunction(Invocable engine, String name, Object... args) | |
97 | { | |
98 |
1
1. invokeFunction : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Javascript::invokeFunction to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Exceptional.apply(engine::invokeFunction, name, args); |
99 | } | |
100 | } | |
Mutations | ||
51 |
1.1 |
|
57 |
1.1 |
|
66 |
1.1 |
|
74 |
1.1 2.2 |
|
82 |
1.1 |
|
88 |
1.1 |
|
89 |
1.1 |
|
98 |
1.1 |