1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.configuration;
16
17 import java.lang.reflect.Member;
18 import java.lang.reflect.Method;
19 import java.util.AbstractMap;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.htmlunit.corejs.javascript.ScriptableObject;
26 import org.htmlunit.corejs.javascript.Symbol;
27 import org.htmlunit.javascript.HtmlUnitScriptable;
28
29
30
31
32
33
34
35
36
37 public final class ClassConfiguration {
38 private Map<String, PropertyInfo> propertyMap_;
39 private Map<Symbol, Method> symbolMap_;
40 private Map<Symbol, String> symbolConstantMap_;
41 private Map<String, Method> functionMap_;
42 private Map<String, PropertyInfo> staticPropertyMap_;
43 private Map<String, Method> staticFunctionMap_;
44 private List<ConstantInfo> constants_;
45 private final String extendedClassName_;
46 private final Class<? extends HtmlUnitScriptable> hostClass_;
47 private final String hostClassSimpleName_;
48
49
50
51
52 private Map.Entry<String, Member> jsConstructor_;
53 private String jsConstructorAlias_;
54 private final Class<?>[] domClasses_;
55 private final boolean jsObject_;
56 private final String className_;
57
58
59
60
61
62
63
64
65
66
67 public ClassConfiguration(final Class<? extends HtmlUnitScriptable> hostClass, final Class<?>[] domClasses,
68 final boolean jsObject, final String className, final String extendedClassName) {
69 hostClass_ = hostClass;
70 hostClassSimpleName_ = hostClass_.getSimpleName();
71 jsObject_ = jsObject;
72 domClasses_ = domClasses;
73 if (className == null) {
74 className_ = getHostClass().getSimpleName();
75 }
76 else {
77 className_ = className;
78 }
79 extendedClassName_ = extendedClassName;
80 }
81
82 void setJSConstructor(final String name, final Member jsConstructor) {
83 if (jsConstructor_ != null) {
84 throw new IllegalStateException("Can not have two constructors for "
85 + jsConstructor_.getValue().getDeclaringClass().getName());
86 }
87 jsConstructor_ = new AbstractMap.SimpleImmutableEntry<>(name, jsConstructor);
88 }
89
90 void setJSConstructorAlias(final String alias) {
91 if (jsConstructor_ == null) {
92 throw new IllegalStateException("Can not define the constructor alias '"
93 + alias + "' for scriptable class -'"
94 + hostClass_.getName()
95 + "' because there is no JsxConstructor defined");
96 }
97 jsConstructorAlias_ = alias;
98 }
99
100
101
102
103
104
105
106 public void addProperty(final String name, final Method getter, final Method setter) {
107 final PropertyInfo info = new PropertyInfo(getter, setter);
108 if (propertyMap_ == null) {
109 propertyMap_ = new HashMap<>();
110 }
111 propertyMap_.put(name, info);
112 }
113
114
115
116
117
118
119
120 public void addStaticProperty(final String name, final Method getter, final Method setter) {
121 final PropertyInfo info = new PropertyInfo(getter, setter);
122 if (staticPropertyMap_ == null) {
123 staticPropertyMap_ = new HashMap<>();
124 }
125 staticPropertyMap_.put(name, info);
126 }
127
128
129
130
131
132
133 public void addConstant(final String name, final Object value) {
134 if (constants_ == null) {
135 constants_ = new ArrayList<>();
136 }
137 int flag = ScriptableObject.READONLY | ScriptableObject.PERMANENT;
138
139 if (getClassName().endsWith("Array")) {
140 flag |= ScriptableObject.DONTENUM;
141 }
142 constants_.add(new ConstantInfo(name, value, flag));
143 }
144
145
146
147
148
149 public Map<String, PropertyInfo> getPropertyMap() {
150 return propertyMap_;
151 }
152
153
154
155
156
157 public Map<Symbol, Method> getSymbolMap() {
158 return symbolMap_;
159 }
160
161
162
163
164
165 public Map<Symbol, String> getSymbolConstantMap() {
166 return symbolConstantMap_;
167 }
168
169
170
171
172
173 public Map<String, PropertyInfo> getStaticPropertyMap() {
174 return staticPropertyMap_;
175 }
176
177
178
179
180
181 public Map<String, Method> getFunctionMap() {
182 return functionMap_;
183 }
184
185
186
187
188
189 public Map<String, Method> getStaticFunctionMap() {
190 return staticFunctionMap_;
191 }
192
193
194
195
196
197 public List<ConstantInfo> getConstants() {
198 return constants_;
199 }
200
201
202
203
204
205
206 public void addSymbol(final Symbol symbol, final Method method) {
207 if (symbolMap_ == null) {
208 symbolMap_ = new HashMap<>();
209 }
210 symbolMap_.put(symbol, method);
211 }
212
213
214
215
216
217
218 public void addSymbolConstant(final Symbol symbol, final String value) {
219 if (symbolConstantMap_ == null) {
220 symbolConstantMap_ = new HashMap<>();
221 }
222 symbolConstantMap_.put(symbol, value);
223 }
224
225
226
227
228
229
230 public void addFunction(final String name, final Method method) {
231 if (functionMap_ == null) {
232 functionMap_ = new HashMap<>();
233 }
234 functionMap_.put(name, method);
235 }
236
237
238
239
240
241
242 public void addStaticFunction(final String name, final Method method) {
243 if (staticFunctionMap_ == null) {
244 staticFunctionMap_ = new HashMap<>();
245 }
246 staticFunctionMap_.put(name, method);
247 }
248
249
250
251
252
253
254 public String getExtendedClassName() {
255 return extendedClassName_;
256 }
257
258
259
260
261
262 public Class<? extends HtmlUnitScriptable> getHostClass() {
263 return hostClass_;
264 }
265
266
267
268
269
270 public String getHostClassSimpleName() {
271 return hostClassSimpleName_;
272 }
273
274
275
276
277
278 public Map.Entry<String, Member> getJsConstructor() {
279 return jsConstructor_;
280 }
281
282
283
284
285
286
287
288 public String getJsConstructorAlias() {
289 return jsConstructorAlias_;
290 }
291
292
293
294
295
296
297 public Class<?>[] getDomClasses() {
298 return domClasses_;
299 }
300
301
302
303
304
305
306 public boolean isJsObject() {
307 return jsObject_;
308 }
309
310
311
312
313
314 public String getClassName() {
315 return className_;
316 }
317
318
319
320
321
322 public static class PropertyInfo {
323 private final Method readMethod_;
324 private final Method writeMethod_;
325
326
327
328
329
330
331
332 public PropertyInfo(final Method readMethod, final Method writeMethod) {
333 readMethod_ = readMethod;
334 writeMethod_ = writeMethod;
335 }
336
337
338
339
340
341
342 public Method getReadMethod() {
343 return readMethod_;
344 }
345
346
347
348
349
350
351 public Method getWriteMethod() {
352 return writeMethod_;
353 }
354 }
355
356
357
358
359 public static class ConstantInfo {
360 private final String name_;
361 private final Object value_;
362 private final int flag_;
363
364
365
366
367
368
369
370
371 public ConstantInfo(final String name, final Object value, final int flag) {
372 name_ = name;
373 value_ = value;
374 flag_ = flag;
375 }
376
377
378
379
380
381
382 public String getName() {
383 return name_;
384 }
385
386
387
388
389
390
391 public Object getValue() {
392 return value_;
393 }
394
395
396
397
398
399
400 public int getFlag() {
401 return flag_;
402 }
403 }
404 }