-
Notifications
You must be signed in to change notification settings - Fork 53
/
ModelImpl.java
executable file
·568 lines (463 loc) · 18.1 KB
/
ModelImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.hk2.xml.internal;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import javax.xml.namespace.QName;
import org.glassfish.hk2.utilities.general.GeneralUtilities;
import org.glassfish.hk2.utilities.reflection.ClassReflectionHelper;
import org.glassfish.hk2.utilities.reflection.MethodWrapper;
import org.glassfish.hk2.xml.internal.alt.AltMethod;
import org.glassfish.hk2.xml.internal.alt.clazz.MethodAltMethodImpl;
import org.glassfish.hk2.xml.spi.Model;
/**
* This model is a description of the children and non-children nodes
* of a Bean. It contains only Strings or other easy to constructs
* structures so that it can be added to the proxy at build time
* and hence save some reflection at runtime
*
* @author jwells
*
*/
public class ModelImpl implements Model {
private static final long serialVersionUID = 752816761552710497L;
private final ReentrantLock lock = new ReentrantLock();
/** For thread safety on the computed fields */
// This is probably wrong, no need for static lock
private final static ReentrantLock slock = new ReentrantLock();
/** The interface from which the JAXB proxy was created, fully qualified */
private String originalInterface;
/** Calculated at runtime lazily this is the original interface as a class */
private volatile Class<?> originalInterfaceAsClass;
/** The JAXB proxy of the originalInterface, fully qualified */
private String translatedClass;
/** Calculated at runtime lazily this is the proxy as a class */
private volatile Class<?> translatedClassAsClass;
/** If this node can be a root, the xml tag of the root of the document */
private QName rootName;
/** A map from the xml tag to the parented child node */
private final Map<QName, ParentedModel> childrenByName = new LinkedHashMap<QName, ParentedModel>();
/** A map from xml tag to information about the non-child property */
private final Map<QName, ChildDataModel> nonChildProperty = new LinkedHashMap<QName, ChildDataModel>();
/** A map from xml tag to child data, ordered */
private final Map<QName, ChildDescriptor> allChildren = new LinkedHashMap<QName, ChildDescriptor>();
/** If this node has a key, this is the property name of the key */
private QName keyProperty;
/**
* These are calculated values and only filled in when asked for
*/
private Set<QName> unKeyedChildren = null;
private Set<QName> keyedChildren = null;
private transient JAUtilities jaUtilities = null;
private ClassLoader myLoader;
private Map<String, String> keyToJavaNameMap = null;
private Set<String> allXmlWrappers;
private String valuePropertyNamespace;
private String valueProperty;
private ChildDataModel valueData;
public ModelImpl() {
}
public ModelImpl(String originalInterface,
String translatedClass) {
this.originalInterface = originalInterface;
this.translatedClass = translatedClass;
}
public void setRootName(QName root) {
this.rootName = root;
}
public void setRootName(String rootNamespace, String rootName) {
this.rootName = QNameUtilities.createQName(rootNamespace, rootName);
}
public void setKeyProperty(QName qName) {
String namespace = QNameUtilities.getNamespace(qName);
String name = qName.getLocalPart();
setKeyProperty(namespace, name);
}
public void setKeyProperty(String keyNamespace, String keyProperty) {
this.keyProperty = QNameUtilities.createQName(keyNamespace, keyProperty);
}
public void addChild(
String childInterface,
String namespace,
String xmlTag,
String xmlAlias,
ChildType childType,
String givenDefault,
AliasType aliased,
String childWrapperTag,
String adapter,
boolean required,
String originalMethodName) {
ParentedModel pm = new ParentedModel(childInterface,
namespace,
xmlTag,
xmlAlias,
childType,
givenDefault,
aliased,
childWrapperTag,
adapter,
required,
originalMethodName);
childrenByName.put(QNameUtilities.createQName(namespace, xmlTag), pm);
allChildren.put(QNameUtilities.createQName(namespace, xmlTag), new ChildDescriptor(pm));
}
public void addNonChild(
QName qName,
String defaultValue,
String childType,
String childListType,
boolean isReference,
Format format,
AliasType aliasType,
String aliasOf,
boolean required,
String originalMethodName) {
String namespace = QNameUtilities.getNamespace(qName);
String xmlTag = qName.getLocalPart();
addNonChild(namespace,
xmlTag,
defaultValue,
childType,
childListType,
isReference,
format,
aliasType,
aliasOf,
required,
originalMethodName);
}
public void addNonChild(
String namespace,
String xmlTag,
String defaultValue,
String childType,
String childListType,
boolean isReference,
Format format,
AliasType aliasType,
String aliasOf,
boolean required,
String originalMethodName) {
ChildDataModel cdm = new ChildDataModel(childType,
childListType,
defaultValue,
isReference,
format,
aliasType,
aliasOf,
required,
originalMethodName);
nonChildProperty.put(QNameUtilities.createQName(namespace, xmlTag), cdm);
allChildren.put(QNameUtilities.createQName(namespace, xmlTag), new ChildDescriptor(cdm));
if (Format.VALUE.equals(format)) {
valuePropertyNamespace = namespace;
valueProperty = xmlTag;
valueData = cdm;
}
}
/**
* @return the originalInterface
*/
@Override
public String getOriginalInterface() {
return originalInterface;
}
/**
* @return the translatedClass
*/
@Override
public String getTranslatedClass() {
return translatedClass;
}
/**
* @return the rootName
*/
@Override
public QName getRootName() {
return rootName;
}
/**
* @return the keyProperty
*/
@Override
public QName getKeyProperty() {
return keyProperty;
}
public Map<QName, ParentedModel> getChildrenByName() {
return childrenByName;
}
public Map<QName, ChildDataModel> getNonChildProperties() {
return nonChildProperty;
}
public Map<QName, ChildDescriptor> getAllChildrenDescriptors() {
return allChildren;
}
public String getValuePropertyNamespace() {
return valuePropertyNamespace;
}
public String getValueProperty() {
return valueProperty;
}
public ChildDataModel getValueData() {
return valueData;
}
public Set<String> getAllXmlWrappers() {
slock.lock();
try {
if (allXmlWrappers != null) return allXmlWrappers;
allXmlWrappers = new LinkedHashSet<String>();
for (ParentedModel pm : childrenByName.values()) {
if (pm.getXmlWrapperTag() != null) {
allXmlWrappers.add(pm.getXmlWrapperTag());
}
}
return allXmlWrappers;
} finally {
slock.unlock();
}
}
public ChildDescriptor getChildDescriptor(QName xmlTag) {
return allChildren.get(xmlTag);
}
public Set<QName> getUnKeyedChildren() {
slock.lock();
try {
if (unKeyedChildren != null) return unKeyedChildren;
unKeyedChildren = new HashSet<QName>();
for (Map.Entry<QName, ParentedModel> entry : childrenByName.entrySet()) {
if (entry.getValue().getChildModel().getKeyProperty() != null) continue;
unKeyedChildren.add(entry.getKey());
}
return unKeyedChildren;
} finally {
slock.unlock();
}
}
public Set<QName> getKeyedChildren() {
slock.lock();
try {
if (keyedChildren != null) return keyedChildren;
keyedChildren = new HashSet<QName>();
for (Map.Entry<QName, ParentedModel> entry : childrenByName.entrySet()) {
if (entry.getValue().getChildModel().getKeyProperty() == null) continue;
keyedChildren.add(entry.getKey());
}
return keyedChildren;
} finally {
slock.unlock();
}
}
public void setJAUtilities(JAUtilities jaUtilities, ClassLoader myLoader) {
slock.lock();
try {
if (this.jaUtilities != null) return;
this.jaUtilities = jaUtilities;
this.myLoader = myLoader;
for (ParentedModel pm : childrenByName.values()) {
pm.setRuntimeInformation(jaUtilities, myLoader);
}
for (ChildDataModel cdm : nonChildProperty.values()) {
cdm.setLoader(myLoader);
}
} finally {
slock.unlock();
}
}
public String getDefaultChildValue(String propNamespace, String propName) {
QName propQName = QNameUtilities.createQName(propNamespace, propName);
slock.lock();
try {
ChildDataModel cd = nonChildProperty.get(propQName);
if (cd == null) return null;
return cd.getDefaultAsString();
} finally {
slock.unlock();
}
}
public ModelPropertyType getModelPropertyType(String propNamespace, String propName) {
QName propQName = QNameUtilities.createQName(propNamespace, propName);
slock.lock();
try {
if (nonChildProperty.containsKey(propQName)) return ModelPropertyType.FLAT_PROPERTY;
if (childrenByName.containsKey(propQName)) return ModelPropertyType.TREE_ROOT;
return ModelPropertyType.UNKNOWN;
} finally {
slock.unlock();
}
}
public Class<?> getNonChildType(String propNamespace, String propName) {
QName propQName = QNameUtilities.createQName(propNamespace, propName);
slock.lock();
try {
ChildDataModel cd = nonChildProperty.get(propQName);
if (cd == null) return null;
return cd.getChildTypeAsClass();
} finally {
slock.unlock();
}
}
public ParentedModel getChild(String propNamespace, String propName) {
QName propQName = QNameUtilities.createQName(propNamespace, propName);
slock.lock();
try {
return childrenByName.get(propQName);
} finally {
slock.unlock();
}
}
@Override
public Class<?> getOriginalInterfaceAsClass() {
if (originalInterfaceAsClass != null) return originalInterfaceAsClass;
slock.lock();
try {
if (originalInterfaceAsClass != null) return originalInterfaceAsClass;
originalInterfaceAsClass = GeneralUtilities.loadClass(myLoader, originalInterface);
return originalInterfaceAsClass;
} finally {
slock.unlock();
}
}
@Override
public Class<?> getProxyAsClass() {
if (translatedClassAsClass != null) return translatedClassAsClass;
slock.lock();
try {
if (translatedClassAsClass != null) return translatedClassAsClass;
translatedClassAsClass = GeneralUtilities.loadClass(myLoader, translatedClass);
return translatedClassAsClass;
} finally {
slock.unlock();
}
}
public Collection<ParentedModel> getAllChildren() {
slock.lock();
try {
return Collections.unmodifiableCollection(childrenByName.values());
} finally {
slock.unlock();
}
}
public Map<QName, ParentedModel> getChildrenProperties() {
slock.lock();
try {
return Collections.unmodifiableMap(childrenByName);
} finally {
slock.unlock();
}
}
public Map<QName, ChildDataModel> getAllAttributeChildren() {
Map<QName, ChildDataModel> retVal = new LinkedHashMap<QName, ChildDataModel>();
for (Map.Entry<QName, ChildDataModel> candidate : nonChildProperty.entrySet()) {
QName xmlKey = candidate.getKey();
ChildDataModel childDataModel = candidate.getValue();
if (!Format.ATTRIBUTE.equals(childDataModel.getFormat())) continue;
retVal.put(xmlKey, childDataModel);
}
return retVal;
}
public Map<QName, ChildDescriptor> getAllElementChildren() {
Map<QName, ChildDescriptor> retVal = new LinkedHashMap<QName, ChildDescriptor>();
for (Map.Entry<QName, ChildDescriptor> candidate : allChildren.entrySet()) {
QName xmlKey = candidate.getKey();
ChildDescriptor childDescriptor = candidate.getValue();
if (childDescriptor.getParentedModel() != null) {
// Is an element since it is a child!
retVal.put(xmlKey, childDescriptor);
continue;
}
ChildDataModel childDataModel = childDescriptor.getChildDataModel();
if (!Format.ELEMENT.equals(childDataModel.getFormat())) continue;
retVal.put(xmlKey, childDescriptor);
}
return retVal;
}
public String getJavaNameFromKey(String key, ClassReflectionHelper reflectionHelper) {
lock.lock();
try {
if (keyToJavaNameMap == null) {
keyToJavaNameMap = new LinkedHashMap<String, String>();
}
String result = keyToJavaNameMap.get(key);
if (result != null) return result;
if (reflectionHelper == null) return null;
Class<?> originalInterface = getOriginalInterfaceAsClass();
for (MethodWrapper wrapper : reflectionHelper.getAllMethods(originalInterface)) {
Method m = wrapper.getMethod();
String xmlName;
XmlElement element = m.getAnnotation(XmlElement.class);
if (element == null) {
XmlAttribute attribute = m.getAnnotation(XmlAttribute.class);
if (attribute == null) continue;
xmlName = attribute.name();
}
else {
xmlName = element.name();
}
String keyName;
String javaName = getJavaNameFromGetterOrSetter(m, reflectionHelper);
if ("##default".equals(xmlName)) {
keyName = javaName;
}
else {
keyName = xmlName;
}
if (!key.equals(keyName)) continue;
// Found it!
keyToJavaNameMap.put(key, javaName);
return javaName;
}
return null;
} finally {
lock.unlock();
}
}
private static String getJavaNameFromGetterOrSetter(Method m, ClassReflectionHelper reflectionHelper) {
AltMethod alt = new MethodAltMethodImpl(m, reflectionHelper);
String retVal = Utilities.isGetter(alt);
if (retVal != null) return retVal;
return Utilities.isSetter(alt);
}
@Override
public int hashCode() {
return translatedClass.hashCode();
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof ModelImpl)) return false;
return translatedClass.equals(((ModelImpl) o).getTranslatedClass());
}
@Override
public String toString() {
return "ModelImpl(interface=" + originalInterface +
",class=" + translatedClass +
",root=" + rootName +
",keyProperty=" + keyProperty +
",valuePropertyNamespace=" + valuePropertyNamespace +
",valueProperty=" + valueProperty +
"," + System.identityHashCode(this) + ")";
}
}