src/jdk/nashorn/internal/runtime/JSONListAdapter.java

changeset 1394
07f32a26bc1e
child 1395
fb99aafd5c0d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/jdk/nashorn/internal/runtime/JSONListAdapter.java	Tue Jun 02 10:55:17 2015 +0200
     1.3 @@ -0,0 +1,156 @@
     1.4 +/*
     1.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package jdk.nashorn.internal.runtime;
    1.30 +
    1.31 +import java.util.Collection;
    1.32 +import java.util.List;
    1.33 +import java.util.Set;
    1.34 +import jdk.nashorn.api.scripting.JSObject;
    1.35 +import jdk.nashorn.api.scripting.ScriptObjectMirror;
    1.36 +import jdk.nashorn.internal.objects.Global;
    1.37 +
    1.38 +/**
    1.39 + * A {@link ListAdapter} that also implements {@link JSObject}. Named {@code JSONListAdapter} as it is used as a
    1.40 + * {@code JSObject} implementing the {@link List} interface, which is the expected interface to be implemented by
    1.41 + * JSON-parsed arrays when they are handled in Java. We aren't implementing {@link JSObject} on {@link ListAdapter}
    1.42 + * directly since that'd have implications for other uses of list adapter (e.g. interferences of JSObject default
    1.43 + * value calculation vs. List's {@code toString()} etc.)
    1.44 + */
    1.45 +public final class JSONListAdapter extends ListAdapter implements JSObject {
    1.46 +    /**
    1.47 +     * Creates a new JSON list adapter.
    1.48 +     * @param obj the underlying object being exposed as a list.
    1.49 +     * @param global the home global of the underlying object.
    1.50 +     */
    1.51 +    public JSONListAdapter(final JSObject obj, final Global global) {
    1.52 +        super(obj, global);
    1.53 +    }
    1.54 +
    1.55 +    /**
    1.56 +     * Unwraps this adapter into its underlying non-JSObject representative.
    1.57 +     * @param homeGlobal the home global for unwrapping
    1.58 +     * @return either the unwrapped object or this if it should not be unwrapped in the specified global.
    1.59 +     */
    1.60 +    public Object unwrap(final Object homeGlobal) {
    1.61 +        final Object unwrapped = ScriptObjectMirror.unwrap(obj, homeGlobal);
    1.62 +        return unwrapped != obj ? unwrapped : this;
    1.63 +    }
    1.64 +
    1.65 +    @Override
    1.66 +    public Object call(final Object thiz, final Object... args) {
    1.67 +        return obj.call(thiz, args);
    1.68 +    }
    1.69 +
    1.70 +    @Override
    1.71 +    public Object newObject(final Object... args) {
    1.72 +        return obj.newObject(args);
    1.73 +    }
    1.74 +
    1.75 +    @Override
    1.76 +    public Object eval(final String s) {
    1.77 +        return obj.eval(s);
    1.78 +    }
    1.79 +
    1.80 +    @Override
    1.81 +    public Object getMember(final String name) {
    1.82 +        return obj.getMember(name);
    1.83 +    }
    1.84 +
    1.85 +    @Override
    1.86 +    public Object getSlot(final int index) {
    1.87 +        return obj.getSlot(index);
    1.88 +    }
    1.89 +
    1.90 +    @Override
    1.91 +    public boolean hasMember(final String name) {
    1.92 +        return obj.hasMember(name);
    1.93 +    }
    1.94 +
    1.95 +    @Override
    1.96 +    public boolean hasSlot(final int slot) {
    1.97 +        return obj.hasSlot(slot);
    1.98 +    }
    1.99 +
   1.100 +    @Override
   1.101 +    public void removeMember(final String name) {
   1.102 +        obj.removeMember(name);
   1.103 +    }
   1.104 +
   1.105 +    @Override
   1.106 +    public void setMember(final String name, final Object value) {
   1.107 +        obj.setMember(name, value);
   1.108 +    }
   1.109 +
   1.110 +    @Override
   1.111 +    public void setSlot(final int index, final Object value) {
   1.112 +        obj.setSlot(index, value);
   1.113 +    }
   1.114 +
   1.115 +    @Override
   1.116 +    public Set<String> keySet() {
   1.117 +        return obj.keySet();
   1.118 +    }
   1.119 +
   1.120 +    @Override
   1.121 +    public Collection<Object> values() {
   1.122 +        return obj.values();
   1.123 +    }
   1.124 +
   1.125 +    @Override
   1.126 +    public boolean isInstance(final Object instance) {
   1.127 +        return obj.isInstance(instance);
   1.128 +    }
   1.129 +
   1.130 +    @Override
   1.131 +    public boolean isInstanceOf(final Object clazz) {
   1.132 +        return obj.isInstanceOf(clazz);
   1.133 +    }
   1.134 +
   1.135 +    @Override
   1.136 +    public String getClassName() {
   1.137 +        return obj.getClassName();
   1.138 +    }
   1.139 +
   1.140 +    @Override
   1.141 +    public boolean isFunction() {
   1.142 +        return obj.isFunction();
   1.143 +    }
   1.144 +
   1.145 +    @Override
   1.146 +    public boolean isStrictFunction() {
   1.147 +        return obj.isStrictFunction();
   1.148 +    }
   1.149 +
   1.150 +    @Override
   1.151 +    public boolean isArray() {
   1.152 +        return obj.isArray();
   1.153 +    }
   1.154 +
   1.155 +    @Override @Deprecated
   1.156 +    public double toNumber() {
   1.157 +        return obj.toNumber();
   1.158 +    }
   1.159 +}

mercurial