2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.java.api.generator
10 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.getGetterMethodForNonnull
11 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isGetterMethodName
12 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isNonnullMethodName
13 import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
14 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
15 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_EQUALS_NAME
16 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_HASHCODE_NAME
17 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_TO_STRING_NAME
18 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
20 import com.google.common.base.MoreObjects
22 import java.util.Map.Entry
24 import org.gaul.modernizer_maven_annotations.SuppressModernizer
25 import org.opendaylight.mdsal.binding.model.api.AnnotationType
26 import org.opendaylight.mdsal.binding.model.api.Constant
27 import org.opendaylight.mdsal.binding.model.api.Enumeration
28 import org.opendaylight.mdsal.binding.model.api.GeneratedType
29 import org.opendaylight.mdsal.binding.model.api.MethodSignature
30 import org.opendaylight.mdsal.binding.model.api.Type
31 import org.opendaylight.mdsal.binding.model.util.Types
32 import org.opendaylight.mdsal.binding.model.util.TypeConstants
35 * Template for generating JAVA interfaces.
38 class InterfaceTemplate extends BaseTemplate {
40 * List of constant instances which are generated as JAVA public static final attributes.
42 val List<Constant> consts
45 * List of method signatures which are generated as method declarations.
47 val List<MethodSignature> methods
50 * List of enumeration which are generated as JAVA enum type.
52 val List<Enumeration> enums
55 * List of generated types which are enclosed inside <code>genType</code>
57 val List<GeneratedType> enclosedGeneratedTypes
59 var Entry<Type, Set<BuilderGeneratedProperty>> typeAnalysis
62 * Creates the instance of this class which is used for generating the interface file source
63 * code from <code>genType</code>.
65 * @throws NullPointerException if <code>genType</code> is <code>null</code>
67 new(GeneratedType genType) {
69 consts = genType.constantDefinitions
70 methods = genType.methodDefinitions
71 enums = genType.enumerations
72 enclosedGeneratedTypes = genType.enclosedTypes
76 * Template method which generate the whole body of the interface.
78 * @return string with code for interface body in JAVA format
81 «wrapToDocumentation(formatDataForJavaDoc(type))»
82 «type.annotations.generateAnnotations»
83 public interface «type.name»
87 «generateInnerClasses»
99 def private generateAnnotations(List<AnnotationType> annotations) '''
100 «IF annotations !== null && !annotations.empty»
101 «FOR annotation : annotations»
102 «annotation.generateAnnotation»
108 * Template method which generates the interface name declaration.
110 * @return string with the code for the interface declaration in JAVA format
112 def private superInterfaces()
114 «IF (!type.implements.empty)»
116 «FOR type : type.implements SEPARATOR ","»
123 * Template method which generates inner classes inside this interface.
125 * @return string with the source code for inner classes in JAVA format
127 def private generateInnerClasses() '''
128 «IF !enclosedGeneratedTypes.empty»
129 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
130 «generateInnerClass(innerClass)»
136 * Template method which generates JAVA enum type.
138 * @return string with inner enum source code in JAVA format
140 def private generateEnums() '''
142 «FOR e : enums SEPARATOR "\n"»
143 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
144 «enumTemplate.generateAsInnerClass»
150 * Template method wich generates JAVA constants.
152 * @return string with constants in JAVA format
154 def private generateConstants() '''
157 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
165 * Template method which generates the declaration of the methods.
167 * @return string with the declaration of methods source code in JAVA format
169 def private generateMethods() '''
171 «FOR m : methods SEPARATOR "\n"»
173 «generateDefaultMethod(m)»
175 «generateStaticMethod(m)»
176 «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
177 «generateAccessorMethod(m)»
185 def private generateDefaultMethod(MethodSignature method) {
186 if (method.name.isNonnullMethodName) {
187 generateNonnullMethod(method)
190 case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
195 def private generateStaticMethod(MethodSignature method) {
197 case BINDING_EQUALS_NAME : generateBindingEquals
198 case BINDING_HASHCODE_NAME : generateBindingHashCode
199 case BINDING_TO_STRING_NAME : generateBindingToString
203 def private generateMethod(MethodSignature method) '''
204 «method.comment.asJavadoc»
205 «method.annotations.generateAnnotations»
206 «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
209 def private generateAccessorMethod(MethodSignature method) '''
210 «val ret = method.returnType»
211 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or " + asCode("null") + " if not present")»
212 «method.annotations.generateAnnotations»
213 «nullableType(ret)» «method.name»();
216 def private generateDefaultImplementedInterface() '''
217 @«OVERRIDE.importedName»
218 default «CLASS.importedName»<«type.fullyQualifiedName»> «DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»() {
219 return «type.fullyQualifiedName».class;
223 def private generateBindingHashCode() '''
224 «val augmentable = analyzeType»
226 * Default implementation of {@link «Object.importedName»#hashCode()} contract for this interface.
227 * Implementations of this interface are encouraged to defer to this method to get consistent hashing
228 * results across all implementations.
231 * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
234 * @param obj Object for which to generate hashCode() result.
235 * @return Hash code value of data modeled by this interface.
236 * @throws «NPE.importedName» if {@code obj} is null
239 static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<?>> int «BINDING_HASHCODE_NAME»(final @«NONNULL.importedName» T$$ obj) {
241 static int «BINDING_HASHCODE_NAME»(final «type.fullyQualifiedName» obj) {
243 final int prime = 31;
245 «FOR property : typeAnalysis.value»
246 result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
249 result = prime * result + «CODEHELPERS.importedName».hashAugmentations(obj);
255 def private generateBindingEquals() '''
256 «val augmentable = analyzeType»
257 «IF augmentable || !typeAnalysis.value.isEmpty»
259 * Default implementation of {@link «Object.importedName»#equals(«Object.importedName»)} contract for this interface.
260 * Implementations of this interface are encouraged to defer to this method to get consistent equality
261 * results across all implementations.
264 * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
267 * @param thisObj Object acting as the receiver of equals invocation
268 * @param obj Object acting as argument to equals invocation
269 * @return True if thisObj and obj are considered equal
270 * @throws «NPE.importedName» if {@code thisObj} is null
273 static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<«type.fullyQualifiedName»>> boolean «BINDING_EQUALS_NAME»(final @«NONNULL.importedName» T$$ thisObj, final «Types.objectType().importedName» obj) {
275 static boolean «BINDING_EQUALS_NAME»(final «type.fullyQualifiedName» thisObj, final «Types.objectType().importedName» obj) {
277 if (thisObj == obj) {
280 final «type.fullyQualifiedName» other = «CODEHELPERS.importedName».checkCast(«type.fullyQualifiedName».class, obj);
284 «FOR property : ByTypeMemberComparator.sort(typeAnalysis.value)»
285 if (!«property.importedUtilClass».equals(thisObj.«property.getterName»(), other.«property.getterName»())) {
289 return «IF augmentable»«CODEHELPERS.importedName».equalsAugmentations(thisObj, other)«ELSE»true«ENDIF»;
294 def generateBindingToString() '''
295 «val augmentable = analyzeType»
297 * Default implementation of {@link «Object.importedName»#toString()} contract for this interface.
298 * Implementations of this interface are encouraged to defer to this method to get consistent string
299 * representations across all implementations.
302 * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
305 * @param obj Object for which to generate toString() result.
306 * @return {@link «STRING.importedName»} value of data modeled by this interface.
307 * @throws «NPE.importedName» if {@code obj} is null
310 static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<«type.fullyQualifiedName»>> «STRING.importedName» «BINDING_TO_STRING_NAME»(final @«NONNULL.importedName» T$$ obj) {
312 static «STRING.importedName» «BINDING_TO_STRING_NAME»(final «type.fullyQualifiedName» obj) {
314 final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«type.name»");
315 «FOR property : typeAnalysis.value»
316 «CODEHELPERS.importedName».appendValue(helper, "«property.name»", obj.«property.getterName»());
319 «CODEHELPERS.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", obj.augmentations().values());
321 return helper.toString();
325 def private generateNonnullMethod(MethodSignature method) '''
326 «val ret = method.returnType»
327 «val name = method.name»
328 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or an empty list if it is not present")»
329 «method.annotations.generateAnnotations»
330 default «ret.importedNonNull» «name»() {
331 return «CODEHELPERS.importedName».nonnull(«name.getGetterMethodForNonnull»());
335 def private String nullableType(Type type) {
337 return type.importedNullable
339 return type.importedName
342 def private static boolean isObject(Type type) {
343 // The return type has a package, so it's not a primitive type
344 return !type.getPackageName().isEmpty()
347 private def boolean analyzeType() {
348 if (typeAnalysis === null) {
349 typeAnalysis = analyzeTypeHierarchy(type)
351 typeAnalysis.key !== null