Import exception references
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / ClassTemplate.xtend
index 4a9fef14b158df6eec3404cb78c4c1f35c5341dc..be2f6e8e034c345e3395036b13c957f869e4ad87 100644 (file)
@@ -8,11 +8,25 @@
 package org.opendaylight.mdsal.binding.java.api.generator
 
 import static java.util.Objects.requireNonNull
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.EMPTY_TYPE;
-import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN;
-import static org.opendaylight.mdsal.binding.model.util.Types.BYTE_ARRAY;
-import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BINARY_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BOOLEAN_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.DECIMAL64_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.EMPTY_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.INSTANCE_IDENTIFIER
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.INT16_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.INT32_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.INT64_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.INT8_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.STRING_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT16_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT32_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT64_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT8_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.SCALAR_TYPE_OBJECT
+import static org.opendaylight.mdsal.binding.model.ri.Types.BOOLEAN
+import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
 import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
+import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isBitsType
 
 import com.google.common.base.Preconditions
 import com.google.common.collect.ImmutableList
@@ -21,9 +35,11 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
 import java.beans.ConstructorProperties
 import java.util.ArrayList
 import java.util.Base64;
+import java.util.Collection
 import java.util.Comparator
 import java.util.List
 import java.util.Map
+import java.util.Set
 import javax.management.ConstructorParameters
 import org.gaul.modernizer_maven_annotations.SuppressModernizer
 import org.opendaylight.mdsal.binding.model.api.ConcreteType
@@ -33,15 +49,9 @@ import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
 import org.opendaylight.mdsal.binding.model.api.Restrictions
 import org.opendaylight.mdsal.binding.model.api.Type
-import org.opendaylight.mdsal.binding.model.util.TypeConstants
-import org.opendaylight.yangtools.yang.common.Empty
-import org.opendaylight.yangtools.yang.common.Uint16
-import org.opendaylight.yangtools.yang.common.Uint32
-import org.opendaylight.yangtools.yang.common.Uint64
-import org.opendaylight.yangtools.yang.common.Uint8
-import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
-import org.opendaylight.mdsal.binding.model.util.BindingTypes
+import org.opendaylight.mdsal.binding.model.ri.TypeConstants
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
+import org.opendaylight.yangtools.yang.common.Empty
 
 /**
  * Template for generating JAVA class.
@@ -49,6 +59,17 @@ import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
 @SuppressModernizer
 class ClassTemplate extends BaseTemplate {
     static val Comparator<GeneratedProperty> PROP_COMPARATOR = Comparator.comparing([prop | prop.name])
+    static val VALUEOF_TYPES = Set.of(
+        BOOLEAN_TYPE,
+        DECIMAL64_TYPE,
+        INT8_TYPE,
+        INT16_TYPE,
+        INT32_TYPE,
+        INT64_TYPE,
+        UINT8_TYPE,
+        UINT16_TYPE,
+        UINT32_TYPE,
+        UINT64_TYPE)
 
     protected val List<GeneratedProperty> properties
     protected val List<GeneratedProperty> finalProperties
@@ -128,8 +149,11 @@ class ClassTemplate extends BaseTemplate {
      * @return string with class source code in JAVA format
      */
     def protected generateBody(boolean isInnerClass) '''
-        «wrapToDocumentation(formatDataForJavaDoc(type))»
+        «type.formatDataForJavaDoc.wrapToDocumentation»
         «annotationDeclaration»
+        «IF !isInnerClass»
+            «generatedAnnotation»
+        «ENDIF»
         «generateClassDeclaration(isInnerClass)» {
             «suidDeclaration»
             «innerClassesDeclarations»
@@ -153,7 +177,7 @@ class ClassTemplate extends BaseTemplate {
 
             «propertyMethods»
 
-            «IF (genTO.isTypedef() && genTO.getBaseType instanceof BitsTypeDefinition)»
+            «IF genTO.isBitsType»
                 «generateGetValueForBitsTypeDef»
             «ENDIF»
 
@@ -175,7 +199,7 @@ class ClassTemplate extends BaseTemplate {
 
     def private isScalarTypeObject() {
         for (impl : genTO.implements) {
-            if (BindingTypes.SCALAR_TYPE_OBJECT.identifier.equals(impl.identifier)) {
+            if (SCALAR_TYPE_OBJECT.identifier.equals(impl.identifier)) {
                 return true
             }
         }
@@ -194,7 +218,7 @@ class ClassTemplate extends BaseTemplate {
     def private scalarTypeObjectValue(GeneratedProperty field) '''
         @«OVERRIDE.importedName»
         public «field.returnType.importedName» «BindingMapping.SCALAR_TYPE_OBJECT_GET_VALUE_NAME»() {
-            return «field.fieldName»«IF field.returnType.name.endsWith("[]")».clone()«ENDIF»;
+            return «field.fieldName»«field.cloneCall»;
         }
     '''
 
@@ -245,7 +269,7 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
-    def private allValuesConstructor() '''
+    def allValuesConstructor() '''
     public «type.name»(«allProperties.asArgumentsDeclaration») {
         «IF !parentProperties.empty»
             super(«parentProperties.asArguments»);
@@ -284,11 +308,7 @@ class ClassTemplate extends BaseTemplate {
 
         «FOR p : properties»
             «val fieldName = p.fieldName»
-            «IF p.returnType.name.endsWith("[]")»
-                this.«fieldName» = «fieldName».clone();
-            «ELSE»
-                this.«fieldName» = «fieldName»;
-            «ENDIF»
+            this.«fieldName» = «fieldName»«p.cloneCall»;
         «ENDFOR»
     }
     '''
