19
19
20
20
package org .elasticsearch .script .javascript .support ;
21
21
22
+ import org .mozilla .javascript .NativeJavaObject ;
22
23
import org .mozilla .javascript .Scriptable ;
23
24
import org .mozilla .javascript .Undefined ;
24
25
import org .mozilla .javascript .Wrapper ;
25
26
27
+ import java .util .Arrays ;
26
28
import java .util .List ;
27
29
28
30
/**
29
31
*
30
32
*/
31
- public class NativeList implements Scriptable , Wrapper {
33
+ public class NativeList extends NativeJavaObject implements Scriptable , Wrapper {
32
34
private static final long serialVersionUID = 3664761893203964569L ;
35
+ private static final String LENGTH_PROPERTY = "length" ;
33
36
34
- private List <Object > list ;
35
- private Scriptable parentScope ;
36
- private Scriptable prototype ;
37
+ private final List <Object > list ;
37
38
38
39
39
- public static NativeList wrap (Scriptable scope , List <Object > list ) {
40
- return new NativeList (scope , list );
40
+ public static NativeList wrap (Scriptable scope , List <Object > list , Class <?> staticType ) {
41
+ return new NativeList (scope , list , staticType );
41
42
}
42
43
43
- public NativeList (Scriptable scope , List <Object > list ) {
44
- this . parentScope = scope ;
44
+ private NativeList (Scriptable scope , List <Object > list , Class <?> staticType ) {
45
+ super ( scope , list , staticType ) ;
45
46
this .list = list ;
46
47
}
47
48
@@ -66,10 +67,10 @@ public String getClassName() {
66
67
*/
67
68
68
69
public Object get (String name , Scriptable start ) {
69
- if ("length" .equals (name )) {
70
+ if (LENGTH_PROPERTY .equals (name )) {
70
71
return list .size ();
71
72
} else {
72
- return Undefined . instance ;
73
+ return super . get ( name , start ) ;
73
74
}
74
75
}
75
76
@@ -78,7 +79,7 @@ public Object get(String name, Scriptable start) {
78
79
*/
79
80
80
81
public Object get (int index , Scriptable start ) {
81
- if (index < 0 || index >= list . size () ) {
82
+ if (has ( index , start ) == false ) {
82
83
return Undefined .instance ;
83
84
}
84
85
return list .get (index );
@@ -89,10 +90,7 @@ public Object get(int index, Scriptable start) {
89
90
*/
90
91
91
92
public boolean has (String name , Scriptable start ) {
92
- if ("length" .equals (name )) {
93
- return true ;
94
- }
95
- return false ;
93
+ return super .has (name , start ) || LENGTH_PROPERTY .equals (name );
96
94
}
97
95
98
96
/* (non-Javadoc)
@@ -103,15 +101,6 @@ public boolean has(int index, Scriptable start) {
103
101
return index >= 0 && index < list .size ();
104
102
}
105
103
106
- /* (non-Javadoc)
107
- * @see org.mozilla.javascript.Scriptable#put(java.lang.String, org.mozilla.javascript.Scriptable, java.lang.Object)
108
- */
109
-
110
- @ SuppressWarnings ("unchecked" )
111
- public void put (String name , Scriptable start , Object value ) {
112
- // do nothing here...
113
- }
114
-
115
104
/* (non-Javadoc)
116
105
* @see org.mozilla.javascript.Scriptable#put(int, org.mozilla.javascript.Scriptable, java.lang.Object)
117
106
*/
@@ -124,14 +113,6 @@ public void put(int index, Scriptable start, Object value) {
124
113
}
125
114
}
126
115
127
- /* (non-Javadoc)
128
- * @see org.mozilla.javascript.Scriptable#delete(java.lang.String)
129
- */
130
-
131
- public void delete (String name ) {
132
- // nothing here
133
- }
134
-
135
116
/* (non-Javadoc)
136
117
* @see org.mozilla.javascript.Scriptable#delete(int)
137
118
*/
@@ -140,59 +121,20 @@ public void delete(int index) {
140
121
list .remove (index );
141
122
}
142
123
143
- /* (non-Javadoc)
144
- * @see org.mozilla.javascript.Scriptable#getPrototype()
145
- */
146
-
147
- public Scriptable getPrototype () {
148
- return this .prototype ;
149
- }
150
-
151
- /* (non-Javadoc)
152
- * @see org.mozilla.javascript.Scriptable#setPrototype(org.mozilla.javascript.Scriptable)
153
- */
154
-
155
- public void setPrototype (Scriptable prototype ) {
156
- this .prototype = prototype ;
157
- }
158
-
159
- /* (non-Javadoc)
160
- * @see org.mozilla.javascript.Scriptable#getParentScope()
161
- */
162
-
163
- public Scriptable getParentScope () {
164
- return this .parentScope ;
165
- }
166
-
167
- /* (non-Javadoc)
168
- * @see org.mozilla.javascript.Scriptable#setParentScope(org.mozilla.javascript.Scriptable)
169
- */
170
-
171
- public void setParentScope (Scriptable parent ) {
172
- this .parentScope = parent ;
173
- }
174
-
175
124
/* (non-Javadoc)
176
125
* @see org.mozilla.javascript.Scriptable#getIds()
177
126
*/
178
127
179
128
public Object [] getIds () {
180
- int size = list .size ();
181
- Object [] ids = new Object [size ];
129
+ final Object [] javaObjectIds = super .getIds ();
130
+ final int size = list .size ();
131
+ final Object [] ids = Arrays .copyOf (javaObjectIds , javaObjectIds .length + size );
182
132
for (int i = 0 ; i < size ; ++i ) {
183
- ids [i ] = i ;
133
+ ids [javaObjectIds . length + i ] = i ;
184
134
}
185
135
return ids ;
186
136
}
187
137
188
- /* (non-Javadoc)
189
- * @see org.mozilla.javascript.Scriptable#getDefaultValue(java.lang.Class)
190
- */
191
-
192
- public Object getDefaultValue (Class hint ) {
193
- return null ;
194
- }
195
-
196
138
/* (non-Javadoc)
197
139
* @see org.mozilla.javascript.Scriptable#hasInstance(org.mozilla.javascript.Scriptable)
198
140
*/
0 commit comments