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.generator.BindingGeneratorUtil.encodeAngleBrackets
11 import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
12 import static org.opendaylight.mdsal.binding.model.ri.Types.objectType;
14 import com.google.common.base.CharMatcher
15 import com.google.common.base.Splitter
16 import java.util.Collection
18 import java.util.Locale
19 import java.util.Map.Entry
20 import java.util.StringTokenizer
21 import java.util.regex.Pattern
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.mdsal.binding.model.api.AnnotationType
24 import org.opendaylight.mdsal.binding.model.api.ConcreteType
25 import org.opendaylight.mdsal.binding.model.api.Constant
26 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
27 import org.opendaylight.mdsal.binding.model.api.GeneratedType
28 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
29 import org.opendaylight.mdsal.binding.model.api.MethodSignature
30 import org.opendaylight.mdsal.binding.model.api.Restrictions
31 import org.opendaylight.mdsal.binding.model.api.Type
32 import org.opendaylight.mdsal.binding.model.api.TypeMemberComment
33 import org.opendaylight.mdsal.binding.model.ri.TypeConstants
34 import org.opendaylight.yangtools.yang.binding.BaseIdentity
35 import org.opendaylight.yangtools.yang.binding.contract.Naming
37 abstract class BaseTemplate extends JavaFileTemplate {
38 static final char NEW_LINE = '\n'
39 static final char SPACE = ' '
40 static val WS_MATCHER = CharMatcher.anyOf("\n\t")
41 static val SPACES_PATTERN = Pattern.compile(" +")
42 static val NL_SPLITTER = Splitter.on(NEW_LINE)
44 new(GeneratedType type) {
48 new(AbstractJavaGeneratedType javaType, GeneratedType type) {
52 final def generate() {
55 package «type.packageName»;
62 protected abstract def CharSequence body();
65 final protected def fieldName(GeneratedProperty property) {
70 * Template method which generates the getter method for <code>field</code>
73 * generated property with data about field which is generated as the getter method
74 * @return string with the getter method source code in JAVA format
76 protected def getterMethod(GeneratedProperty field) '''
77 «val methodName = field.getterMethodName»
78 public «field.returnType.importedName» «methodName»() {
79 «val fieldName = field.fieldName»
80 «IF field.returnType.name.endsWith("[]")»
81 return «fieldName» == null ? null : «fieldName».clone();
88 final protected def getterMethodName(GeneratedProperty field) {
89 return field.name.getterMethodName
92 final protected def getterMethodName(String propName) {
93 return '''«Naming.GETTER_PREFIX»«propName.toFirstUpper»'''
97 * Template method which generates the setter method for <code>field</code>
100 * generated property with data about field which is generated as the setter method
101 * @return string with the setter method source code in JAVA format
103 final protected def setterMethod(GeneratedProperty field) '''
104 «val returnType = field.returnType.importedName»
105 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
106 this.«field.fieldName» = value;
112 * Template method which generates method parameters with their types from <code>parameters</code>.
115 * group of generated property instances which are transformed to the method parameters
116 * @return string with the list of the method parameters with their types in JAVA format
118 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
119 returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
122 * Template method which generates method parameters with their types from <code>parameters</code>, annotating them
123 * with {@link NonNull}.
125 * @param parameters group of generated property instances which are transformed to the method parameters
126 * @return string with the list of the method parameters with their types in JAVA format
128 def final protected asNonNullArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»
129 «FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedNonNull» «parameter
130 .fieldName»«ENDFOR»«ENDIF»'''
133 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
136 * group of generated property instances which are transformed to the sequence of parameter names
137 * @return string with the list of the parameter names of the <code>parameters</code>
139 def final protected asArguments(Collection<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
140 fieldName»«ENDFOR»«ENDIF»'''
143 * Template method which generates JAVA comments.
145 * @param comment string with the comment for whole JAVA class
146 * @return string with comment in JAVA format
148 def final protected asJavadoc(TypeMemberComment comment) {
149 if (comment === null) {
152 return wrapToDocumentation('''
153 «comment.contractDescription»
155 «comment.referenceDescription.formatReference»
157 «comment.typeSignature»
161 def static String wrapToDocumentation(String text) {
165 val StringBuilder sb = new StringBuilder().append("/**\n")
166 for (String t : NL_SPLITTER.split(text)) {
169 sb.append(SPACE).append(t)
178 def protected String formatDataForJavaDoc(GeneratedType type) {
179 val sb = new StringBuilder()
180 val comment = type.comment
181 if (comment !== null) {
182 sb.append(comment.javadoc)
185 appendSnippet(sb, type)
194 def protected String formatDataForJavaDoc(GeneratedType type, String additionalComment) {
195 val comment = type.comment
196 if (comment === null) {
202 val sb = new StringBuilder().append(comment.javadoc)
203 appendSnippet(sb, type)
208 .append(additionalComment)
215 def static formatReference(String reference) '''
216 «IF reference !== null»
219 «reference.encodeAngleBrackets.formatToParagraph»
226 def asLink(String text) {
227 val StringBuilder sb = new StringBuilder()
229 var char lastChar = SPACE
230 var boolean badEnding = false
232 if (text.endsWith('.') || text.endsWith(':') || text.endsWith(',')) {
233 tempText = text.substring(0, text.length - 1)
234 lastChar = text.charAt(text.length - 1)
237 sb.append("<a href = \"").append(tempText).append("\">").append(tempText).append("</a>")
245 protected static def formatToParagraph(String inputText) {
246 val StringBuilder sb = new StringBuilder();
247 var StringBuilder lineBuilder = new StringBuilder();
248 var boolean isFirstElementOnNewLineEmptyChar = false;
250 var formattedText = WS_MATCHER.replaceFrom(inputText.encodeJavadocSymbols, SPACE)
251 formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
253 val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true)
254 while (tokenizer.hasMoreTokens) {
255 val nextElement = tokenizer.nextToken
257 if (lineBuilder.length != 0 && lineBuilder.length + nextElement.length > 80) {
258 if (lineBuilder.charAt(lineBuilder.length - 1) == SPACE) {
259 lineBuilder.setLength(lineBuilder.length - 1)
261 if (lineBuilder.length != 0 && lineBuilder.charAt(0) == SPACE) {
262 lineBuilder.deleteCharAt(0)
265 sb.append(lineBuilder).append(NEW_LINE)
266 lineBuilder.setLength(0)
268 if (" ".equals(nextElement)) {
269 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
272 if (isFirstElementOnNewLineEmptyChar) {
273 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
275 lineBuilder.append(nextElement)
279 return sb.append(lineBuilder).append(NEW_LINE).toString
283 * Template method which generates method parameters with their types from <code>parameters</code>.
286 * list of parameter instances which are transformed to the method parameters
287 * @return string with the list of the method parameters with their types in JAVA format
289 def protected generateParameters(List<MethodSignature.Parameter> parameters) '''«
290 IF !parameters.empty»«
291 FOR parameter : parameters SEPARATOR ", "»«
292 parameter.type.importedName» «parameter.name»«
297 def protected emitConstant(Constant c) '''
298 «IF Naming.QNAME_STATIC_FIELD_NAME.equals(c.name)»
299 «val entry = c.value as Entry<JavaTypeName, String>»
301 * YANG identifier of the statement represented by this class.
303 public static final «c.type.importedNonNull» «c.name» = «entry.key.importedName».«Naming.MODULE_INFO_QNAMEOF_METHOD_NAME»("«entry.value»");
304 «ELSEIF Naming.NAME_STATIC_FIELD_NAME.equals(c.name)»
305 «val entry = c.value as Entry<JavaTypeName, String>»
307 * Yang Data template name of the statement represented by this class.
309 public static final «c.type.importedNonNull» «c.name» = «entry.key.importedName».«Naming.MODULE_INFO_YANGDATANAMEOF_METHOD_NAME»("«entry.value»");
310 «ELSEIF Naming.VALUE_STATIC_FIELD_NAME.equals(c.name) && BaseIdentity.equals(c.value)»
311 «val typeName = c.type.importedName»
312 «val override = OVERRIDE.importedName»
314 * Singleton value representing the {@link «typeName»} identity.
316 public static final «c.type.importedNonNull» «c.name» = new «typeName»() {
318 public «CLASS.importedName»<«typeName»> «Naming.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»() {
319 return «typeName».class;
323 public int hashCode() {
324 return «typeName».class.hashCode();
328 public boolean equals(final «objectType.importedName» obj) {
329 return obj == this || obj instanceof «typeName» other
330 && «typeName».class.equals(other.«Naming.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»());
334 public «STRING.importedName» toString() {
335 return «MOREOBJECTS.importedName».toStringHelper("«c.type.name»").add("qname", QNAME).toString();
339 public static final «c.type.importedName» «c.name» = «c.value»;
343 def protected generateCheckers(GeneratedProperty field, Restrictions restrictions, Type actualType) '''
344 «IF restrictions.rangeConstraint.present»
345 «AbstractRangeGenerator.forType(actualType).generateRangeChecker(field.name.toFirstUpper,
346 restrictions.rangeConstraint.orElseThrow, this)»
348 «IF restrictions.lengthConstraint.present»
349 «LengthGenerator.generateLengthChecker(field.fieldName, actualType, restrictions.lengthConstraint.orElseThrow, this)»
353 def protected checkArgument(GeneratedProperty property, Restrictions restrictions, Type actualType, String value) '''
354 «IF restrictions.getRangeConstraint.isPresent»
355 «IF actualType instanceof ConcreteType»
356 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value)»
358 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value + ".getValue()")»
361 «val fieldName = property.fieldName»
362 «IF restrictions.getLengthConstraint.isPresent»
363 «IF actualType instanceof ConcreteType»
364 «LengthGenerator.generateLengthCheckerCall(fieldName, value)»
366 «LengthGenerator.generateLengthCheckerCall(fieldName, value + ".getValue()")»
370 «val fieldUpperCase = fieldName.toUpperCase(Locale.ENGLISH)»
371 «FOR currentConstant : type.getConstantDefinitions»
372 «IF currentConstant.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)
373 && fieldUpperCase.equals(currentConstant.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length))»
374 «CODEHELPERS.importedName».checkPattern(value, «Constants.MEMBER_PATTERN_LIST»«fieldName», «Constants.MEMBER_REGEX_LIST»«fieldName»);
379 def protected final generateAnnotation(AnnotationType annotation) '''
380 @«annotation.importedName»
381 «IF annotation.parameters !== null && !annotation.parameters.empty»
383 «FOR param : annotation.parameters SEPARATOR ","»
384 «param.name»=«param.value»