Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
Rhino JavaScript Java Library Source Code
Rhino JavaScript Java Library is an open-source implementation of JavaScript
written entirely in Java.
Rhino JavaScript Java Library Source Code files are provided in binary package (rhino-1.7.14.zip).
You can also browse the source code below:
✍: FYIcenter.com
⏎ org/mozilla/javascript/NativeJavaMap.java
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.javascript;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* <code>NativeJavaMap</code> is a wrapper for java objects implementing <code>java.util.Map
* </code> interface. When {@link Context#FEATURE_ENABLE_JAVA_MAP_ACCESS} is enabled, property based
* access like <code>map[key]</code> is delegated to {@link Map#get(Object)} or {@link
* Map#put(Object, Object)} operations so that a <code>JavaMap</code> acts very similar to a
* javascript <code>Object</code> There is also an iterator to iterate over entries with <code>
* for .. of</code>.
*
* <p><b>Limitations:</b> The wrapped map should have <code>String</code> or <code>Integer</code> as
* key. Otherwise, property based access may not work properly.
*/
public class NativeJavaMap extends NativeJavaObject {
private static final long serialVersionUID = -3786257752907047381L;
private Map<Object, Object> map;
static void init(ScriptableObject scope, boolean sealed) {
NativeJavaMapIterator.init(scope, sealed);
}
@SuppressWarnings("unchecked")
public NativeJavaMap(Scriptable scope, Object map) {
super(scope, map, map.getClass());
assert map instanceof Map;
this.map = (Map<Object, Object>) map;
}
@Override
public String getClassName() {
return "JavaMap";
}
@Override
public boolean has(String name, Scriptable start) {
Context cx = Context.getCurrentContext();
if (cx != null && cx.hasFeature(Context.FEATURE_ENABLE_JAVA_MAP_ACCESS)) {
if (map.containsKey(name)) {
return true;
}
}
return super.has(name, start);
}
@Override
public boolean has(int index, Scriptable start) {
Context cx = Context.getCurrentContext();
if (cx != null && cx.hasFeature(Context.FEATURE_ENABLE_JAVA_MAP_ACCESS)) {
if (map.containsKey(Integer.valueOf(index))) {
return true;
}
}
return super.has(index, start);
}
@Override
public boolean has(Symbol key, Scriptable start) {
if (SymbolKey.ITERATOR.equals(key)) {
return true;
}
return false;
}
@Override
public Object get(String name, Scriptable start) {
Context cx = Context.getCurrentContext();
if (cx != null && cx.hasFeature(Context.FEATURE_ENABLE_JAVA_MAP_ACCESS)) {
if (map.containsKey(name)) {
Object obj = map.get(name);
return cx.getWrapFactory().wrap(cx, this, obj, obj == null ? null : obj.getClass());
}
}
return super.get(name, start);
}
@Override
public Object get(int index, Scriptable start) {
Context cx = Context.getCurrentContext();
if (cx != null && cx.hasFeature(Context.FEATURE_ENABLE_JAVA_MAP_ACCESS)) {
if (map.containsKey(Integer.valueOf(index))) {
Object obj = map.get(Integer.valueOf(index));
return cx.getWrapFactory().wrap(cx, this, obj, obj == null ? null : obj.getClass());
}
}
return super.get(index, start);
}
@Override
public Object get(Symbol key, Scriptable start) {
if (SymbolKey.ITERATOR.equals(key)) {
return symbol_iterator;
}
return super.get(key, start);
}
@Override
public void put(String name, Scriptable start, Object value) {
Context cx = Context.getCurrentContext();
if (cx != null && cx.hasFeature(Context.FEATURE_ENABLE_JAVA_MAP_ACCESS)) {
map.put(name, Context.jsToJava(value, Object.class));
} else {
super.put(name, start, value);
}
}
@Override
public void put(int index, Scriptable start, Object value) {
Context cx = Context.getContext();
if (cx != null && cx.hasFeature(Context.FEATURE_ENABLE_JAVA_MAP_ACCESS)) {
map.put(Integer.valueOf(index), Context.jsToJava(value, Object.class));
} else {
super.put(index, start, value);
}
}
@Override
public Object[] getIds() {
Context cx = Context.getCurrentContext();
if (cx != null && cx.hasFeature(Context.FEATURE_ENABLE_JAVA_MAP_ACCESS)) {
List<Object> ids = new ArrayList<>(map.size());
for (Object key : map.keySet()) {
if (key instanceof Integer) {
ids.add(key);
} else {
ids.add(ScriptRuntime.toString(key));
}
}
return ids.toArray();
}
return super.getIds();
}
private static Callable symbol_iterator =
(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) -> {
if (!(thisObj instanceof NativeJavaMap)) {
throw ScriptRuntime.typeErrorById("msg.incompat.call", SymbolKey.ITERATOR);
}
return new NativeJavaMapIterator(scope, ((NativeJavaMap) thisObj).map);
};
private static final class NativeJavaMapIterator extends ES6Iterator {
private static final long serialVersionUID = 1L;
private static final String ITERATOR_TAG = "JavaMapIterator";
static void init(ScriptableObject scope, boolean sealed) {
ES6Iterator.init(scope, sealed, new NativeJavaMapIterator(), ITERATOR_TAG);
}
/** Only for constructing the prototype object. */
private NativeJavaMapIterator() {
super();
}
NativeJavaMapIterator(Scriptable scope, Map<Object, Object> map) {
super(scope, ITERATOR_TAG);
this.iterator = map.entrySet().iterator();
}
@Override
public String getClassName() {
return "Java Map Iterator";
}
@Override
protected boolean isDone(Context cx, Scriptable scope) {
return !iterator.hasNext();
}
@Override
protected Object nextValue(Context cx, Scriptable scope) {
if (!iterator.hasNext()) {
return cx.newArray(scope, new Object[] {Undefined.instance, Undefined.instance});
}
Map.Entry e = iterator.next();
return cx.newArray(scope, new Object[] {e.getKey(), e.getValue()});
}
@Override
protected String getTag() {
return ITERATOR_TAG;
}
private Iterator<Map.Entry<Object, Object>> iterator;
}
}
⏎ org/mozilla/javascript/NativeJavaMap.java
Or download all of them as a single archive file:
File name: rhino-1.7.14-sources.jar File size: 1029165 bytes Release date: 2022-01-06 Download
⇒ Example code to Test rhino-runtime-1.7.14.jar
⇐ Download Rhino JavaScript Binary Package
2022-05-03, ≈192🔥, 1💬
Popular Posts:
JDK 17 java.base.jmod is the JMOD file for JDK 17 Base module. JDK 17 Base module compiled class fil...
maven-settings-builder-3 .8.6.jaris the JAR file for Apache Maven 3.8.6 Settings Builder module. Apa...
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms, it was develo...
How to read XML document from socket connections with the socket\DelayedInput.java provided in the A...
What Is commons-lang3-3.1.jar? commons-lang3-3.1.jar is the JAR file for Apache Commons Lang 3.1, wh...