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.BOOLEAN
14 import static org.opendaylight.mdsal.binding.model.util.Types.STRING
15 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
16 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_EQUALS_NAME
17 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_HASHCODE_NAME
18 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_TO_STRING_NAME
19 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BOOLEAN_GETTER_PREFIX
20 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
21 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.GETTER_PREFIX
23 import com.google.common.annotations.VisibleForTesting;
24 import com.google.common.base.MoreObjects
26 import java.util.Map.Entry
28 import org.gaul.modernizer_maven_annotations.SuppressModernizer
29 import org.opendaylight.mdsal.binding.model.api.AnnotationType
30 import org.opendaylight.mdsal.binding.model.api.Constant
31 import org.opendaylight.mdsal.binding.model.api.Enumeration
32 import org.opendaylight.mdsal.binding.model.api.GeneratedType
33 import org.opendaylight.mdsal.binding.model.api.MethodSignature
34 import org.opendaylight.mdsal.binding.model.api.Type
35 import org.opendaylight.mdsal.binding.model.util.Types
36 import org.opendaylight.mdsal.binding.model.util.TypeConstants
39 * Template for generating JAVA interfaces.
42 class InterfaceTemplate extends BaseTemplate {
44 * List of constant instances which are generated as JAVA public static final attributes.
46 val List<Constant> consts
49 * List of method signatures which are generated as method declarations.
51 val List<MethodSignature> methods
54 * List of enumeration which are generated as JAVA enum type.
56 val List<Enumeration> enums
59 * List of generated types which are enclosed inside <code>genType</code>
61 val List<GeneratedType> enclosedGeneratedTypes
63 var Entry<Type, Set<BuilderGeneratedProperty>> typeAnalysis
66 * Creates the instance of this class which is used for generating the interface file source
67 * code from <code>genType</code>.
69 * @throws NullPointerException if <code>genType</code> is <code>null</code>
71 new(GeneratedType genType) {
73 consts = genType.constantDefinitions
74 methods = genType.methodDefinitions
75 enums = genType.enumerations
76 enclosedGeneratedTypes = genType.enclosedTypes
80 * Template method which generate the whole body of the interface.
82 * @return string with code for interface body in JAVA format
85 «type.formatDataForJavaDoc.wrapToDocumentation»
86 «type.annotations.generateAnnotations»
88 public interface «type.name»
92 «generateInnerClasses»
104 def private generateAnnotations(List<AnnotationType> annotations) '''
105 «IF annotations !== null && !annotations.empty»
106 «FOR annotation : annotations»
107 «annotation.generateAnnotation»
112 def private generateAccessorAnnotations(MethodSignature method) '''
113 «val annotations = method.annotations»
114 «IF annotations !== null && !annotations.empty»
115 «FOR annotation : annotations»
116 «IF method.returnType != BOOLEAN || !(annotation.identifier == OVERRIDE)»
117 «annotation.generateAnnotation»
124 * Template method which generates the interface name declaration.
126 * @return string with the code for the interface declaration in JAVA format
128 def private superInterfaces()
130 «IF (!type.implements.empty)»
132 «FOR type : type.implements SEPARATOR ","»
139 * Template method which generates inner classes inside this interface.
141 * @return string with the source code for inner classes in JAVA format
143 def private generateInnerClasses() '''
144 «IF !enclosedGeneratedTypes.empty»
145 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
146 «generateInnerClass(innerClass)»
152 * Template method which generates JAVA enum type.
154 * @return string with inner enum source code in JAVA format
156 def private generateEnums() '''
158 «FOR e : enums SEPARATOR "\n"»
159 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
160 «enumTemplate.generateAsInnerClass»
166 * Template method wich generates JAVA constants.
168 * @return string with constants in JAVA format
170 def private generateConstants() '''
173 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
181 * Template method which generates the declaration of the methods.
183 * @return string with the declaration of methods source code in JAVA format
185 def private generateMethods() '''
187 «FOR m : methods SEPARATOR "\n"»
189 «generateDefaultMethod(m)»
191 «generateStaticMethod(m)»
192 «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
193 «generateAccessorMethod(m)»
201 def private generateDefaultMethod(MethodSignature method) {
202 if (method.name.isNonnullMethodName) {
203 generateNonnullMethod(method)
206 case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
208 if (VOID == method.returnType.identifier) {
209 generateNoopVoidInterfaceMethod(method)
210 } else if (method.name.startsWith(BOOLEAN_GETTER_PREFIX)) {
211 generateIsAccessorMethod(method)
217 def private generateStaticMethod(MethodSignature method) {
219 case BINDING_EQUALS_NAME : generateBindingEquals
220 case BINDING_HASHCODE_NAME : generateBindingHashCode
221 case BINDING_TO_STRING_NAME : generateBindingToString
225 def private generateMethod(MethodSignature method) '''
226 «method.comment.asJavadoc»
227 «method.annotations.generateAnnotations»
228 «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
231 def private generateNoopVoidInterfaceMethod(MethodSignature method) '''
232 «method.comment.asJavadoc»
233 «method.annotations.generateAnnotations»
234 default «VOID.importedName» «method.name»(«method.parameters.generateParameters») {
239 def private generateIsAccessorMethod(MethodSignature method) '''
240 @«DEPRECATED.importedName»(forRemoval = true)
241 default «method.returnType.importedName» «method.name»(«method.parameters.generateParameters») {
242 return «GETTER_PREFIX»«method.name.substring(BOOLEAN_GETTER_PREFIX.length)»();
246 def private static accessorJavadoc(MethodSignature method, String orString) {
247 val reference = method.comment?.referenceDescription
248 val propReturn = method.propertyNameFromGetter + ", or " + orString + " if it is not present."
250 return wrapToDocumentation('''
253 «reference.formatReference»
254 @return {@code «method.returnType.fullyQualifiedName»} «propReturn»
258 def private generateAccessorMethod(MethodSignature method) {
260 «accessorJavadoc(method, "{@code null}")»
261 «method.generateAccessorAnnotations»
262 «method.returnType.nullableType» «method.name»();
266 def private generateDefaultImplementedInterface() '''
267 @«OVERRIDE.importedName»
268 default «CLASS.importedName»<«type.fullyQualifiedName»> «DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»() {
269 return «type.fullyQualifiedName».class;
274 def generateBindingHashCode() '''
275 «val augmentable = analyzeType»
276 «IF augmentable || !typeAnalysis.value.empty»
278 * Default implementation of {@link «Object.importedName»#hashCode()} contract for this interface.
279 * Implementations of this interface are encouraged to defer to this method to get consistent hashing
280 * results across all implementations.
282 * @param obj Object for which to generate hashCode() result.
283 * @return Hash code value of data modeled by this interface.
284 * @throws «NPE.importedName» if {@code obj} is null
286 static int «BINDING_HASHCODE_NAME»(final «type.fullyQualifiedNonNull» obj) {
287 final int prime = 31;
289 «FOR property : typeAnalysis.value»
290 result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
293 result = prime * result + obj.augmentations().hashCode();
300 def private generateBindingEquals() '''
301 «val augmentable = analyzeType»
302 «IF augmentable || !typeAnalysis.value.isEmpty»
304 * Default implementation of {@link «Object.importedName»#equals(«Object.importedName»)} contract for this interface.
305 * Implementations of this interface are encouraged to defer to this method to get consistent equality
306 * results across all implementations.
308 * @param thisObj Object acting as the receiver of equals invocation
309 * @param obj Object acting as argument to equals invocation
310 * @return True if thisObj and obj are considered equal
311 * @throws «NPE.importedName» if {@code thisObj} is null
313 static boolean «BINDING_EQUALS_NAME»(final «type.fullyQualifiedNonNull» thisObj, final «Types.objectType().importedName» obj) {
314 if (thisObj == obj) {
317 final «type.fullyQualifiedName» other = «CODEHELPERS.importedName».checkCast(«type.fullyQualifiedName».class, obj);
321 «FOR property : ByTypeMemberComparator.sort(typeAnalysis.value)»
322 if (!«property.importedUtilClass».equals(thisObj.«property.getterName»(), other.«property.getterName»())) {
326 return «IF augmentable»thisObj.augmentations().equals(other.augmentations())«ELSE»true«ENDIF»;
331 def generateBindingToString() '''
332 «val augmentable = analyzeType»
334 * Default implementation of {@link «Object.importedName»#toString()} contract for this interface.
335 * Implementations of this interface are encouraged to defer to this method to get consistent string
336 * representations across all implementations.
338 * @param obj Object for which to generate toString() result.
339 * @return {@link «STRING.importedName»} value of data modeled by this interface.
340 * @throws «NPE.importedName» if {@code obj} is null
342 static «STRING.importedName» «BINDING_TO_STRING_NAME»(final «type.fullyQualifiedNonNull» obj) {
343 final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«type.name»");
344 «FOR property : typeAnalysis.value»
345 «CODEHELPERS.importedName».appendValue(helper, "«property.name»", obj.«property.getterName»());
348 «CODEHELPERS.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", obj.augmentations().values());
350 return helper.toString();
354 def private generateNonnullMethod(MethodSignature method) '''
355 «val ret = method.returnType»
356 «val name = method.name»
357 «accessorJavadoc(method, "an empty list")»
358 «method.annotations.generateAnnotations»
359 default «ret.importedNonNull» «name»() {
360 return «CODEHELPERS.importedName».nonnull(«name.getGetterMethodForNonnull»());
364 def private String nullableType(Type type) {
365 if (type.isObject && (Types.isMapType(type) || Types.isListType(type))) {
366 return type.importedNullable
368 return type.importedName
371 def private static boolean isObject(Type type) {
372 // The return type has a package, so it's not a primitive type
373 return !type.getPackageName().isEmpty()
376 private def boolean analyzeType() {
377 if (typeAnalysis === null) {
378 typeAnalysis = analyzeTypeHierarchy(type)
380 typeAnalysis.key !== null