Debian Codegraph @

rhino/src/org/mozilla/javascript/Undefined.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import java.io.Serializable;
10 import java.lang.reflect.InvocationHandler;
11 import java.lang.reflect.Method;
12 import java.lang.reflect.Proxy;
13
14 /**
15 * This class implements the Undefined value in JavaScript.
16 */
17 public class Undefined implements Serializable
18 {
19 static final long serialVersionUID = 9195680630202616767L;
20
21 public static final Object instance = new Undefined();
22
23 private Undefined()
24 {
25 }
26
27 public Object readResolve()
28 {
29 return instance;
30 }
31
32 @Override
33 public boolean equals(Object obj) {
34 return isUndefined(obj) || super.equals(obj);
35 }
36
37 @Override
38 public int hashCode() {
39 // All instances of Undefined are equivalent!
40 return 0;
41 }
42
43 public static final Scriptable SCRIPTABLE_UNDEFINED;
44
45 static {
46 SCRIPTABLE_UNDEFINED = (Scriptable) Proxy.newProxyInstance(Undefined.class.getClassLoader(), new Class[]{Scriptable.class}, new InvocationHandler() {
47 @Override
48 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
49 if (method.getName().equals("toString")) return "undefined";
50 if (method.getName().equals("equals")) {
51 return args.length > 0 && isUndefined(args[0]);
52 }
53 throw new UnsupportedOperationException("undefined doesn't support " + method.getName());
54 }
55 });
56 }
57
58 public static boolean isUndefined(Object obj)
59 {
60 return Undefined.instance == obj || Undefined.SCRIPTABLE_UNDEFINED == obj;
61 }
62 }