Fix list Key data object nullness guarantees 22/91722/7
authorillia.ihushev <illia.ihushev@pantheon.tech>
Tue, 28 Jul 2020 12:20:58 +0000 (15:20 +0300)
committerRobert Varga <nite@hq.sk>
Mon, 3 Aug 2020 10:52:07 +0000 (10:52 +0000)
Verify is values nonnull in the constructors. Annotate getters return
types, constructor parameters with @NonNull.

JIRA: MDSAL-491
Change-Id: Idd8adacd9f8b2916b92a171df8d7e5001b1557d3
Signed-off-by: illia.ihushev <illia.ihushev@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BaseTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ClassTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ListKeyTemplate.xtend [new file with mode: 0644]
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/TOGenerator.java
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/test/ClassCodeGeneratorTest.java

index 63689ab1cd15a728bf6479bdd148e7d0c91cfb25..d73e9317758d82fd755d4cd3356d8c16f1168671 100644 (file)
@@ -20,6 +20,7 @@ import java.util.Locale
 import java.util.Map.Entry
 import java.util.StringTokenizer
 import java.util.regex.Pattern
+import org.eclipse.jdt.annotation.NonNull;
 import org.gaul.modernizer_maven_annotations.SuppressModernizer
 import org.opendaylight.mdsal.binding.model.api.AnnotationType
 import org.opendaylight.mdsal.binding.model.api.ConcreteType
@@ -165,6 +166,17 @@ abstract class BaseTemplate extends JavaFileTemplate {
     def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
         returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
 
+    /**
+     * Template method which generates method parameters with their types from <code>parameters</code>, annotating them
+     * with {@link NonNull}.
+     *
+     * @param parameters group of generated property instances which are transformed to the method parameters
+     * @return string with the list of the method parameters with their types in JAVA format
+     */
+    def final protected asNonNullArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»
+        «FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedNonNull» «parameter
+        .fieldName»«ENDFOR»«ENDIF»'''
+
     /**
      * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
      *
index 4a5f3913c1d0d7210cf98fef8ceed20eb621d7e9..3199d9122db03b8452823d35d21c4ce675c5eeb5 100644 (file)
@@ -267,7 +267,7 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
-    def private allValuesConstructor() '''
+    def allValuesConstructor() '''
     public «type.name»(«allProperties.asArgumentsDeclaration») {
         «IF !parentProperties.empty»
             super(«parentProperties.asArguments»);
@@ -379,7 +379,7 @@ class ClassTemplate extends BaseTemplate {
         }
     }
 
-    def private generateRestrictions(Type type, String paramName, Type returnType) '''
+    def generateRestrictions(Type type, String paramName, Type returnType) '''
         «val restrictions = type.restrictions»
         «IF restrictions !== null»
             «IF restrictions.lengthConstraint.present || restrictions.rangeConstraint.present»
diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ListKeyTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ListKeyTemplate.xtend
new file mode 100644 (file)
index 0000000..64dfc08
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2020 Pantheon Technologies, s.r.o. 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.mdsal.binding.java.api.generator
+
+import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
+import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
+
+/**
+ * Template for generating JAVA class.
+ */
+class ListKeyTemplate 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 final allValuesConstructor() '''
+        public «type.name»(«allProperties.asNonNullArgumentsDeclaration») {
+            «FOR p : allProperties»
+                «CODEHELPERS.importedName».requireValue(«p.fieldName»);
+            «ENDFOR»
+            «FOR p : properties»
+                «generateRestrictions(type, p.fieldName, p.returnType)»
+            «ENDFOR»
+
+            «FOR p : allProperties»
+                «val fieldName = p.fieldName»
+                «IF p.returnType.name.endsWith("[]")»
+                    this.«fieldName» = «fieldName».clone();
+                «ELSE»
+                    this.«fieldName» = «fieldName»;
+                «ENDIF»
+            «ENDFOR»
+        }
+    '''
+
+    override final def getterMethod(GeneratedProperty field) {
+        '''
+            public «field.returnType.importedNonNull» «field.getterMethodName»() {
+                «val fieldName = field.fieldName»
+                «IF field.returnType.name.endsWith("[]")»
+                return «fieldName».clone();
+                «ELSE»
+                return «fieldName»;
+                «ENDIF»
+            }
+        '''
+    }
+}
index fc2c4efcf81f47af270e7cd0df12bbc26e376af8..404f5eda7dd0975bedeb169363a9329e94167d6b 100644 (file)
@@ -30,9 +30,12 @@ public final class TOGenerator implements CodeGenerator {
             } else if (genTO.isUnionTypeBuilder()) {
                 final UnionBuilderTemplate template = new UnionBuilderTemplate(genTO);
                 return template.generate();
-            } else {
+            } else if (genTO.isTypedef()) {
                 final ClassTemplate template = new ClassTemplate(genTO);
                 return template.generate();
+            } else {
+                final ListKeyTemplate template = new ListKeyTemplate(genTO);
+                return template.generate();
             }
         }
         return "";
index 952cc1e47a8cb56aa131f9b544a5e3ede3687779..7526afaf5563d7e35a67d2191b0c6e183adf375d 100644 (file)
@@ -61,7 +61,8 @@ public class ClassCodeGeneratorTest {
                     final String outputStr = clsGen.generate(genTO);
 
                     assertNotNull(outputStr);
-                    assertTrue(outputStr.contains("public CompositeKeyListKey(Byte _key1, String _key2)"));
+                    assertTrue(outputStr.contains("public CompositeKeyListKey(@NonNull Byte _key1, @NonNull String"
+                            + " _key2)"));
 
                     assertEquals(2, propertyCount);
                     genTOsCount++;