+ static boolean hasOverrideAnnotation(final MethodSignature method) {
+ for (final AnnotationType annotation : method.getAnnotations()) {
+ if (OVERRIDE.equals(annotation.getIdentifier())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ static void appendSnippet(final StringBuilder sb, final GeneratedType type) {
+ type.getYangSourceDefinition().ifPresent(def -> {
+ sb.append('\n');
+
+ if (def instanceof Single) {
+ final DocumentedNode node = ((Single) def).getNode();
+
+ sb.append("<p>\n")
+ .append("This class represents the following YANG schema fragment defined in module <b>")
+ .append(def.getModule().argument().getLocalName()).append("</b>\n")
+ .append("<pre>\n");
+ appendYangSnippet(sb, def.getModule(), ((EffectiveStatement<?, ?>) node).getDeclared());
+ sb.append("</pre>");
+
+ if (node instanceof SchemaNode) {
+ final SchemaNode schema = (SchemaNode) node;
+ sb.append("The schema path to identify an instance is\n");
+ appendPath(sb.append("<i>"), def.getModule(), schema.getPath().getPathFromRoot());
+ sb.append("</i>\n");
+
+ if (hasBuilderClass(schema)) {
+ final String builderName = type.getName() + BindingMapping.BUILDER_SUFFIX;
+
+ sb.append("\n<p>To create instances of this class use {@link ").append(builderName)
+ .append("}.\n")
+ .append("@see ").append(builderName).append('\n');
+ if (node instanceof ListSchemaNode) {
+ final var keyDef = ((ListSchemaNode) node).getKeyDefinition();
+ if (!keyDef.isEmpty()) {
+ sb.append("@see ").append(type.getName()).append(BindingMapping.KEY_SUFFIX);
+ }
+ sb.append('\n');
+ }
+ }
+ }
+ } else if (def instanceof Multiple) {
+ sb.append("<pre>\n");
+ for (SchemaNode node : ((Multiple) def).getNodes()) {
+ appendYangSnippet(sb, def.getModule(), ((EffectiveStatement<?, ?>) node).getDeclared());
+ }
+ sb.append("</pre>\n");
+ }
+ });
+ }
+
+ static String encodeJavadocSymbols(final String description) {
+ // FIXME: Use String.isBlank()?
+ return description == null || description.isEmpty() ? description
+ : TAIL_COMMENT_PATTERN.matcher(AMP_MATCHER.replaceFrom(description, "&")).replaceAll("*/");
+ }
+
+ private static void appendYangSnippet(final StringBuilder sb, final ModuleEffectiveStatement module,
+ final DeclaredStatement<?> stmt) {
+ for (String str : YANG_FORMATTER.toYangTextSnippet(module, stmt)) {
+ sb.append(replaceAllIllegalChars(encodeAngleBrackets(encodeJavadocSymbols(str))));
+ }
+ }
+
+ private static void appendPath(final StringBuilder sb, final ModuleEffectiveStatement module,
+ final List<QName> path) {
+ if (!path.isEmpty()) {
+ // FIXME: this is module name, while when we switch, we end up using QName.toString() -- which is weird
+ sb.append(module.argument().getLocalName());
+ XMLNamespace currentNamespace = path.get(0).getNamespace();
+
+ for (QName pathElement : path) {
+ final XMLNamespace elementNamespace = pathElement.getNamespace();
+ if (!elementNamespace.equals(currentNamespace)) {
+ sb.append(pathElement);
+ currentNamespace = elementNamespace;
+ } else {
+ sb.append(pathElement.getLocalName());
+ }
+ }
+ }
+ }
+
+ private static boolean hasBuilderClass(final SchemaNode schemaNode) {
+ return schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode
+ || schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition;
+ }
+
+ private static boolean isSameProperty(final String getterName1, final String getterName2) {
+ return propertyNameFromGetter(getterName1).equals(propertyNameFromGetter(getterName2));
+ }
+
+ /**
+ * Creates set of generated property instances from getter <code>methods</code>.
+ *
+ * @param methods set of method signature instances which should be transformed to list of properties
+ * @return set of generated property instances which represents the getter <code>methods</code>
+ */
+ private static Set<BuilderGeneratedProperty> propertiesFromMethods(final Collection<MethodSignature> methods) {
+ if (methods == null || methods.isEmpty()) {
+ return Collections.emptySet();
+ }
+ final Set<BuilderGeneratedProperty> result = new LinkedHashSet<>();
+ for (MethodSignature m : methods) {
+ final BuilderGeneratedProperty createdField = propertyFromGetter(m);
+ if (createdField != null) {
+ result.add(createdField);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Creates generated property instance from the getter <code>method</code> name and return type.
+ *
+ * @param method method signature from which is the method name and return type obtained
+ * @return generated property instance for the getter <code>method</code>
+ * @throws IllegalArgumentException <ul>
+ * <li>if the {@code method} equals {@code null}</li>
+ * <li>if the name of the {@code method} equals {@code null}</li>
+ * <li>if the name of the {@code method} is empty</li>
+ * <li>if the return type of the {@code method} equals {@code null}</li>
+ * </ul>
+ */
+ private static BuilderGeneratedProperty propertyFromGetter(final MethodSignature method) {
+ checkArgument(method != null);
+ checkArgument(method.getReturnType() != null);
+ checkArgument(method.getName() != null);
+ checkArgument(!method.getName().isEmpty());
+ if (method.isDefault()) {
+ return null;
+ }
+ if (!BindingMapping.isGetterMethodName(method.getName())) {
+ return null;
+ }
+
+ final String fieldName = StringExtensions.toFirstLower(method.getName().substring(GETTER_PREFIX_LENGTH));
+ return new BuilderGeneratedProperty(fieldName, method);