+ return result.getResult().data();
+ }
+
+ static final <T extends NormalizedNode> @NonNull T checkDataArgument(final @NonNull Class<T> expectedType,
+ final NormalizedNode data) {
+ try {
+ return expectedType.cast(requireNonNull(data));
+ } catch (ClassCastException e) {
+ throw new IllegalArgumentException("Expected " + expectedType.getSimpleName(), e);
+ }
+ }
+
+ /**
+ * Determines if two augmentation classes or case classes represents same data.
+ *
+ * <p>
+ * Two augmentations or cases could be substituted only if and if:
+ * <ul>
+ * <li>Both implements same interfaces</li>
+ * <li>Both have same children</li>
+ * <li>If augmentations: Both have same augmentation target class. Target class was generated for data node in a
+ * grouping.</li>
+ * <li>If cases: Both are from same choice. Choice class was generated for data node in grouping.</li>
+ * </ul>
+ *
+ * <p>
+ * <b>Explanation:</b>
+ * Binding Specification reuses classes generated for groupings as part of normal data tree, this classes from
+ * grouping could be used at various locations and user may not be aware of it and may use incorrect case or
+ * augmentation in particular subtree (via copy constructors, etc).
+ *
+ * @param potential Class which is potential substitution
+ * @param target Class which should be used at particular subtree
+ * @return true if and only if classes represents same data.
+ * @throws NullPointerException if any argument is {@code null}
+ */
+ // FIXME: MDSAL-785: this really should live in BindingRuntimeTypes and should not be based on reflection. The only
+ // user is binding-dom-codec and the logic could easily be performed on GeneratedType instead. For
+ // a particular world this boils down to a matrix, which can be calculated either on-demand or
+ // when we create BindingRuntimeTypes. Achieving that will bring us one step closer to being able
+ // to have a pre-compiled Binding Runtime.
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ static final boolean isSubstitutionFor(final Class potential, final Class target) {
+ Set<Class> subImplemented = new HashSet<>(Arrays.asList(potential.getInterfaces()));
+ Set<Class> targetImplemented = new HashSet<>(Arrays.asList(target.getInterfaces()));
+ if (!subImplemented.equals(targetImplemented)) {
+ return false;
+ }
+ if (Augmentation.class.isAssignableFrom(potential)
+ && !findAugmentationTarget(potential).equals(findAugmentationTarget(target))) {
+ return false;
+ }
+ for (Method potentialMethod : potential.getMethods()) {
+ if (Modifier.isStatic(potentialMethod.getModifiers())) {
+ // Skip any static methods, as we are not interested in those
+ continue;
+ }
+
+ try {
+ Method targetMethod = target.getMethod(potentialMethod.getName(), potentialMethod.getParameterTypes());
+ if (!potentialMethod.getReturnType().equals(targetMethod.getReturnType())) {
+ return false;
+ }
+ } catch (NoSuchMethodException e) {
+ // Counterpart method is missing, so classes could not be substituted.
+ return false;
+ } catch (SecurityException e) {
+ throw new IllegalStateException("Could not compare methods", e);
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Find augmentation target class from concrete Augmentation class. This method uses first generic argument of
+ * implemented {@link Augmentation} interface.
+ *
+ * @param augmentation {@link Augmentation} subclass for which we want to determine augmentation target.
+ * @return Augmentation target - class which augmentation provides additional extensions.
+ */
+ static final Class<? extends Augmentable<?>> findAugmentationTarget(
+ final Class<? extends Augmentation<?>> augmentation) {
+ final Optional<Class<Augmentable<?>>> opt = ClassLoaderUtils.findFirstGenericArgument(augmentation,
+ Augmentation.class);
+ return opt.orElse(null);
+ }
+
+ private static @NonNull ChildAddressabilitySummary computeChildAddressabilitySummary(final Object nodeSchema) {
+ // FIXME: rework this to work on EffectiveStatements
+ if (nodeSchema instanceof DataNodeContainer contaner) {
+ boolean haveAddressable = false;
+ boolean haveUnaddressable = false;
+ for (DataSchemaNode child : contaner.getChildNodes()) {
+ if (child instanceof ContainerSchemaNode || child instanceof AugmentationSchemaNode) {
+ haveAddressable = true;
+ } else if (child instanceof ListSchemaNode list) {
+ if (list.getKeyDefinition().isEmpty()) {
+ haveUnaddressable = true;
+ } else {
+ haveAddressable = true;
+ }
+ } else if (child instanceof AnydataSchemaNode || child instanceof AnyxmlSchemaNode
+ || child instanceof TypedDataSchemaNode) {
+ haveUnaddressable = true;
+ } else if (child instanceof ChoiceSchemaNode choice) {
+ switch (computeChildAddressabilitySummary(choice)) {
+ case ADDRESSABLE -> haveAddressable = true;
+ case UNADDRESSABLE -> haveUnaddressable = true;
+ case MIXED -> {
+ haveAddressable = true;
+ haveUnaddressable = true;
+ }
+ default -> throw new IllegalStateException("Unhandled accessibility summary for " + child);
+ }
+ } else {
+ LOG.warn("Unhandled child node {}", child);
+ }
+ }
+
+ if (!haveAddressable) {
+ // Empty or all are unaddressable
+ return ChildAddressabilitySummary.UNADDRESSABLE;
+ }
+
+ return haveUnaddressable ? ChildAddressabilitySummary.MIXED : ChildAddressabilitySummary.ADDRESSABLE;
+ } else if (nodeSchema instanceof ChoiceSchemaNode choice) {
+ return computeChildAddressabilitySummary(choice);
+ }
+
+ // No child nodes possible: return unaddressable
+ return ChildAddressabilitySummary.UNADDRESSABLE;