Specialize relative leafref types during instantiation
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / JavaFileTemplate.java
index 3aa4f8f7901ba0a5cc998ebb7c4aba8072e3400b..6997b4d3f73ccd749556990f6d9614e2b8f8c4d8 100644 (file)
@@ -11,6 +11,7 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME;
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.GETTER_PREFIX;
 
 import com.google.common.collect.ImmutableSortedSet;
 import java.lang.reflect.Method;
@@ -30,6 +31,7 @@ import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.xtext.xbase.lib.StringExtensions;
+import org.opendaylight.mdsal.binding.model.api.AnnotationType;
 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
 import org.opendaylight.mdsal.binding.model.api.DefaultType;
 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
@@ -43,10 +45,8 @@ import org.opendaylight.mdsal.binding.model.api.Type;
 import org.opendaylight.mdsal.binding.model.util.Types;
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.yangtools.yang.binding.Augmentable;
-import org.opendaylight.yangtools.yang.binding.AugmentationHolder;
 import org.opendaylight.yangtools.yang.binding.CodeHelpers;
 
-
 /**
  * Base Java file template. Contains a non-null type and imports which the generated code refers to.
  */
@@ -68,9 +68,13 @@ class JavaFileTemplate {
      */
     static final @NonNull JavaTypeName OVERRIDE = JavaTypeName.create(Override.class);
     /**
-     * {@code java.lang.SuppressWarnings} as a JavaTypeName.
+     * {@code java.lang.void} as a JavaTypeName.
      */
     static final @NonNull JavaTypeName SUPPRESS_WARNINGS = JavaTypeName.create(SuppressWarnings.class);
+    /**
+     * {@code java.lang.SuppressWarnings} as a JavaTypeName.
+     */
+    static final @NonNull JavaTypeName VOID = JavaTypeName.create(void.class);
 
     /**
      * {@code java.util.Arrays} as a JavaTypeName.
@@ -110,10 +114,6 @@ class JavaFileTemplate {
      * {@code org.opendaylight.yangtools.yang.binding.CodeHelpers} as a JavaTypeName.
      */
     static final @NonNull JavaTypeName CODEHELPERS = JavaTypeName.create(CodeHelpers.class);
-    /**
-     * {@code org.opendaylight.yangtools.yang.binding.AugmentationHolder} as a JavaTypeName.
-     */
-    static final @NonNull JavaTypeName AUGMENTATION_HOLDER = JavaTypeName.create(AugmentationHolder.class);
 
     private static final Comparator<MethodSignature> METHOD_COMPARATOR = new AlphabeticallyTypeMemberComparator<>();
     private static final Type AUGMENTATION_RET_TYPE;
@@ -183,6 +183,14 @@ class JavaFileTemplate {
         return importedName(intype, importedName(NULLABLE));
     }
 
+    final @NonNull String fullyQualifiedNonNull(final @NonNull Type intype) {
+        return fullyQualifiedName(intype, importedName(NONNULL));
+    }
+
+    final @NonNull String fullyQualifiedName(final @NonNull Type intype, final @NonNull String annotation) {
+        return javaType.getFullyQualifiedReference(intype, annotation);
+    }
+
     // Exposed for BuilderTemplate
     boolean isLocalInnerClass(final JavaTypeName name) {
         final Optional<JavaTypeName> optEnc = name.immediatelyEnclosingClass();
@@ -238,6 +246,16 @@ class JavaFileTemplate {
         return null;
     }
 
+    /**
+     * Generate a call to {@link Object#clone()} if target field represents an array. Returns an empty string otherwise.
+     *
+     * @param property Generated property
+     * @return The string used to clone the property, or an empty string
+     */
+    static final String cloneCall(final GeneratedProperty property) {
+        return property.getReturnType().getName().endsWith("[]") ? ".clone()" : "";
+    }
+
     /**
      * Returns set of method signature instances which contains all the methods of the <code>genType</code>
      * and all the methods of the implemented interfaces.
@@ -266,7 +284,7 @@ class JavaFileTemplate {
         for (Type implementedIfc : implementedIfcs) {
             if (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject)) {
                 final GeneratedType ifc = (GeneratedType) implementedIfc;
-                methods.addAll(ifc.getMethodDefinitions());
+                addImplMethods(methods, ifc);
 
                 final ParameterizedType t = collectImplementedMethods(type, methods, ifc.getImplements());
                 if (t != null && augmentType == null) {
@@ -280,6 +298,69 @@ class JavaFileTemplate {
         return augmentType;
     }
 
+    private static void addImplMethods(final Set<MethodSignature> methods, final GeneratedType implType) {
+        for (final MethodSignature implMethod : implType.getMethodDefinitions()) {
+            if (hasOverrideAnnotation(implMethod)) {
+                methods.add(implMethod);
+            } else {
+                final String implMethodName = implMethod.getName();
+                if (BindingMapping.isGetterMethodName(implMethodName)
+                        && getterByName(methods, implMethodName).isEmpty()) {
+
+                    methods.add(implMethod);
+                }
+            }
+        }
+    }
+
+    protected static Optional<MethodSignature> getterByName(final Iterable<MethodSignature> methods,
+            final String implMethodName) {
+        for (MethodSignature method : methods) {
+            final String methodName = method.getName();
+            if (BindingMapping.isGetterMethodName(methodName)) {
+                if (isSameProperty(method.getName(), implMethodName)) {
+                    return Optional.of(method);
+                }
+            }
+        }
+        return Optional.empty();
+    }
+
+    protected static String propertyNameFromGetter(final MethodSignature getter) {
+        return propertyNameFromGetter(getter.getName());
+    }
+
+    protected static String propertyNameFromGetter(final String getterName) {
+        final String prefix;
+        if (BindingMapping.isGetterMethodName(getterName)) {
+            prefix = BindingMapping.GETTER_PREFIX;
+        } else if (BindingMapping.isNonnullMethodName(getterName)) {
+            prefix = BindingMapping.NONNULL_PREFIX;
+        } else {
+            throw new IllegalArgumentException(getterName + " is not a getter");
+        }
+        return StringExtensions.toFirstLower(getterName.substring(prefix.length()));
+    }
+
+    /**
+     * Check whether specified method has an attached annotation which corresponds to {@code @Override}.
+     *
+     * @param method Method to examine
+     * @return True if there is an override annotation
+     */
+    static boolean hasOverrideAnnotation(final MethodSignature method) {
+        for (final AnnotationType annotation : method.getAnnotations()) {
+            if (OVERRIDE.equals(annotation.getIdentifier())) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static boolean isSameProperty(final String getterName1, final String getterName2) {
+        return propertyNameFromGetter(getterName1).equals(propertyNameFromGetter(getterName2));
+    }
+
     /**
      * Creates set of generated property instances from getter <code>methods</code>.
      *
@@ -320,12 +401,11 @@ class JavaFileTemplate {
         if (method.isDefault()) {
             return null;
         }
-        final String prefix = BindingMapping.getGetterPrefix(Types.BOOLEAN.equals(method.getReturnType()));
-        if (!method.getName().startsWith(prefix)) {
+        if (!BindingMapping.isGetterMethodName(method.getName())) {
             return null;
         }
 
-        final String fieldName = StringExtensions.toFirstLower(method.getName().substring(prefix.length()));
+        final String fieldName = StringExtensions.toFirstLower(method.getName().substring(GETTER_PREFIX.length()));
         return new BuilderGeneratedProperty(fieldName, method);
     }
 }