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
36 import org.opendaylight.yangtools.yang.common.YangDataName
38 abstract class BaseTemplate extends JavaFileTemplate {
39 static final char NEW_LINE = '\n'
40 static final char SPACE = ' '
41 static val WS_MATCHER = CharMatcher.anyOf("\n\t")
42 static val SPACES_PATTERN = Pattern.compile(" +")
43 static val NL_SPLITTER = Splitter.on(NEW_LINE)
45 new(GeneratedType type) {
49 new(AbstractJavaGeneratedType javaType, GeneratedType type) {
53 final def generate() {
56 package «type.packageName»;
63 protected abstract def CharSequence body();
66 final protected def fieldName(GeneratedProperty property) {
71 * Template method which generates the getter method for <code>field</code>
74 * generated property with data about field which is generated as the getter method
75 * @return string with the getter method source code in JAVA format
77 protected def getterMethod(GeneratedProperty field) '''
78 «val methodName = field.getterMethodName»
79 public «field.returnType.importedName» «methodName»() {
80 «val fieldName = field.fieldName»
81 «IF field.returnType.name.endsWith("[]")»
82 return «fieldName» == null ? null : «fieldName».clone();
89 final protected def getterMethodName(GeneratedProperty field) {
90 return field.name.getterMethodName
93 final protected def getterMethodName(String propName) {
94 return '''«Naming.GETTER_PREFIX»«propName.toFirstUpper»'''
98 * Template method which generates the setter method for <code>field</code>
101 * generated property with data about field which is generated as the setter method
102 * @return string with the setter method source code in JAVA format
104 final protected def setterMethod(GeneratedProperty field) '''
105 «val returnType = field.returnType.importedName»
106 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
107 this.«field.fieldName» = value;
113 * Template method which generates method parameters with their types from <code>parameters</code>.
116 * group of generated property instances which are transformed to the method parameters
117 * @return string with the list of the method parameters with their types in JAVA format
119 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
120 returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
123 * Template method which generates method parameters with their types from <code>parameters</code>, annotating them
124 * with {@link NonNull}.
126 * @param parameters group of generated property instances which are transformed to the method parameters
127 * @return string with the list of the method parameters with their types in JAVA format
129 def final protected asNonNullArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»
130 «FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedNonNull» «parameter
131 .fieldName»«ENDFOR»«ENDIF»'''
134 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
137 * group of generated property instances which are transformed to the sequence of parameter names
138 * @return string with the list of the parameter names of the <code>parameters</code>
140 def final protected asArguments(Collection<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
141 fieldName»«ENDFOR»«ENDIF»'''
144 * Template method which generates JAVA comments.
146 * @param comment string with the comment for whole JAVA class
147 * @return string with comment in JAVA format
149 def final protected asJavadoc(TypeMemberComment comment) {
150 if (comment === null) {
153 return wrapToDocumentation('''
154 «comment.contractDescription»
156 «comment.referenceDescription.formatReference»
158 «comment.typeSignature»
162 def static String wrapToDocumentation(String text) {
166 val StringBuilder sb = new StringBuilder().append("/**\n")
167 for (String t : NL_SPLITTER.split(text)) {
170 sb.append(SPACE).append(t)
179 def protected String formatDataForJavaDoc(GeneratedType type) {
180 val sb = new StringBuilder()
181 val comment = type.comment
182 if (comment !== null) {
183 sb.append(comment.javadoc)
186 appendSnippet(sb, type)
195 def protected String formatDataForJavaDoc(GeneratedType type, String additionalComment) {
196 val comment = type.comment
197 if (comment === null) {
203 val sb = new StringBuilder().append(comment.javadoc)
204 appendSnippet(sb, type)
209 .append(additionalComment)
216 def static formatReference(String reference) '''
217 «IF reference !== null»
220 «reference.encodeAngleBrackets.formatToParagraph»
227 def asLink(String text) {
228 val StringBuilder sb = new StringBuilder()
230 var char lastChar = SPACE
231 var boolean badEnding = false
233 if (text.endsWith('.') || text.endsWith(':') || text.endsWith(',')) {
234 tempText = text.substring(0, text.length - 1)
235 lastChar = text.charAt(text.length - 1)
238 sb.append("<a href = \"").append(tempText).append("\">").append(tempText).append("</a>")
246 protected static def formatToParagraph(String inputText) {
247 val StringBuilder sb = new StringBuilder();
248 var StringBuilder lineBuilder = new StringBuilder();
249 var boolean isFirstElementOnNewLineEmptyChar = false;
251 var formattedText = WS_MATCHER.replaceFrom(inputText.encodeJavadocSymbols, SPACE)
252 formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
254 val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true)
255 while (tokenizer.hasMoreTokens) {
256 val nextElement = tokenizer.nextToken
258 if (lineBuilder.length != 0 && lineBuilder.length + nextElement.length > 80) {
259 if (lineBuilder.charAt(lineBuilder.length - 1) == SPACE) {
260 lineBuilder.setLength(lineBuilder.length - 1)
262 if (lineBuilder.length != 0 && lineBuilder.charAt(0) == SPACE) {
263 lineBuilder.deleteCharAt(0)
266 sb.append(lineBuilder).append(NEW_LINE)
267 lineBuilder.setLength(0)
269 if (" ".equals(nextElement)) {
270 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
273 if (isFirstElementOnNewLineEmptyChar) {
274 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
276 lineBuilder.append(nextElement)
280 return sb.append(lineBuilder).append(NEW_LINE).toString
284 * Template method which generates method parameters with their types from <code>parameters</code>.
287 * list of parameter instances which are transformed to the method parameters
288 * @return string with the list of the method parameters with their types in JAVA format
290 def protected generateParameters(List<MethodSignature.Parameter> parameters) '''«
291 IF !parameters.empty»«
292 FOR parameter : parameters SEPARATOR ", "»«
293 parameter.type.importedName» «parameter.name»«
298 def protected emitConstant(Constant c) '''
299 «IF Naming.QNAME_STATIC_FIELD_NAME.equals(c.name)»
300 «val entry = c.value as Entry<JavaTypeName, String>»
302 * YANG identifier of the statement represented by this class.
304 public static final «c.type.importedNonNull» «c.name» = «entry.key.importedName».«Naming.MODULE_INFO_QNAMEOF_METHOD_NAME»("«entry.value»");
305 «ELSEIF Naming.NAME_STATIC_FIELD_NAME.equals(c.name)»
306 «val entry = c.value as Entry<JavaTypeName, YangDataName>»
308 * Yang Data template name of the statement represented by this class.
310 public static final «c.type.importedNonNull» «c.name» = «entry.key.importedName».«Naming.MODULE_INFO_YANGDATANAMEOF_METHOD_NAME»("«entry.value.name»");
311 «ELSEIF Naming.VALUE_STATIC_FIELD_NAME.equals(c.name) && BaseIdentity.equals(c.value)»
312 «val typeName = c.type.importedName»
313 «val override = OVERRIDE.importedName»
315 * Singleton value representing the {@link «typeName»} identity.
317 public static final «c.type.importedNonNull» «c.name» = new «typeName»() {
319 public «CLASS.importedName»<«typeName»> «Naming.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»() {
320 return «typeName».class;
324 public int hashCode() {
325 return «typeName».class.hashCode();
329 public boolean equals(final «objectType.importedName» obj) {
330 return obj == this || obj instanceof «typeName» other
331 && «typeName».class.equals(other.«Naming.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»());
335 public «STRING.importedName» toString() {
336 return «MOREOBJECTS.importedName».toStringHelper("«c.type.name»").add("qname", QNAME).toString();
340 public static final «c.type.importedName» «c.name» = «c.value»;
344 def protected generateCheckers(GeneratedProperty field, Restrictions restrictions, Type actualType) '''
345 «IF restrictions.rangeConstraint.present»
346 «AbstractRangeGenerator.forType(actualType).generateRangeChecker(field.name.toFirstUpper,
347 restrictions.rangeConstraint.orElseThrow, this)»
349 «IF restrictions.lengthConstraint.present»
350 «LengthGenerator.generateLengthChecker(field.fieldName, actualType, restrictions.lengthConstraint.orElseThrow, this)»
354 def protected checkArgument(GeneratedProperty property, Restrictions restrictions, Type actualType, String value) '''
355 «IF restrictions.getRangeConstraint.isPresent»
356 «IF actualType instanceof ConcreteType»
357 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value)»
359 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value + ".getValue()")»
362 «val fieldName = property.fieldName»
363 «IF restrictions.getLengthConstraint.isPresent»
364 «IF actualType instanceof ConcreteType»
365 «LengthGenerator.generateLengthCheckerCall(fieldName, value)»
367 «LengthGenerator.generateLengthCheckerCall(fieldName, value + ".getValue()")»
371 «val fieldUpperCase = fieldName.toUpperCase(Locale.ENGLISH)»
372 «FOR currentConstant : type.getConstantDefinitions»
373 «IF currentConstant.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)
374 && fieldUpperCase.equals(currentConstant.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length))»
375 «CODEHELPERS.importedName».checkPattern(value, «Constants.MEMBER_PATTERN_LIST»«fieldName», «Constants.MEMBER_REGEX_LIST»«fieldName»);
380 def protected final generateAnnotation(AnnotationType annotation) '''
381 @«annotation.importedName»
382 «IF annotation.parameters !== null && !annotation.parameters.empty»
384 «FOR param : annotation.parameters SEPARATOR ","»
385 «param.name»=«param.value»