Fix IndexOutOfBoundsException in ScriptEngineFactoryHelper (#3549)

* Fix IndexOutOfBoundsException in ScriptEngineFactoryHelper

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
This commit is contained in:
jimtng 2023-04-15 02:39:20 +10:00 committed by GitHub
parent 4f2af88e73
commit 5d46377d22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -59,9 +59,14 @@ public class ScriptEngineFactoryHelper {
}
public static String getPreferredMimeType(ScriptEngineFactory factory) {
List<String> mimeTypes = new ArrayList<>(factory.getScriptTypes());
List<String> scriptTypes = factory.getScriptTypes();
if (scriptTypes.isEmpty()) {
throw new IllegalStateException(
factory.getClass().getName() + " does not support any scriptTypes. Please report it as a bug.");
}
List<String> mimeTypes = new ArrayList<>(scriptTypes);
mimeTypes.removeIf(mimeType -> !mimeType.contains("application") || "application/python".equals(mimeType));
return mimeTypes.isEmpty() ? factory.getScriptTypes().get(0) : mimeTypes.get(0);
return mimeTypes.isEmpty() ? scriptTypes.get(0) : mimeTypes.get(0);
}
public static String getLanguageName(javax.script.ScriptEngineFactory factory) {