JSONListAdapter.java revision 1786:80120e9b3273
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.internal.runtime;
27
28import java.util.Collection;
29import java.util.List;
30import java.util.Set;
31import jdk.nashorn.api.scripting.JSObject;
32import jdk.nashorn.api.scripting.ScriptObjectMirror;
33import jdk.nashorn.internal.objects.Global;
34
35/**
36 * A {@link ListAdapter} that also implements {@link JSObject}. Named {@code JSONListAdapter} as it is used as a
37 * {@code JSObject} implementing the {@link List} interface, which is the expected interface to be implemented by
38 * JSON-parsed arrays when they are handled in Java. We aren't implementing {@link JSObject} on {@link ListAdapter}
39 * directly since that'd have implications for other uses of list adapter (e.g. interferences of JSObject default
40 * value calculation vs. List's {@code toString()} etc.)
41 */
42public final class JSONListAdapter extends ListAdapter implements JSObject {
43    /**
44     * Creates a new JSON list adapter.
45     * @param obj the underlying object being exposed as a list.
46     * @param global the home global of the underlying object.
47     */
48    public JSONListAdapter(final JSObject obj, final Global global) {
49        super(obj, global);
50    }
51
52    /**
53     * Unwraps this adapter into its underlying non-JSObject representative.
54     * @param homeGlobal the home global for unwrapping
55     * @return either the unwrapped object or this if it should not be unwrapped in the specified global.
56     */
57    public Object unwrap(final Object homeGlobal) {
58        final Object unwrapped = ScriptObjectMirror.unwrap(obj, homeGlobal);
59        return unwrapped != obj ? unwrapped : this;
60    }
61
62    @Override
63    public Object call(final Object thiz, final Object... args) {
64        return obj.call(thiz, args);
65    }
66
67    @Override
68    public Object newObject(final Object... args) {
69        return obj.newObject(args);
70    }
71
72    @Override
73    public Object eval(final String s) {
74        return obj.eval(s);
75    }
76
77    @Override
78    public Object getMember(final String name) {
79        return obj.getMember(name);
80    }
81
82    @Override
83    public Object getSlot(final int index) {
84        return obj.getSlot(index);
85    }
86
87    @Override
88    public boolean hasMember(final String name) {
89        return obj.hasMember(name);
90    }
91
92    @Override
93    public boolean hasSlot(final int slot) {
94        return obj.hasSlot(slot);
95    }
96
97    @Override
98    public void removeMember(final String name) {
99        obj.removeMember(name);
100    }
101
102    @Override
103    public void setMember(final String name, final Object value) {
104        obj.setMember(name, value);
105    }
106
107    @Override
108    public void setSlot(final int index, final Object value) {
109        obj.setSlot(index, value);
110    }
111
112    @Override
113    public Set<String> keySet() {
114        return obj.keySet();
115    }
116
117    @Override
118    public Collection<Object> values() {
119        return obj.values();
120    }
121
122    @Override
123    public boolean isInstance(final Object instance) {
124        return obj.isInstance(instance);
125    }
126
127    @Override
128    public boolean isInstanceOf(final Object clazz) {
129        return obj.isInstanceOf(clazz);
130    }
131
132    @Override
133    public String getClassName() {
134        return obj.getClassName();
135    }
136
137    @Override
138    public boolean isFunction() {
139        return obj.isFunction();
140    }
141
142    @Override
143    public boolean isStrictFunction() {
144        return obj.isStrictFunction();
145    }
146
147    @Override
148    public boolean isArray() {
149        return obj.isArray();
150    }
151
152    @Override @Deprecated
153    public double toNumber() {
154        return obj.toNumber();
155    }
156
157    @Override
158    public Object getDefaultValue(final Class<?> hint) throws UnsupportedOperationException {
159        return obj.getDefaultValue(hint);
160    }
161}
162