Debian Codegraph @

rhino/src/org/mozilla/javascript/NativeJavaArray.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.lang.reflect.Array;
10
11 /**
12 * This class reflects Java arrays into the JavaScript environment.
13 *
14 * @author Mike Shaver
15 * @see NativeJavaClass
16 * @see NativeJavaObject
17 * @see NativeJavaPackage
18 */
19
20 public class NativeJavaArray extends NativeJavaObject
21 {
22 static final long serialVersionUID = -924022554283675333L;
23
24 @Override
25 public String getClassName() {
26 return "JavaArray";
27 }
28
29 public static NativeJavaArray wrap(Scriptable scope, Object array) {
30 return new NativeJavaArray(scope, array);
31 }
32
33 @Override
34 public Object unwrap() {
35 return array;
36 }
37
38 public NativeJavaArray(Scriptable scope, Object array) {
39 super(scope, null, ScriptRuntime.ObjectClass);
40 Class<?> cl = array.getClass();
41 if (!cl.isArray()) {
42 throw new RuntimeException("Array expected");
43 }
44 this.array = array;
45 this.length = Array.getLength(array);
46 this.cls = cl.getComponentType();
47 }
48
49 @Override
50 public boolean has(String id, Scriptable start) {
51 return id.equals("length") || super.has(id, start);
52 }
53
54 @Override
55 public boolean has(int index, Scriptable start) {
56 return 0 <= index && index < length;
57 }
58
59 @Override
60 public Object get(String id, Scriptable start) {
61 if (id.equals("length"))
62 return Integer.valueOf(length);
63 Object result = super.get(id, start);
64 if (result == NOT_FOUND &&
65 !ScriptableObject.hasProperty(getPrototype(), id))
66 {
67 throw Context.reportRuntimeError2(
68 "msg.java.member.not.found", array.getClass().getName(), id);
69 }
70 return result;
71 }
72
73 @Override
74 public Object get(int index, Scriptable start) {
75 if (0 <= index && index < length) {
76 Context cx = Context.getContext();
77 Object obj = Array.get(array, index);
78 return cx.getWrapFactory().wrap(cx, this, obj, cls);
79 }
80 return Undefined.instance;
81 }
82
83 @Override
84 public void put(String id, Scriptable start, Object value) {
85 // Ignore assignments to "length"--it's readonly.
86 if (!id.equals("length"))
87 throw Context.reportRuntimeError1(
88 "msg.java.array.member.not.found", id);
89 }
90
91 @Override
92 public void put(int index, Scriptable start, Object value) {
93 if (0 <= index && index < length) {
94 Array.set(array, index, Context.jsToJava(value, cls));
95 }
96 else {
97 throw Context.reportRuntimeError2(
98 "msg.java.array.index.out.of.bounds", String.valueOf(index),
99 String.valueOf(length - 1));
100 }
101 }
102
103 @Override
104 public Object getDefaultValue(Class<?> hint) {
105 if (hint == null || hint == ScriptRuntime.StringClass)
106 return array.toString();
107 if (hint == ScriptRuntime.BooleanClass)
108 return Boolean.TRUE;
109 if (hint == ScriptRuntime.NumberClass)
110 return ScriptRuntime.NaNobj;
111 return this;
112 }
113
114 @Override
115 public Object[] getIds() {
116 Object[] result = new Object[length];
117 int i = length;
118 while (--i >= 0)
119 result[i] = Integer.valueOf(i);
120 return result;
121 }
122
123 @Override
124 public boolean hasInstance(Scriptable value) {
125 if (!(value instanceof Wrapper))
126 return false;
127 Object instance = ((Wrapper)value).unwrap();
128 return cls.isInstance(instance);
129 }
130
131 @Override
132 public Scriptable getPrototype() {
133 if (prototype == null) {
134 prototype =
135 ScriptableObject.getArrayPrototype(this.getParentScope());
136 }
137 return prototype;
138 }
139
140 Object array;
141 int length;
142 Class<?> cls;
143 }