2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.sal.binding.generator.impl
10 import javassist.ClassPool
11 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
12 import org.opendaylight.yangtools.yang.model.api.SchemaNode
13 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils
14 import javassist.CtClass
16 import org.opendaylight.yangtools.yang.common.QName
17 import javassist.CtField
18 import static javassist.Modifier.*
19 import static org.opendaylight.yangtools.sal.binding.generator.impl.CodecMapping.*
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
23 import org.opendaylight.yangtools.sal.binding.model.api.Type
24 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder
25 import org.opendaylight.yangtools.binding.generator.util.Types
26 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
27 import java.util.HashMap
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
29 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
31 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode
33 import java.util.TreeSet
34 import com.google.common.base.Joiner
35 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
36 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
37 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode
38 import static org.opendaylight.yangtools.sal.binding.generator.util.ClassLoaderUtils.*;
39 import org.opendaylight.yangtools.yang.binding.BindingDeserializer
40 import org.opendaylight.yangtools.yang.binding.BindingCodec
41 import org.slf4j.LoggerFactory
42 import org.opendaylight.yangtools.sal.binding.generator.util.CodeGenerationException
43 import org.opendaylight.yangtools.yang.model.api.ChoiceNode
44 import java.security.ProtectionDomain
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
47 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
48 import java.util.Map.Entry
49 import java.util.AbstractMap.SimpleEntry
50 import org.opendaylight.yangtools.yang.binding.DataObject
51 import org.opendaylight.yangtools.yang.binding.Augmentation
52 import java.util.Iterator
53 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema
54 import java.util.concurrent.ConcurrentHashMap
55 import static extension org.opendaylight.yangtools.sal.binding.generator.util.YangSchemaUtils.*;
56 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl
57 import org.opendaylight.yangtools.yang.model.util.ExtendedType
58 import org.opendaylight.yangtools.yang.model.util.EnumerationType
59 import static com.google.common.base.Preconditions.*
60 import org.opendaylight.yangtools.yang.model.api.SchemaPath
61 import javassist.CtMethod
62 import javassist.CannotCompileException
63 import java.util.concurrent.locks.Lock
64 import java.util.concurrent.Callable
65 import org.opendaylight.yangtools.sal.binding.generator.util.ClassLoaderUtils
66 import org.opendaylight.yangtools.yang.model.api.TypeDefinition
67 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition
68 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
69 import java.util.HashSet
70 import java.util.Collections
71 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit
73 import org.opendaylight.yangtools.sal.binding.generator.util.XtendHelper
74 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
75 import org.opendaylight.yangtools.yang.binding.BindingMapping
76 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition
78 class TransformerGenerator {
80 private static val log = LoggerFactory.getLogger(TransformerGenerator)
82 public static val STRING = Types.typeForClass(String);
83 public static val BOOLEAN = Types.typeForClass(Boolean);
84 public static val INTEGER = Types.typeForClass(Integer);
85 public static val INSTANCE_IDENTIFIER = Types.typeForClass(InstanceIdentifier)
87 //public static val DECIMAL = Types.typeForClass(Decimal);
88 public static val LONG = Types.typeForClass(Long);
90 val ClassPool classPool
91 val extension JavassistUtils utils;
98 var File classFileCapturePath;
101 var Map<Type, Type> typeDefinitions = new ConcurrentHashMap();
104 var Map<Type, GeneratedTypeBuilder> typeToDefinition = new ConcurrentHashMap();
107 var Map<SchemaPath, GeneratedTypeBuilder> pathToType = new ConcurrentHashMap();
110 var Map<Type, SchemaNode> typeToSchemaNode = new ConcurrentHashMap();
113 var Map<Type, AugmentationSchema> typeToAugmentation = new ConcurrentHashMap();
116 var GeneratorListener listener;
119 var extension GeneratedClassLoadingStrategy classLoadingStrategy
121 public static val CLASS_TYPE = Types.typeForClass(Class);
123 public new(ClassPool pool) {
125 utils = new JavassistUtils(pool)
127 BINDING_CODEC = BindingCodec.asCtClass;
128 ctQName = QName.asCtClass
130 this.classLoadingStrategy = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
133 def Class<? extends BindingCodec<Map<QName, Object>, Object>> transformerFor(Class<?> inputType) {
134 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
135 val ret = getGeneratedClass(inputType)
137 listener.onClassProcessed(inputType);
138 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
140 val ref = Types.typeForClass(inputType)
141 val node = typeToSchemaNode.get(ref)
142 val typeSpecBuilder = typeToDefinition.get(ref)
143 checkState(typeSpecBuilder !== null, "Could not find typedefinition for %s", inputType.name);
144 val typeSpec = typeSpecBuilder.toInstance();
145 val newret = generateTransformerFor(inputType, typeSpec, node);
146 listener.onClassProcessed(inputType);
147 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
151 def Class<? extends BindingCodec<Map<QName, Object>, Object>> transformerFor(Class<?> inputType, DataSchemaNode node) {
152 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
153 val ret = getGeneratedClass(inputType)
155 listener.onClassProcessed(inputType);
156 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
158 val ref = Types.typeForClass(inputType)
159 var typeSpecBuilder = typeToDefinition.get(ref)
160 if (typeSpecBuilder == null) {
161 typeSpecBuilder = pathToType.get(node.path);
164 checkState(typeSpecBuilder !== null, "Could not find TypeDefinition for %s, $s", inputType.name, node);
165 val typeSpec = typeSpecBuilder.toInstance();
166 val newret = generateTransformerFor(inputType, typeSpec, node);
167 listener.onClassProcessed(inputType);
168 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
172 def Class<? extends BindingCodec<Map<QName, Object>, Object>> augmentationTransformerFor(Class<?> inputType) {
173 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
174 val ret = getGeneratedClass(inputType)
176 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
178 val ref = Types.typeForClass(inputType)
179 val node = typeToAugmentation.get(ref)
180 val typeSpecBuilder = typeToDefinition.get(ref)
181 val typeSpec = typeSpecBuilder.toInstance();
182 val newret = generateAugmentationTransformerFor(inputType, typeSpec, node);
183 listener.onClassProcessed(inputType);
184 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
188 def Class<? extends BindingCodec<Object, Object>> caseCodecFor(Class<?> inputType, ChoiceCaseNode node) {
189 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
190 val ret = getGeneratedClass(inputType)
192 return ret as Class<? extends BindingCodec<Object, Object>>;
194 val ref = Types.typeForClass(inputType)
195 val typeSpecBuilder = typeToDefinition.get(ref)
196 val typeSpec = typeSpecBuilder.toInstance();
197 val newret = generateCaseCodec(inputType, typeSpec, node);
198 return newret as Class<? extends BindingCodec<Object, Object>>;
202 def Class<? extends BindingCodec<Map<QName, Object>, Object>> keyTransformerForIdentifiable(Class<?> parentType) {
203 return withClassLoaderAndLock(parentType.classLoader, lock) [ |
204 val inputName = parentType.name + "Key";
205 val inputType = loadClass(inputName);
206 val ret = getGeneratedClass(inputType)
208 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
210 val ref = Types.typeForClass(parentType)
211 val node = typeToSchemaNode.get(ref) as ListSchemaNode
212 val typeSpecBuilder = typeToDefinition.get(ref)
213 val typeSpec = typeSpecBuilder.identifierDefinition;
214 val newret = generateKeyTransformerFor(inputType, typeSpec, node);
215 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
219 def getIdentifierDefinition(GeneratedTypeBuilder builder) {
220 val inst = builder.toInstance
221 val keyMethod = inst.methodDefinitions.findFirst[name == "getKey"]
222 return keyMethod.returnType as GeneratedTransferObject
225 def Class<? extends BindingCodec<Map<QName, Object>, Object>> keyTransformerForIdentifier(Class<?> inputType) {
226 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
227 val ret = getGeneratedClass(inputType)
229 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
231 val ref = Types.typeForClass(inputType)
232 val node = typeToSchemaNode.get(ref) as ListSchemaNode
233 val typeSpecBuilder = typeToDefinition.get(ref)
234 val typeSpec = typeSpecBuilder.toInstance();
235 val newret = generateKeyTransformerFor(inputType, typeSpec, node);
236 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
240 private def Class<?> keyTransformerFor(Class<?> inputType, GeneratedType type, ListSchemaNode schema) {
241 return withClassLoaderAndLock(inputType.classLoader, lock) [ |
242 val transformer = getGeneratedClass(inputType)
243 if (transformer != null) {
246 val newret = generateKeyTransformerFor(inputType, type, schema);
247 return newret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
251 private def Class<?> getGeneratedClass(Class<? extends Object> cls) {
254 return loadClass(cls.codecClassName)
255 } catch (ClassNotFoundException e) {
260 private def Class<?> keyTransformer(GeneratedType type, ListSchemaNode node) {
261 val cls = loadClass(type.resolvedName + "Key");
262 keyTransformerFor(cls, type, node);
265 private def serializer(Type type, DataSchemaNode node) {
266 val cls = loadClass(type.resolvedName);
267 transformerFor(cls, node);
270 private def Class<?> valueSerializer(GeneratedTransferObject type, TypeDefinition<?> typeDefinition) {
271 val cls = loadClass(type.resolvedName);
272 val transformer = cls.generatedClass;
273 if (transformer !== null) {
276 var baseType = typeDefinition;
277 while (baseType.baseType != null) {
278 baseType = baseType.baseType;
280 val finalType = baseType;
281 return withClassLoaderAndLock(cls.classLoader, lock) [ |
282 val valueTransformer = generateValueTransformer(cls, type, finalType);
283 return valueTransformer;
287 private def Class<?> valueSerializer(Enumeration type, TypeDefinition<?> typeDefinition) {
288 val cls = loadClass(type.resolvedName);
289 val transformer = cls.generatedClass;
290 if (transformer !== null) {
294 return withClassLoaderAndLock(cls.classLoader, lock) [ |
295 val valueTransformer = generateValueTransformer(cls, type);
296 return valueTransformer;
300 private def generateKeyTransformerFor(Class<? extends Object> inputType, GeneratedType typeSpec, ListSchemaNode node) {
303 //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
304 val properties = typeSpec.allProperties;
305 val ctCls = createClass(inputType.codecClassName) [
306 //staticField(Map,"AUGMENTATION_SERIALIZERS");
307 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
308 staticField(it, IDENTITYREF_CODEC, BindingCodec)
309 staticQNameField(node.QName);
310 implementsType(BINDING_CODEC)
311 method(Object, "toDomStatic", QName, Object) [
312 modifiers = PUBLIC + FINAL + STATIC
315 «QName.name» _resultName;
317 _resultName = «QName.name».create($1,QNAME.getLocalName());
321 java.util.List _childNodes = new java.util.ArrayList();
322 «inputType.resolvedName» value = («inputType.name») $2;
323 «FOR key : node.keyDefinition»
324 «val propertyName = key.getterName»
325 «val keyDef = node.getDataChildByName(key)»
326 «val property = properties.get(propertyName)»
327 «serializeProperty(keyDef, property, propertyName)»;
329 return ($r) java.util.Collections.singletonMap(_resultName,_childNodes);
333 method(Object, "fromDomStatic", QName, Object) [
334 modifiers = PUBLIC + FINAL + STATIC
340 «QName.name» _localQName = $1;
341 java.util.Map _compositeNode = (java.util.Map) $2;
342 boolean _is_empty = true;
343 «FOR key : node.keyDefinition»
344 «val propertyName = key.getterName»
345 «val keyDef = node.getDataChildByName(key)»
346 «val property = properties.get(propertyName)»
347 «deserializeProperty(keyDef, property, propertyName)»;
349 «inputType.resolvedName» _value = new «inputType.name»(«node.keyDefinition.
350 keyConstructorList»);
355 method(Object, "serialize", Object) [
358 java.util.Map.Entry _input = (java.util.Map.Entry) $1;
359 «QName.name» _localQName = («QName.name») _input.getKey();
360 «inputType.name» _keyValue = («inputType.name») _input.getValue();
361 return toDomStatic(_localQName,_keyValue);
365 method(Object, "deserialize", Object) [
368 «QName.name» _qname = QNAME;
369 if($1 instanceof java.util.Map.Entry) {
370 _qname = («QName.name») ((java.util.Map.Entry) $1).getKey();
372 return fromDomStatic(_qname,$1);
377 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
378 log.debug("DOM Codec for {} was generated {}", inputType, ret)
379 return ret as Class<? extends BindingCodec<Map<QName,Object>, ?>>;
380 } catch (Exception e) {
381 processException(inputType, e);
386 private def Class<? extends BindingCodec<Object, Object>> generateCaseCodec(Class<?> inputType, GeneratedType type,
387 ChoiceCaseNode node) {
390 //log.info("Generating DOM Codec for {} with {}, TCCL is: {}", inputType, inputType.classLoader,Thread.currentThread.contextClassLoader)
391 val ctCls = createClass(type.codecClassName) [
392 //staticField(Map,"AUGMENTATION_SERIALIZERS");
393 implementsType(BINDING_CODEC)
394 staticQNameField(node.QName);
395 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
396 staticField(it, AUGMENTATION_CODEC, BindingCodec)
397 staticField(it, IDENTITYREF_CODEC, BindingCodec)
398 method(Object, "toDomStatic", QName, Object) [
399 modifiers = PUBLIC + FINAL + STATIC
402 «QName.name» _resultName = «QName.name».create($1,QNAME.getLocalName());
403 java.util.List _childNodes = new java.util.ArrayList();
404 «type.resolvedName» value = («type.resolvedName») $2;
405 «transformDataContainerBody(type, type.allProperties, node)»
406 return ($r) _childNodes;
410 method(Object, "serialize", Object) [
413 java.util.Map.Entry _input = (java.util.Map.Entry) $1;
414 «QName.name» _localName = QNAME;
415 if(_input.getKey() != null) {
416 _localName = («QName.name») _input.getKey();
418 return toDomStatic(_localName,_input.getValue());
422 method(Object, "fromDomStatic", QName, Object) [
423 modifiers = PUBLIC + FINAL + STATIC
424 bodyChecked = deserializeBody(type, node)
426 method(Object, "deserialize", Object) [
429 //System.out.println("«type.name»#deserialize: " +$1);
430 java.util.Map.Entry _input = (java.util.Map.Entry) $1;
431 return fromDomStatic((«QName.name»)_input.getKey(),_input.getValue());
437 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain) as Class<? extends BindingCodec<Object, Object>>
438 listener?.onDataContainerCodecCreated(inputType, ret);
439 log.debug("DOM Codec for {} was generated {}", inputType, ret)
441 } catch (Exception e) {
442 processException(inputType, e);
447 private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateTransformerFor(
448 Class<?> inputType, GeneratedType typeSpec, SchemaNode node) {
451 //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
452 val ctCls = createClass(typeSpec.codecClassName) [
453 //staticField(Map,"AUGMENTATION_SERIALIZERS");
454 staticQNameField(node.QName);
455 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
456 staticField(it, IDENTITYREF_CODEC, BindingCodec)
457 staticField(it, AUGMENTATION_CODEC, BindingCodec)
458 implementsType(BINDING_CODEC)
459 method(Object, "toDomStatic", QName, Object) [
460 modifiers = PUBLIC + FINAL + STATIC
461 bodyChecked = serializeBodyFacade(typeSpec, node)
463 method(Object, "serialize", Object) [
466 java.util.Map.Entry _input = (java.util.Map.Entry) $1;
467 «QName.name» _localName = QNAME;
468 if(_input.getKey() != null) {
469 _localName = («QName.name») _input.getKey();
471 return toDomStatic(_localName,_input.getValue());
475 method(Object, "fromDomStatic", QName, Object) [
476 modifiers = PUBLIC + FINAL + STATIC
477 bodyChecked = deserializeBody(typeSpec, node)
479 method(Object, "deserialize", Object) [
482 «QName.name» _qname = QNAME;
483 if($1 instanceof java.util.Map.Entry) {
484 _qname = («QName.name») ((java.util.Map.Entry) $1).getKey();
486 return fromDomStatic(_qname,$1);
492 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain) as Class<? extends BindingCodec<Map<QName,Object>, Object>>
493 listener?.onDataContainerCodecCreated(inputType, ret);
494 log.debug("DOM Codec for {} was generated {}", inputType, ret)
496 } catch (Exception e) {
497 processException(inputType, e);
502 private def Class<? extends BindingCodec<Map<QName, Object>, Object>> generateAugmentationTransformerFor(
503 Class<?> inputType, GeneratedType type, AugmentationSchema node) {
506 //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
507 val properties = type.allProperties
508 val ctCls = createClass(type.codecClassName) [
509 //staticField(Map,"AUGMENTATION_SERIALIZERS");
510 staticQNameField(node.augmentationQName);
511 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
512 staticField(it, AUGMENTATION_CODEC, BindingCodec)
513 staticField(it, IDENTITYREF_CODEC, BindingCodec)
514 implementsType(BINDING_CODEC)
515 method(Object, "toDomStatic", QName, Object) [
516 modifiers = PUBLIC + FINAL + STATIC
519 ////System.out.println("Qname " + $1);
520 ////System.out.println("Value " + $2);
521 «QName.name» _resultName = «QName.name».create(QNAME,QNAME.getLocalName());
522 java.util.List _childNodes = new java.util.ArrayList();
523 «type.resolvedName» value = («type.resolvedName») $2;
524 «FOR child : node.childNodes»
525 «var signature = properties.getFor(child)»
526 ////System.out.println("«signature.key»" + value.«signature.key»());
527 «serializeProperty(child, signature.value, signature.key)»
529 return ($r) _childNodes;
533 method(Object, "serialize", Object) [
536 java.util.Map.Entry _input = (java.util.Map.Entry) $1;
537 «QName.name» _localName = QNAME;
538 if(_input.getKey() != null) {
539 _localName = («QName.name») _input.getKey();
541 return toDomStatic(_localName,_input.getValue());
545 method(Object, "fromDomStatic", QName, Object) [
546 modifiers = PUBLIC + FINAL + STATIC
549 «QName.name» _localQName = QNAME;
554 java.util.Map _compositeNode = (java.util.Map) $2;
555 //System.out.println(_localQName + " " + _compositeNode);
556 «type.builderName» _builder = new «type.builderName»();
557 boolean _is_empty = true;
558 «FOR child : node.childNodes»
559 «val signature = properties.getFor(child)»
560 «deserializeProperty(child, signature.value, signature.key)»
561 _builder.«signature.key.toSetter»(«signature.key»);
566 return _builder.build();
570 method(Object, "deserialize", Object) [
572 return fromDomStatic(QNAME,$1);
577 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain) as Class<? extends BindingCodec<Map<QName,Object>, Object>>
578 listener?.onDataContainerCodecCreated(inputType, ret);
580 } catch (Exception e) {
581 processException(inputType, e);
586 private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateTransformerFor(
587 Class<?> inputType, GeneratedType typeSpec, ChoiceNode node) {
590 //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
591 val ctCls = createClass(typeSpec.codecClassName) [
592 //staticField(Map,"AUGMENTATION_SERIALIZERS");
593 //staticQNameField(inputType);
594 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
595 staticField(it, IDENTITYREF_CODEC, BindingCodec)
596 staticField(it, CLASS_TO_CASE_MAP, Map)
597 staticField(it, COMPOSITE_TO_CASE, Map)
598 //staticField(it,QNAME_TO_CASE_MAP,BindingCodec)
599 implementsType(BINDING_CODEC)
600 method(List, "toDomStatic", QName, Object) [
601 modifiers = PUBLIC + FINAL + STATIC
607 «DataObject.name» _baValue = («DataObject.name») $2;
608 Class _baClass = _baValue.getImplementedInterface();
609 «BINDING_CODEC.name» _codec = «CLASS_TO_CASE_MAP».get(_baClass);
613 java.util.Map.Entry _input = new «SimpleEntry.name»($1,_baValue);
614 Object _ret = _codec.serialize(_input);
615 ////System.out.println("«typeSpec.name»#toDomStatic: " + _ret);
616 return («List.name») _ret;
620 method(Object, "serialize", Object) [
622 throw new «UnsupportedOperationException.name»("Direct invocation not supported.");
625 method(Object, "fromDomStatic", QName, Map) [
626 modifiers = PUBLIC + FINAL + STATIC
629 «BINDING_CODEC.name» _codec = («BINDING_CODEC.name») «COMPOSITE_TO_CASE».get($2);
631 return _codec.deserialize(new «SimpleEntry.name»($1,$2));
637 method(Object, "deserialize", Object) [
639 throw new «UnsupportedOperationException.name»("Direct invocation not supported.");
644 val rawRet = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
645 val ret = rawRet as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
646 listener?.onChoiceCodecCreated(inputType, ret, node);
647 log.debug("DOM Codec for {} was generated {}", inputType, ret)
649 } catch (Exception e) {
650 processException(inputType, e);
655 private def keyConstructorList(List<QName> qnames) {
656 val names = new TreeSet<String>()
657 for (name : qnames) {
658 val fieldName = name.getterName;
659 names.add(fieldName);
661 return Joiner.on(",").join(names);
664 private def serializeBodyFacade(GeneratedType type, SchemaNode node) {
665 val ret = serializeBody(type, node);
669 private def String deserializeBody(GeneratedType type, SchemaNode node) {
670 val ret = deserializeBodyImpl(type, node);
674 private def deserializeKey(GeneratedType type, ListSchemaNode node) {
675 if (node.keyDefinition != null && !node.keyDefinition.empty) {
677 «type.resolvedName»Key getKey = («type.resolvedName»Key) «keyTransformer(type, node).canonicalName».fromDomStatic(_localQName,_compositeNode);
678 _builder.setKey(getKey);
683 private def String deserializeBodyWithAugmentations(GeneratedType type, DataNodeContainer node) '''
685 «QName.name» _localQName = «QName.name».create($1,QNAME.getLocalName());
689 java.util.Map _compositeNode = (java.util.Map) $2;
690 //System.out.println(_localQName + " " + _compositeNode);
691 «type.builderName» _builder = new «type.builderName»();
692 «deserializeDataNodeContainerBody(type, node)»
693 «deserializeAugmentations»
694 return _builder.build();
698 private def dispatch String deserializeBodyImpl(GeneratedType type, SchemaNode node) '''
700 «QName.name» _localQName = «QName.name».create($1,QNAME.getLocalName());
705 java.util.Map _compositeNode = (java.util.Map) $2;
706 «type.builderName» _builder = new «type.builderName»();
707 return _builder.build();
711 private def dispatch String deserializeBodyImpl(GeneratedType type, ListSchemaNode node) '''
713 «QName.name» _localQName = «QName.name».create($1,QNAME.getLocalName());
717 java.util.Map _compositeNode = (java.util.Map) $2;
718 //System.out.println(_localQName + " " + _compositeNode);
719 «type.builderName» _builder = new «type.builderName»();
720 «deserializeKey(type, node)»
721 «deserializeDataNodeContainerBody(type, node)»
722 «deserializeAugmentations»
723 return _builder.build();
727 private def dispatch String deserializeBodyImpl(GeneratedType type, ContainerSchemaNode node) {
728 return deserializeBodyWithAugmentations(type, node);
731 private def dispatch String deserializeBodyImpl(GeneratedType type, NotificationDefinition node) {
732 return deserializeBodyWithAugmentations(type, node);
735 private def dispatch String deserializeBodyImpl(GeneratedType type, ChoiceCaseNode node) {
736 return deserializeBodyWithAugmentations(type, node);
739 private def deserializeDataNodeContainerBody(GeneratedType type, DataNodeContainer node) {
740 deserializeNodeContainerBodyImpl(type, type.allProperties, node);
743 private def deserializeNodeContainerBodyImpl(GeneratedType type, HashMap<String, Type> properties,
744 DataNodeContainer node) {
746 boolean _is_empty = true;
747 «FOR child : node.childNodes»
748 «val signature = properties.getFor(child)»
749 «IF signature !== null»
750 «deserializeProperty(child, signature.value, signature.key)»
751 _builder.«signature.key.toSetter»(«signature.key»);
758 def deserializeAugmentations() '''
759 java.util.Map _augmentation = (java.util.Map) «AUGMENTATION_CODEC».deserialize(_compositeNode);
760 if(_augmentation != null) {
761 «Iterator.name» _entries = _augmentation.entrySet().iterator();
762 while(_entries.hasNext()) {
763 java.util.Map.Entry _entry = (java.util.Map.Entry) _entries.next();
764 ////System.out.println("Aug. key:" + _entry.getKey());
765 Class _type = (Class) _entry.getKey();
766 «Augmentation.resolvedName» _value = («Augmentation.name») _entry.getValue();
768 _builder.addAugmentation(_type,_value);
774 private def dispatch CharSequence deserializeProperty(ListSchemaNode schema, ParameterizedType type,
775 String propertyName) '''
776 java.util.List _dom_«propertyName» = _compositeNode.get(«QName.name».create(_localQName,"«schema.QName.
778 ////System.out.println("«propertyName»#deCode"+_dom_«propertyName»);
779 java.util.List «propertyName» = new java.util.ArrayList();
780 if(_dom_«propertyName» != null) {
781 java.util.List _serialized = new java.util.ArrayList();
782 java.util.Iterator _iterator = _dom_«propertyName».iterator();
783 boolean _hasNext = _iterator.hasNext();
785 Object _listItem = _iterator.next();
787 ////System.out.println(" item" + _listItem);
788 Object _value = «type.actualTypeArguments.get(0).serializer(schema).resolvedName».fromDomStatic(_localQName,_listItem);
789 ////System.out.println(" value" + _value);
790 «propertyName».add(_value);
791 _hasNext = _iterator.hasNext();
795 ////System.out.println(" list" + «propertyName»);
798 private def dispatch CharSequence deserializeProperty(LeafListSchemaNode schema, ParameterizedType type,
799 String propertyName) '''
800 java.util.List _dom_«propertyName» = _compositeNode.get(«QName.name».create(_localQName,"«schema.QName.
802 java.util.List «propertyName» = new java.util.ArrayList();
803 if(_dom_«propertyName» != null) {
804 java.util.List _serialized = new java.util.ArrayList();
805 java.util.Iterator _iterator = _dom_«propertyName».iterator();
806 boolean _hasNext = _iterator.hasNext();
809 Object _listItem = _iterator.next();
810 if(_listItem instanceof java.util.Map.Entry) {
811 Object _innerValue = ((java.util.Map.Entry) _listItem).getValue();
812 Object _value = «deserializeValue(type.actualTypeArguments.get(0), "_innerValue", schema.type)»;
813 «propertyName».add(_value);
815 _hasNext = _iterator.hasNext();
820 private def dispatch CharSequence deserializeProperty(LeafSchemaNode schema, Type type, String propertyName) '''
821 java.util.List _dom_«propertyName»_list =
822 _compositeNode.get(«QName.name».create(_localQName,"«schema.QName.localName»"));
823 «type.resolvedName» «propertyName» = null;
824 if(_dom_«propertyName»_list != null && _dom_«propertyName»_list.size() > 0) {
826 java.util.Map.Entry _dom_«propertyName» = (java.util.Map.Entry) _dom_«propertyName»_list.get(0);
827 Object _inner_value = _dom_«propertyName».getValue();
828 «propertyName» = «deserializeValue(type, "_inner_value", schema.type)»;
832 private def dispatch CharSequence deserializeProperty(ContainerSchemaNode schema, Type type,
833 String propertyName) '''
834 java.util.List _dom_«propertyName»_list =
835 _compositeNode.get(«QName.name».create(_localQName,"«schema.QName.localName»"));
836 «type.resolvedName» «propertyName» = null;
837 if(_dom_«propertyName»_list != null && _dom_«propertyName»_list.size() > 0) {
839 java.util.Map _dom_«propertyName» = (java.util.Map) _dom_«propertyName»_list.get(0);
840 «propertyName» = «type.serializer(schema).resolvedName».fromDomStatic(_localQName,_dom_«propertyName»);
844 private def dispatch CharSequence deserializeProperty(ChoiceNode schema, Type type, String propertyName) '''
845 «type.resolvedName» «propertyName» = «type.serializer(schema).resolvedName».fromDomStatic(_localQName,_compositeNode);
846 if(«propertyName» != null) {
851 private def dispatch String deserializeValue(GeneratedTransferObject type, String domParameter,
852 TypeDefinition<?> typeDefinition) '''
853 («type.resolvedName») «type.valueSerializer(typeDefinition).resolvedName».fromDomValue(«domParameter»)
856 private def dispatch String deserializeValue(Enumeration type, String domParameter, TypeDefinition<?> typeDefinition) '''
857 («type.resolvedName») «type.valueSerializer(typeDefinition).resolvedName».fromDomValue(«domParameter»)
860 private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateValueTransformer(
861 Class<?> inputType, GeneratedTransferObject typeSpec, TypeDefinition<?> typeDef) {
864 val returnType = typeSpec.valueReturnType;
865 if (returnType == null) {
866 val ctCls = createDummyImplementation(inputType, typeSpec);
867 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
868 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
871 val ctCls = createClass(typeSpec.codecClassName) [
872 //staticField(Map,"AUGMENTATION_SERIALIZERS");
873 if (inputType.isYangBindingAvailable) {
874 implementsType(BINDING_CODEC)
875 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
876 staticField(it, IDENTITYREF_CODEC, BindingCodec)
877 implementsType(BindingDeserializer.asCtClass)
879 method(Object, "toDomValue", Object) [
880 modifiers = PUBLIC + FINAL + STATIC
881 val ctSpec = typeSpec.asCtClass;
884 ////System.out.println("«inputType.simpleName»#toDomValue: "+$1);
889 «typeSpec.resolvedName» _encapsulatedValue = («typeSpec.resolvedName») $1;
890 ////System.out.println("«inputType.simpleName»#toDomValue:Enc: "+_encapsulatedValue);
891 «returnType.resolvedName» _value = _encapsulatedValue.getValue();
892 ////System.out.println("«inputType.simpleName»#toDomValue:DeEnc: "+_value);
893 Object _domValue = «serializeValue(returnType, "_value", null)»;
898 method(Object, "serialize", Object) [
901 return toDomValue($1);
905 method(Object, "fromDomValue", Object) [
906 modifiers = PUBLIC + FINAL + STATIC
909 ////System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
914 «returnType.resolvedName» _simpleValue = «deserializeValue(returnType, "$1", null)»;
915 «typeSpec.resolvedName» _value = new «typeSpec.resolvedName»(_simpleValue);
920 method(Object, "deserialize", Object) [
922 return fromDomValue($1);
928 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
929 log.debug("DOM Codec for {} was generated {}", inputType, ret)
930 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
931 } catch (Exception e) {
932 log.error("Cannot compile DOM Codec for {}", inputType, e);
933 val exception = new CodeGenerationException("Cannot compile Transformator for " + inputType);
934 exception.addSuppressed(e);
939 private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateValueTransformer(
940 Class<?> inputType, GeneratedTransferObject typeSpec, UnionTypeDefinition typeDef) {
942 val ctCls = createClass(typeSpec.codecClassName) [
943 val properties = typeSpec.allProperties;
944 val getterToTypeDefinition = XtendHelper.getTypes(typeDef).toMap[type|type.QName.getterName];
945 //staticField(Map,"AUGMENTATION_SERIALIZERS");
946 if (inputType.isYangBindingAvailable) {
947 implementsType(BINDING_CODEC)
948 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
949 staticField(it, IDENTITYREF_CODEC, BindingCodec)
950 implementsType(BindingDeserializer.asCtClass)
952 method(Object, "toDomValue", Object) [
953 modifiers = PUBLIC + FINAL + STATIC
954 val ctSpec = inputType.asCtClass;
957 ////System.out.println("«inputType.simpleName»#toDomValue: "+$1);
962 «typeSpec.resolvedName» _value = («typeSpec.resolvedName») $1;
963 «FOR property : properties.entrySet»
964 «IF property.key != "getValue"»
965 «property.value.resolvedName» «property.key» = («property.value.resolvedName») _value.«property.
967 if(«property.key» != null) {
968 return «serializeValue(property.value, property.key,
969 getterToTypeDefinition.get(property.key))»;
978 method(Object, "serialize", Object) [
981 return toDomValue($1);
985 method(Object, "fromDomValue", Object) [
986 modifiers = PUBLIC + FINAL + STATIC
989 ////System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
994 if($1 instanceof String) {
995 String _simpleValue = (String) $1;
996 return new «typeSpec.resolvedName»(_simpleValue.toCharArray());
1002 method(Object, "deserialize", Object) [
1004 return fromDomValue($1);
1010 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
1011 log.debug("DOM Codec for {} was generated {}", inputType, ret)
1012 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
1013 } catch (Exception e) {
1014 log.error("Cannot compile DOM Codec for {}", inputType, e);
1015 val exception = new CodeGenerationException("Cannot compile Transformator for " + inputType);
1016 exception.addSuppressed(e);
1021 private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateValueTransformer(
1022 Class<?> inputType, GeneratedTransferObject typeSpec, BitsTypeDefinition typeDef) {
1024 val ctCls = createClass(typeSpec.codecClassName) [
1025 //staticField(Map,"AUGMENTATION_SERIALIZERS");
1026 if (inputType.isYangBindingAvailable) {
1027 implementsType(BINDING_CODEC)
1028 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
1029 staticField(it, IDENTITYREF_CODEC, BindingCodec)
1030 implementsType(BindingDeserializer.asCtClass)
1032 method(Object, "toDomValue", Object) [
1033 modifiers = PUBLIC + FINAL + STATIC
1034 val ctSpec = typeSpec.asCtClass;
1037 ////System.out.println("«inputType.simpleName»#toDomValue: "+$1);
1042 «typeSpec.resolvedName» _encapsulatedValue = («typeSpec.resolvedName») $1;
1043 «HashSet.resolvedName» _value = new «HashSet.resolvedName»();
1044 //System.out.println("«inputType.simpleName»#toDomValue:Enc: "+_encapsulatedValue);
1046 «FOR bit : typeDef.bits»
1047 «val getter = bit.getterName()»
1048 if(Boolean.TRUE.equals(_encapsulatedValue.«getter»())) {
1049 _value.add("«bit.name»");
1052 «Set.resolvedName» _domValue = «Collections.resolvedName».unmodifiableSet(_value);
1053 //System.out.println("«inputType.simpleName»#toDomValue:DeEnc: "+_domValue);
1059 method(Object, "serialize", Object) [
1062 return toDomValue($1);
1066 method(Object, "fromDomValue", Object) [
1067 modifiers = PUBLIC + FINAL + STATIC
1068 val sortedBits = typeDef.bits.sort[o1, o2|o1.propertyName.compareTo(o2.propertyName)]
1071 //System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
1076 «Set.resolvedName» _domValue = («Set.resolvedName») $1;
1077 «FOR bit : sortedBits»
1078 Boolean «bit.propertyName» = Boolean.valueOf(_domValue.contains("«bit.name»"));
1081 return new «inputType.resolvedName»(«FOR bit : sortedBits SEPARATOR ","»«bit.propertyName»«ENDFOR»);
1085 method(Object, "deserialize", Object) [
1087 return fromDomValue($1);
1093 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
1094 log.debug("DOM Codec for {} was generated {}", inputType, ret)
1095 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
1096 } catch (Exception e) {
1097 log.error("Cannot compile DOM Codec for {}", inputType, e);
1098 val exception = new CodeGenerationException("Cannot compile Transformator for " + inputType);
1099 exception.addSuppressed(e);
1104 def String getPropertyName(Bit bit) {
1105 '''_«BindingGeneratorUtil.parseToValidParamName(bit.name)»'''
1108 def String getterName(Bit bit) {
1110 val paramName = BindingGeneratorUtil.parseToValidParamName(bit.name);
1111 return '''is«paramName.toFirstUpper»''';
1114 def boolean isYangBindingAvailable(Class<?> class1) {
1116 val bindingCodecClass = class1.classLoader.loadClass(BINDING_CODEC.name);
1117 return bindingCodecClass !== null;
1118 } catch (ClassNotFoundException e) {
1123 private def createDummyImplementation(Class<?> object, GeneratedTransferObject typeSpec) {
1124 log.trace("Generating Dummy DOM Codec for {} with {}", object, object.classLoader)
1125 return createClass(typeSpec.codecClassName) [
1126 if (object.isYangBindingAvailable) {
1127 implementsType(BINDING_CODEC)
1128 staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
1129 staticField(it, IDENTITYREF_CODEC, BindingCodec)
1130 implementsType(BindingDeserializer.asCtClass)
1132 //implementsType(BindingDeserializer.asCtClass)
1133 method(Object, "toDomValue", Object) [
1134 modifiers = PUBLIC + FINAL + STATIC
1139 return $1.toString();
1143 method(Object, "serialize", Object) [
1146 return toDomValue($1);
1150 method(Object, "fromDomValue", Object) [
1151 modifiers = PUBLIC + FINAL + STATIC
1152 bodyChecked = '''return null;'''
1154 method(Object, "deserialize", Object) [
1156 return fromDomValue($1);
1163 private def Type getValueReturnType(GeneratedTransferObject object) {
1164 for (prop : object.properties) {
1165 if (prop.name == "value") {
1166 return prop.returnType;
1169 if (object.superType != null) {
1170 return getValueReturnType(object.superType);
1175 private def Class<?> generateValueTransformer(Class<?> inputType, Enumeration typeSpec) {
1177 val typeRef = new ReferencedTypeImpl(typeSpec.packageName, typeSpec.name);
1178 val schema = typeToSchemaNode.get(typeRef) as ExtendedType;
1179 val enumSchema = schema.baseType as EnumerationType;
1181 //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
1182 val ctCls = createClass(typeSpec.codecClassName) [
1183 //staticField(Map,"AUGMENTATION_SERIALIZERS");
1184 //implementsType(BINDING_CODEC)
1185 method(Object, "toDomValue", Object) [
1186 modifiers = PUBLIC + FINAL + STATIC
1191 «typeSpec.resolvedName» _value = («typeSpec.resolvedName») $1;
1192 «FOR en : enumSchema.values»
1193 if(«typeSpec.resolvedName».«BindingGeneratorUtil.parseToClassName(en.name)».equals(_value)) {
1201 method(Object, "serialize", Object) [
1203 return toDomValue($1);
1206 method(Object, "fromDomValue", Object) [
1207 modifiers = PUBLIC + FINAL + STATIC
1213 String _value = (String) $1;
1214 «FOR en : enumSchema.values»
1215 if("«en.name»".equals(_value)) {
1216 return «typeSpec.resolvedName».«BindingGeneratorUtil.parseToClassName(en.name)»;
1223 method(Object, "deserialize", Object) [
1225 return fromDomValue($1);
1230 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
1231 log.debug("DOM Codec for {} was generated {}", inputType, ret)
1233 } catch (CodeGenerationException e) {
1234 throw new CodeGenerationException("Cannot compile Transformator for " + inputType, e);
1235 } catch (Exception e) {
1236 log.error("Cannot compile DOM Codec for {}", inputType, e);
1237 val exception = new CodeGenerationException("Cannot compile Transformator for " + inputType);
1238 exception.addSuppressed(e);
1244 def Class<?> toClassImpl(CtClass newClass, ClassLoader loader, ProtectionDomain domain) {
1245 val cls = newClass.toClass(loader, domain);
1246 if (classFileCapturePath !== null) {
1247 newClass.writeFile(classFileCapturePath.absolutePath);
1249 listener?.onCodecCreated(cls);
1253 def debugWriteClass(CtClass class1) {
1254 val path = class1.name.replace(".", "/") + ".class"
1256 val captureFile = new File(classFileCapturePath, path);
1257 captureFile.createNewFile
1261 private def dispatch String deserializeValue(Type type, String domParameter, TypeDefinition<?> typeDef) {
1262 if (INSTANCE_IDENTIFIER.equals(type)) {
1263 return '''(«InstanceIdentifier.name») «INSTANCE_IDENTIFIER_CODEC».deserialize(«domParameter»)'''
1264 } else if (CLASS_TYPE.equals(type)) {
1265 return '''(«Class.name») «IDENTITYREF_CODEC».deserialize(«domParameter»)'''
1267 return '''(«type.resolvedName») «domParameter»'''
1275 private def dispatch CharSequence deserializeProperty(DataSchemaNode container, Type type, String propertyName) '''
1276 «type.resolvedName» «propertyName» = null;
1279 private def dispatch CharSequence deserializeProperty(DataSchemaNode container, GeneratedTypeBuilder type,
1280 String propertyName) {
1281 _deserializeProperty(container, type.toInstance, propertyName)
1284 public static def toSetter(String it) {
1286 if (startsWith("is")) {
1287 return "set" + substring(2);
1288 } else if (startsWith("get")) {
1289 return "set" + substring(3);
1295 private def dispatch CharSequence deserializeProperty(DataSchemaNode container,GeneratedType type, String propertyName) '''
1296 «type.resolvedName» «propertyName» = value.«propertyName»();
1297 if(«propertyName» != null) {
1298 Object domValue = «type.serializer».toDomStatic(QNAME,«propertyName»);
1299 _childNodes.add(domValue);
1303 private def getBuilderName(GeneratedType type) '''«type.resolvedName»Builder'''
1305 private def staticQNameField(CtClass it, QName node) {
1306 val field = new CtField(ctQName, "QNAME", it);
1307 field.modifiers = PUBLIC + FINAL + STATIC;
1309 '''«QName.asCtClass.name».create("«node.namespace»","«node.formattedRevision»","«node.localName»")''')
1312 private def String serializeBodyImpl(GeneratedType type, DataNodeContainer nodeContainer) '''
1314 «QName.name» _resultName = «QName.name».create($1,QNAME.getLocalName());
1315 java.util.List _childNodes = new java.util.ArrayList();
1316 «type.resolvedName» value = («type.resolvedName») $2;
1317 «transformDataContainerBody(type, type.allProperties, nodeContainer)»
1318 «serializeAugmentations»
1319 return ($r) java.util.Collections.singletonMap(_resultName,_childNodes);
1323 private def dispatch String serializeBody(GeneratedType type, ListSchemaNode node) {
1324 return serializeBodyImpl(type, node);
1327 private def dispatch String serializeBody(GeneratedType type, NotificationDefinition node) {
1328 return serializeBodyImpl(type, node);
1331 private def dispatch String serializeBody(GeneratedType type, ContainerSchemaNode node) {
1332 return serializeBodyImpl(type, node);
1335 private def dispatch String serializeBody(GeneratedType type, ChoiceCaseNode node) {
1336 return serializeBodyImpl(type, node);
1339 private def dispatch String serializeBody(GeneratedType type, SchemaNode node) '''
1341 «QName.name» _resultName = «QName.name».create($1,QNAME.getLocalName());
1342 java.util.List _childNodes = new java.util.ArrayList();
1343 «type.resolvedName» value = («type.resolvedName») $2;
1344 return ($r) java.util.Collections.singletonMap(_resultName,_childNodes);
1348 private def transformDataContainerBody(Type type, Map<String, Type> properties, DataNodeContainer node) {
1350 «FOR child : node.childNodes»
1351 «val signature = properties.getFor(child)»
1352 «IF signature !== null»
1353 ////System.out.println("«type.name»#«signature.key»" + value.«signature.key»());
1354 «serializeProperty(child, signature.value, signature.key)»
1361 private def serializeAugmentations() '''
1362 java.util.List _augmentations = (java.util.List) «AUGMENTATION_CODEC».serialize(value);
1363 if(_augmentations != null) {
1364 _childNodes.addAll(_augmentations);
1368 def Entry<String, Type> getFor(Map<String, Type> map, DataSchemaNode node) {
1369 var sig = map.get(node.getterName);
1371 return new SimpleEntry(node.getterName, sig);
1373 sig = map.get(node.booleanGetterName);
1375 return new SimpleEntry(node.booleanGetterName, map.get(node.booleanGetterName));
1380 private static def String getBooleanGetterName(DataSchemaNode node) {
1381 return "is" + BindingMapping.getPropertyName(node.QName.localName).toFirstUpper;
1384 private static def String getGetterName(DataSchemaNode node) {
1385 return "get" + BindingMapping.getPropertyName(node.QName.localName).toFirstUpper;
1388 private static def String getGetterName(QName node) {
1389 return "get" + BindingMapping.getPropertyName(node.localName).toFirstUpper;
1392 private def dispatch CharSequence serializeProperty(ListSchemaNode schema, ParameterizedType type,
1393 String propertyName) '''
1394 «type.resolvedName» «propertyName» = value.«propertyName»();
1395 ////System.out.println("«propertyName»:" + «propertyName»);
1396 if(«propertyName» != null) {
1397 java.util.Iterator _iterator = «propertyName».iterator();
1398 boolean _hasNext = _iterator.hasNext();
1400 Object _listItem = _iterator.next();
1401 Object _domValue = «type.actualTypeArguments.get(0).serializer(schema).resolvedName».toDomStatic(_resultName,_listItem);
1402 _childNodes.add(_domValue);
1403 _hasNext = _iterator.hasNext();
1408 private def dispatch CharSequence serializeProperty(LeafSchemaNode schema, Type type, String propertyName) '''
1409 «type.resolvedName» «propertyName» = value.«propertyName»();
1411 if(«propertyName» != null) {
1412 «QName.name» _qname = «QName.name».create(_resultName,"«schema.QName.localName»");
1413 Object _propValue = «serializeValue(type, propertyName, schema.type)»;
1414 if(_propValue != null) {
1415 Object _domValue = java.util.Collections.singletonMap(_qname,_propValue);
1416 _childNodes.add(_domValue);
1421 private def dispatch serializeValue(GeneratedTransferObject type, String parameter, TypeDefinition<?> typeDefinition) {
1422 '''«type.valueSerializer(typeDefinition).resolvedName».toDomValue(«parameter»)'''
1425 private def dispatch serializeValue(Enumeration type, String parameter, TypeDefinition<?> typeDefinition) {
1426 '''«type.valueSerializer(typeDefinition).resolvedName».toDomValue(«parameter»)'''
1429 private def dispatch serializeValue(Type type, String parameter, EmptyTypeDefinition typeDefinition) {
1430 '''(«parameter».booleanValue() ? "" : null)'''
1433 private def dispatch serializeValue(Type signature, String property, TypeDefinition<?> typeDefinition) {
1434 serializeValue(signature,property)
1437 private def dispatch serializeValue(Type signature, String property, Void typeDefinition) {
1438 serializeValue(signature,property)
1441 private def dispatch serializeValue(Type signature, String property) {
1442 if (INSTANCE_IDENTIFIER == signature) {
1443 return '''«INSTANCE_IDENTIFIER_CODEC».serialize(«property»)'''
1444 } else if (CLASS_TYPE.equals(signature)) {
1445 return '''(«QName.resolvedName») «IDENTITYREF_CODEC».serialize(«property»)'''
1447 if ("char[]" == signature.name) {
1448 return '''new String(«property»)''';
1450 return '''«property»''';
1453 private def dispatch CharSequence serializeProperty(LeafListSchemaNode schema, ParameterizedType type,
1454 String propertyName) '''
1455 «type.resolvedName» «propertyName» = value.«propertyName»();
1456 if(«propertyName» != null) {
1457 «QName.name» _qname = «QName.name».create(_resultName,"«schema.QName.localName»");
1458 java.util.Iterator _iterator = «propertyName».iterator();
1459 boolean _hasNext = _iterator.hasNext();
1461 Object _listItem = _iterator.next();
1462 Object _propValue = «serializeValue(type.actualTypeArguments.get(0), "_listItem", schema.type)»;
1463 Object _domValue = java.util.Collections.singletonMap(_qname,_propValue);
1464 _childNodes.add(_domValue);
1465 _hasNext = _iterator.hasNext();
1470 private def dispatch CharSequence serializeProperty(ChoiceNode container, GeneratedType type,
1471 String propertyName) '''
1472 «type.resolvedName» «propertyName» = value.«propertyName»();
1473 if(«propertyName» != null) {
1474 java.util.List domValue = «type.serializer(container).resolvedName».toDomStatic(_resultName,«propertyName»);
1475 _childNodes.addAll(domValue);
1483 private def dispatch CharSequence serializeProperty(DataSchemaNode container, Type type, String propertyName) '''
1484 «type.resolvedName» «propertyName» = value.«propertyName»();
1485 if(«propertyName» != null) {
1486 Object domValue = «propertyName»;
1487 _childNodes.add(domValue);
1491 private def dispatch CharSequence serializeProperty(DataSchemaNode container, GeneratedTypeBuilder type,
1492 String propertyName) {
1493 serializeProperty(container, type.toInstance, propertyName)
1496 private def dispatch CharSequence serializeProperty(DataSchemaNode container, GeneratedType type,
1497 String propertyName) '''
1498 «type.resolvedName» «propertyName» = value.«propertyName»();
1499 if(«propertyName» != null) {
1500 Object domValue = «type.serializer(container).resolvedName».toDomStatic(_resultName,«propertyName»);
1501 _childNodes.add(domValue);
1505 private def codecClassName(GeneratedType typeSpec) {
1506 return '''«typeSpec.resolvedName»$Broker$Codec$DOM'''
1509 private def codecClassName(Class<?> typeSpec) {
1510 return '''«typeSpec.name»$Broker$Codec$DOM'''
1513 private def HashMap<String, Type> getAllProperties(GeneratedType type) {
1514 val ret = new HashMap<String, Type>();
1515 type.collectAllProperties(ret);
1519 private def dispatch void collectAllProperties(GeneratedType type, Map<String, Type> set) {
1520 for (definition : type.methodDefinitions) {
1521 set.put(definition.name, definition.returnType);
1523 for (property : type.properties) {
1524 set.put(property.getterName, property.returnType);
1526 for (parent : type.implements) {
1527 parent.collectAllProperties(set);
1531 def String getGetterName(GeneratedProperty property) {
1532 return "get" + property.name.toFirstUpper
1535 private def dispatch void collectAllProperties(Type type, Map<String, Type> set) {
1536 // NOOP for generic type.
1539 def String getResolvedName(Type type) {
1540 return type.asCtClass.name;
1543 def String getResolvedName(Class<?> type) {
1544 return type.asCtClass.name;
1547 def CtClass asCtClass(Type type) {
1548 val cls = loadClass(type.fullyQualifiedName)
1549 return cls.asCtClass;
1552 private def dispatch processException(Class<?> inputType, CodeGenerationException e) {
1553 log.error("Cannot compile DOM Codec for {}. One of it's prerequisites was not generated.", inputType);
1557 private def dispatch processException(Class<?> inputType, Exception e) {
1558 log.error("Cannot compile DOM Codec for {}", inputType, e);
1559 val exception = new CodeGenerationException("Cannot compile Transformator for " + inputType, e);
1563 private def setBodyChecked(CtMethod method, String body) {
1565 method.setBody(body);
1566 } catch (CannotCompileException e) {
1567 log.error("Cannot compile method: {}#{} {}, Reason: {} Body: {}", method.declaringClass, method.name,
1568 method.signature, e.message, body)
1573 private def <V> V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable<V> function) throws Exception {
1574 appendClassLoaderIfMissing(cls);
1575 ClassLoaderUtils.withClassLoaderAndLock(cls, lock, function);
1581 class PropertyPair {
1590 SchemaNode schemaNode;