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.annotations.VisibleForTesting;
21 import com.google.common.base.MoreObjects
23 import java.util.Map.Entry
25 import org.gaul.modernizer_maven_annotations.SuppressModernizer
26 import org.opendaylight.mdsal.binding.model.api.AnnotationType
27 import org.opendaylight.mdsal.binding.model.api.Constant
28 import org.opendaylight.mdsal.binding.model.api.Enumeration
29 import org.opendaylight.mdsal.binding.model.api.GeneratedType
30 import org.opendaylight.mdsal.binding.model.api.MethodSignature
31 import org.opendaylight.mdsal.binding.model.api.Type
32 import org.opendaylight.mdsal.binding.model.util.Types
33 import org.opendaylight.mdsal.binding.model.util.TypeConstants
36 * Template for generating JAVA interfaces.
39 class InterfaceTemplate extends BaseTemplate {
41 * List of constant instances which are generated as JAVA public static final attributes.
43 val List<Constant> consts
46 * List of method signatures which are generated as method declarations.
48 val List<MethodSignature> methods
51 * List of enumeration which are generated as JAVA enum type.
53 val List<Enumeration> enums
56 * List of generated types which are enclosed inside <code>genType</code>
58 val List<GeneratedType> enclosedGeneratedTypes
60 var Entry<Type, Set<BuilderGeneratedProperty>> typeAnalysis
63 * Creates the instance of this class which is used for generating the interface file source
64 * code from <code>genType</code>.
66 * @throws NullPointerException if <code>genType</code> is <code>null</code>
68 new(GeneratedType genType) {
70 consts = genType.constantDefinitions
71 methods = genType.methodDefinitions
72 enums = genType.enumerations
73 enclosedGeneratedTypes = genType.enclosedTypes
77 * Template method which generate the whole body of the interface.
79 * @return string with code for interface body in JAVA format
82 «wrapToDocumentation(formatDataForJavaDoc(type))»
83 «type.annotations.generateAnnotations»
84 public interface «type.name»
88 «generateInnerClasses»
100 def private generateAnnotations(List<AnnotationType> annotations) '''
101 «IF annotations !== null && !annotations.empty»
102 «FOR annotation : annotations»
103 «annotation.generateAnnotation»
109 * Template method which generates the interface name declaration.
111 * @return string with the code for the interface declaration in JAVA format
113 def private superInterfaces()
115 «IF (!type.implements.empty)»
117 «FOR type : type.implements SEPARATOR ","»
124 * Template method which generates inner classes inside this interface.
126 * @return string with the source code for inner classes in JAVA format
128 def private generateInnerClasses() '''
129 «IF !enclosedGeneratedTypes.empty»
130 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
131 «generateInnerClass(innerClass)»
137 * Template method which generates JAVA enum type.
139 * @return string with inner enum source code in JAVA format
141 def private generateEnums() '''
143 «FOR e : enums SEPARATOR "\n"»
144 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
145 «enumTemplate.generateAsInnerClass»
151 * Template method wich generates JAVA constants.
153 * @return string with constants in JAVA format
155 def private generateConstants() '''
158 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
166 * Template method which generates the declaration of the methods.
168 * @return string with the declaration of methods source code in JAVA format
170 def private generateMethods() '''
172 «FOR m : methods SEPARATOR "\n"»
174 «generateDefaultMethod(m)»
176 «generateStaticMethod(m)»
177 «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
178 «generateAccessorMethod(m)»
186 def private generateDefaultMethod(MethodSignature method) {
187 if (method.name.isNonnullMethodName) {
188 generateNonnullMethod(method)
191 case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
196 def private generateStaticMethod(MethodSignature method) {
198 case BINDING_EQUALS_NAME : generateBindingEquals
199 case BINDING_HASHCODE_NAME : generateBindingHashCode
200 case BINDING_TO_STRING_NAME : generateBindingToString
204 def private generateMethod(MethodSignature method) '''
205 «method.comment.asJavadoc»
206 «method.annotations.generateAnnotations»
207 «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
210 def private generateAccessorMethod(MethodSignature method) '''
211 «val ret = method.returnType»
212 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or " + asCode("null") + " if not present")»
213 «method.annotations.generateAnnotations»
214 «nullableType(ret)» «method.name»();
217 def private generateDefaultImplementedInterface() '''
218 @«OVERRIDE.importedName»
219 default «CLASS.importedName»<«type.fullyQualifiedName»> «DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»() {
220 return «type.fullyQualifiedName».class;
225 def generateBindingHashCode() '''
226 «val augmentable = analyzeType»
227 «IF augmentable || !typeAnalysis.value.empty»
229 * Default implementation of {@link «Object.importedName»#hashCode()} contract for this interface.
230 * Implementations of this interface are encouraged to defer to this method to get consistent hashing
231 * results across all implementations.
234 * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
237 * @param obj Object for which to generate hashCode() result.
238 * @return Hash code value of data modeled by this interface.
239 * @throws «NPE.importedName» if {@code obj} is null
242 static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<?>> int «BINDING_HASHCODE_NAME»(final @«NONNULL.importedName» T$$ obj) {
244 static int «BINDING_HASHCODE_NAME»(final «type.fullyQualifiedName» obj) {
246 final int prime = 31;
248 «FOR property : typeAnalysis.value»
249 result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
252 result = prime * result + «CODEHELPERS.importedName».hashAugmentations(obj);
259 def private generateBindingEquals() '''
260 «val augmentable = analyzeType»
261 «IF augmentable || !typeAnalysis.value.isEmpty»
263 * Default implementation of {@link «Object.importedName»#equals(«Object.importedName»)} contract for this interface.
264 * Implementations of this interface are encouraged to defer to this method to get consistent equality
265 * results across all implementations.
268 * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
271 * @param thisObj Object acting as the receiver of equals invocation
272 * @param obj Object acting as argument to equals invocation
273 * @return True if thisObj and obj are considered equal
274 * @throws «NPE.importedName» if {@code thisObj} is null
277 static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<«type.fullyQualifiedName»>> boolean «BINDING_EQUALS_NAME»(final @«NONNULL.importedName» T$$ thisObj, final «Types.objectType().importedName» obj) {
279 static boolean «BINDING_EQUALS_NAME»(final «type.fullyQualifiedName» thisObj, final «Types.objectType().importedName» obj) {
281 if (thisObj == obj) {
284 final «type.fullyQualifiedName» other = «CODEHELPERS.importedName».checkCast(«type.fullyQualifiedName».class, obj);
288 «FOR property : ByTypeMemberComparator.sort(typeAnalysis.value)»
289 if (!«property.importedUtilClass».equals(thisObj.«property.getterName»(), other.«property.getterName»())) {
293 return «IF augmentable»«CODEHELPERS.importedName».equalsAugmentations(thisObj, other)«ELSE»true«ENDIF»;
298 def generateBindingToString() '''
299 «val augmentable = analyzeType»
301 * Default implementation of {@link «Object.importedName»#toString()} contract for this interface.
302 * Implementations of this interface are encouraged to defer to this method to get consistent string
303 * representations across all implementations.
306 * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
309 * @param obj Object for which to generate toString() result.
310 * @return {@link «STRING.importedName»} value of data modeled by this interface.
311 * @throws «NPE.importedName» if {@code obj} is null
314 static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<«type.fullyQualifiedName»>> «STRING.importedName» «BINDING_TO_STRING_NAME»(final @«NONNULL.importedName» T$$ obj) {
316 static «STRING.importedName» «BINDING_TO_STRING_NAME»(final «type.fullyQualifiedName» obj) {
318 final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«type.name»");
319 «FOR property : typeAnalysis.value»
320 «CODEHELPERS.importedName».appendValue(helper, "«property.name»", obj.«property.getterName»());
323 «CODEHELPERS.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", obj.augmentations().values());
325 return helper.toString();
329 def private generateNonnullMethod(MethodSignature method) '''
330 «val ret = method.returnType»
331 «val name = method.name»
332 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or an empty list if it is not present")»
333 «method.annotations.generateAnnotations»
334 default «ret.importedNonNull» «name»() {
335 return «CODEHELPERS.importedName».nonnull(«name.getGetterMethodForNonnull»());
339 def private String nullableType(Type type) {
341 return type.importedNullable
343 return type.importedName
346 def private static boolean isObject(Type type) {
347 // The return type has a package, so it's not a primitive type
348 return !type.getPackageName().isEmpty()
351 private def boolean analyzeType() {
352 if (typeAnalysis === null) {
353 typeAnalysis = analyzeTypeHierarchy(type)
355 typeAnalysis.key !== null