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.BindingGeneratorUtil
37 import org.opendaylight.mdsal.binding.model.util.TypeConstants
38 import org.opendaylight.mdsal.binding.model.util.Types
39 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
40 import org.opendaylight.yangtools.yang.common.QName
41 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
42 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
43 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
44 import org.opendaylight.yangtools.yang.model.api.RpcDefinition
45 import org.opendaylight.yangtools.yang.model.api.SchemaNode
46 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping
47 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement
48 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement
49 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement
50 import org.opendaylight.yangtools.yang.model.export.DeclaredStatementFormatter
53 abstract class BaseTemplate extends JavaFileTemplate {
54 static final char NEW_LINE = '\n'
55 static final char SPACE = ' '
56 static val AMP_MATCHER = CharMatcher.is('&')
57 static val WS_MATCHER = CharMatcher.anyOf("\n\t")
58 static val SPACES_PATTERN = Pattern.compile(" +")
59 static val NL_SPLITTER = Splitter.on(NEW_LINE)
60 static val TAIL_COMMENT_PATTERN = Pattern.compile("*/", Pattern.LITERAL);
61 static val YANG_FORMATTER = DeclaredStatementFormatter.builder()
62 .addIgnoredStatement(YangStmtMapping.CONTACT)
63 .addIgnoredStatement(YangStmtMapping.DESCRIPTION)
64 .addIgnoredStatement(YangStmtMapping.REFERENCE)
65 .addIgnoredStatement(YangStmtMapping.ORGANIZATION)
68 new(GeneratedType type) {
72 new(AbstractJavaGeneratedType javaType, GeneratedType type) {
76 final def generate() {
79 package «type.packageName»;
86 protected abstract def CharSequence body();
89 final protected def fieldName(GeneratedProperty property) {
93 final protected def propertyNameFromGetter(MethodSignature getter) {
95 if (getter.name.startsWith(BindingMapping.BOOLEAN_GETTER_PREFIX)) {
96 prefix = BindingMapping.BOOLEAN_GETTER_PREFIX
97 } else if (getter.name.startsWith(BindingMapping.GETTER_PREFIX)) {
98 prefix = BindingMapping.GETTER_PREFIX
99 } else if (getter.name.startsWith(BindingMapping.NONNULL_PREFIX)) {
100 prefix = BindingMapping.NONNULL_PREFIX
102 throw new IllegalArgumentException(getter + " is not a getter")
104 return getter.name.substring(prefix.length).toFirstLower;
108 * Template method which generates the getter method for <code>field</code>
111 * generated property with data about field which is generated as the getter method
112 * @return string with the getter method source code in JAVA format
114 protected def getterMethod(GeneratedProperty field) {
116 public «field.returnType.importedName» «field.getterMethodName»() {
117 «val fieldName = field.fieldName»
118 «IF field.returnType.name.endsWith("[]")»
119 return «fieldName» == null ? null : «fieldName».clone();
127 final protected def getterMethodName(GeneratedProperty field) {
128 val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
129 return '''«prefix»«field.name.toFirstUpper»'''
133 * Template method which generates the setter method for <code>field</code>
136 * generated property with data about field which is generated as the setter method
137 * @return string with the setter method source code in JAVA format
139 final protected def setterMethod(GeneratedProperty field) '''
140 «val returnType = field.returnType.importedName»
141 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
142 this.«field.fieldName» = value;
148 * Template method which generates method parameters with their types from <code>parameters</code>.
151 * group of generated property instances which are transformed to the method parameters
152 * @return string with the list of the method parameters with their types in JAVA format
154 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
155 returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
158 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
161 * group of generated property instances which are transformed to the sequence of parameter names
162 * @return string with the list of the parameter names of the <code>parameters</code>
164 def final protected asArguments(Collection<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
165 fieldName»«ENDFOR»«ENDIF»'''
168 * Template method which generates JAVA comments.
170 * @param comment string with the comment for whole JAVA class
171 * @return string with comment in JAVA format
173 def protected CharSequence asJavadoc(String comment) {
174 if (comment === null) {
178 «wrapToDocumentation(formatToParagraph(comment.trim))»
182 def static String wrapToDocumentation(String text) {
186 val StringBuilder sb = new StringBuilder().append("/**\n")
187 for (String t : NL_SPLITTER.split(text)) {
190 sb.append(SPACE).append(t)
199 def protected String formatDataForJavaDoc(GeneratedType type) {
200 val sb = new StringBuilder()
201 val comment = type.comment
202 if (comment !== null) {
203 sb.append(comment.javadoc)
206 appendSnippet(sb, type)
215 def static encodeJavadocSymbols(String description) {
216 if (description.nullOrEmpty) {
220 return TAIL_COMMENT_PATTERN.matcher(AMP_MATCHER.replaceFrom(description, "&")).replaceAll("*/")
223 def protected String formatDataForJavaDoc(GeneratedType type, String additionalComment) {
224 val comment = type.comment
225 if (comment === null) {
231 val sb = new StringBuilder().append(comment.javadoc)
232 appendSnippet(sb, type)
237 .append(additionalComment)
244 def private static void appendSnippet(StringBuilder sb, GeneratedType type) {
245 val optDef = type.yangSourceDefinition
246 if (optDef.present) {
250 if (def instanceof Single) {
253 .append("This class represents the following YANG schema fragment defined in module <b>")
254 .append(def.module.argument).append("</b>\n")
256 appendYangSnippet(sb, def.module, (node as EffectiveStatement<?, ?>).declared)
259 if (node instanceof SchemaNode) {
260 sb.append("The schema path to identify an instance is\n")
262 .append(formatSchemaPath(def.module.argument, node.path.pathFromRoot))
265 if (hasBuilderClass(node)) {
266 val builderName = type.name + "Builder";
268 sb.append("\n<p>To create instances of this class use {@link ").append(builderName)
270 .append("@see ").append(builderName).append('\n')
271 if (node instanceof ListSchemaNode) {
272 val keyDef = node.keyDefinition
273 if (keyDef !== null && !keyDef.empty) {
274 sb.append("@see ").append(type.name).append("Key")
280 } else if (def instanceof Multiple) {
282 for (SchemaNode node : def.nodes) {
283 appendYangSnippet(sb, def.module, (node as EffectiveStatement<?, ?>).declared)
285 sb.append("</pre>\n")
290 def private static void appendYangSnippet(StringBuilder sb, ModuleEffectiveStatement module,
291 DeclaredStatement<?> stmt) {
292 for (String str : YANG_FORMATTER.toYangTextSnippet(module, stmt)) {
293 sb.append(BindingGeneratorUtil.replaceAllIllegalChars(encodeAngleBrackets(encodeJavadocSymbols(str))))
297 def private static boolean hasBuilderClass(SchemaNode schemaNode) {
298 return schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode
299 || schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition;
302 def private static String formatSchemaPath(String moduleName, Iterable<QName> schemaPath) {
303 val sb = new StringBuilder().append(moduleName);
305 var currentElement = Iterables.getFirst(schemaPath, null);
306 for (QName pathElement : schemaPath) {
308 if (!currentElement.namespace.equals(pathElement.namespace)) {
309 currentElement = pathElement
310 sb.append(pathElement)
312 sb.append(pathElement.getLocalName())
315 return sb.toString();
318 def protected static String formatDataForJavaDoc(TypeMember type, String additionalComment) {
319 val StringBuilder typeDescriptionBuilder = new StringBuilder();
320 if (!type.comment.nullOrEmpty) {
321 typeDescriptionBuilder.append(formatToParagraph(type.comment))
322 typeDescriptionBuilder.append(NEW_LINE)
323 typeDescriptionBuilder.append(NEW_LINE)
324 typeDescriptionBuilder.append(NEW_LINE)
326 typeDescriptionBuilder.append(additionalComment)
327 var typeDescription = wrapToDocumentation(typeDescriptionBuilder.toString)
333 def asCode(String text) {
334 return "<code>" + text + "</code>"
337 def asLink(String text) {
338 val StringBuilder sb = new StringBuilder()
340 var char lastChar = SPACE
341 var boolean badEnding = false
343 if (text.endsWith('.') || text.endsWith(':') || text.endsWith(',')) {
344 tempText = text.substring(0, text.length - 1)
345 lastChar = text.charAt(text.length - 1)
348 sb.append("<a href = \"")
360 protected static def formatToParagraph(String text) {
361 if(text === null || text.isEmpty)
364 var formattedText = text
365 val StringBuilder sb = new StringBuilder();
366 var StringBuilder lineBuilder = new StringBuilder();
367 var boolean isFirstElementOnNewLineEmptyChar = false;
369 formattedText = encodeJavadocSymbols(formattedText)
370 formattedText = WS_MATCHER.replaceFrom(formattedText, SPACE)
371 formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
373 val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true);
375 while (tokenizer.hasMoreTokens) {
376 val nextElement = tokenizer.nextToken
378 if (lineBuilder.length != 0 && lineBuilder.length + nextElement.length > 80) {
379 if (lineBuilder.charAt(lineBuilder.length - 1) == SPACE) {
380 lineBuilder.setLength(lineBuilder.length - 1)
382 if (lineBuilder.length != 0 && lineBuilder.charAt(0) == SPACE) {
383 lineBuilder.deleteCharAt(0)
386 sb.append(lineBuilder).append(NEW_LINE)
387 lineBuilder.setLength(0)
389 if (nextElement == " ") {
390 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
394 if (isFirstElementOnNewLineEmptyChar) {
395 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
397 lineBuilder.append(nextElement)
401 return sb.append(lineBuilder).append(NEW_LINE).toString
404 def protected generateToString(Collection<? extends GeneratedProperty> properties) '''
405 «IF !properties.empty»
406 @«OVERRIDE.importedName»
407 public «STRING.importedName» toString() {
408 final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper(«type.importedName».class);
409 «FOR property : properties»
410 «CODEHELPERS.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
412 return helper.toString();
418 * Template method which generates method parameters with their types from <code>parameters</code>.
421 * list of parameter instances which are transformed to the method parameters
422 * @return string with the list of the method parameters with their types in JAVA format
424 def protected generateParameters(List<MethodSignature.Parameter> parameters) '''«
425 IF !parameters.empty»«
426 FOR parameter : parameters SEPARATOR ", "»«
427 parameter.type.importedName» «parameter.name»«
432 def protected emitConstant(Constant c) '''
433 «IF BindingMapping.QNAME_STATIC_FIELD_NAME.equals(c.name)»
434 «val entry = c.value as Entry<JavaTypeName, String>»
435 public static final «c.type.importedNonNull» «c.name» = «entry.key.importedName».«BindingMapping.MODULE_INFO_QNAMEOF_METHOD_NAME»("«entry.value»");
437 public static final «c.type.importedName» «c.name» = «c.value»;
441 def protected generateCheckers(GeneratedProperty field, Restrictions restrictions, Type actualType) '''
442 «IF restrictions.rangeConstraint.present»
443 «AbstractRangeGenerator.forType(actualType).generateRangeChecker(field.name.toFirstUpper,
444 restrictions.rangeConstraint.get, this)»
446 «IF restrictions.lengthConstraint.present»
447 «LengthGenerator.generateLengthChecker(field.fieldName, actualType, restrictions.lengthConstraint.get, this)»
451 def protected checkArgument(GeneratedProperty property, Restrictions restrictions, Type actualType, String value) '''
452 «IF restrictions.getRangeConstraint.isPresent»
453 «IF actualType instanceof ConcreteType»
454 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value)»
456 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value + ".getValue()")»
459 «val fieldName = property.fieldName»
460 «IF restrictions.getLengthConstraint.isPresent»
461 «IF actualType instanceof ConcreteType»
462 «LengthGenerator.generateLengthCheckerCall(fieldName, value)»
464 «LengthGenerator.generateLengthCheckerCall(fieldName, value + ".getValue()")»
468 «val fieldUpperCase = fieldName.toUpperCase(Locale.ENGLISH)»
469 «FOR currentConstant : type.getConstantDefinitions»
470 «IF currentConstant.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)
471 && fieldUpperCase.equals(currentConstant.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length))»
472 «CODEHELPERS.importedName».checkPattern(value, «Constants.MEMBER_PATTERN_LIST»«fieldName», «Constants.MEMBER_REGEX_LIST»«fieldName»);
477 def protected hashCodeResult(Collection<? extends GeneratedProperty> properties) '''
478 final int prime = 31;
480 «FOR property : properties»
481 result = prime * result + «property.importedUtilClass».hashCode(«property.fieldName»);
485 def protected final generateAnnotation(AnnotationType annotation) '''
486 @«annotation.importedName»
487 «IF annotation.parameters !== null && !annotation.parameters.empty»
489 «FOR param : annotation.parameters SEPARATOR ","»
490 «param.name»=«param.value»