Bug 5151 Java binding missing @return
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / InterfaceTemplate.xtend
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.sal.java.api.generator
9
10 import java.util.List
11 import org.opendaylight.yangtools.binding.generator.util.TypeConstants
12 import org.opendaylight.yangtools.sal.binding.model.api.Constant
13 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
14 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
15 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
16 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature
17 import org.opendaylight.yangtools.sal.binding.model.api.AnnotationType
18
19 /**
20  * Template for generating JAVA interfaces.
21  */
22 class InterfaceTemplate extends BaseTemplate {
23
24     /**
25      * List of constant instances which are generated as JAVA public static final attributes.
26      */
27     val List<Constant> consts
28
29     /**
30      * List of method signatures which are generated as method declarations.
31      */
32     val List<MethodSignature> methods
33
34     /**
35      * List of enumeration which are generated as JAVA enum type.
36      */
37     val List<Enumeration> enums
38
39     /**
40      * List of generated types which are enclosed inside <code>genType</code>
41      */
42     val List<GeneratedType> enclosedGeneratedTypes
43
44     /**
45      * Creates the instance of this class which is used for generating the interface file source
46      * code from <code>genType</code>.
47      *
48      * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
49      */
50     new(GeneratedType genType) {
51         super(genType)
52         if (genType == null) {
53             throw new IllegalArgumentException("Generated type reference cannot be NULL!")
54         }
55
56         consts = genType.constantDefinitions
57         methods = genType.methodDefinitions
58         enums = genType.enumerations
59         enclosedGeneratedTypes = genType.enclosedTypes
60     }
61
62     /**
63      * Template method which generate the whole body of the interface.
64      *
65      * @return string with code for interface body in JAVA format
66      */
67     override body() '''
68         «wrapToDocumentation(formatDataForJavaDoc(type))»
69         «type.annotations.generateAnnotations»
70         public interface «type.name»
71             «superInterfaces»
72         {
73
74             «generateInnerClasses»
75
76             «generateEnums»
77
78             «generateConstants»
79
80             «generateMethods»
81
82         }
83
84     '''
85
86
87     def private generateAnnotations(List<AnnotationType> annotations) '''
88         «IF annotations != null && !annotations.empty»
89             «FOR annotation : annotations»
90                 @«annotation.importedName»
91                 «IF annotation.parameters != null && !annotation.parameters.empty»
92                 (
93                 «FOR param : annotation.parameters SEPARATOR ","»
94                     «param.name»=«param.value»
95                 «ENDFOR»
96                 )
97                 «ENDIF»
98             «ENDFOR»
99         «ENDIF»
100     '''
101
102     /**
103      * Template method which generates the interface name declaration.
104      *
105      * @return string with the code for the interface declaration in JAVA format
106      */
107     def private superInterfaces()
108     '''
109     «IF (!type.implements.empty)»
110          extends
111          «FOR type : type.implements SEPARATOR ","»
112              «type.importedName»
113          «ENDFOR»
114      « ENDIF»
115      '''
116
117     /**
118      * Template method which generates inner classes inside this interface.
119      *
120      * @return string with the source code for inner classes in JAVA format
121      */
122     def private generateInnerClasses() '''
123         «IF !enclosedGeneratedTypes.empty»
124             «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
125                 «IF (innerClass instanceof GeneratedTransferObject)»
126                     «IF innerClass.unionType»
127                         «val unionTemplate = new UnionTemplate(innerClass)»
128                         «unionTemplate.generateAsInnerClass»
129                         «this.importMap.putAll(unionTemplate.importMap)»
130                     «ELSE»
131                         «val classTemplate = new ClassTemplate(innerClass)»
132                         «classTemplate.generateAsInnerClass»
133                         «this.importMap.putAll(classTemplate.importMap)»
134                     «ENDIF»
135
136                 «ENDIF»
137             «ENDFOR»
138         «ENDIF»
139     '''
140
141     /**
142      * Template method which generates JAVA enum type.
143      *
144      * @return string with inner enum source code in JAVA format
145      */
146     def private generateEnums() '''
147         «IF !enums.empty»
148             «FOR e : enums SEPARATOR "\n"»
149                 «val enumTemplate = new EnumTemplate(e)»
150                 «enumTemplate.generateAsInnerClass»
151             «ENDFOR»
152         «ENDIF»
153     '''
154
155     /**
156      * Template method wich generates JAVA constants.
157      *
158      * @return string with constants in JAVA format
159      */
160     def private generateConstants() '''
161         «IF !consts.empty»
162             «FOR c : consts»
163                 «IF c.name != TypeConstants.PATTERN_CONSTANT_NAME»
164                     «emitConstant(c)»
165                 «ENDIF»
166             «ENDFOR»
167         «ENDIF»
168     '''
169
170     /**
171      * Template method which generates the declaration of the methods.
172      *
173      * @return string with the declaration of methods source code in JAVA format
174      */
175     def private generateMethods() '''
176         «IF !methods.empty»
177             «FOR m : methods SEPARATOR "\n"»
178                 «IF !m.isAccessor»
179                     «m.comment.asJavadoc»
180                 «ELSE»
181                     «formatDataForJavaDoc(m, "@return " + asCode(m.returnType.fullyQualifiedName) + " "
182                     + asCode(propertyNameFromGetter(m)) + ", or " + asCode("null") + " if not present")»
183                 «ENDIF»
184                 «m.annotations.generateAnnotations»
185                 «m.returnType.importedName» «m.name»(«m.parameters.generateParameters»);
186             «ENDFOR»
187         «ENDIF»
188     '''
189
190 }
191