+ private static void addImplMethods(final Set<MethodSignature> methods, final GeneratedType implType) {
+ for (final MethodSignature implMethod : implType.getMethodDefinitions()) {
+ if (hasOverrideAnnotation(implMethod)) {
+ methods.add(implMethod);
+ } else {
+ final String implMethodName = implMethod.getName();
+ if (BindingMapping.isGetterMethodName(implMethodName)
+ && getterByName(methods, implMethodName).isEmpty()) {
+
+ methods.add(implMethod);
+ }
+ }
+ }
+ }
+
+ protected static Optional<MethodSignature> getterByName(final Iterable<MethodSignature> methods,
+ final String implMethodName) {
+ for (MethodSignature method : methods) {
+ final String methodName = method.getName();
+ if (BindingMapping.isGetterMethodName(methodName) && isSameProperty(method.getName(), implMethodName)) {
+ return Optional.of(method);
+ }
+ }
+ return Optional.empty();
+ }
+
+ protected static String propertyNameFromGetter(final MethodSignature getter) {
+ return propertyNameFromGetter(getter.getName());
+ }
+
+ protected static String propertyNameFromGetter(final String getterName) {
+ final String prefix;
+ if (BindingMapping.isGetterMethodName(getterName)) {
+ prefix = BindingMapping.GETTER_PREFIX;
+ } else if (BindingMapping.isNonnullMethodName(getterName)) {
+ prefix = BindingMapping.NONNULL_PREFIX;
+ } else if (BindingMapping.isRequireMethodName(getterName)) {
+ prefix = BindingMapping.REQUIRE_PREFIX;
+ } else {
+ throw new IllegalArgumentException(getterName + " is not a getter");
+ }
+ return StringExtensions.toFirstLower(getterName.substring(prefix.length()));
+ }
+
+ /**
+ * Check whether specified method has an attached annotation which corresponds to {@code @Override}.
+ *
+ * @param method Method to examine
+ * @return True if there is an override annotation
+ */
+ 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));
+ }
+