Java Code Examples for jdk.nashorn.api.scripting.ScriptObjectMirror#isArray()
The following examples show how to use
jdk.nashorn.api.scripting.ScriptObjectMirror#isArray() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptBridge.java From karate with MIT License | 6 votes |
public Object append(Object... items) { List out = new ArrayList(); if (items == null) { return out; } for (Object item : items) { if (item == null) { continue; } if (item instanceof ScriptObjectMirror) { // no need when graal ScriptObjectMirror som = (ScriptObjectMirror) item; if (som.isArray()) { out.addAll(som.values()); } else { out.add(som); } } else if (item instanceof Collection) { out.addAll((Collection) item); } else { out.add(item); } } return out; }
Example 2
Source File: MapHelper.java From hsac-fitnesse-fixtures with Apache License 2.0 | 6 votes |
protected Object getIndexedListValue(Map<String, Object> map, String name, boolean throwIfNoList) { Object value = null; String prop = getListKeyName(name); Object val = getValue(map, prop); if (!(val instanceof List) && val instanceof ScriptObjectMirror) { ScriptObjectMirror mirror = (ScriptObjectMirror) val; if (mirror.isArray()) { val = mirror.to(List.class); } } if (val instanceof List) { List list = (List) val; int index = getListIndex(name); if (index < list.size()) { value = list.get(index); } else { value = null; } } else { if (val != null || throwIfNoList) { throw new SlimFixtureException(false, prop + " is not a list, but " + val); } } return value; }
Example 3
Source File: DFJsUtil.java From dfactor with MIT License | 5 votes |
public static boolean isJsArray(Object arr){ if(arr != null && arr instanceof ScriptObjectMirror){ ScriptObjectMirror mir = (ScriptObjectMirror) arr; boolean b = mir.isArray(); b = mir.isExtensible(); return mir.isArray(); } return false; }
Example 4
Source File: ScriptObjectMirrorSerializer.java From opentest with MIT License | 5 votes |
@Override public JsonElement serialize(ScriptObjectMirror src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) { if (src.isArray()) { return this.gson.toJsonTree(src.values()); } else { Map<String, Object> map = new HashMap<>(); map.putAll(src); return this.gson.toJsonTree(map); } }
Example 5
Source File: TypeUtil.java From opentest with MIT License | 5 votes |
public static byte[] jsNumberArrayToJavaByteArray(ScriptObjectMirror scriptObj) { if (scriptObj.isArray()) { byte[] javaByteArray = new byte[scriptObj.values().size()]; int currentIndex = 0; for (Object currentValue : scriptObj.values()) { if (currentValue instanceof Integer) { Integer intValue = (Integer) currentValue; if (intValue >= 0 && intValue <= 255) { javaByteArray[currentIndex] = (byte) intValue.intValue(); } else { throw new RuntimeException(String.format( "Failed to convert JS array to Java byte array because array " + "element %s is not a number between 0 and 255.", intValue)); } } else { throw new RuntimeException(String.format( "Failed to encode JS array to base64. Array " + "element %s is not a number.", currentValue)); } currentIndex++; } return javaByteArray; } else { throw new RuntimeException( "The provided JavaScript native value was not an array."); } }
Example 6
Source File: ScriptValueFactoryImpl.java From purplejs with Apache License 2.0 | 5 votes |
private ScriptValue newValueFromObjectMirror( final ScriptObjectMirror value ) { if ( value.isFunction() ) { return new FunctionScriptValue( this, value, this.runtime ); } if ( value.isArray() ) { return new ArrayScriptValue( this, value ); } return new ObjectScriptValue( this, value ); }
Example 7
Source File: ConversionTestBase.java From vertx-codetrans with Apache License 2.0 | 5 votes |
private Object unwrapJsonElement(ScriptObjectMirror obj) { if (obj.isArray()) { return unwrapJsonArray(obj); } else { return unwrapJsonObject(obj); } }
Example 8
Source File: NashornScriptEngine.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
private Object convertNashornValue(Object nashornValue) { if (nashornValue == null) { return null; } Object convertedValue; if (nashornValue instanceof ScriptObjectMirror) { ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) nashornValue; if (scriptObjectMirror.isArray()) { convertedValue = scriptObjectMirror.values().stream() .map(this::convertNashornValue) .collect(Collectors.toList()); } else { if ("Date".equals(scriptObjectMirror.getClassName())) { // convert to Java Interface JsDate jsDate = ((Invocable) scriptEngine).getInterface(scriptObjectMirror, JsDate.class); return jsDate.getTime(); } else return ((ScriptObjectMirror) nashornValue).getOrDefault(KEY_ID_VALUE, null); } } else if (nashornValue instanceof Map) { Map mapValue = (Map) (nashornValue); if (mapValue.get(KEY_ID_VALUE) != null) { convertedValue = mapValue.get(KEY_ID_VALUE); } else { convertedValue = nashornValue; } } else { convertedValue = nashornValue; } return convertedValue; }
Example 9
Source File: TestActor.java From opentest with MIT License | 4 votes |
/** * Converts the provided argument to a native JavaScript type. */ public Object toJsType(Object inputValue) { if (this.scriptEngine == null) { throw new RuntimeException( "Failed to convert value to a native JavaScript type, because " + "the actor's JavaScript engine was not initialized yet."); } Object result = inputValue; try { if (inputValue instanceof String) { // Nothing to do here. We'll be returning the exact // same String instance that was passed-in. } else if (inputValue instanceof Map) { result = this.evalScript("(" + Factory.getGson().toJson(inputValue) + ")"); } else if (inputValue.getClass().isArray()) { this.scriptEngine.put("$transientVarName", inputValue); result = this.scriptEngine.eval("Array.prototype.slice.call($transientVarName)"); } else if (inputValue instanceof List) { this.scriptEngine.put("$transientVarName", ((List) inputValue).toArray()); result = this.scriptEngine.eval("Array.prototype.slice.call($transientVarName)"); } else if (inputValue instanceof ScriptObjectMirror) { ScriptObjectMirror scriptObject = (ScriptObjectMirror) inputValue; if (scriptObject.isArray()) { result = scriptObject; } else { this.scriptEngine.put("$__transientVarName", inputValue); result = this.scriptEngine.eval("[$__transientVarName]"); } } else { this.scriptEngine.put("$__transientVarName", inputValue); result = this.scriptEngine.eval("[$__transientVarName]"); } this.scriptEngine.eval("delete $__transientVarName"); } catch (Exception ex) { throw new RuntimeException(String.format( "Failed converting value %s to a native JavaScript object", inputValue.toString()), ex); } return result; }