X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-java-api-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fjava%2Fapi%2Fgenerator%2FInterfaceTemplate.xtend;h=903b99fdd251f1c6de74a5b67fc320a3422894d2;hb=99459c3a8270ef41ff6c95f88f2db7bc1b278269;hp=8fde4618a27e1f9803aebaccf60622c3ee93a317;hpb=5e0e06c1b91e060e7dd8523d59315b7b371a9166;p=mdsal.git diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/InterfaceTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/InterfaceTemplate.xtend index 8fde4618a2..903b99fdd2 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/InterfaceTemplate.xtend +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/InterfaceTemplate.xtend @@ -10,25 +10,33 @@ 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.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.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.MoreObjects import java.util.List +import java.util.Map.Entry +import java.util.Set +import org.gaul.modernizer_maven_annotations.SuppressModernizer import org.opendaylight.mdsal.binding.model.api.AnnotationType import org.opendaylight.mdsal.binding.model.api.Constant import org.opendaylight.mdsal.binding.model.api.Enumeration import org.opendaylight.mdsal.binding.model.api.GeneratedType -import org.opendaylight.mdsal.binding.model.api.JavaTypeName import org.opendaylight.mdsal.binding.model.api.MethodSignature import org.opendaylight.mdsal.binding.model.api.Type +import org.opendaylight.mdsal.binding.model.util.Types import org.opendaylight.mdsal.binding.model.util.TypeConstants -import org.opendaylight.yangtools.yang.binding.CodeHelpers /** * Template for generating JAVA interfaces. */ + @SuppressModernizer class InterfaceTemplate extends BaseTemplate { - static val JavaTypeName NONNULL = JavaTypeName.create("org.eclipse.jdt.annotation", "NonNull") - static val JavaTypeName NULLABLE = JavaTypeName.create("org.eclipse.jdt.annotation", "Nullable") - /** * List of constant instances which are generated as JAVA public static final attributes. */ @@ -49,18 +57,16 @@ class InterfaceTemplate extends BaseTemplate { */ val List enclosedGeneratedTypes + var Entry> typeAnalysis + /** * Creates the instance of this class which is used for generating the interface file source * code from genType. * - * @throws IllegalArgumentException if genType equals null + * @throws NullPointerException if genType is null */ new(GeneratedType genType) { super(genType) - if (genType === null) { - throw new IllegalArgumentException("Generated type reference cannot be NULL!") - } - consts = genType.constantDefinitions methods = genType.methodDefinitions enums = genType.enumerations @@ -73,7 +79,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» @@ -91,18 +97,10 @@ class InterfaceTemplate extends BaseTemplate { ''' - def private generateAnnotations(List annotations) ''' «IF annotations !== null && !annotations.empty» «FOR annotation : annotations» - @«annotation.importedName» - «IF annotation.parameters !== null && !annotation.parameters.empty» - ( - «FOR param : annotation.parameters SEPARATOR ","» - «param.name»=«param.value» - «ENDFOR» - ) - «ENDIF» + «annotation.generateAnnotation» «ENDFOR» «ENDIF» ''' @@ -172,10 +170,12 @@ class InterfaceTemplate extends BaseTemplate { def private generateMethods() ''' «IF !methods.empty» «FOR m : methods SEPARATOR "\n"» - «IF m.name.isGetterMethodName» + «IF m.isDefault» + «generateDefaultMethod(m)» + «ELSEIF m.isStatic» + «generateStaticMethod(m)» + «ELSEIF m.parameters.empty && m.name.isGetterMethodName» «generateAccessorMethod(m)» - «ELSEIF m.name.isNonnullMethodName» - «generateNonnullMethod(m)» «ELSE» «generateMethod(m)» «ENDIF» @@ -183,32 +183,163 @@ class InterfaceTemplate extends BaseTemplate { «ENDIF» ''' + def private generateDefaultMethod(MethodSignature method) { + if (method.name.isNonnullMethodName) { + generateNonnullMethod(method) + } else { + switch method.name { + case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface + default : + if (VOID == method.returnType.identifier) { + generateNoopVoidInterfaceMethod(method) + } + } + } + } + + def private generateStaticMethod(MethodSignature method) { + switch method.name { + case BINDING_EQUALS_NAME : generateBindingEquals + case BINDING_HASHCODE_NAME : generateBindingHashCode + case BINDING_TO_STRING_NAME : generateBindingToString + } + } + def private generateMethod(MethodSignature method) ''' «method.comment.asJavadoc» «method.annotations.generateAnnotations» «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 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.annotations.generateAnnotations» + «method.returnType.nullableType» «method.name»(); + ''' + } + + def private generateDefaultImplementedInterface() ''' + @«OVERRIDE.importedName» + default «CLASS.importedName»<«type.fullyQualifiedName»> «DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»() { + return «type.fullyQualifiedName».class; + } + ''' + + @VisibleForTesting + def generateBindingHashCode() ''' + «val augmentable = analyzeType» + «IF augmentable || !typeAnalysis.value.empty» + /** + * Default implementation of {@link «Object.importedName»#hashCode()} contract for this interface. + * Implementations of this interface are encouraged to defer to this method to get consistent hashing + * results across all implementations. + * + * @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 + */ + 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 + obj.augmentations().hashCode(); + «ENDIF» + return result; + } + «ENDIF» + ''' + + def private generateBindingEquals() ''' + «val augmentable = analyzeType» + «IF augmentable || !typeAnalysis.value.isEmpty» + /** + * Default implementation of {@link «Object.importedName»#equals(«Object.importedName»)} contract for this interface. + * Implementations of this interface are encouraged to defer to this method to get consistent equality + * results across all implementations. + * + * @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 + */ + static boolean «BINDING_EQUALS_NAME»(final «type.fullyQualifiedNonNull» thisObj, final «Types.objectType().importedName» obj) { + if (thisObj == obj) { + return true; + } + final «type.fullyQualifiedName» other = «CODEHELPERS.importedName».checkCast(«type.fullyQualifiedName».class, obj); + if (other == null) { + return false; + } + «FOR property : ByTypeMemberComparator.sort(typeAnalysis.value)» + if (!«property.importedUtilClass».equals(thisObj.«property.getterName»(), other.«property.getterName»())) { + return false; + } + «ENDFOR» + return «IF augmentable»thisObj.augmentations().equals(other.augmentations())«ELSE»true«ENDIF»; + } + «ENDIF» + ''' + + def generateBindingToString() ''' + «val augmentable = analyzeType» + /** + * Default implementation of {@link «Object.importedName»#toString()} contract for this interface. + * Implementations of this interface are encouraged to defer to this method to get consistent string + * representations across all implementations. + * + * @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 + */ + static «STRING.importedName» «BINDING_TO_STRING_NAME»(final «type.fullyQualifiedNonNull» obj) { + final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«type.name»"); + «FOR property : typeAnalysis.value» + «CODEHELPERS.importedName».appendValue(helper, "«property.name»", obj.«property.getterName»()); + «ENDFOR» + «IF augmentable» + «CODEHELPERS.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", obj.augmentations().values()); + «ENDIF» + return helper.toString(); + } ''' 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.importedName(NONNULL.importedName)» «name»() { - return «CodeHelpers.importedName».nonnull(«getGetterMethodForNonnull(name)»()); + default «ret.importedNonNull» «name»() { + return «CODEHELPERS.importedName».nonnull(«name.getGetterMethodForNonnull»()); } ''' def private String nullableType(Type type) { if (type.isObject) { - return type.importedName(NULLABLE.importedName) + return type.importedNullable } return type.importedName } @@ -217,4 +348,11 @@ class InterfaceTemplate extends BaseTemplate { // The return type has a package, so it's not a primitive type return !type.getPackageName().isEmpty() } + + private def boolean analyzeType() { + if (typeAnalysis === null) { + typeAnalysis = analyzeTypeHierarchy(type) + } + typeAnalysis.key !== null + } }