Bug 5446: toString() throws exception for 'type binary' binding
[mdsal.git] / binding / mdsal-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 import static org.opendaylight.yangtools.binding.generator.util.Types.*
14
15 /**
16  * Template for generating JAVA class.
17  */
18 class UnionTemplate extends ClassTemplate {
19
20     /**
21      * Creates instance of this class with concrete <code>genType</code>.
22      *
23      * @param genType generated transfer object which will be transformed to JAVA class source code
24      */
25     new(GeneratedTransferObject genType) {
26         super(genType)
27         if(isBaseEncodingImportRequired) {
28             this.importMap.put("BaseEncoding","com.google.common.io")
29         }
30     }
31
32     final private def boolean isBaseEncodingImportRequired() {
33         for (property : finalProperties) {
34             val propRet = property.returnType
35             if (propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).typedef &&
36                 (propRet as GeneratedTransferObject).properties != null &&
37                 !(propRet as GeneratedTransferObject).properties.empty &&
38                 ((propRet as GeneratedTransferObject).properties.size == 1) &&
39                 (propRet as GeneratedTransferObject).properties.get(0).name.equals("value") &&
40                 "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)) {
41                 return true;
42             }
43         }
44     }
45
46     override constructors() '''
47         «unionConstructorsParentProperties»
48         «unionConstructors»
49         «IF !allProperties.empty»
50             «copyConstructor»
51         «ENDIF»
52         «IF properties.empty && !parentProperties.empty»
53             «parentConstructor»
54         «ENDIF»
55     '''
56
57     private def unionConstructors() '''
58         «FOR property : finalProperties SEPARATOR "\n"»
59             «val propRet = property.returnType»
60             «val isCharArray = "char[]".equals(propRet.name)»
61             «IF isCharArray»
62                 /**
63                  * Constructor provided only for using in JMX. Don't use it for
64                  * construction new object of this union type.
65                  */
66                 @«ConstructorProperties.importedName»("«property.name»")
67                 public «type.name»(«propRet.importedName» «property.fieldName») {
68                     «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
69                     «type.name» defInst = «typeBuilder()».getDefaultInstance(defVal);
70                     «FOR other : finalProperties»
71                         «IF other.name.equals("value")»
72                             «IF other.returnType.importedName.contains("[]")»
73                             this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
74                             «ELSE»
75                             this.«other.fieldName» = «other.fieldName»;
76                             «ENDIF»
77                         «ELSE»
78                             this.«other.fieldName» = defInst.«other.fieldName»;
79                         «ENDIF»
80                     «ENDFOR»
81                 }
82             «ELSE»
83                 «val propertyAndTopParentProperties = parentProperties + #[property]»
84                 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
85                     super(«parentProperties.asArguments»);
86                     this.«property.fieldName» = «property.fieldName»;
87                     «FOR other : finalProperties»
88                         «IF property != other»
89                             «IF "value".equals(other.name)»
90                                 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
91                                     ««« type string
92                                     this.«other.fieldName» = «property.fieldName».toCharArray();
93                                 «ELSEIF "byte[]".equals(propRet.name)»
94                                     ««« type binary
95                                     this.«other.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
96                                 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
97                                     || propRet instanceof Enumeration
98                                     || propRet.fullyQualifiedName.startsWith("java.math")»
99                                     ««« type int*, uint, decimal64 or enumeration*
100                                     this.«other.fieldName» = «property.fieldName».toString().toCharArray();
101                                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
102                                     ««« union type
103                                     this.«other.fieldName» = «property.fieldName».getValue();
104                                 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
105                                         && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
106                                         && (propRet as GeneratedTransferObject).properties != null
107                                         && !(propRet as GeneratedTransferObject).properties.empty
108                                         && ((propRet as GeneratedTransferObject).properties.size == 1)
109                                         && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
110                                         && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
111                                     ««« generated boolean typedef
112                                     this.«other.fieldName» = «property.fieldName».isValue().toString().toCharArray();
113                                 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
114                                         && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
115                                         && (propRet as GeneratedTransferObject).properties != null
116                                         && !(propRet as GeneratedTransferObject).properties.empty
117                                         && ((propRet as GeneratedTransferObject).properties.size == 1)
118                                         && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
119                                         && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
120                                     ««« generated byte[] typedef
121                                     this.«other.fieldName» = BaseEncoding.base64().encode(«property.fieldName».getValue()).toCharArray();
122                                 «ELSE»
123                                     ««« generated type
124                                     this.«other.fieldName» = «property.fieldName».getValue().toString().toCharArray();
125                                 «ENDIF»
126                             «ELSE»
127                                 this.«other.fieldName» = null;
128                             «ENDIF»
129                         «ENDIF»
130                     «ENDFOR»
131                 }
132             «ENDIF»
133         «ENDFOR»
134     '''
135     
136     def typeBuilder() {
137         val outerCls = getOuterClassName(type);
138         if(outerCls !== null) {
139             return outerCls + type.name + "Builder"
140         }
141         return type.name + "Builder"
142     }
143
144     private def unionConstructorsParentProperties() '''
145         «FOR property : parentProperties SEPARATOR "\n"»
146             public «type.name»(«property.returnType.importedName» «property.fieldName») {
147                 super(«property.fieldName»);
148             }
149         «ENDFOR»
150     '''
151
152     override protected copyConstructor() '''
153         /**
154          * Creates a copy from Source Object.
155          *
156          * @param source Source object
157          */
158         public «type.name»(«type.name» source) {
159             «IF !parentProperties.empty»
160                 super(source);
161             «ENDIF»
162             «IF !properties.empty»
163                 «FOR p : properties»
164                     «IF p.returnType.importedName.contains("[]")»
165                     this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
166                     «ELSE»
167                     this.«p.fieldName» = source.«p.fieldName»;
168                     «ENDIF»
169                 «ENDFOR»
170             «ENDIF»
171         }
172     '''
173
174 }