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 org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
11 import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
13 import com.google.common.base.CharMatcher
14 import com.google.common.base.MoreObjects
15 import com.google.common.base.Splitter
16 import com.google.common.collect.Iterables
17 import java.util.Collection
19 import java.util.Locale
20 import java.util.Map.Entry
21 import java.util.StringTokenizer
22 import java.util.regex.Pattern
23 import org.gaul.modernizer_maven_annotations.SuppressModernizer
24 import org.opendaylight.mdsal.binding.model.api.AnnotationType
25 import org.opendaylight.mdsal.binding.model.api.ConcreteType
26 import org.opendaylight.mdsal.binding.model.api.Constant
27 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
28 import org.opendaylight.mdsal.binding.model.api.GeneratedType
29 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
30 import org.opendaylight.mdsal.binding.model.api.MethodSignature
31 import org.opendaylight.mdsal.binding.model.api.Restrictions
32 import org.opendaylight.mdsal.binding.model.api.Type
33 import org.opendaylight.mdsal.binding.model.api.TypeMember
34 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Single
35 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Multiple
36 import org.opendaylight.mdsal.binding.model.util.TypeConstants
37 import org.opendaylight.mdsal.binding.model.util.Types
38 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
39 import org.opendaylight.yangtools.yang.common.QName
40 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
41 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
42 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
43 import org.opendaylight.yangtools.yang.model.api.RpcDefinition
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode
45 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping
46 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement
47 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement
48 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement
49 import org.opendaylight.yangtools.yang.model.export.DeclaredStatementFormatter
52 abstract class BaseTemplate extends JavaFileTemplate {
53 static final char NEW_LINE = '\n'
54 static final char SPACE = ' '
55 static val AMP_MATCHER = CharMatcher.is('&')
56 static val WS_MATCHER = CharMatcher.anyOf("\n\t")
57 static val SPACES_PATTERN = Pattern.compile(" +")
58 static val NL_SPLITTER = Splitter.on(NEW_LINE)
59 static val TAIL_COMMENT_PATTERN = Pattern.compile("*/", Pattern.LITERAL);
60 static val YANG_FORMATTER = DeclaredStatementFormatter.builder()
61 .addIgnoredStatement(YangStmtMapping.CONTACT)
62 .addIgnoredStatement(YangStmtMapping.DESCRIPTION)
63 .addIgnoredStatement(YangStmtMapping.REFERENCE)
64 .addIgnoredStatement(YangStmtMapping.ORGANIZATION)
67 new(GeneratedType type) {
71 new(AbstractJavaGeneratedType javaType, GeneratedType type) {
75 final def generate() {
78 package «type.packageName»;
85 protected abstract def CharSequence body();
88 final protected def fieldName(GeneratedProperty property) {
92 final protected def propertyNameFromGetter(MethodSignature getter) {
94 if (getter.name.startsWith(BindingMapping.BOOLEAN_GETTER_PREFIX)) {
95 prefix = BindingMapping.BOOLEAN_GETTER_PREFIX
96 } else if (getter.name.startsWith(BindingMapping.GETTER_PREFIX)) {
97 prefix = BindingMapping.GETTER_PREFIX
98 } else if (getter.name.startsWith(BindingMapping.NONNULL_PREFIX)) {
99 prefix = BindingMapping.NONNULL_PREFIX
101 throw new IllegalArgumentException(getter + " is not a getter")
103 return getter.name.substring(prefix.length).toFirstLower;
107 * Template method which generates the getter method for <code>field</code>
110 * generated property with data about field which is generated as the getter method
111 * @return string with the getter method source code in JAVA format
113 protected def getterMethod(GeneratedProperty field) {
115 public «field.returnType.importedName» «field.getterMethodName»() {
116 «val fieldName = field.fieldName»
117 «IF field.returnType.name.endsWith("[]")»
118 return «fieldName» == null ? null : «fieldName».clone();
126 final protected def getterMethodName(GeneratedProperty field) {
127 val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
128 return '''«prefix»«field.name.toFirstUpper»'''
132 * Template method which generates the setter method for <code>field</code>
135 * generated property with data about field which is generated as the setter method
136 * @return string with the setter method source code in JAVA format
138 final protected def setterMethod(GeneratedProperty field) '''
139 «val returnType = field.returnType.importedName»
140 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
141 this.«field.fieldName» = value;
147 * Template method which generates method parameters with their types from <code>parameters</code>.
150 * group of generated property instances which are transformed to the method parameters
151 * @return string with the list of the method parameters with their types in JAVA format
153 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
154 returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
157 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
160 * group of generated property instances which are transformed to the sequence of parameter names
161 * @return string with the list of the parameter names of the <code>parameters</code>
163 def final protected asArguments(Collection<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
164 fieldName»«ENDFOR»«ENDIF»'''
167 * Template method which generates JAVA comments.
169 * @param comment string with the comment for whole JAVA class
170 * @return string with comment in JAVA format
172 def protected CharSequence asJavadoc(String comment) {
173 if (comment === null) {
177 «wrapToDocumentation(formatToParagraph(comment.trim))»
181 def static String wrapToDocumentation(String text) {
185 val StringBuilder sb = new StringBuilder().append("/**\n")
186 for (String t : NL_SPLITTER.split(text)) {
189 sb.append(SPACE).append(t)
198 def protected String formatDataForJavaDoc(GeneratedType type) {
199 val sb = new StringBuilder()
200 val comment = type.comment
201 if (comment !== null) {
202 sb.append(comment.javadoc)
205 appendSnippet(sb, type)
214 def static encodeJavadocSymbols(String description) {
215 if (description.nullOrEmpty) {
219 return TAIL_COMMENT_PATTERN.matcher(AMP_MATCHER.replaceFrom(description, "&")).replaceAll("*/")
222 def protected String formatDataForJavaDoc(GeneratedType type, String additionalComment) {
223 val comment = type.comment
224 if (comment === null) {
230 val sb = new StringBuilder().append(comment.javadoc)
231 appendSnippet(sb, type)
236 .append(additionalComment)
243 def private static void appendSnippet(StringBuilder sb, GeneratedType type) {
244 val optDef = type.yangSourceDefinition
245 if (optDef.present) {
249 if (def instanceof Single) {
252 .append("This class represents the following YANG schema fragment defined in module <b>")
253 .append(def.module.argument).append("</b>\n")
255 appendYangSnippet(sb, def.module, (node as EffectiveStatement<?, ?>).declared)
258 if (node instanceof SchemaNode) {
259 sb.append("The schema path to identify an instance is\n")
261 .append(formatSchemaPath(def.module.argument, node.path.pathFromRoot))
264 if (hasBuilderClass(node)) {
265 val builderName = type.name + "Builder";
267 sb.append("\n<p>To create instances of this class use {@link ").append(builderName)
269 .append("@see ").append(builderName).append('\n')
270 if (node instanceof ListSchemaNode) {
271 val keyDef = node.keyDefinition
272 if (keyDef !== null && !keyDef.empty) {
273 sb.append("@see ").append(type.name).append("Key")
279 } else if (def instanceof Multiple) {
281 for (SchemaNode node : def.nodes) {
282 appendYangSnippet(sb, def.module, (node as EffectiveStatement<?, ?>).declared)
284 sb.append("</pre>\n")
289 def private static void appendYangSnippet(StringBuilder sb, ModuleEffectiveStatement module,
290 DeclaredStatement<?> stmt) {
291 for (String str : YANG_FORMATTER.toYangTextSnippet(module, stmt)) {
292 sb.append(encodeAngleBrackets(encodeJavadocSymbols(str)))
296 def private static boolean hasBuilderClass(SchemaNode schemaNode) {
297 return schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode
298 || schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition;
301 def private static String formatSchemaPath(String moduleName, Iterable<QName> schemaPath) {
302 val sb = new StringBuilder().append(moduleName);
304 var currentElement = Iterables.getFirst(schemaPath, null);
305 for (QName pathElement : schemaPath) {
307 if (!currentElement.namespace.equals(pathElement.namespace)) {
308 currentElement = pathElement
309 sb.append(pathElement)
311 sb.append(pathElement.getLocalName())
314 return sb.toString();
317 def protected static String formatDataForJavaDoc(TypeMember type, String additionalComment) {
318 val StringBuilder typeDescriptionBuilder = new StringBuilder();
319 if (!type.comment.nullOrEmpty) {
320 typeDescriptionBuilder.append(formatToParagraph(type.comment))
321 typeDescriptionBuilder.append(NEW_LINE)
322 typeDescriptionBuilder.append(NEW_LINE)
323 typeDescriptionBuilder.append(NEW_LINE)
325 typeDescriptionBuilder.append(additionalComment)
326 var typeDescription = wrapToDocumentation(typeDescriptionBuilder.toString)
332 def asCode(String text) {
333 return "<code>" + text + "</code>"
336 def asLink(String text) {
337 val StringBuilder sb = new StringBuilder()
339 var char lastChar = SPACE
340 var boolean badEnding = false
342 if (text.endsWith('.') || text.endsWith(':') || text.endsWith(',')) {
343 tempText = text.substring(0, text.length - 1)
344 lastChar = text.charAt(text.length - 1)
347 sb.append("<a href = \"")
359 protected static def formatToParagraph(String text) {
360 if(text === null || text.isEmpty)
363 var formattedText = text
364 val StringBuilder sb = new StringBuilder();
365 var StringBuilder lineBuilder = new StringBuilder();
366 var boolean isFirstElementOnNewLineEmptyChar = false;
368 formattedText = encodeJavadocSymbols(formattedText)
369 formattedText = WS_MATCHER.replaceFrom(formattedText, SPACE)
370 formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
372 val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true);
374 while (tokenizer.hasMoreTokens) {
375 val nextElement = tokenizer.nextToken
377 if (lineBuilder.length != 0 && lineBuilder.length + nextElement.length > 80) {
378 if (lineBuilder.charAt(lineBuilder.length - 1) == SPACE) {
379 lineBuilder.setLength(lineBuilder.length - 1)
381 if (lineBuilder.length != 0 && lineBuilder.charAt(0) == SPACE) {
382 lineBuilder.deleteCharAt(0)
385 sb.append(lineBuilder).append(NEW_LINE)
386 lineBuilder.setLength(0)
388 if (nextElement == " ") {
389 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
393 if (isFirstElementOnNewLineEmptyChar) {
394 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
396 lineBuilder.append(nextElement)
400 return sb.append(lineBuilder).append(NEW_LINE).toString
403 def protected generateToString(Collection<GeneratedProperty> properties) '''
404 «IF !properties.empty»
405 @«OVERRIDE.importedName»
406 public «STRING.importedName» toString() {
407 final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper(«type.importedName».class);
408 «FOR property : properties»
409 «CODEHELPERS.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
411 return helper.toString();
417 * Template method which generates method parameters with their types from <code>parameters</code>.
420 * list of parameter instances which are transformed to the method parameters
421 * @return string with the list of the method parameters with their types in JAVA format
423 def protected generateParameters(List<MethodSignature.Parameter> parameters) '''«
424 IF !parameters.empty»«
425 FOR parameter : parameters SEPARATOR ", "»«
426 parameter.type.importedName» «parameter.name»«
431 def protected emitConstant(Constant c) '''
432 «IF BindingMapping.QNAME_STATIC_FIELD_NAME.equals(c.name)»
433 «val entry = c.value as Entry<JavaTypeName, String>»
434 public static final «c.type.importedNonNull» «c.name» = «entry.key.importedName».«BindingMapping.MODULE_INFO_QNAMEOF_METHOD_NAME»("«entry.value»");
436 public static final «c.type.importedName» «c.name» = «c.value»;
440 def protected generateCheckers(GeneratedProperty field, Restrictions restrictions, Type actualType) '''
441 «IF restrictions.rangeConstraint.present»
442 «AbstractRangeGenerator.forType(actualType).generateRangeChecker(field.name.toFirstUpper,
443 restrictions.rangeConstraint.get, this)»
445 «IF restrictions.lengthConstraint.present»
446 «LengthGenerator.generateLengthChecker(field.fieldName, actualType, restrictions.lengthConstraint.get, this)»
450 def protected checkArgument(GeneratedProperty property, Restrictions restrictions, Type actualType, String value) '''
451 «IF restrictions.getRangeConstraint.isPresent»
452 «IF actualType instanceof ConcreteType»
453 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value)»
455 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value + ".getValue()")»
458 «val fieldName = property.fieldName»
459 «IF restrictions.getLengthConstraint.isPresent»
460 «IF actualType instanceof ConcreteType»
461 «LengthGenerator.generateLengthCheckerCall(fieldName, value)»
463 «LengthGenerator.generateLengthCheckerCall(fieldName, value + ".getValue()")»
467 «val fieldUpperCase = fieldName.toUpperCase(Locale.ENGLISH)»
468 «FOR currentConstant : type.getConstantDefinitions»
469 «IF currentConstant.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)
470 && fieldUpperCase.equals(currentConstant.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length))»
471 «CODEHELPERS.importedName».checkPattern(value, «Constants.MEMBER_PATTERN_LIST»«fieldName», «Constants.MEMBER_REGEX_LIST»«fieldName»);
476 def protected hashCodeResult(Collection<GeneratedProperty> properties) '''
477 final int prime = 31;
479 «FOR property : properties»
480 result = prime * result + «property.importedUtilClass».hashCode(«property.fieldName»);
484 def protected final generateAnnotation(AnnotationType annotation) '''
485 @«annotation.importedName»
486 «IF annotation.parameters !== null && !annotation.parameters.empty»
488 «FOR param : annotation.parameters SEPARATOR ","»
489 «param.name»=«param.value»