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 07315c4a728df7555ad9685e9e0ca6f8d480d9af..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));
             }
         }
     '''
@@ -240,7 +251,7 @@ abstract class BaseTemplate {
                 }
             }
             if (!isValidRange) {
-                throw new IllegalArgumentException(String.format("Invalid range: {}, expected: {}.", «paramName», rangeConstraints));
+                throw new IllegalArgumentException(String.format("Invalid range: %s, expected: %s.", «paramName», rangeConstraints));
             }
         }
     '''
@@ -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»
     '''
@@ -279,15 +292,6 @@ abstract class BaseTemplate {
         return null;
     }
 
-    def GeneratedProperty getPropByName(Collection<GeneratedProperty> props, String name) {
-        for (GeneratedProperty prop : props) {
-            if (prop.name.equals(name)) {
-                return prop;
-            }
-        }
-        return null;
-    }
-
     def getRestrictions(Type type) {
         var Restrictions restrictions = null
         if (type instanceof ConcreteType) {
@@ -322,4 +326,19 @@ abstract class BaseTemplate {
         return "\"" + obj.toString + "\"";
     }
 
+    /**
+     * Template method which generates method parameters with their types from <code>parameters</code>.
+     * 
+     * @param parameters
+     * list of parameter instances which are transformed to the method parameters
+     * @return string with the list of the method parameters with their types in JAVA format
+     */
+    def protected generateParameters(List<MethodSignature.Parameter> parameters) '''«
+        IF !parameters.empty»«
+            FOR parameter : parameters SEPARATOR ", "»«
+                parameter.type.importedName» «parameter.name»«
+            ENDFOR»«
+        ENDIF
+    »'''
+
 }