Bug 1434 - Make arrays non-mutable
authorLadislav Borak <lborak@cisco.com>
Tue, 5 Aug 2014 11:13:58 +0000 (13:13 +0200)
committerRobert Varga <nite@hq.sk>
Fri, 23 Jan 2015 07:10:01 +0000 (07:10 +0000)
- changed getters, if fields return type is array,
  create local copy and send temporary reference
- constructor which has field of type array,
  save this array by temporary reference

Change-Id: Ifd96bd8f16eabec95409675723d5c13684914e16
Signed-off-by: Ladislav Borak <lborak@cisco.com>
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BaseTemplate.xtend
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/UnionTemplate.xtend

index 272a9f643897684db93ee02b93a54c057ff491bd..3cb9d8ce61703976b6d54ab578305aa656b6e980 100644 (file)
@@ -101,7 +101,11 @@ abstract class BaseTemplate {
     final protected def getterMethod(GeneratedProperty field) {
         '''
             public «field.returnType.importedName» «field.getterMethodName»() {
+                «IF field.returnType.importedName.contains("[]")»
+                return Arrays.copyOf(«field.fieldName», «field.fieldName».length);
+                «ELSE»
                 return «field.fieldName»;
+                «ENDIF»
             }
         '''
     }
@@ -310,19 +314,6 @@ abstract class BaseTemplate {
         return sb.toString
     }
 
-    def isDocumentationParametersNullOrEmtpy(GeneratedType type) {
-        val boolean isTypeDescriptionNullOrEmpty = type.description.nullOrEmpty
-        val boolean isTypeReferenceNullOrEmpty = type.reference.nullOrEmpty
-        val boolean isTypeModuleNameNullOrEmpty = type.moduleName.nullOrEmpty
-        val boolean isTypeSchemaPathNullOrEmpty = type.schemaPath.nullOrEmpty
-
-        if (isTypeDescriptionNullOrEmpty && isTypeReferenceNullOrEmpty && isTypeModuleNameNullOrEmpty
-            && isTypeSchemaPathNullOrEmpty) {
-            return true
-        }
-        return false
-    }
-
     def generateRestrictions(Type type, String paramName, Type returnType) '''
         «val restrictions = type.getRestrictions»
         «IF restrictions !== null»
index dba7356f9bd68cd61ce72d99407a8bcebc26c28b..1f9d9d0a4b81a7fe2d497673a08317a26ed9988c 100644 (file)
@@ -296,7 +296,11 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
 
         «FOR p : properties»
+            «IF p.returnType.importedName.contains("[]")»
+            this.«p.fieldName» = Arrays.copyOf(«p.fieldName», «p.fieldName».length);
+            «ELSE»
             this.«p.fieldName» = «p.fieldName»;
+            «ENDIF»
         «ENDFOR»
     }
 
@@ -513,7 +517,7 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
         «IF !properties.empty»
             «FOR f : properties»
-                «IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»;
+                private«IF f.readOnly» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
             «ENDFOR»
         «ENDIF»
     '''
index 2b56179964c464b17ec2259dd5a4835a14d32d4d..ecaaf8af01510af20257ba4a72e22ef20d8c7767 100644 (file)
@@ -52,7 +52,11 @@ class UnionTemplate extends ClassTemplate {
                     «type.name» defInst = «type.name»Builder.getDefaultInstance(defVal);
                     «FOR other : finalProperties»
                         «IF other.name.equals("value")»
+                            «IF other.returnType.importedName.contains("[]")»
+                            this.«other.fieldName» = Arrays.copyOf(«other.fieldName», «other.fieldName».length);
+                            «ELSE»
                             this.«other.fieldName» = «other.fieldName»;
+                            «ENDIF»
                         «ELSE»
                             this.«other.fieldName» = defInst.«other.fieldName»;
                         «ENDIF»
@@ -121,7 +125,11 @@ class UnionTemplate extends ClassTemplate {
             «ENDIF»
             «IF !properties.empty»
                 «FOR p : properties»
+                    «IF p.returnType.importedName.contains("[]")»
+                    this.«p.fieldName» = Arrays.copyOf(source.«p.fieldName», source.«p.fieldName».length);
+                    «ELSE»
                     this.«p.fieldName» = source.«p.fieldName»;
+                    «ENDIF»
                 «ENDFOR»
             «ENDIF»
         }