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
index a17bdadf73a679197ff1e1e1ffc36891cc87cc69..94cfe1e8d05cec8076711637a6da1129e06d8fed 100644 (file)
-package org.opendaylight.yangtools.sal.java.api.generator\r
-\r
-import java.util.List\r
-import java.util.Map\r
-import org.opendaylight.yangtools.binding.generator.util.TypeConstants\r
-import org.opendaylight.yangtools.sal.binding.model.api.Constant\r
-import org.opendaylight.yangtools.sal.binding.model.api.Enumeration\r
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty\r
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject\r
-import org.opendaylight.yangtools.sal.binding.model.api.Type\r
-import org.opendaylight.yangtools.binding.generator.util.Types\r
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType\r
-\r
-\r
-/**\r
- * Template for generating JAVA class. \r
- */\r
-class UnionTemplate extends ClassTemplate {\r
-\r
-\r
-    \r
-    /**\r
-     * Creates instance of this class with concrete <code>genType</code>.\r
-     * \r
-     * @param genType generated transfer object which will be transformed to JAVA class source code\r
-     */\r
-    new(GeneratedTransferObject genType) {\r
-        super(genType)\r
-    }\r
-    \r
-\r
-    \r
-    \r
-    override constructors() '''\r
-    «unionConstructors»\r
-    «IF !allProperties.empty»\r
-    «copyConstructor»\r
-    «ENDIF»\r
-    «IF properties.empty && !parentProperties.empty »\r
-        «parentConstructor»\r
-    «ENDIF»\r
-    '''\r
-    \r
-\r
-     def unionConstructors() '''\r
-        «FOR property : finalProperties SEPARATOR "\n"»\r
-                «val propertyAndTopParentProperties = parentProperties + #[property]»\r
-                public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {\r
-                    super(«parentProperties.asArguments»);\r
-                    this.«property.fieldName» = «property.fieldName»;\r
-                    «FOR other : finalProperties»\r
-                    «IF property != other»this.«other.fieldName» = null;«ENDIF»\r
-                    «ENDFOR»\r
-                }\r
-        «ENDFOR»\r
-     ''' \r
-}\r
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.java.api.generator
+
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
+import java.beans.ConstructorProperties
+import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
+
+/**
+ * Template for generating JAVA class. 
+ */
+class UnionTemplate extends ClassTemplate {
+
+    /**
+     * Creates instance of this class with concrete <code>genType</code>.
+     * 
+     * @param genType generated transfer object which will be transformed to JAVA class source code
+     */
+    new(GeneratedTransferObject genType) {
+        super(genType)
+    }
+
+    override constructors() '''
+        «unionConstructorsParentProperties»
+        «unionConstructors»
+        «IF !allProperties.empty»
+            «copyConstructor»
+        «ENDIF»
+        «IF properties.empty && !parentProperties.empty»
+            «parentConstructor»
+        «ENDIF»
+    '''
+
+    private def unionConstructors() '''
+        «FOR property : finalProperties SEPARATOR "\n"»
+            «val propRet = property.returnType»
+            «val isCharArray = "char[]".equals(propRet.name)»
+            «IF isCharArray»
+                /**
+                 * Constructor provided only for using in JMX. Don't use it for
+                 * construction new object of this union type. 
+                 */
+                @«ConstructorProperties.importedName»("«property.name»")
+                public «type.name»(«propRet.importedName» «property.fieldName») {
+                    «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
+                    «type.name» defInst = «type.name»Builder.getDefaultInstance(defVal);
+                    «FOR other : finalProperties»
+                        «IF other.name.equals("value")»
+                            this.«other.fieldName» = «other.fieldName»;
+                        «ELSE»
+                            this.«other.fieldName» = defInst.«other.fieldName»;
+                        «ENDIF»
+                    «ENDFOR»
+                }
+            «ELSE»
+                «val propertyAndTopParentProperties = parentProperties + #[property]»
+                public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
+                    super(«parentProperties.asArguments»);
+                    this.«property.fieldName» = «property.fieldName»;
+                    «FOR other : finalProperties»
+                        «IF property != other»
+                            «IF "value".equals(other.name)»
+                                «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
+                                    ««« type string
+                                    this.«other.fieldName» = «property.fieldName».toCharArray();
+                                «ELSEIF "byte[]".equals(propRet.name)»
+                                    ««« type binary
+                                    this.«other.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
+                                «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
+                                    ««« type int*, uint or enumeration*
+                                    this.«other.fieldName» = «property.fieldName».toString().toCharArray();
+                                «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
+                                    ««« union type
+                                    this.«other.fieldName» = «property.fieldName».getValue();
+                                «ELSE»
+                                    ««« generated type
+                                    this.«other.fieldName» = «property.fieldName».getValue().toString().toCharArray();
+                                «ENDIF»
+                            «ELSE»
+                                this.«other.fieldName» = null;
+                            «ENDIF»
+                        «ENDIF»
+                    «ENDFOR»
+                }
+            «ENDIF»
+        «ENDFOR»
+    '''
+
+    private def unionConstructorsParentProperties() '''
+        «FOR property : parentProperties SEPARATOR "\n"»
+            public «type.name»(«property.returnType.importedName» «property.fieldName») {
+                super(«property.fieldName»);
+            }
+        «ENDFOR»
+    '''
+
+    override protected copyConstructor() '''
+        /**
+         * Creates a copy from Source Object.
+         *
+         * @param source Source object
+         */
+        public «type.name»(«type.name» source) {
+            «IF !parentProperties.empty»
+                super(source);
+            «ENDIF»
+            «IF !properties.empty»
+                «FOR p : properties»
+                    this.«p.fieldName» = source.«p.fieldName»;
+                «ENDFOR»
+            «ENDIF»
+        }
+    '''
+
+}