Specialize relative leafref types during instantiation
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / InterfaceTemplate.xtend
index 2ead021a95d9a7dde5a758eec068ac1d841ffb7b..b778c9e45cad56086020232ded23185ce1ba44e2 100644 (file)
@@ -10,12 +10,15 @@ package org.opendaylight.mdsal.binding.java.api.generator
 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.getGetterMethodForNonnull
 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isGetterMethodName
 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isNonnullMethodName
-import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
+import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN
+import static org.opendaylight.mdsal.binding.model.util.Types.STRING
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_EQUALS_NAME
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_HASHCODE_NAME
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_TO_STRING_NAME
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BOOLEAN_GETTER_PREFIX
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.GETTER_PREFIX
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.MoreObjects
@@ -79,7 +82,7 @@ class InterfaceTemplate extends BaseTemplate {
      * @return string with code for interface body in JAVA format
      */
     override body() '''
-        «wrapToDocumentation(formatDataForJavaDoc(type))»
+        «type.formatDataForJavaDoc.wrapToDocumentation»
         «type.annotations.generateAnnotations»
         public interface «type.name»
             «superInterfaces»
@@ -105,6 +108,17 @@ class InterfaceTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
+    def private generateAccessorAnnotations(MethodSignature method) '''
+         «val annotations = method.annotations»
+         «IF annotations !== null && !annotations.empty»
+             «FOR annotation : annotations»
+                  «IF method.returnType != BOOLEAN || !(annotation.identifier == OVERRIDE)»
+                      «annotation.generateAnnotation»
+                  «ENDIF»
+             «ENDFOR»
+        «ENDIF»
+    '''
+
     /**
      * Template method which generates the interface name declaration.
      *
@@ -189,6 +203,12 @@ class InterfaceTemplate extends BaseTemplate {
         } else {
             switch method.name {
                 case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
+                default :
+                    if (VOID == method.returnType.identifier) {
+                        generateNoopVoidInterfaceMethod(method)
+                    } else if (method.name.startsWith(BOOLEAN_GETTER_PREFIX)) {
+                        generateIsAccessorMethod(method)
+                    }
             }
         }
     }
@@ -207,13 +227,41 @@ class InterfaceTemplate extends BaseTemplate {
         «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
     '''
 
-    def private generateAccessorMethod(MethodSignature method) '''
-        «val ret = method.returnType»
-        «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or " + asCode("null") + " if not present")»
+    def private generateNoopVoidInterfaceMethod(MethodSignature method) '''
+        «method.comment.asJavadoc»
         «method.annotations.generateAnnotations»
-        «nullableType(ret)» «method.name»();
+        default «VOID.importedName» «method.name»(«method.parameters.generateParameters») {
+            // No-op
+        }
     '''
 
+    def private generateIsAccessorMethod(MethodSignature method) '''
+        @«DEPRECATED.importedName»(forRemoval = true)
+        default «method.returnType.importedName» «method.name»(«method.parameters.generateParameters») {
+            return «GETTER_PREFIX»«method.name.substring(BOOLEAN_GETTER_PREFIX.length)»();
+        }
+    '''
+
+    def private static accessorJavadoc(MethodSignature method, String orString) {
+        val reference = method.comment?.referenceDescription
+        val propReturn = method.propertyNameFromGetter + ", or " + orString + " if it is not present."
+
+        return wrapToDocumentation('''
+            Return «propReturn».
+
+            «reference.formatReference»
+            @return {@code «method.returnType.fullyQualifiedName»} «propReturn»
+        ''')
+    }
+
+    def private generateAccessorMethod(MethodSignature method) {
+        return '''
+            «accessorJavadoc(method, "{@code null}")»
+            «method.generateAccessorAnnotations»
+            «method.returnType.nullableType» «method.name»();
+        '''
+    }
+
     def private generateDefaultImplementedInterface() '''
         @«OVERRIDE.importedName»
         default «CLASS.importedName»<«type.fullyQualifiedName»> «DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»() {
@@ -230,26 +278,18 @@ class InterfaceTemplate extends BaseTemplate {
              * Implementations of this interface are encouraged to defer to this method to get consistent hashing
              * results across all implementations.
              *
-             «IF augmentable»
-             * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
-             *              contract.
-             «ENDIF»
              * @param obj Object for which to generate hashCode() result.
              * @return Hash code value of data modeled by this interface.
              * @throws «NPE.importedName» if {@code obj} is null
              */
-            «IF augmentable»
-                static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<?>> int «BINDING_HASHCODE_NAME»(final @«NONNULL.importedName» T$$ obj) {
-            «ELSE»
-                static int «BINDING_HASHCODE_NAME»(final «type.fullyQualifiedNonNull» obj) {
-            «ENDIF»
+            static int «BINDING_HASHCODE_NAME»(final «type.fullyQualifiedNonNull» obj) {
                 final int prime = 31;
                 int result = 1;
                 «FOR property : typeAnalysis.value»
                     result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
                 «ENDFOR»
                 «IF augmentable»
-                    result = prime * result + «CODEHELPERS.importedName».hashAugmentations(obj);
+                    result = prime * result + obj.augmentations().hashCode();
                 «ENDIF»
                 return result;
             }
@@ -264,20 +304,12 @@ class InterfaceTemplate extends BaseTemplate {
              * Implementations of this interface are encouraged to defer to this method to get consistent equality
              * results across all implementations.
              *
-             «IF augmentable»
-             * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
-             *              contract.
-             «ENDIF»
              * @param thisObj Object acting as the receiver of equals invocation
              * @param obj Object acting as argument to equals invocation
              * @return True if thisObj and obj are considered equal
              * @throws «NPE.importedName» if {@code thisObj} is null
              */
-            «IF augmentable»
-            static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<«type.fullyQualifiedName»>> boolean «BINDING_EQUALS_NAME»(final @«NONNULL.importedName» T$$ thisObj, final «Types.objectType().importedName» obj) {
-            «ELSE»
             static boolean «BINDING_EQUALS_NAME»(final «type.fullyQualifiedNonNull» thisObj, final «Types.objectType().importedName» obj) {
-            «ENDIF»
                 if (thisObj == obj) {
                     return true;
                 }
@@ -290,7 +322,7 @@ class InterfaceTemplate extends BaseTemplate {
                         return false;
                     }
                 «ENDFOR»
-                return «IF augmentable»«CODEHELPERS.importedName».equalsAugmentations(thisObj, other)«ELSE»true«ENDIF»;
+                return «IF augmentable»thisObj.augmentations().equals(other.augmentations())«ELSE»true«ENDIF»;
             }
         «ENDIF»
     '''
@@ -302,19 +334,11 @@ class InterfaceTemplate extends BaseTemplate {
          * Implementations of this interface are encouraged to defer to this method to get consistent string
          * representations across all implementations.
          *
-         «IF augmentable»
-         * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
-         *              contract.
-         «ENDIF»
          * @param obj Object for which to generate toString() result.
          * @return {@link «STRING.importedName»} value of data modeled by this interface.
          * @throws «NPE.importedName» if {@code obj} is null
          */
-        «IF augmentable»
-        static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<«type.fullyQualifiedName»>> «STRING.importedName» «BINDING_TO_STRING_NAME»(final @«NONNULL.importedName» T$$ obj) {
-        «ELSE»
         static «STRING.importedName» «BINDING_TO_STRING_NAME»(final «type.fullyQualifiedNonNull» obj) {
-        «ENDIF»
             final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«type.name»");
             «FOR property : typeAnalysis.value»
                 «CODEHELPERS.importedName».appendValue(helper, "«property.name»", obj.«property.getterName»());
@@ -329,7 +353,7 @@ class InterfaceTemplate extends BaseTemplate {
     def private generateNonnullMethod(MethodSignature method) '''
         «val ret = method.returnType»
         «val name = method.name»
-        «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or an empty list if it is not present")»
+        «accessorJavadoc(method, "an empty list")»
         «method.annotations.generateAnnotations»
         default «ret.importedNonNull» «name»() {
             return «CODEHELPERS.importedName».nonnull(«name.getGetterMethodForNonnull»());
@@ -337,7 +361,7 @@ class InterfaceTemplate extends BaseTemplate {
     '''
 
     def private String nullableType(Type type) {
-        if (type.isObject) {
+        if (type.isObject && (Types.isMapType(type) || Types.isListType(type))) {
             return type.importedNullable
         }
         return type.importedName