Enforce patterns in supertype constructors 29/73129/5
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 18 Jun 2018 12:02:15 +0000 (14:02 +0200)
committerTom Pantelis <tompantelis@gmail.com>
Wed, 27 Jun 2018 19:53:07 +0000 (19:53 +0000)
We have failed to enforce patterns when being constructed from
a supertype, which would allow creating invalid instances.

Fix that by enforcing patterns consistently.

JIRA: MDSAL-351
Change-Id: I16f4485ec8b6535c7094e3aac018cb6fbb0e5570
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ClassTemplate.xtend

index 90b2ac3701e65ca4756f9673fda620c2a117f240..c9a20bfe658df4cafd0db4df54052597faed6fe0 100644 (file)
@@ -223,11 +223,7 @@ class ClassTemplate extends BaseTemplate {
          */
         IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
             «Objects.importedName».requireNonNull(_value, "Supplied value may not be null");
-            «FOR c : consts»
-                «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
-                «CodeHelpers.importedName».checkPattern(_value, «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
-                «ENDIF»
-            «ENDFOR»
+            «genPatternEnforcer("_value")»
         «ENDIF»
 
         «FOR p : properties»
@@ -271,6 +267,14 @@ class ClassTemplate extends BaseTemplate {
     }
     '''
 
+    def private genPatternEnforcer(String ref) '''
+        «FOR c : consts»
+            «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
+            «CodeHelpers.importedName».checkPattern(«ref», «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
+            «ENDIF»
+        «ENDFOR»
+    '''
+
     def private static paramValue(Type returnType, String paramName) {
         if (returnType instanceof ConcreteType) {
             return paramName
@@ -318,7 +322,8 @@ class ClassTemplate extends BaseTemplate {
      * @param source Source object
      */
     public «type.name»(«genTO.superType.importedName» source) {
-            super(source);
+        super(source);
+        «genPatternEnforcer("getValue()")»
     }
     '''