AbstractJSObject.java revision 1631:93854b0b5e5e
1/*
2 * Copyright (c) 2010, 2013, 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.api.scripting;
27
28import java.util.Collection;
29import java.util.Collections;
30import java.util.Objects;
31import java.util.Set;
32
33/**
34 * This is the base class for nashorn ScriptObjectMirror class.
35 *
36 * This class can also be subclassed by an arbitrary Java class. Nashorn will
37 * treat objects of such classes just like nashorn script objects. Usual nashorn
38 * operations like obj[i], obj.foo, obj.func(), delete obj.foo will be delegated
39 * to appropriate method call of this class.
40 *
41 * @since 1.8u40
42 */
43public abstract class AbstractJSObject implements JSObject {
44    /**
45     * The default constructor.
46     */
47    public AbstractJSObject() {}
48
49    /**
50     * @implSpec This implementation always throws UnsupportedOperationException
51     */
52    @Override
53    public Object call(final Object thiz, final Object... args) {
54        throw new UnsupportedOperationException("call");
55    }
56
57    /**
58     * @implSpec This implementation always throws UnsupportedOperationException
59     */
60    @Override
61    public Object newObject(final Object... args) {
62        throw new UnsupportedOperationException("newObject");
63    }
64
65    /**
66     * @implSpec This imlementation always throws UnsupportedOperationException
67     */
68    @Override
69    public Object eval(final String s) {
70        throw new UnsupportedOperationException("eval");
71    }
72
73    /**
74     * @implSpec This implementation always returns null
75     */
76    @Override
77    public Object getMember(final String name) {
78        Objects.requireNonNull(name);
79        return null;
80    }
81
82    /**
83     * @implSpec This implementation always returns null
84     */
85    @Override
86    public Object getSlot(final int index) {
87        return null;
88    }
89
90    /**
91     * @implSpec This implementation always returns false
92     */
93    @Override
94    public boolean hasMember(final String name) {
95        Objects.requireNonNull(name);
96        return false;
97    }
98
99    /**
100     * @implSpec This implementation always returns false
101     */
102    @Override
103    public boolean hasSlot(final int slot) {
104        return false;
105    }
106
107    /**
108     * @implSpec This implementation is a no-op
109     */
110    @Override
111    public void removeMember(final String name) {
112        Objects.requireNonNull(name);
113        //empty
114    }
115
116    /**
117     * @implSpec This implementation is a no-op
118     */
119    @Override
120    public void setMember(final String name, final Object value) {
121        Objects.requireNonNull(name);
122        //empty
123    }
124
125    /**
126     * @implSpec This implementation is a no-op
127     */
128    @Override
129    public void setSlot(final int index, final Object value) {
130        //empty
131    }
132
133    // property and value iteration
134
135    /**
136     * @implSpec This implementation returns empty set
137     */
138    @Override
139    public Set<String> keySet() {
140        return Collections.emptySet();
141    }
142
143    /**
144     * @implSpec This implementation returns empty set
145     */
146    @Override
147    public Collection<Object> values() {
148        return Collections.emptySet();
149    }
150
151    // JavaScript instanceof check
152
153    /**
154     * @implSpec This implementation always returns false
155     */
156    @Override
157    public boolean isInstance(final Object instance) {
158        return false;
159    }
160
161    @Override
162    public boolean isInstanceOf(final Object clazz) {
163        if (clazz instanceof JSObject) {
164            return ((JSObject)clazz).isInstance(this);
165        }
166
167        return false;
168    }
169
170    @Override
171    public String getClassName() {
172        return getClass().getName();
173    }
174
175    /**
176     * @implSpec This implementation always returns false
177     */
178    @Override
179    public boolean isFunction() {
180        return false;
181    }
182
183    /**
184     * @implSpec This implementation always returns false
185     */
186    @Override
187    public boolean isStrictFunction() {
188        return false;
189    }
190
191    /**
192     * @implSpec This implementation always returns false
193     */
194    @Override
195    public boolean isArray() {
196        return false;
197    }
198
199    /**
200     * Returns this object's numeric value.
201     *
202     * @return this object's numeric value.
203     * @deprecated use {@link #getDefaultValue(Class)} with {@link Number} hint instead.
204     */
205    @Override @Deprecated
206    public double toNumber() {
207        return Double.NaN;
208    }
209
210    /**
211     * When passed an {@link AbstractJSObject}, invokes its {@link #getDefaultValue(Class)} method. When passed any
212     * other {@link JSObject}, it will obtain its {@code [[DefaultValue]]} method as per ECMAScript 5.1 section
213     * 8.6.2.
214     *
215     * @param jsobj the {@link JSObject} whose {@code [[DefaultValue]]} is obtained.
216     * @param hint the type hint. Should be either {@code null}, {@code Number.class} or {@code String.class}.
217     * @return this object's default value.
218     * @throws UnsupportedOperationException if the conversion can't be performed. The engine will convert this
219     * exception into a JavaScript {@code TypeError}.
220     * @deprecated use {@link JSObject#getDefaultValue(Class)} instead.
221     */
222    @Deprecated
223    public static Object getDefaultValue(final JSObject jsobj, final Class<?> hint) {
224        return jsobj.getDefaultValue(hint);
225    }
226}
227