Search in sources :

Example 1 with ReadOnlyList

use of com.scratchdisk.list.ReadOnlyList in project scriptographer by scriptographer.

the class RhinoWrapFactory method wrapAsJavaObject.

public Scriptable wrapAsJavaObject(Context cx, Scriptable scope, Object javaObj, Class<?> staticType, boolean newObject) {
    // Keep track of wrappers so that if a given object needs to be
    // wrapped again, take the wrapper from the pool...
    WeakReference<Scriptable> ref = wrappers.get(javaObj);
    Scriptable obj = ref == null ? null : ref.get();
    if (obj == null) {
        boolean cache = true;
        // Allays override staticType and set it to the native type
        // of the class. Sometimes the interface used to access an
        // object of a certain class is passed. But why should it
        // be wrapped that way?
        staticType = javaObj.getClass();
        if (staticType != null && staticType.isArray())
            obj = new ExtendedJavaArray(scope, javaObj, staticType, true);
        else {
            if (javaObj instanceof ReadOnlyList) {
                obj = new ListWrapper(scope, (ReadOnlyList) javaObj, staticType, true);
            } else if (javaObj instanceof Map) {
                obj = new MapWrapper(scope, (Map) javaObj);
            } else {
                obj = wrapCustom(cx, scope, javaObj, staticType, newObject);
                if (obj == null) {
                    obj = new ExtendedJavaObject(scope, javaObj, staticType, true);
                    // See the comment in wrapCustom for an explanation of
                    // this:
                    cache = false;
                }
            }
        }
        if (cache)
            wrappers.put(javaObj, new WeakReference<Scriptable>(obj));
    }
    return obj;
}
Also used : ReadOnlyList(com.scratchdisk.list.ReadOnlyList) WeakReference(java.lang.ref.WeakReference) Scriptable(org.mozilla.javascript.Scriptable) IdentityHashMap(java.util.IdentityHashMap) WeakIdentityHashMap(com.scratchdisk.util.WeakIdentityHashMap) Map(java.util.Map)

Example 2 with ReadOnlyList

use of com.scratchdisk.list.ReadOnlyList in project scriptographer by scriptographer.

the class ListWrapper method getDefaultValue.

public Object getDefaultValue(Class hint) {
    if (hint == null || hint == ScriptRuntime.StringClass) {
        StringBuffer buffer = new StringBuffer();
        ReadOnlyList list = (ReadOnlyList) javaObject;
        for (int i = 0, l = list.size(); i < l; i++) {
            if (i > 0)
                buffer.append(",");
            Object entry = list.get(i);
            if (entry != null) {
                Scriptable obj = Context.toObject(entry, this);
                buffer.append(obj.getDefaultValue(hint));
            } else {
                buffer.append("null");
            }
        }
        return buffer.toString();
    } else {
        return super.getDefaultValue(hint);
    }
}
Also used : ReadOnlyList(com.scratchdisk.list.ReadOnlyList) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

ReadOnlyList (com.scratchdisk.list.ReadOnlyList)2 Scriptable (org.mozilla.javascript.Scriptable)2 WeakIdentityHashMap (com.scratchdisk.util.WeakIdentityHashMap)1 WeakReference (java.lang.ref.WeakReference)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1