List of usage examples for jdk.nashorn.api.scripting ScriptUtils unwrap
public static Object unwrap(final Object obj)
From source file:net.orzo.lib.DataStructures.java
License:Apache License
/** * From a JavaScript array it creates a new one with unique occurrence of * items./* w w w. j ava 2s . co m*/ * * @param jsArray * a JavaScript or Java array * @param key * a JavaScript function to access values to be considered; null * is also possible (in such case, the value itself is used) * @return a JavaScript array with unique items */ public Object uniq(Object jsArray, ScriptFunction key) { Set<Object> set = new HashSet<>(); Collection<?> origData; if (jsArray.getClass().isArray()) { origData = Arrays.asList(jsArray); } else { NativeArray tmp = (NativeArray) ScriptUtils.unwrap(jsArray); origData = Arrays.asList(tmp.asObjectArray()); } try { if (key == null) { set.addAll(origData); } else { for (Object item : origData) { MethodHandle mh = key.getBoundInvokeHandle(item); set.add(mh.invoke(item)); } } return set; // TODO wrapping??? } catch (Throwable ex) { throw new LibException(ex); } }
From source file:org.structr.core.script.StructrScriptable.java
License:Open Source License
public Object unwrap(final Object source) { if (source != null) { if (source instanceof Wrapper) { return unwrap(((Wrapper) source).unwrap()); } else if (source.getClass().isArray()) { final List list = new ArrayList(); for (final Object obj : (Object[]) source) { list.add(unwrap(obj));//from w w w . jav a 2 s . c o m } return list; } else if (source instanceof StructrArray) { final List list = new ArrayList(); for (final Object obj : ((StructrArray) source).toArray()) { list.add(unwrap(obj)); } return list; } else if (source.getClass().getName().equals("org.mozilla.javascript.NativeDate")) { // FIXME: this is one of the worst ways I've ever resorted to in order // to extract the value of a wrapped NativeDate which is not accessible // in Java 8 any more, but will be returned by the Rhino Javascript // engine. try { final Class type = source.getClass(); final Field field = type.getDeclaredField("date"); field.setAccessible(true); final Double value = field.getDouble(source); return new Date(value.longValue()); } catch (Throwable t) { logger.log(Level.WARNING, "", t); } } else { return ScriptUtils.unwrap(source); } } return source; }
From source file:org.wso2.carbon.identity.application.authentication.framework.config.model.graph.SerializableJsFunction.java
License:Open Source License
/** * This will return the converted SerializableJsFunction if the given ScriptObjectMirror is a function. * @param scriptObjectMirror/*from w w w . j a va 2 s .com*/ * @return null if the ScriptObjectMirror is not a function. */ public static SerializableJsFunction toSerializableForm(ScriptObjectMirror scriptObjectMirror) { if (!scriptObjectMirror.isFunction()) { return null; } //TODO try to get rid of ScriptFunction Object unwrapped = ScriptUtils.unwrap(scriptObjectMirror); if (unwrapped instanceof ScriptFunction) { ScriptFunction scriptFunction = (ScriptFunction) unwrapped; boolean isFunction = scriptObjectMirror.isFunction(); String source = scriptFunction.toSource(); return new SerializableJsFunction(source, isFunction); } else { return new SerializableJsFunction(unwrapped.toString(), true); } }