@@ -334,7 +354,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»
@@ -382,37 +402,22 @@ class ClassTemplate extends BaseTemplate {
     def protected defaultInstance() '''
         «IF genTO.typedef && !allProperties.empty && !genTO.unionType»
             «val prop = allProperties.get(0)»
-            «IF !("org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(prop.returnType.fullyQualifiedName))»
-            public static «genTO.name» getDefaultInstance(String defaultValue) {
-                «IF BYTE_ARRAY.equals(prop.returnType)»
-                    return new «genTO.name»(«Base64.importedName».getDecoder().decode(defaultValue));
-                «ELSEIF STRING.equals(prop.returnType)»
+            «val propType = prop.returnType»
+            «IF !(INSTANCE_IDENTIFIER.identifier.equals(propType.identifier))»
+            public static «genTO.name» getDefaultInstance(final String defaultValue) {
+                «IF allProperties.size > 1»
+                    «bitsArgs»
+                «ELSEIF VALUEOF_TYPES.contains(propType)»
+                    return new «genTO.name»(«propType.importedName».valueOf(defaultValue));
+                «ELSEIF STRING_TYPE.equals(propType)»
                     return new «genTO.name»(defaultValue);
-                «ELSEIF EMPTY_TYPE.equals(prop.returnType)»
+                «ELSEIF BINARY_TYPE.equals(propType)»
+                    return new «genTO.name»(«Base64.importedName».getDecoder().decode(defaultValue));
+                «ELSEIF EMPTY_TYPE.equals(propType)»
                     «Preconditions.importedName».checkArgument(defaultValue.isEmpty(), "Invalid value %s", defaultValue);
-                    return new «genTO.name»(«Empty.importedName».getInstance());
-                «ELSEIF allProperties.size > 1»
-                    «bitsArgs»
-                «ELSEIF BOOLEAN.equals(prop.returnType)»
-                    return new «genTO.name»(«Boolean.importedName».valueOf(defaultValue));
-                «ELSEIF "java.lang.Byte".equals(prop.returnType.fullyQualifiedName)»
-                    return new «genTO.name»(«Byte.importedName».valueOf(defaultValue));
-                «ELSEIF "java.lang.Short".equals(prop.returnType.fullyQualifiedName)»
-                    return new «genTO.name»(«Short.importedName».valueOf(defaultValue));
-                «ELSEIF "java.lang.Integer".equals(prop.returnType.fullyQualifiedName)»
-                    return new «genTO.name»(«Integer.importedName».valueOf(defaultValue));
-                «ELSEIF "java.lang.Long".equals(prop.returnType.fullyQualifiedName)»
-                    return new «genTO.name»(«Long.importedName».valueOf(defaultValue));
-                «ELSEIF "org.opendaylight.yangtools.yang.common.Uint8".equals(prop.returnType.fullyQualifiedName)»
-                    return new «genTO.name»(«Uint8.importedName».valueOf(defaultValue));
-                «ELSEIF "org.opendaylight.yangtools.yang.common.Uint16".equals(prop.returnType.fullyQualifiedName)»
-                    return new «genTO.name»(«Uint16.importedName».valueOf(defaultValue));
-                «ELSEIF "org.opendaylight.yangtools.yang.common.Uint32".equals(prop.returnType.fullyQualifiedName)»
-                    return new «genTO.name»(«Uint32.importedName».valueOf(defaultValue));
-                «ELSEIF "org.opendaylight.yangtools.yang.common.Uint64".equals(prop.returnType.fullyQualifiedName)»
-                    return new «genTO.name»(«Uint64.importedName».valueOf(defaultValue));
+                    return new «genTO.name»(«Empty.importedName».value());
                 «ELSE»
-                    return new «genTO.name»(new «prop.returnType.importedName»(defaultValue));
+                    return new «genTO.name»(new «propType.importedName»(defaultValue));
                 «ENDIF»
             }
             «ENDIF»
@@ -423,12 +428,12 @@ class ClassTemplate extends BaseTemplate {
     def protected bitsArgs() '''
         «JU_LIST.importedName»<«STRING.importedName»> properties = «Lists.importedName».newArrayList(«allProperties.propsAsArgs»);
         if (!properties.contains(defaultValue)) {
-            throw new «IllegalArgumentException.importedName»("invalid default parameter");
+            throw new «IAE.importedName»("invalid default parameter");
         }
         int i = 0;
         return new «genTO.name»(
         «FOR prop : allProperties SEPARATOR ","»
-            properties.get(i++).equals(defaultValue) ? «Boolean.importedName».TRUE : null
+            properties.get(i++).equals(defaultValue) ? «BOOLEAN.importedName».TRUE : null
         «ENDFOR»
         );
     '''
@@ -551,7 +556,11 @@ class ClassTemplate extends BaseTemplate {
             @«OVERRIDE.importedName»
             public int hashCode() {
                 «IF size != 1»
-                    «hashCodeResult(genTO.hashCodeIdentifiers)»
+                    final int prime = 31;
+                    int result = 1;
+                    «FOR property : genTO.hashCodeIdentifiers»
+                        result = prime * result + «property.importedUtilClass».hashCode(«property.fieldName»);
+                    «ENDFOR»
                     return result;
                 «ELSE»
                     return «CODEHELPERS.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
@@ -587,6 +596,19 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
+    def private generateToString(Collection<? extends GeneratedProperty> properties) '''
+        «IF !properties.empty»
+            @«OVERRIDE.importedName»
+            public «STRING.importedName» toString() {
+                final var helper = «MOREOBJECTS.importedName».toStringHelper(«type.importedName».class);
+                «FOR property : properties»
+                    «CODEHELPERS.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
+                «ENDFOR»
+                return helper.toString();
+            }
+        «ENDIF»
+    '''
+
     def GeneratedProperty getPropByName(String name) {
         for (GeneratedProperty prop : allProperties) {
             if (prop.name.equals(name)) {