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.yangtools.sal.java.api.generator
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
\rimport org.opendaylight.yangtools.sal.binding.model.api.AnnotationType
19 * Template for generating JAVA interfaces.
21 class InterfaceTemplate extends BaseTemplate {
24 * List of constant instances which are generated as JAVA public static final attributes.
26 val List<Constant> consts
29 * List of method signatures which are generated as method declarations.
31 val List<MethodSignature> methods
34 * List of enumeration which are generated as JAVA enum type.
36 val List<Enumeration> enums
39 * List of generated types which are enclosed inside <code>genType</code>
41 val List<GeneratedType> enclosedGeneratedTypes
44 * Creates the instance of this class which is used for generating the interface file source
45 * code from <code>genType</code>.
47 * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
49 new(GeneratedType genType) {
51 if (genType == null) {
52 throw new IllegalArgumentException("Generated type reference cannot be NULL!")
55 consts = genType.constantDefinitions
56 methods = genType.methodDefinitions
57 enums = genType.enumerations
58 enclosedGeneratedTypes = genType.enclosedTypes
62 * Template method which generate the whole body of the interface.
64 * @return string with code for interface body in JAVA format
67 «wrapToDocumentation(formatDataForJavaDoc(type))»
68 public interface «type.name»
72 «generateInnerClasses»
85 def private generateAnnotations(List<AnnotationType> annotations) '''
86 «IF annotations != null && !annotations.empty»
87 «FOR annotation : annotations»
88 @«annotation.importedName»
89 «IF annotation.parameters != null && !annotation.parameters.empty»
91 «FOR param : annotation.parameters SEPARATOR ","»
92 «param.name»=«param.value»
101 * Template method which generates the interface name declaration.
103 * @return string with the code for the interface declaration in JAVA format
105 def private superInterfaces()
107 «IF (!type.implements.empty)»
109 «FOR type : type.implements SEPARATOR ","»
116 * Template method which generates inner classes inside this interface.
118 * @return string with the source code for inner classes in JAVA format
120 def private generateInnerClasses() '''
121 «IF !enclosedGeneratedTypes.empty»
122 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
123 «IF (innerClass instanceof GeneratedTransferObject)»
124 «IF innerClass.unionType»
125 «val unionTemplate = new UnionTemplate(innerClass)»
126 «unionTemplate.generateAsInnerClass»
127 «this.importMap.putAll(unionTemplate.importMap)»
129 «val classTemplate = new ClassTemplate(innerClass)»
130 «classTemplate.generateAsInnerClass»
131 «this.importMap.putAll(classTemplate.importMap)»
140 * Template method which generates JAVA enum type.
142 * @return string with inner enum source code in JAVA format
144 def private generateEnums() '''
146 «FOR e : enums SEPARATOR "\n"»
147 «val enumTemplate = new EnumTemplate(e)»
148 «enumTemplate.generateAsInnerClass»
154 * Template method wich generates JAVA constants.
156 * @return string with constants in JAVA format
158 def private generateConstants() '''
161 «IF c.name != TypeConstants.PATTERN_CONSTANT_NAME»
162 public static final «c.type.importedName» «c.name» = «c.value»;
169 * Template method which generates the declaration of the methods.
171 * @return string with the declaration of methods source code in JAVA format
173 def private generateMethods() '''
175 «FOR m : methods SEPARATOR "\n"»
176 «m.comment.asJavadoc»
177 «m.annotations.generateAnnotations»
178 «m.returnType.importedName» «m.name»(«m.parameters.generateParameters»);