RFR: 8264326: Modernize javax.script.ScriptEngineManager and related classes' implementation

Attila Szegedi attila at openjdk.java.net
Sat Mar 27 15:34:32 UTC 2021


On Sat, 27 Mar 2021 15:19:37 GMT, Attila Szegedi <attila at openjdk.org> wrote:

> I noticed that `javax.script.ScriptEngineManager` `getEngineByXxx` methods had a lot of code duplication among themselves, and even within each method. I refactored them into a modern unified implementation. While there I also took the opportunity to introduce `Objects.requireNonNull` in place of null checks followed by NPE throws, mark private fields final where possible, use lambdas for `doPrivileged` block, use `List.of` and `List.copyOf` where possible, and generally sanitize/deduplicate.

src/java.scripting/share/classes/javax/script/ScriptEngineManager.java line 224:

> 222:                 try {
> 223:                     List<String> matches = valuesFn.apply(spi);
> 224:                     return matches != null && matches.contains(selector);

Note this under anomalous conditions, calling `List.contains` here can result in somewhat different behavior than [what was here before](https://github.com/openjdk/jdk/blob/master/src/java.scripting/share/classes/javax/script/ScriptEngineManager.java#L238). Notably:
* if `spi` returns a list that contains the selector value _at least twice_, and
* it throws an exception while creating the engine for the first time, then
* the previous implementation will invoke `getScriptEngine` for the second time too.

My modified implementation will ignore multiple occurrences of the selector value in the returned list. I believe that behavior is correct, especially since the list of values for engine names, extensions, and mime types is expected to contain unique entries (too bad the interface didn't define these methods to return a `Set<String>`…)

-------------

PR: https://git.openjdk.java.net/jdk/pull/3229


More information about the core-libs-dev mailing list