Specialize relative leafref types during instantiation
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / BaseTemplate.xtend
index 5528a657337a5b27ab2adddef46354db31e22e83..224d212df5da03a771730e02dacb19d892f91ff3 100644 (file)
@@ -7,7 +7,8 @@
  */
 package org.opendaylight.mdsal.binding.java.api.generator
 
-import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
+import static extension org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
+import static extension org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.replaceAllIllegalChars
 
 import com.google.common.base.CharMatcher
 import com.google.common.base.Splitter
@@ -31,10 +32,9 @@ import org.opendaylight.mdsal.binding.model.api.JavaTypeName
 import org.opendaylight.mdsal.binding.model.api.MethodSignature
 import org.opendaylight.mdsal.binding.model.api.Restrictions
 import org.opendaylight.mdsal.binding.model.api.Type
-import org.opendaylight.mdsal.binding.model.api.TypeMember
+import org.opendaylight.mdsal.binding.model.api.TypeMemberComment
 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Single
 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Multiple
-import org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil
 import org.opendaylight.mdsal.binding.model.util.TypeConstants
 import org.opendaylight.mdsal.binding.model.util.Types
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
@@ -102,20 +102,6 @@ abstract class BaseTemplate extends JavaFileTemplate {
         "_" + property.name
     }
 
-    final protected def propertyNameFromGetter(MethodSignature getter) {
-        var String prefix;
-        if (getter.name.startsWith(BindingMapping.BOOLEAN_GETTER_PREFIX)) {
-            prefix = BindingMapping.BOOLEAN_GETTER_PREFIX
-        } else if (getter.name.startsWith(BindingMapping.GETTER_PREFIX)) {
-            prefix = BindingMapping.GETTER_PREFIX
-        } else if (getter.name.startsWith(BindingMapping.NONNULL_PREFIX)) {
-            prefix = BindingMapping.NONNULL_PREFIX
-        } else {
-            throw new IllegalArgumentException(getter + " is not a getter")
-        }
-        return getter.name.substring(prefix.length).toFirstLower;
-    }
-
     /**
      * Template method which generates the getter method for <code>field</code>
      *
@@ -123,22 +109,27 @@ abstract class BaseTemplate extends JavaFileTemplate {
      * generated property with data about field which is generated as the getter method
      * @return string with the getter method source code in JAVA format
      */
-    protected def getterMethod(GeneratedProperty field) {
-        '''
-            public «field.returnType.importedName» «field.getterMethodName»() {
-                «val fieldName = field.fieldName»
-                «IF field.returnType.name.endsWith("[]")»
-                return «fieldName» == null ? null : «fieldName».clone();
-                «ELSE»
-                return «fieldName»;
-                «ENDIF»
-            }
-        '''
-    }
+    protected def getterMethod(GeneratedProperty field) '''
+        «val methodName = field.getterMethodName»
+        public «field.returnType.importedName» «methodName»() {
+            «val fieldName = field.fieldName»
+            «IF field.returnType.name.endsWith("[]")»
+            return «fieldName» == null ? null : «fieldName».clone();
+            «ELSE»
+            return «fieldName»;
+            «ENDIF»
+        }
+        «IF field.returnType == Types.BOOLEAN»
+
+        @«DEPRECATED.importedName»(forRemoval = true)
+        public final «field.returnType.importedName» «BindingMapping.BOOLEAN_GETTER_PREFIX»«field.name.toFirstUpper»() {
+            return «methodName»();
+        }
+        «ENDIF»
+    '''
 
     final protected def getterMethodName(GeneratedProperty field) {
-        val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
-        return '''«prefix»«field.name.toFirstUpper»'''
+        return '''«BindingMapping.GETTER_PREFIX»«field.name.toFirstUpper»'''
     }
 
     /**
@@ -193,13 +184,17 @@ abstract class BaseTemplate extends JavaFileTemplate {
      * @param comment string with the comment for whole JAVA class
      * @return string with comment in JAVA format
      */
-    def protected CharSequence asJavadoc(String comment) {
+    def final protected asJavadoc(TypeMemberComment comment) {
         if (comment === null) {
             return ''
         }
-        return '''
-            «wrapToDocumentation(formatToParagraph(comment.trim))»
-        '''
+        return wrapToDocumentation('''
+           «comment.contractDescription»
+
+           «comment.referenceDescription.formatReference»
+
+           «comment.typeSignature»
+        ''')
     }
 
     def static String wrapToDocumentation(String text) {
@@ -313,7 +308,7 @@ abstract class BaseTemplate extends JavaFileTemplate {
     def private static void appendYangSnippet(StringBuilder sb, ModuleEffectiveStatement module,
             DeclaredStatement<?> stmt) {
         for (String str : YANG_FORMATTER.toYangTextSnippet(module, stmt)) {
-            sb.append(BindingGeneratorUtil.replaceAllIllegalChars(encodeAngleBrackets(encodeJavadocSymbols(str))))
+            sb.append(str.encodeJavadocSymbols.encodeAngleBrackets.replaceAllIllegalChars)
         }
     }
 
@@ -338,24 +333,16 @@ abstract class BaseTemplate extends JavaFileTemplate {
         return sb.toString();
     }
 
-    def protected static String formatDataForJavaDoc(TypeMember type, String additionalComment) {
-        val StringBuilder typeDescriptionBuilder = new StringBuilder();
-        if (!type.comment.nullOrEmpty) {
-            typeDescriptionBuilder.append(formatToParagraph(type.comment))
-            typeDescriptionBuilder.append(NEW_LINE)
-            typeDescriptionBuilder.append(NEW_LINE)
-            typeDescriptionBuilder.append(NEW_LINE)
-        }
-        typeDescriptionBuilder.append(additionalComment)
-        var typeDescription = wrapToDocumentation(typeDescriptionBuilder.toString)
-        return '''
-            «typeDescription»
-        '''.toString
-    }
+    def static formatReference(String reference) '''
+        «IF reference !== null»
+            <pre>
+                <code>
+                    «reference.encodeAngleBrackets.formatToParagraph»
+                </code>
+            </pre>
 
-    def asCode(String text) {
-        return "<code>" + text + "</code>"
-    }
+        «ENDIF»
+    '''
 
     def asLink(String text) {
         val StringBuilder sb = new StringBuilder()
@@ -380,21 +367,15 @@ abstract class BaseTemplate extends JavaFileTemplate {
         return sb.toString
     }
 
-    protected static def formatToParagraph(String text) {
-        if(text === null || text.isEmpty)
-            return text
-
-        var formattedText = text
+    protected static def formatToParagraph(String inputText) {
         val StringBuilder sb = new StringBuilder();
         var StringBuilder lineBuilder = new StringBuilder();
         var boolean isFirstElementOnNewLineEmptyChar = false;
 
-        formattedText = encodeJavadocSymbols(formattedText)
-        formattedText = WS_MATCHER.replaceFrom(formattedText, SPACE)
+        var formattedText = WS_MATCHER.replaceFrom(inputText.encodeJavadocSymbols, SPACE)
         formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
 
-        val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true);
-
+        val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true)
         while (tokenizer.hasMoreTokens) {
             val nextElement = tokenizer.nextToken
 
@@ -413,7 +394,6 @@ abstract class BaseTemplate extends JavaFileTemplate {
                     isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
                 }
             }
-
             if (isFirstElementOnNewLineEmptyChar) {
                 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
             } else {