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 public String getExtendedClassName() {
253 return extendedClassName_;
254 }
255
256
257
258
259
260 public Class<? extends HtmlUnitScriptable> getHostClass() {
261 return hostClass_;
262 }
263
264
265
266
267 public String getHostClassSimpleName() {
268 return hostClassSimpleName_;
269 }
270
271
272
273
274
275 public Map.Entry<String, Member> getJsConstructor() {
276 return jsConstructor_;
277 }
278
279
280
281
282
283 public String getJsConstructorAlias() {
284 return jsConstructorAlias_;
285 }
286
287
288
289
290
291
292 public Class<?>[] getDomClasses() {
293 return domClasses_;
294 }
295
296
297
298
299 public boolean isJsObject() {
300 return jsObject_;
301 }
302
303
304
305
306
307 public String getClassName() {
308 return className_;
309 }
310
311
312
313
314
315 public static class PropertyInfo {
316 private final Method readMethod_;
317 private final Method writeMethod_;
318
319
320
321
322
323
324
325 public PropertyInfo(final Method readMethod, final Method writeMethod) {
326 readMethod_ = readMethod;
327 writeMethod_ = writeMethod;
328 }
329
330
331
332
333 public Method getReadMethod() {
334 return readMethod_;
335 }
336
337
338
339
340 public Method getWriteMethod() {
341 return writeMethod_;
342 }
343 }
344
345
346
347
348 public static class ConstantInfo {
349 private final String name_;
350 private final Object value_;
351 private final int flag_;
352
353
354
355
356
357
358
359
360 public ConstantInfo(final String name, final Object value, final int flag) {
361 name_ = name;
362 value_ = value;
363 flag_ = flag;
364 }
365
366
367
368
369 public String getName() {
370 return name_;
371 }
372
373
374
375
376 public Object getValue() {
377 return value_;
378 }
379
380
381
382
383 public int getFlag() {
384 return flag_;
385 }
386 }
387 }