BUG-1276: fixed generated union constructor
[yangtools.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / UnionTemplate.xtend
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.sal.java.api.generator
9
10 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
11 import java.beans.ConstructorProperties
12 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
13
14 /**
15  * Template for generating JAVA class. 
16  */
17 class UnionTemplate extends ClassTemplate {
18
19     /**
20      * Creates instance of this class with concrete <code>genType</code>.
21      * 
22      * @param genType generated transfer object which will be transformed to JAVA class source code
23      */
24     new(GeneratedTransferObject genType) {
25         super(genType)
26     }
27
28     override constructors() '''
29         «unionConstructorsParentProperties»
30         «unionConstructors»
31         «IF !allProperties.empty»
32             «copyConstructor»
33         «ENDIF»
34         «IF properties.empty && !parentProperties.empty»
35             «parentConstructor»
36         «ENDIF»
37     '''
38
39     private def unionConstructors() '''
40         «FOR property : finalProperties SEPARATOR "\n"»
41             «val propRet = property.returnType»
42             «val isCharArray = "char[]".equals(propRet.name)»
43             «IF isCharArray»
44                 /**
45                  * Constructor provided only for using in JMX. Don't use it for
46                  * construction new object of this union type. 
47                  */
48                 @«ConstructorProperties.importedName»("«property.name»")
49                 public «type.name»(«propRet.importedName» «property.fieldName») {
50                     «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
51                     «type.name» defInst = «type.name»Builder.getDefaultInstance(defVal);
52                     «FOR other : finalProperties»
53                         «IF other.name.equals("value")»
54                             this.«other.fieldName» = «other.fieldName»;
55                         «ELSE»
56                             this.«other.fieldName» = defInst.«other.fieldName»;
57                         «ENDIF»
58                     «ENDFOR»
59                 }
60             «ELSE»
61                 «val propertyAndTopParentProperties = parentProperties + #[property]»
62                 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
63                     super(«parentProperties.asArguments»);
64                     this.«property.fieldName» = «property.fieldName»;
65                     «FOR other : finalProperties»
66                         «IF property != other»
67                             «IF "value".equals(other.name)»
68                                 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
69                                     ««« type string
70                                     this.«other.fieldName» = «property.fieldName».toCharArray();
71                                 «ELSEIF "byte[]".equals(propRet.name)»
72                                     ««« type binary
73                                     this.«other.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
74                                 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
75                                     ««« type int*, uint or enumeration*
76                                     this.«other.fieldName» = «property.fieldName».toString().toCharArray();
77                                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
78                                     ««« union type
79                                     this.«other.fieldName» = «property.fieldName».getValue();
80                                 «ELSE»
81                                     ««« generated type
82                                     this.«other.fieldName» = «property.fieldName».getValue().toString().toCharArray();
83                                 «ENDIF»
84                             «ELSE»
85                                 this.«other.fieldName» = null;
86                             «ENDIF»
87                         «ENDIF»
88                     «ENDFOR»
89                 }
90             «ENDIF»
91         «ENDFOR»
92     '''
93
94     private def unionConstructorsParentProperties() '''
95         «FOR property : parentProperties SEPARATOR "\n"»
96             public «type.name»(«property.returnType.importedName» «property.fieldName») {
97                 super(«property.fieldName»);
98             }
99         «ENDFOR»
100     '''
101
102     override protected copyConstructor() '''
103         /**
104          * Creates a copy from Source Object.
105          *
106          * @param source Source object
107          */
108         public «type.name»(«type.name» source) {
109             «IF !parentProperties.empty»
110                 super(source);
111             «ENDIF»
112             «IF !properties.empty»
113                 «FOR p : properties»
114                     this.«p.fieldName» = source.«p.fieldName»;
115                 «ENDFOR»
116             «ENDIF»
117         }
118     '''
119
120 }