1 package org.opendaylight.controller.sal.binding.dom.serializer.impl
3 import javassist.ClassPool
4 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
5 import org.opendaylight.yangtools.yang.model.api.SchemaNode
6 import org.opendaylight.controller.sal.binding.codegen.util.JavassistUtils
7 import javassist.CtClass
9 import org.opendaylight.yangtools.yang.common.QName
10 import javassist.CtField
11 import static javassist.Modifier.*
12 import static org.opendaylight.controller.sal.binding.dom.serializer.impl.CodecMapping.*
13 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
14 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
15 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
16 import org.opendaylight.yangtools.sal.binding.model.api.Type
17 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder
18 import org.opendaylight.yangtools.binding.generator.util.Types
19 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
20 import java.util.HashMap
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
22 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
24 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode
26 import java.util.TreeSet
27 import com.google.common.base.Joiner
28 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
29 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
30 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode
31 import static org.opendaylight.controller.sal.binding.impl.util.ClassLoaderUtils.*;
32 import org.opendaylight.yangtools.yang.binding.BindingDeserializer
33 import org.opendaylight.yangtools.yang.binding.BindingCodec
34 import org.slf4j.LoggerFactory
35 import org.opendaylight.controller.sal.binding.codegen.CodeGenerationException
36 import org.opendaylight.yangtools.yang.model.api.ChoiceNode
37 import java.security.ProtectionDomain
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
40 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
41 import java.util.Map.Entry
42 import java.util.AbstractMap.SimpleEntry
43 import org.opendaylight.yangtools.yang.binding.DataObject
44 import org.opendaylight.yangtools.yang.binding.Augmentation
45 import java.util.Iterator
46 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema
47 import java.util.concurrent.ConcurrentHashMap
48 import static extension org.opendaylight.controller.sal.binding.impl.util.YangSchemaUtils.*;
49 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl
50 import org.opendaylight.yangtools.yang.model.util.ExtendedType
51 import org.opendaylight.yangtools.yang.model.util.EnumerationType
52 import static com.google.common.base.Preconditions.*
53 import org.opendaylight.yangtools.yang.model.api.SchemaPath
54 import javassist.CtMethod
55 import javassist.CannotCompileException
56 import java.util.concurrent.locks.Lock
57 import java.util.concurrent.Callable
58 import org.opendaylight.controller.sal.binding.impl.util.ClassLoaderUtils
59 import org.opendaylight.yangtools.yang.model.api.TypeDefinition
60 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition
61 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
62 import java.util.HashSet
63 import java.util.Collections
64 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit
66 import org.opendaylight.controller.sal.binding.codegen.impl.XtendHelper
68 class TransformerGenerator {
70 private static val log = LoggerFactory.getLogger(TransformerGenerator)
72 public static val STRING = Types.typeForClass(String);
73 public static val BOOLEAN = Types.typeForClass(Boolean);
74 public static val INTEGER = Types.typeForClass(Integer);
75 public static val INSTANCE_IDENTIFIER = Types.typeForClass(InstanceIdentifier)
77 //public static val DECIMAL = Types.typeForClass(Decimal);
78 public static val LONG = Types.typeForClass(Long);
80 val ClassPool classPool
81 val extension JavassistUtils utils;
88 var File classFileCapturePath;
91 var Map<Type, Type> typeDefinitions = new ConcurrentHashMap();
94 var Map<Type, GeneratedTypeBuilder> typeToDefinition = new ConcurrentHashMap();
97 var Map<SchemaPath, GeneratedTypeBuilder> pathToType = new ConcurrentHashMap();
100 var Map<Type, SchemaNode> typeToSchemaNode = new ConcurrentHashMap();
103 var Map<Type, AugmentationSchema> typeToAugmentation = new ConcurrentHashMap();
106 var GeneratorListener listener;
108 public static val CLASS_TYPE = Types.typeForClass(Class);
110 public new(ClassPool pool) {
112 utils = new JavassistUtils(pool)
114 BINDING_CODEC = BindingCodec.asCtClass;
115 ctQName = QName.asCtClass
118 def Class<? extends BindingCodec<Map<QName, Object>, Object>> transformerFor(Class<?> inputType) {
119 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
120 val ret = getGeneratedClass(inputType)
122 listener.onClassProcessed(inputType);
123 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
125 val ref = Types.typeForClass(inputType)
126 val node = typeToSchemaNode.get(ref)
127 val typeSpecBuilder = typeToDefinition.get(ref)
128 checkState(typeSpecBuilder !== null, "Could not find typedefinition for %s", inputType.name);
129 val typeSpec = typeSpecBuilder.toInstance();
130 val newret = generateTransformerFor(inputType, typeSpec, node);
131 listener.onClassProcessed(inputType);
132 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
136 def Class<? extends BindingCodec<Map<QName, Object>, Object>> transformerFor(Class<?> inputType, DataSchemaNode node) {
137 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
138 val ret = getGeneratedClass(inputType)
140 listener.onClassProcessed(inputType);
141 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
143 val ref = Types.typeForClass(inputType)
144 var typeSpecBuilder = typeToDefinition.get(ref)
145 if (typeSpecBuilder == null) {
146 typeSpecBuilder = pathToType.get(node.path);
148 var schemaNode = typeToSchemaNode.get(ref);
149 if(schemaNode === null) {
152 checkState(typeSpecBuilder !== null, "Could not find TypeDefinition for %s, $s", inputType.name, node);
153 val typeSpec = typeSpecBuilder.toInstance();
154 val newret = generateTransformerFor(inputType, typeSpec, schemaNode);
155 listener.onClassProcessed(inputType);
156 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
160 def Class<? extends BindingCodec<Map<QName, Object>, Object>> augmentationTransformerFor(Class<?> inputType) {
161 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
162 val ret = getGeneratedClass(inputType)
164 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
166 val ref = Types.typeForClass(inputType)
167 val node = typeToAugmentation.get(ref)
168 val typeSpecBuilder = typeToDefinition.get(ref)
169 val typeSpec = typeSpecBuilder.toInstance();
170 val newret = generateAugmentationTransformerFor(inputType, typeSpec, node);
171 listener.onClassProcessed(inputType);
172 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
176 def Class<? extends BindingCodec<Object, Object>> caseCodecFor(Class<?> inputType, ChoiceCaseNode node) {
177 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
178 val ret = getGeneratedClass(inputType)
180 return ret as Class<? extends BindingCodec<Object, Object>>;
182 val ref = Types.typeForClass(inputType)
183 val typeSpecBuilder = typeToDefinition.get(ref)
184 val typeSpec = typeSpecBuilder.toInstance();
185 val newret = generateCaseCodec(inputType, typeSpec, node);
186 return newret as Class<? extends BindingCodec<Object, Object>>;
190 def Class<? extends BindingCodec<Map<QName, Object>, Object>> keyTransformerForIdentifiable(Class<?> parentType) {
191 return withClassLoaderAndLock(parentType.classLoader, lock) [ |
192 val inputName = parentType.name + "Key";
193 val inputType = loadClassWithTCCL(inputName);
194 val ret = getGeneratedClass(inputType)
196 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
198 val ref = Types.typeForClass(parentType)
199 val node = typeToSchemaNode.get(ref) as ListSchemaNode
200 val typeSpecBuilder = typeToDefinition.get(ref)
201 val typeSpec = typeSpecBuilder.identifierDefinition;
202 val newret = generateKeyTransformerFor(inputType, typeSpec, node);
203 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
207 def getIdentifierDefinition(GeneratedTypeBuilder builder) {
208 val inst = builder.toInstance
209 val keyMethod = inst.methodDefinitions.findFirst[name == "getKey"]
210 return keyMethod.returnType as GeneratedTransferObject
213 def Class<? extends BindingCodec<Map<QName, Object>, Object>> keyTransformerForIdentifier(Class<?> inputType) {
214 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
215 val ret = getGeneratedClass(inputType)
217 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
219 val ref = Types.typeForClass(inputType)
220 val node = typeToSchemaNode.get(ref) as ListSchemaNode
221 val typeSpecBuilder = typeToDefinition.get(ref)
222 val typeSpec = typeSpecBuilder.toInstance();
223 val newret = generateKeyTransformerFor(inputType, typeSpec, node);
224 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
228 private def Class<?> keyTransformerFor(Class<?> inputType, GeneratedType type, ListSchemaNode schema) {
229 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
230 val transformer = getGeneratedClass(inputType)
231 if (transformer != null) {
234 val newret = generateKeyTransformerFor(inputType, type, schema);
235 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
239 private def Class<?> getGeneratedClass(Class<? extends Object> cls) {
242 return loadClassWithTCCL(cls.codecClassName)
243 } catch (ClassNotFoundException e) {
248 private def Class<?> keyTransformer(GeneratedType type, ListSchemaNode node) {
249 val cls = loadClassWithTCCL(type.resolvedName + "Key");
250 keyTransformerFor(cls, type, node);
253 private def serializer(Type type, DataSchemaNode node) {
254 val cls = loadClassWithTCCL(type.resolvedName);
255 transformerFor(cls, node);
258 private def Class<?> valueSerializer(GeneratedTransferObject type, TypeDefinition<?> typeDefinition) {
259 val cls = loadClassWithTCCL(type.resolvedName);
260 val transformer = cls.generatedClass;
261 if (transformer !== null) {
264 var baseType = typeDefinition;
265 while (baseType.baseType != null) {
266 baseType = baseType.baseType;
268 val finalType = baseType;
269 return withClassLoaderAndLock(cls.classLoader, lock) [ |
270 val valueTransformer = generateValueTransformer(cls, type, finalType);
271 return valueTransformer;
275 private def Class<?> valueSerializer(Enumeration type, TypeDefinition<?> typeDefinition) {
276 val cls = loadClassWithTCCL(type.resolvedName);
277 val transformer = cls.generatedClass;
278 if (transformer !== null) {
282 return withClassLoaderAndLock(cls.classLoader, lock) [ |
283 val valueTransformer = generateValueTransformer(cls, type);
284 return valueTransformer;
288 private def generateKeyTransformerFor(Class<? extends Object> inputType, GeneratedType typeSpec, ListSchemaNode node) {
291 //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
292 val properties = typeSpec.allProperties;
293 val ctCls = createClass(inputType.codecClassName) [
294 //staticField(Map,"AUGMENTATION_SERIALIZERS");
295 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
296 staticField(it, IDENTITYREF_CODEC, BindingCodec)
297 staticQNameField(node.QName);
298 implementsType(BINDING_CODEC)
299 method(Object, "toDomStatic", QName, Object) [
300 modifiers = PUBLIC + FINAL + STATIC
303 «QName.name» _resultName;
305 _resultName = «QName.name».create($1,QNAME.getLocalName());
309 java.util.List _childNodes = new java.util.ArrayList();
310 «inputType.resolvedName» value = («inputType.name») $2;
311 «FOR key : node.keyDefinition»
312 «val propertyName = key.getterName»
313 «val keyDef = node.getDataChildByName(key)»
314 «val property = properties.get(propertyName)»
315 «serializeProperty(keyDef, property, propertyName)»;
317 return ($r) java.util.Collections.singletonMap(_resultName,_childNodes);
321 method(Object, "fromDomStatic", QName, Object) [
322 modifiers = PUBLIC + FINAL + STATIC
328 «QName.name» _localQName = $1;
329 java.util.Map _compositeNode = (java.util.Map) $2;
330 boolean _is_empty = true;
331 «FOR key : node.keyDefinition»
332 «val propertyName = key.getterName»
333 «val keyDef = node.getDataChildByName(key)»
334 «val property = properties.get(propertyName)»
335 «deserializeProperty(keyDef, property, propertyName)»;
337 «inputType.resolvedName» _value = new «inputType.name»(«node.keyDefinition.
338 keyConstructorList»);
343 method(Object, "serialize", Object) [
346 java.util.Map.Entry _input = (java.util.Map.Entry) $1;
347 «QName.name» _localQName = («QName.name») _input.getKey();
348 «inputType.name» _keyValue = («inputType.name») _input.getValue();
349 return toDomStatic(_localQName,_keyValue);
353 method(Object, "deserialize", Object) [
355 return fromDomStatic(QNAME,$1);
359 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
360 log.debug("DOM Codec for {} was generated {}", inputType, ret)
361 return ret as Class<? extends BindingCodec<Map<QName,Object>, ?>>;
362 } catch (Exception e) {
363 processException(inputType, e);
368 private def Class<? extends BindingCodec<Object, Object>> generateCaseCodec(Class<?> inputType, GeneratedType type,
369 ChoiceCaseNode node) {
372 //log.info("Generating DOM Codec for {} with {}, TCCL is: {}", inputType, inputType.classLoader,Thread.currentThread.contextClassLoader)
373 val ctCls = createClass(type.codecClassName) [
374 //staticField(Map,"AUGMENTATION_SERIALIZERS");
375 implementsType(BINDING_CODEC)
376 staticQNameField(node.QName);
377 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
378 staticField(it, AUGMENTATION_CODEC, BindingCodec)
379 staticField(it, IDENTITYREF_CODEC, BindingCodec)
380 method(Object, "toDomStatic", QName, Object) [
381 modifiers = PUBLIC + FINAL + STATIC
384 «QName.name» _resultName = «QName.name».create($1,QNAME.getLocalName());
385 java.util.List _childNodes = new java.util.ArrayList();
386 «type.resolvedName» value = («type.resolvedName») $2;
387 «transformDataContainerBody(type, type.allProperties, node)»
388 return ($r) _childNodes;
392 method(Object, "serialize", Object) [
395 java.util.Map.Entry _input = (java.util.Map.Entry) $1;
396 «QName.name» _localName = QNAME;
397 if(_input.getKey() != null) {
398 _localName = («QName.name») _input.getKey();
400 return toDomStatic(_localName,_input.getValue());
404 method(Object, "fromDomStatic", QName, Object) [
405 modifiers = PUBLIC + FINAL + STATIC
406 bodyChecked = deserializeBody(type, node)
408 method(Object, "deserialize", Object) [
411 ////System.out.println("«type.name»#deserialize: " +$1);
412 java.util.Map.Entry _input = (java.util.Map.Entry) $1;
413 return fromDomStatic((«QName.name»)_input.getKey(),_input.getValue());
419 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain) as Class<? extends BindingCodec<Object, Object>>
420 listener?.onDataContainerCodecCreated(inputType, ret);
421 log.debug("DOM Codec for {} was generated {}", inputType, ret)
423 } catch (Exception e) {
424 processException(inputType, e);
429 private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateTransformerFor(
430 Class<?> inputType, GeneratedType typeSpec, SchemaNode node) {
433 //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
434 val ctCls = createClass(typeSpec.codecClassName) [
435 //staticField(Map,"AUGMENTATION_SERIALIZERS");
436 staticQNameField(node.QName);
437 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
438 staticField(it, IDENTITYREF_CODEC, BindingCodec)
439 staticField(it, AUGMENTATION_CODEC, BindingCodec)
440 implementsType(BINDING_CODEC)
441 method(Object, "toDomStatic", QName, Object) [
442 modifiers = PUBLIC + FINAL + STATIC
443 bodyChecked = serializeBodyFacade(typeSpec, node)
445 method(Object, "serialize", Object) [
448 java.util.Map.Entry _input = (java.util.Map.Entry) $1;
449 «QName.name» _localName = QNAME;
450 if(_input.getKey() != null) {
451 _localName = («QName.name») _input.getKey();
453 return toDomStatic(_localName,_input.getValue());
457 method(Object, "fromDomStatic", QName, Object) [
458 modifiers = PUBLIC + FINAL + STATIC
459 bodyChecked = deserializeBody(typeSpec, node)
461 method(Object, "deserialize", Object) [
463 return fromDomStatic(QNAME,$1);
468 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain) as Class<? extends BindingCodec<Map<QName,Object>, Object>>
469 listener?.onDataContainerCodecCreated(inputType, ret);
470 log.debug("DOM Codec for {} was generated {}", inputType, ret)
472 } catch (Exception e) {
473 processException(inputType, e);
478 private def Class<? extends BindingCodec<Map<QName, Object>, Object>> generateAugmentationTransformerFor(
479 Class<?> inputType, GeneratedType type, AugmentationSchema node) {
482 //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
483 val properties = type.allProperties
484 val ctCls = createClass(type.codecClassName) [
485 //staticField(Map,"AUGMENTATION_SERIALIZERS");
486 staticQNameField(node.augmentationQName);
487 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
488 staticField(it, AUGMENTATION_CODEC, BindingCodec)
489 staticField(it, IDENTITYREF_CODEC, BindingCodec)
490 implementsType(BINDING_CODEC)
491 method(Object, "toDomStatic", QName, Object) [
492 modifiers = PUBLIC + FINAL + STATIC
495 ////System.out.println("Qname " + $1);
496 ////System.out.println("Value " + $2);
497 «QName.name» _resultName = «QName.name».create(QNAME,QNAME.getLocalName());
498 java.util.List _childNodes = new java.util.ArrayList();
499 «type.resolvedName» value = («type.resolvedName») $2;
500 «FOR child : node.childNodes»
501 «var signature = properties.getFor(child)»
502 ////System.out.println("«signature.key»" + value.«signature.key»());
503 «serializeProperty(child, signature.value, signature.key)»
505 return ($r) _childNodes;
509 method(Object, "serialize", Object) [
512 java.util.Map.Entry _input = (java.util.Map.Entry) $1;
513 «QName.name» _localName = QNAME;
514 if(_input.getKey() != null) {
515 _localName = («QName.name») _input.getKey();
517 return toDomStatic(_localName,_input.getValue());
521 method(Object, "fromDomStatic", QName, Object) [
522 modifiers = PUBLIC + FINAL + STATIC
525 «QName.name» _localQName = QNAME;
530 java.util.Map _compositeNode = (java.util.Map) $2;
531 System.out.println(_localQName + " " + _compositeNode);
532 «type.builderName» _builder = new «type.builderName»();
533 boolean _is_empty = true;
534 «FOR child : node.childNodes»
535 «val signature = properties.getFor(child)»
536 «deserializeProperty(child, signature.value, signature.key)»
537 _builder.«signature.key.toSetter»(«signature.key»);
542 return _builder.build();
546 method(Object, "deserialize", Object) [
548 return fromDomStatic(QNAME,$1);
553 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain) as Class<? extends BindingCodec<Map<QName,Object>, Object>>
554 listener?.onDataContainerCodecCreated(inputType, ret);
556 } catch (Exception e) {
557 processException(inputType, e);
562 private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateTransformerFor(
563 Class<?> inputType, GeneratedType typeSpec, ChoiceNode node) {
566 //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
567 val ctCls = createClass(typeSpec.codecClassName) [
568 //staticField(Map,"AUGMENTATION_SERIALIZERS");
569 //staticQNameField(inputType);
570 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
571 staticField(it, IDENTITYREF_CODEC, BindingCodec)
572 staticField(it, CLASS_TO_CASE_MAP, Map)
573 staticField(it, COMPOSITE_TO_CASE, Map)
574 //staticField(it,QNAME_TO_CASE_MAP,BindingCodec)
575 implementsType(BINDING_CODEC)
576 method(List, "toDomStatic", QName, Object) [
577 modifiers = PUBLIC + FINAL + STATIC
583 «DataObject.name» _baValue = («DataObject.name») $2;
584 Class _baClass = _baValue.getImplementedInterface();
585 «BINDING_CODEC.name» _codec = «CLASS_TO_CASE_MAP».get(_baClass);
589 java.util.Map.Entry _input = new «SimpleEntry.name»($1,_baValue);
590 Object _ret = _codec.serialize(_input);
591 ////System.out.println("«typeSpec.name»#toDomStatic: " + _ret);
592 return («List.name») _ret;
596 method(Object, "serialize", Object) [
598 throw new «UnsupportedOperationException.name»("Direct invocation not supported.");
601 method(Object, "fromDomStatic", QName, Map) [
602 modifiers = PUBLIC + FINAL + STATIC
605 «BINDING_CODEC.name» _codec = («BINDING_CODEC.name») «COMPOSITE_TO_CASE».get($2);
607 return _codec.deserialize(new «SimpleEntry.name»($1,$2));
613 method(Object, "deserialize", Object) [
615 throw new «UnsupportedOperationException.name»("Direct invocation not supported.");
620 val rawRet = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
621 val ret = rawRet as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
622 listener?.onChoiceCodecCreated(inputType, ret, node);
623 log.debug("DOM Codec for {} was generated {}", inputType, ret)
625 } catch (Exception e) {
626 processException(inputType, e);
631 private def keyConstructorList(List<QName> qnames) {
632 val names = new TreeSet<String>()
633 for (name : qnames) {
634 val fieldName = name.getterName;
635 names.add(fieldName);
637 return Joiner.on(",").join(names);
640 private def serializeBodyFacade(GeneratedType type, SchemaNode node) {
641 val ret = serializeBody(type, node);
645 private def String deserializeBody(GeneratedType type, SchemaNode node) {
646 val ret = deserializeBodyImpl(type, node);
650 private def deserializeKey(GeneratedType type, ListSchemaNode node) {
651 if (node.keyDefinition != null && !node.keyDefinition.empty) {
653 «type.resolvedName»Key getKey = («type.resolvedName»Key) «keyTransformer(type, node).canonicalName».fromDomStatic(_localQName,_compositeNode);
654 _builder.setKey(getKey);
659 private def dispatch String deserializeBodyImpl(GeneratedType type, SchemaNode node) '''
661 «QName.name» _localQName = «QName.name».create($1,QNAME.getLocalName());
666 java.util.Map _compositeNode = (java.util.Map) $2;
667 «type.builderName» _builder = new «type.builderName»();
668 return _builder.build();
672 private def dispatch String deserializeBodyImpl(GeneratedType type, ListSchemaNode node) '''
674 «QName.name» _localQName = «QName.name».create($1,QNAME.getLocalName());
678 java.util.Map _compositeNode = (java.util.Map) $2;
679 System.out.println(_localQName + " " + _compositeNode);
680 «type.builderName» _builder = new «type.builderName»();
681 «deserializeKey(type, node)»
682 «deserializeDataNodeContainerBody(type, node)»
683 «deserializeAugmentations»
684 return _builder.build();
688 private def dispatch String deserializeBodyImpl(GeneratedType type, ContainerSchemaNode node) '''
690 «QName.name» _localQName = «QName.name».create($1,QNAME.getLocalName());
694 java.util.Map _compositeNode = (java.util.Map) $2;
695 System.out.println(_localQName + " " + _compositeNode);
696 «type.builderName» _builder = new «type.builderName»();
697 «deserializeDataNodeContainerBody(type, node)»
698 «deserializeAugmentations»
699 return _builder.build();
703 private def dispatch String deserializeBodyImpl(GeneratedType type, ChoiceCaseNode node) '''
705 «QName.name» _localQName = «QName.name».create($1,QNAME.getLocalName());
710 java.util.Map _compositeNode = (java.util.Map) $2;
711 System.out.println(_localQName + " " + _compositeNode);
712 «type.builderName» _builder = new «type.builderName»();
713 «deserializeDataNodeContainerBody(type, node)»
714 «deserializeAugmentations»
715 return _builder.build();
719 private def deserializeDataNodeContainerBody(GeneratedType type, DataNodeContainer node) {
720 deserializeNodeContainerBodyImpl(type, type.allProperties, node);
723 private def deserializeNodeContainerBodyImpl(GeneratedType type, HashMap<String, Type> properties,
724 DataNodeContainer node) {
726 boolean _is_empty = true;
727 «FOR child : node.childNodes»
728 «val signature = properties.getFor(child)»
729 «IF signature !== null»
730 «deserializeProperty(child, signature.value, signature.key)»
731 _builder.«signature.key.toSetter»(«signature.key»);
738 def deserializeAugmentations() '''
739 java.util.Map _augmentation = (java.util.Map) «AUGMENTATION_CODEC».deserialize(_compositeNode);
740 if(_augmentation != null) {
741 «Iterator.name» _entries = _augmentation.entrySet().iterator();
742 while(_entries.hasNext()) {
743 java.util.Map.Entry _entry = (java.util.Map.Entry) _entries.next();
744 ////System.out.println("Aug. key:" + _entry.getKey());
745 Class _type = (Class) _entry.getKey();
746 «Augmentation.resolvedName» _value = («Augmentation.name») _entry.getValue();
748 _builder.addAugmentation(_type,_value);
754 private def dispatch CharSequence deserializeProperty(ListSchemaNode schema, ParameterizedType type,
755 String propertyName) '''
756 java.util.List _dom_«propertyName» = _compositeNode.get(«QName.name».create(_localQName,"«schema.QName.
758 ////System.out.println("«propertyName»#deCode"+_dom_«propertyName»);
759 java.util.List «propertyName» = new java.util.ArrayList();
760 if(_dom_«propertyName» != null) {
761 java.util.List _serialized = new java.util.ArrayList();
762 java.util.Iterator _iterator = _dom_«propertyName».iterator();
763 boolean _hasNext = _iterator.hasNext();
765 Object _listItem = _iterator.next();
767 ////System.out.println(" item" + _listItem);
768 Object _value = «type.actualTypeArguments.get(0).serializer(schema).resolvedName».fromDomStatic(_localQName,_listItem);
769 ////System.out.println(" value" + _value);
770 «propertyName».add(_value);
771 _hasNext = _iterator.hasNext();
775 ////System.out.println(" list" + «propertyName»);
778 private def dispatch CharSequence deserializeProperty(LeafListSchemaNode schema, ParameterizedType type,
779 String propertyName) '''
780 java.util.List _dom_«propertyName» = _compositeNode.get(«QName.name».create(_localQName,"«schema.QName.
782 java.util.List «propertyName» = new java.util.ArrayList();
783 if(_dom_«propertyName» != null) {
784 java.util.List _serialized = new java.util.ArrayList();
785 java.util.Iterator _iterator = _dom_«propertyName».iterator();
786 boolean _hasNext = _iterator.hasNext();
789 Object _listItem = _iterator.next();
790 if(_listItem instanceof java.util.Map.Entry) {
791 Object _innerValue = ((java.util.Map.Entry) _listItem).getValue();
792 Object _value = «deserializeValue(type.actualTypeArguments.get(0), "_innerValue", schema.type)»;
793 «propertyName».add(_value);
795 _hasNext = _iterator.hasNext();
800 private def dispatch CharSequence deserializeProperty(LeafSchemaNode schema, Type type, String propertyName) '''
801 java.util.List _dom_«propertyName»_list =
802 _compositeNode.get(«QName.name».create(_localQName,"«schema.QName.localName»"));
803 «type.resolvedName» «propertyName» = null;
804 if(_dom_«propertyName»_list != null && _dom_«propertyName»_list.size() > 0) {
806 java.util.Map.Entry _dom_«propertyName» = (java.util.Map.Entry) _dom_«propertyName»_list.get(0);
807 Object _inner_value = _dom_«propertyName».getValue();
808 «propertyName» = «deserializeValue(type, "_inner_value", schema.type)»;
812 private def dispatch CharSequence deserializeProperty(ContainerSchemaNode schema, Type type,
813 String propertyName) '''
814 java.util.List _dom_«propertyName»_list =
815 _compositeNode.get(«QName.name».create(_localQName,"«schema.QName.localName»"));
816 «type.resolvedName» «propertyName» = null;
817 if(_dom_«propertyName»_list != null && _dom_«propertyName»_list.size() > 0) {
819 java.util.Map _dom_«propertyName» = (java.util.Map) _dom_«propertyName»_list.get(0);
820 «propertyName» = «type.serializer(schema).resolvedName».fromDomStatic(_localQName,_dom_«propertyName»);
824 private def dispatch CharSequence deserializeProperty(ChoiceNode schema, Type type, String propertyName) '''
825 «type.resolvedName» «propertyName» = «type.serializer(schema).resolvedName».fromDomStatic(_localQName,_compositeNode);
826 if(«propertyName» != null) {
831 private def dispatch String deserializeValue(GeneratedTransferObject type, String domParameter,
832 TypeDefinition<?> typeDefinition) '''
833 («type.resolvedName») «type.valueSerializer(typeDefinition).resolvedName».fromDomValue(«domParameter»)
836 private def dispatch String deserializeValue(Enumeration type, String domParameter, TypeDefinition<?> typeDefinition) '''
837 («type.resolvedName») «type.valueSerializer(typeDefinition).resolvedName».fromDomValue(«domParameter»)
840 private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateValueTransformer(
841 Class<?> inputType, GeneratedTransferObject typeSpec, TypeDefinition<?> typeDef) {
844 val returnType = typeSpec.valueReturnType;
845 if (returnType == null) {
846 val ctCls = createDummyImplementation(inputType, typeSpec);
847 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
848 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
851 val ctCls = createClass(typeSpec.codecClassName) [
852 //staticField(Map,"AUGMENTATION_SERIALIZERS");
853 if (inputType.isYangBindingAvailable) {
854 implementsType(BINDING_CODEC)
855 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
856 staticField(it, IDENTITYREF_CODEC, BindingCodec)
857 implementsType(BindingDeserializer.asCtClass)
859 method(Object, "toDomValue", Object) [
860 modifiers = PUBLIC + FINAL + STATIC
861 val ctSpec = typeSpec.asCtClass;
864 ////System.out.println("«inputType.simpleName»#toDomValue: "+$1);
869 «typeSpec.resolvedName» _encapsulatedValue = («typeSpec.resolvedName») $1;
870 ////System.out.println("«inputType.simpleName»#toDomValue:Enc: "+_encapsulatedValue);
871 «returnType.resolvedName» _value = _encapsulatedValue.getValue();
872 ////System.out.println("«inputType.simpleName»#toDomValue:DeEnc: "+_value);
873 Object _domValue = «serializeValue(returnType, "_value", null)»;
878 method(Object, "serialize", Object) [
881 return toDomValue($1);
885 method(Object, "fromDomValue", Object) [
886 modifiers = PUBLIC + FINAL + STATIC
889 ////System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
894 «returnType.resolvedName» _simpleValue = «deserializeValue(returnType, "$1", null)»;
895 «typeSpec.resolvedName» _value = new «typeSpec.resolvedName»(_simpleValue);
900 method(Object, "deserialize", Object) [
902 return fromDomValue($1);
908 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
909 log.debug("DOM Codec for {} was generated {}", inputType, ret)
910 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
911 } catch (Exception e) {
912 log.error("Cannot compile DOM Codec for {}", inputType, e);
913 val exception = new CodeGenerationException("Cannot compile Transformator for " + inputType);
914 exception.addSuppressed(e);
919 private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateValueTransformer(
920 Class<?> inputType, GeneratedTransferObject typeSpec, UnionTypeDefinition typeDef) {
922 val ctCls = createClass(typeSpec.codecClassName) [
923 val properties = typeSpec.allProperties;
924 val getterToTypeDefinition = XtendHelper.getTypes(typeDef).toMap[type | type.QName.getterName];
925 //staticField(Map,"AUGMENTATION_SERIALIZERS");
926 if (inputType.isYangBindingAvailable) {
927 implementsType(BINDING_CODEC)
928 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
929 staticField(it, IDENTITYREF_CODEC, BindingCodec)
930 implementsType(BindingDeserializer.asCtClass)
932 method(Object, "toDomValue", Object) [
933 modifiers = PUBLIC + FINAL + STATIC
934 val ctSpec = inputType.asCtClass;
938 ////System.out.println("«inputType.simpleName»#toDomValue: "+$1);
943 «typeSpec.resolvedName» _value = («typeSpec.resolvedName») $1;
944 «FOR property : properties.entrySet»
945 «IF property.key != "getValue"»
946 «property.value.resolvedName» «property.key» = («property.value.resolvedName») _value.«property.
948 if(«property.key» != null) {
949 return «serializeValue(property.value, property.key, getterToTypeDefinition.get(property.key))»;
958 method(Object, "serialize", Object) [
961 return toDomValue($1);
965 method(Object, "fromDomValue", Object) [
966 modifiers = PUBLIC + FINAL + STATIC
969 ////System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
974 if($1 instanceof String) {
975 String _simpleValue = (String) $1;
976 return new «typeSpec.resolvedName»(_simpleValue.toCharArray());
982 method(Object, "deserialize", Object) [
984 return fromDomValue($1);
990 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
991 log.debug("DOM Codec for {} was generated {}", inputType, ret)
992 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
993 } catch (Exception e) {
994 log.error("Cannot compile DOM Codec for {}", inputType, e);
995 val exception = new CodeGenerationException("Cannot compile Transformator for " + inputType);
996 exception.addSuppressed(e);
1002 private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateValueTransformer(
1003 Class<?> inputType, GeneratedTransferObject typeSpec, BitsTypeDefinition typeDef) {
1005 val ctCls = createClass(typeSpec.codecClassName) [
1006 //staticField(Map,"AUGMENTATION_SERIALIZERS");
1007 if (inputType.isYangBindingAvailable) {
1008 implementsType(BINDING_CODEC)
1009 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
1010 staticField(it, IDENTITYREF_CODEC, BindingCodec)
1011 implementsType(BindingDeserializer.asCtClass)
1013 method(Object, "toDomValue", Object) [
1014 modifiers = PUBLIC + FINAL + STATIC
1015 val ctSpec = typeSpec.asCtClass;
1018 ////System.out.println("«inputType.simpleName»#toDomValue: "+$1);
1023 «typeSpec.resolvedName» _encapsulatedValue = («typeSpec.resolvedName») $1;
1024 «HashSet.resolvedName» _value = new «HashSet.resolvedName»();
1025 //System.out.println("«inputType.simpleName»#toDomValue:Enc: "+_encapsulatedValue);
1027 «FOR bit : typeDef.bits»
1028 «val getter = bit.getterName()»
1029 if(Boolean.TRUE.equals(_encapsulatedValue.«getter»())) {
1030 _value.add("«bit.name»");
1033 «Set.resolvedName» _domValue = «Collections.resolvedName».unmodifiableSet(_value);
1034 //System.out.println("«inputType.simpleName»#toDomValue:DeEnc: "+_domValue);
1040 method(Object, "serialize", Object) [
1043 return toDomValue($1);
1047 method(Object, "fromDomValue", Object) [
1048 modifiers = PUBLIC + FINAL + STATIC
1049 val sortedBits = typeDef.bits.sort[o1, o2|o1.propertyName.compareTo(o2.propertyName)]
1052 //System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
1057 «Set.resolvedName» _domValue = («Set.resolvedName») $1;
1058 «FOR bit : sortedBits»
1059 Boolean «bit.propertyName» = Boolean.valueOf(_domValue.contains("«bit.name»"));
1062 return new «inputType.resolvedName»(«FOR bit : sortedBits SEPARATOR ","»«bit.propertyName»«ENDFOR»);
1066 method(Object, "deserialize", Object) [
1068 return fromDomValue($1);
1074 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
1075 log.debug("DOM Codec for {} was generated {}", inputType, ret)
1076 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
1077 } catch (Exception e) {
1078 log.error("Cannot compile DOM Codec for {}", inputType, e);
1079 val exception = new CodeGenerationException("Cannot compile Transformator for " + inputType);
1080 exception.addSuppressed(e);
1085 def String getPropertyName(Bit bit) {
1086 '''_«BindingGeneratorUtil.parseToValidParamName(bit.name)»'''
1089 def String getterName(Bit bit) {
1091 val paramName = BindingGeneratorUtil.parseToValidParamName(bit.name);
1092 return '''is«paramName.toFirstUpper»''';
1095 def boolean isYangBindingAvailable(Class<?> class1) {
1097 val bindingCodecClass = class1.classLoader.loadClass(BINDING_CODEC.name);
1098 return bindingCodecClass !== null;
1099 } catch (ClassNotFoundException e) {
1104 private def createDummyImplementation(Class<?> object, GeneratedTransferObject typeSpec) {
1105 log.info("Generating Dummy DOM Codec for {} with {}", object, object.classLoader)
1106 return createClass(typeSpec.codecClassName) [
1107 if (object.isYangBindingAvailable) {
1108 implementsType(BINDING_CODEC)
1109 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
1110 staticField(it, IDENTITYREF_CODEC, BindingCodec)
1111 implementsType(BindingDeserializer.asCtClass)
1113 //implementsType(BindingDeserializer.asCtClass)
1114 method(Object, "toDomValue", Object) [
1115 modifiers = PUBLIC + FINAL + STATIC
1120 return $1.toString();
1124 method(Object, "serialize", Object) [
1127 return toDomValue($1);
1131 method(Object, "fromDomValue", Object) [
1132 modifiers = PUBLIC + FINAL + STATIC
1133 bodyChecked = '''return null;'''
1135 method(Object, "deserialize", Object) [
1137 return fromDomValue($1);
1144 private def Type getValueReturnType(GeneratedTransferObject object) {
1145 for (prop : object.properties) {
1146 if (prop.name == "value") {
1147 return prop.returnType;
1150 if (object.superType != null) {
1151 return getValueReturnType(object.superType);
1156 private def Class<?> generateValueTransformer(Class<?> inputType, Enumeration typeSpec) {
1158 val typeRef = new ReferencedTypeImpl(typeSpec.packageName, typeSpec.name);
1159 val schema = typeToSchemaNode.get(typeRef) as ExtendedType;
1160 val enumSchema = schema.baseType as EnumerationType;
1162 //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
1163 val ctCls = createClass(typeSpec.codecClassName) [
1164 //staticField(Map,"AUGMENTATION_SERIALIZERS");
1165 //implementsType(BINDING_CODEC)
1166 method(Object, "toDomValue", Object) [
1167 modifiers = PUBLIC + FINAL + STATIC
1172 «typeSpec.resolvedName» _value = («typeSpec.resolvedName») $1;
1173 «FOR en : enumSchema.values»
1174 if(«typeSpec.resolvedName».«BindingGeneratorUtil.parseToClassName(en.name)».equals(_value)) {
1182 method(Object, "serialize", Object) [
1184 return toDomValue($1);
1187 method(Object, "fromDomValue", Object) [
1188 modifiers = PUBLIC + FINAL + STATIC
1194 String _value = (String) $1;
1195 «FOR en : enumSchema.values»
1196 if("«en.name»".equals(_value)) {
1197 return «typeSpec.resolvedName».«BindingGeneratorUtil.parseToClassName(en.name)»;
1204 method(Object, "deserialize", Object) [
1206 return fromDomValue($1);
1211 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
1212 log.debug("DOM Codec for {} was generated {}", inputType, ret)
1214 } catch (CodeGenerationException e) {
1215 throw new CodeGenerationException("Cannot compile Transformator for " + inputType, e);
1216 } catch (Exception e) {
1217 log.error("Cannot compile DOM Codec for {}", inputType, e);
1218 val exception = new CodeGenerationException("Cannot compile Transformator for " + inputType);
1219 exception.addSuppressed(e);
1225 def Class<?> toClassImpl(CtClass newClass, ClassLoader loader, ProtectionDomain domain) {
1226 val cls = newClass.toClass(loader, domain);
1227 if (classFileCapturePath !== null) {
1228 newClass.writeFile(classFileCapturePath.absolutePath);
1230 listener?.onCodecCreated(cls);
1234 def debugWriteClass(CtClass class1) {
1235 val path = class1.name.replace(".", "/") + ".class"
1237 val captureFile = new File(classFileCapturePath, path);
1238 captureFile.createNewFile
1242 private def dispatch String deserializeValue(Type type, String domParameter, TypeDefinition<?> typeDef) {
1243 if (INSTANCE_IDENTIFIER.equals(type)) {
1244 return '''(«InstanceIdentifier.name») «INSTANCE_IDENTIFIER_CODEC».deserialize(«domParameter»)'''
1245 } else if (CLASS_TYPE.equals(type)) {
1246 return '''(«Class.name») «IDENTITYREF_CODEC».deserialize(«domParameter»)'''
1248 return '''(«type.resolvedName») «domParameter»'''
1256 private def dispatch CharSequence deserializeProperty(DataSchemaNode container, Type type, String propertyName) '''
1257 «type.resolvedName» «propertyName» = null;
1260 private def dispatch CharSequence deserializeProperty(DataSchemaNode container, GeneratedTypeBuilder type,
1261 String propertyName) {
1262 _deserializeProperty(container, type.toInstance, propertyName)
1265 public static def toSetter(String it) {
1267 if (startsWith("is")) {
1268 return "set" + substring(2);
1269 } else if (startsWith("get")) {
1270 return "set" + substring(3);
1276 private def dispatch CharSequence deserializeProperty(DataSchemaNode container,GeneratedType type, String propertyName) '''
1277 «type.resolvedName» «propertyName» = value.«propertyName»();
1278 if(«propertyName» != null) {
1279 Object domValue = «type.serializer».toDomStatic(QNAME,«propertyName»);
1280 _childNodes.add(domValue);
1284 private def getBuilderName(GeneratedType type) '''«type.resolvedName»Builder'''
1286 private def staticQNameField(CtClass it, QName node) {
1287 val field = new CtField(ctQName, "QNAME", it);
1288 field.modifiers = PUBLIC + FINAL + STATIC;
1290 '''«QName.asCtClass.name».create("«node.namespace»","«node.formattedRevision»","«node.localName»")''')
1293 private def dispatch String serializeBody(GeneratedType type, ListSchemaNode node) '''
1295 «QName.name» _resultName = «QName.name».create($1,QNAME.getLocalName());
1296 java.util.List _childNodes = new java.util.ArrayList();
1297 «type.resolvedName» value = («type.resolvedName») $2;
1298 «transformDataContainerBody(type, type.allProperties, node)»
1299 «serializeAugmentations»
1300 return ($r) java.util.Collections.singletonMap(_resultName,_childNodes);
1304 private def dispatch String serializeBody(GeneratedType type, ContainerSchemaNode node) '''
1306 «QName.name» _resultName = «QName.name».create($1,QNAME.getLocalName());
1307 java.util.List _childNodes = new java.util.ArrayList();
1308 «type.resolvedName» value = («type.resolvedName») $2;
1309 «transformDataContainerBody(type, type.allProperties, node)»
1310 «serializeAugmentations»
1311 return ($r) java.util.Collections.singletonMap(_resultName,_childNodes);
1315 private def dispatch String serializeBody(GeneratedType type, ChoiceCaseNode node) '''
1317 «QName.name» _resultName = «QName.name».create($1,QNAME.getLocalName());
1318 java.util.List _childNodes = new java.util.ArrayList();
1319 «type.resolvedName» value = («type.resolvedName») $2;
1320 «transformDataContainerBody(type, type.allProperties, node)»
1321 «serializeAugmentations»
1322 return ($r) java.util.Collections.singletonMap(_resultName,_childNodes);
1326 private def dispatch String serializeBody(GeneratedType type, SchemaNode node) '''
1328 «QName.name» _resultName = «QName.name».create($1,QNAME.getLocalName());
1329 java.util.List _childNodes = new java.util.ArrayList();
1330 «type.resolvedName» value = («type.resolvedName») $2;
1331 return ($r) java.util.Collections.singletonMap(_resultName,_childNodes);
1335 private def transformDataContainerBody(Type type, Map<String, Type> properties, DataNodeContainer node) {
1337 «FOR child : node.childNodes»
1338 «val signature = properties.getFor(child)»
1339 «IF signature !== null»
1340 ////System.out.println("«type.name»#«signature.key»" + value.«signature.key»());
1341 «serializeProperty(child, signature.value, signature.key)»
1348 private def serializeAugmentations() '''
1349 java.util.List _augmentations = (java.util.List) «AUGMENTATION_CODEC».serialize(value);
1350 if(_augmentations != null) {
1351 _childNodes.addAll(_augmentations);
1355 def Entry<String, Type> getFor(Map<String, Type> map, DataSchemaNode node) {
1356 var sig = map.get(node.getterName);
1358 return new SimpleEntry(node.getterName, sig);
1360 sig = map.get(node.booleanGetterName);
1362 return new SimpleEntry(node.booleanGetterName, map.get(node.booleanGetterName));
1367 private static def String getBooleanGetterName(DataSchemaNode node) {
1368 return "is" + BindingGeneratorUtil.parseToClassName(node.QName.localName);
1371 private static def String getGetterName(DataSchemaNode node) {
1372 return "get" + BindingGeneratorUtil.parseToClassName(node.QName.localName);
1375 private static def String getGetterName(QName node) {
1376 return "get" + BindingGeneratorUtil.parseToClassName(node.localName);
1379 private def dispatch CharSequence serializeProperty(ListSchemaNode schema, ParameterizedType type,
1380 String propertyName) '''
1381 «type.resolvedName» «propertyName» = value.«propertyName»();
1382 ////System.out.println("«propertyName»:" + «propertyName»);
1383 if(«propertyName» != null) {
1384 java.util.Iterator _iterator = «propertyName».iterator();
1385 boolean _hasNext = _iterator.hasNext();
1387 Object _listItem = _iterator.next();
1388 Object _domValue = «type.actualTypeArguments.get(0).serializer(schema).resolvedName».toDomStatic(_resultName,_listItem);
1389 _childNodes.add(_domValue);
1390 _hasNext = _iterator.hasNext();
1395 private def dispatch CharSequence serializeProperty(LeafSchemaNode schema, Type type, String propertyName) '''
1396 «type.resolvedName» «propertyName» = value.«propertyName»();
1398 if(«propertyName» != null) {
1399 «QName.name» _qname = «QName.name».create(_resultName,"«schema.QName.localName»");
1400 Object _propValue = «serializeValue(type, propertyName, schema.type)»;
1401 if(_propValue != null) {
1402 Object _domValue = java.util.Collections.singletonMap(_qname,_propValue);
1403 _childNodes.add(_domValue);
1408 private def dispatch serializeValue(GeneratedTransferObject type, String parameter, TypeDefinition<?> typeDefinition) {
1409 '''«type.valueSerializer(typeDefinition).resolvedName».toDomValue(«parameter»)'''
1412 private def dispatch serializeValue(Enumeration type, String parameter, TypeDefinition<?> typeDefinition) {
1413 '''«type.valueSerializer(typeDefinition).resolvedName».toDomValue(«parameter»)'''
1416 private def dispatch serializeValue(Type signature, String property, TypeDefinition<?> typeDefinition) {
1417 if (INSTANCE_IDENTIFIER == signature) {
1418 return '''«INSTANCE_IDENTIFIER_CODEC».serialize(«property»)'''
1419 } else if (CLASS_TYPE.equals(signature)) {
1420 return '''(«QName.resolvedName») «IDENTITYREF_CODEC».serialize(«property»)'''
1422 if ("char[]" == signature.name) {
1423 return '''new String(«property»)''';
1425 return '''«property»''';
1428 private def dispatch CharSequence serializeProperty(LeafListSchemaNode schema, ParameterizedType type,
1429 String propertyName) '''
1430 «type.resolvedName» «propertyName» = value.«propertyName»();
1431 if(«propertyName» != null) {
1432 «QName.name» _qname = «QName.name».create(_resultName,"«schema.QName.localName»");
1433 java.util.Iterator _iterator = «propertyName».iterator();
1434 boolean _hasNext = _iterator.hasNext();
1436 Object _listItem = _iterator.next();
1437 Object _propValue = «serializeValue(type.actualTypeArguments.get(0), "_listItem", schema.type)»;
1438 Object _domValue = java.util.Collections.singletonMap(_qname,_propValue);
1439 _childNodes.add(_domValue);
1440 _hasNext = _iterator.hasNext();
1445 private def dispatch CharSequence serializeProperty(ChoiceNode container, GeneratedType type,
1446 String propertyName) '''
1447 «type.resolvedName» «propertyName» = value.«propertyName»();
1448 if(«propertyName» != null) {
1449 java.util.List domValue = «type.serializer(container).resolvedName».toDomStatic(_resultName,«propertyName»);
1450 _childNodes.addAll(domValue);
1458 private def dispatch CharSequence serializeProperty(DataSchemaNode container, Type type, String propertyName) '''
1459 «type.resolvedName» «propertyName» = value.«propertyName»();
1460 if(«propertyName» != null) {
1461 Object domValue = «propertyName»;
1462 _childNodes.add(domValue);
1466 private def dispatch CharSequence serializeProperty(DataSchemaNode container, GeneratedTypeBuilder type,
1467 String propertyName) {
1468 serializeProperty(container, type.toInstance, propertyName)
1471 private def dispatch CharSequence serializeProperty(DataSchemaNode container, GeneratedType type,
1472 String propertyName) '''
1473 «type.resolvedName» «propertyName» = value.«propertyName»();
1474 if(«propertyName» != null) {
1475 Object domValue = «type.serializer(container).resolvedName».toDomStatic(_resultName,«propertyName»);
1476 _childNodes.add(domValue);
1480 private def codecClassName(GeneratedType typeSpec) {
1481 return '''«typeSpec.resolvedName»$Broker$Codec$DOM'''
1484 private def codecClassName(Class<?> typeSpec) {
1485 return '''«typeSpec.name»$Broker$Codec$DOM'''
1488 private def HashMap<String, Type> getAllProperties(GeneratedType type) {
1489 val ret = new HashMap<String, Type>();
1490 type.collectAllProperties(ret);
1494 private def dispatch void collectAllProperties(GeneratedType type, Map<String, Type> set) {
1495 for (definition : type.methodDefinitions) {
1496 set.put(definition.name, definition.returnType);
1498 for (property : type.properties) {
1499 set.put(property.getterName, property.returnType);
1501 for (parent : type.implements) {
1502 parent.collectAllProperties(set);
1506 def String getGetterName(GeneratedProperty property) {
1507 return "get" + property.name.toFirstUpper
1510 private def dispatch void collectAllProperties(Type type, Map<String, Type> set) {
1511 // NOOP for generic type.
1514 def String getResolvedName(Type type) {
1515 return type.asCtClass.name;
1518 def String getResolvedName(Class<?> type) {
1519 return type.asCtClass.name;
1522 def CtClass asCtClass(Type type) {
1523 val cls = loadClassWithTCCL(type.fullyQualifiedName)
1524 return cls.asCtClass;
1527 private def dispatch processException(Class<?> inputType, CodeGenerationException e) {
1528 log.error("Cannot compile DOM Codec for {}. One of it's prerequisites was not generated.", inputType);
1532 private def dispatch processException(Class<?> inputType, Exception e) {
1533 log.error("Cannot compile DOM Codec for {}", inputType, e);
1534 val exception = new CodeGenerationException("Cannot compile Transformator for " + inputType, e);
1538 private def setBodyChecked(CtMethod method, String body) {
1540 method.setBody(body);
1541 } catch (CannotCompileException e) {
1542 log.error("Cannot compile method: {}#{} {}, Reason: {} Body: {}", method.declaringClass, method.name,
1543 method.signature, e.message, body)
1548 private def <V> V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable<V> function) throws Exception {
1549 appendClassLoaderIfMissing(cls);
1550 ClassLoaderUtils.withClassLoaderAndLock(cls, lock, function);
1556 class PropertyPair {
1565 SchemaNode schemaNode;