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
12 import com.google.common.base.CharMatcher
13 import com.google.common.base.Splitter
14 import java.util.Collection
16 import java.util.Locale
17 import java.util.Map.Entry
18 import java.util.StringTokenizer
19 import java.util.regex.Pattern
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.gaul.modernizer_maven_annotations.SuppressModernizer
22 import org.opendaylight.mdsal.binding.model.api.AnnotationType
23 import org.opendaylight.mdsal.binding.model.api.ConcreteType
24 import org.opendaylight.mdsal.binding.model.api.Constant
25 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
26 import org.opendaylight.mdsal.binding.model.api.GeneratedType
27 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
28 import org.opendaylight.mdsal.binding.model.api.MethodSignature
29 import org.opendaylight.mdsal.binding.model.api.Restrictions
30 import org.opendaylight.mdsal.binding.model.api.Type
31 import org.opendaylight.mdsal.binding.model.api.TypeMemberComment
32 import org.opendaylight.mdsal.binding.model.ri.TypeConstants
33 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
36 abstract class BaseTemplate extends JavaFileTemplate {
37 static final char NEW_LINE = '\n'
38 static final char SPACE = ' '
39 static val WS_MATCHER = CharMatcher.anyOf("\n\t")
40 static val SPACES_PATTERN = Pattern.compile(" +")
41 static val NL_SPLITTER = Splitter.on(NEW_LINE)
43 new(GeneratedType type) {
47 new(AbstractJavaGeneratedType javaType, GeneratedType type) {
51 final def generate() {
54 package «type.packageName»;
61 protected abstract def CharSequence body();
64 final protected def fieldName(GeneratedProperty property) {
69 * Template method which generates the getter method for <code>field</code>
72 * generated property with data about field which is generated as the getter method
73 * @return string with the getter method source code in JAVA format
75 protected def getterMethod(GeneratedProperty field) '''
76 «val methodName = field.getterMethodName»
77 public «field.returnType.importedName» «methodName»() {
78 «val fieldName = field.fieldName»
79 «IF field.returnType.name.endsWith("[]")»
80 return «fieldName» == null ? null : «fieldName».clone();
87 final protected def getterMethodName(GeneratedProperty field) {
88 return '''«BindingMapping.GETTER_PREFIX»«field.name.toFirstUpper»'''
92 * Template method which generates the setter method for <code>field</code>
95 * generated property with data about field which is generated as the setter method
96 * @return string with the setter method source code in JAVA format
98 final protected def setterMethod(GeneratedProperty field) '''
99 «val returnType = field.returnType.importedName»
100 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
101 this.«field.fieldName» = value;
107 * Template method which generates method parameters with their types from <code>parameters</code>.
110 * group of generated property instances which are transformed to the method parameters
111 * @return string with the list of the method parameters with their types in JAVA format
113 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
114 returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
117 * Template method which generates method parameters with their types from <code>parameters</code>, annotating them
118 * with {@link NonNull}.
120 * @param parameters group of generated property instances which are transformed to the method parameters
121 * @return string with the list of the method parameters with their types in JAVA format
123 def final protected asNonNullArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»
124 «FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedNonNull» «parameter
125 .fieldName»«ENDFOR»«ENDIF»'''
128 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
131 * group of generated property instances which are transformed to the sequence of parameter names
132 * @return string with the list of the parameter names of the <code>parameters</code>
134 def final protected asArguments(Collection<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
135 fieldName»«ENDFOR»«ENDIF»'''
138 * Template method which generates JAVA comments.
140 * @param comment string with the comment for whole JAVA class
141 * @return string with comment in JAVA format
143 def final protected asJavadoc(TypeMemberComment comment) {
144 if (comment === null) {
147 return wrapToDocumentation('''
148 «comment.contractDescription»
150 «comment.referenceDescription.formatReference»
152 «comment.typeSignature»
156 def static String wrapToDocumentation(String text) {
160 val StringBuilder sb = new StringBuilder().append("/**\n")
161 for (String t : NL_SPLITTER.split(text)) {
164 sb.append(SPACE).append(t)
173 def protected String formatDataForJavaDoc(GeneratedType type) {
174 val sb = new StringBuilder()
175 val comment = type.comment
176 if (comment !== null) {
177 sb.append(comment.javadoc)
180 appendSnippet(sb, type)
189 def protected String formatDataForJavaDoc(GeneratedType type, String additionalComment) {
190 val comment = type.comment
191 if (comment === null) {
197 val sb = new StringBuilder().append(comment.javadoc)
198 appendSnippet(sb, type)
203 .append(additionalComment)
210 def static formatReference(String reference) '''
211 «IF reference !== null»
214 «reference.encodeAngleBrackets.formatToParagraph»
221 def asLink(String text) {
222 val StringBuilder sb = new StringBuilder()
224 var char lastChar = SPACE
225 var boolean badEnding = false
227 if (text.endsWith('.') || text.endsWith(':') || text.endsWith(',')) {
228 tempText = text.substring(0, text.length - 1)
229 lastChar = text.charAt(text.length - 1)
232 sb.append("<a href = \"")
244 protected static def formatToParagraph(String inputText) {
245 val StringBuilder sb = new StringBuilder();
246 var StringBuilder lineBuilder = new StringBuilder();
247 var boolean isFirstElementOnNewLineEmptyChar = false;
249 var formattedText = WS_MATCHER.replaceFrom(inputText.encodeJavadocSymbols, SPACE)
250 formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
252 val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true)
253 while (tokenizer.hasMoreTokens) {
254 val nextElement = tokenizer.nextToken
256 if (lineBuilder.length != 0 && lineBuilder.length + nextElement.length > 80) {
257 if (lineBuilder.charAt(lineBuilder.length - 1) == SPACE) {
258 lineBuilder.setLength(lineBuilder.length - 1)
260 if (lineBuilder.length != 0 && lineBuilder.charAt(0) == SPACE) {
261 lineBuilder.deleteCharAt(0)
264 sb.append(lineBuilder).append(NEW_LINE)
265 lineBuilder.setLength(0)
267 if (nextElement == " ") {
268 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
271 if (isFirstElementOnNewLineEmptyChar) {
272 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
274 lineBuilder.append(nextElement)
278 return sb.append(lineBuilder).append(NEW_LINE).toString
282 * Template method which generates method parameters with their types from <code>parameters</code>.
285 * list of parameter instances which are transformed to the method parameters
286 * @return string with the list of the method parameters with their types in JAVA format
288 def protected generateParameters(List<MethodSignature.Parameter> parameters) '''«
289 IF !parameters.empty»«
290 FOR parameter : parameters SEPARATOR ", "»«
291 parameter.type.importedName» «parameter.name»«
296 def protected emitConstant(Constant c) '''
297 «IF BindingMapping.QNAME_STATIC_FIELD_NAME.equals(c.name)»
298 «val entry = c.value as Entry<JavaTypeName, String>»
299 public static final «c.type.importedNonNull» «c.name» = «entry.key.importedName».«BindingMapping.MODULE_INFO_QNAMEOF_METHOD_NAME»("«entry.value»");
301 public static final «c.type.importedName» «c.name» = «c.value»;
305 def protected generateCheckers(GeneratedProperty field, Restrictions restrictions, Type actualType) '''
306 «IF restrictions.rangeConstraint.present»
307 «AbstractRangeGenerator.forType(actualType).generateRangeChecker(field.name.toFirstUpper,
308 restrictions.rangeConstraint.get, this)»
310 «IF restrictions.lengthConstraint.present»
311 «LengthGenerator.generateLengthChecker(field.fieldName, actualType, restrictions.lengthConstraint.get, this)»
315 def protected checkArgument(GeneratedProperty property, Restrictions restrictions, Type actualType, String value) '''
316 «IF restrictions.getRangeConstraint.isPresent»
317 «IF actualType instanceof ConcreteType»
318 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value)»
320 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value + ".getValue()")»
323 «val fieldName = property.fieldName»
324 «IF restrictions.getLengthConstraint.isPresent»
325 «IF actualType instanceof ConcreteType»
326 «LengthGenerator.generateLengthCheckerCall(fieldName, value)»
328 «LengthGenerator.generateLengthCheckerCall(fieldName, value + ".getValue()")»
332 «val fieldUpperCase = fieldName.toUpperCase(Locale.ENGLISH)»
333 «FOR currentConstant : type.getConstantDefinitions»
334 «IF currentConstant.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)
335 && fieldUpperCase.equals(currentConstant.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length))»
336 «CODEHELPERS.importedName».checkPattern(value, «Constants.MEMBER_PATTERN_LIST»«fieldName», «Constants.MEMBER_REGEX_LIST»«fieldName»);
341 def protected hashCodeResult(Collection<? extends GeneratedProperty> properties) '''
342 final int prime = 31;
344 «FOR property : properties»
345 result = prime * result + «property.importedUtilClass».hashCode(«property.fieldName»);
349 def protected final generateAnnotation(AnnotationType annotation) '''
350 @«annotation.importedName»
351 «IF annotation.parameters !== null && !annotation.parameters.empty»
353 «FOR param : annotation.parameters SEPARATOR ","»
354 «param.name»=«param.value»