Bug 1193: Use format string correctly and fix corresponding unit test
[yangtools.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / BaseTemplate.xtend
index 99a19687142de6054cd602c2c9f7994657801541..fc9535e0d2bd84386d3fd8545be3511bb22b6f06 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * 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.GeneratedProperty
@@ -146,7 +153,11 @@ abstract class BaseTemplate {
      */
     def protected CharSequence asJavadoc(String comment) {
         if(comment == null) return '';
-        val paragraphs = paragraphSplitter.split(comment)
+        var txt = comment
+        if (txt.contains("*/")) {
+            txt = txt.replace("*/", "*/")
+        }
+        val paragraphs = paragraphSplitter.split(txt)
 
         return '''
             /**
@@ -214,7 +225,7 @@ abstract class BaseTemplate {
                 }
             }
             if (!isValidLength) {
-                throw new IllegalArgumentException(String.format("Invalid length: {}, expected: {}.", «paramName», lengthConstraints));
+                throw new IllegalArgumentException(String.format("Invalid length: %s, expected: %s.", «paramName», lengthConstraints));
             }
         }
     '''
@@ -249,23 +260,25 @@ abstract class BaseTemplate {
         «IF !properties.empty»
             @Override
             public String toString() {
-                StringBuilder builder = new StringBuilder();
-                builder.append("«type.name» [«properties.get(0).fieldName»=");
-                «IF properties.get(0).returnType.name.contains("[")»
-                    builder.append(«Arrays.importedName».toString(«properties.get(0).fieldName»));
-                «ELSE»
-                    builder.append(«properties.get(0).fieldName»);
-                «ENDIF»
-                «FOR i : 1..<properties.size»
-                    builder.append(", «properties.get(i).fieldName»=");
-                    «IF properties.get(i).returnType.name.contains("[")»
-                        builder.append(«Arrays.importedName».toString(«properties.get(i).fieldName»));
-                    «ELSE»
-                        builder.append(«properties.get(i).fieldName»);
-                    «ENDIF»
+                StringBuilder builder = new StringBuilder("«type.name» [");
+                boolean first = true;
+
+                «FOR property : properties»
+                    if («property.fieldName» != null) {
+                        if (first) {
+                            first = false;
+                        } else {
+                            builder.append(", ");
+                        }
+                        builder.append("«property.fieldName»=");
+                        «IF property.returnType.name.contains("[")»
+                            builder.append(«Arrays.importedName».toString(«property.fieldName»));
+                        «ELSE»
+                            builder.append(«property.fieldName»);
+                        «ENDIF»
+                     }
                 «ENDFOR»
-                builder.append("]");
-                return builder.toString();
+                return builder.append(']').toString();
             }
         «ENDIF»
     '''