- private DataContainerCodecPrototype<?> loadChildPrototype(final Class<?> childClass) {
- final DataSchemaNode origDef = factory().getRuntimeContext().getSchemaDefinition(childClass);
- // Direct instantiation or use in same module in which grouping
- // was defined.
- DataSchemaNode sameName;
- try {
- sameName = getSchema().getDataChildByName(origDef.getQName());
- } catch (final IllegalArgumentException e) {
- sameName = null;
- }
- final DataSchemaNode childSchema;
- if (sameName != null) {
- // Exactly same schema node
- if (origDef.equals(sameName)) {
- childSchema = sameName;
- // We check if instantiated node was added via uses
- // statement and is instantiation of same grouping
- } else if (origDef.equals(SchemaNodeUtils.getRootOriginalIfPossible(sameName))) {
- childSchema = sameName;
- } else {
- // Node has same name, but clearly is different
- childSchema = null;
- }
- } else {
- // We are looking for instantiation via uses in other module
- final QName instantiedName = origDef.getQName().withModule(namespace());
- final DataSchemaNode potential = getSchema().getDataChildByName(instantiedName);
- // We check if it is really instantiated from same
- // definition as class was derived
- if (potential != null && origDef.equals(SchemaNodeUtils.getRootOriginalIfPossible(potential))) {
- childSchema = potential;
- } else {
- childSchema = null;
- }
- }
- final DataSchemaNode nonNullChild =
- childNonNull(childSchema, childClass, "Node %s does not have child named %s", getSchema(), childClass);
- return DataContainerCodecPrototype.from(createBindingArg(childClass, nonNullChild), nonNullChild, factory());
- }
-
- @SuppressWarnings("unchecked")
- Item<?> createBindingArg(final Class<?> childClass, final DataSchemaNode childSchema) {
- return Item.of((Class<? extends DataObject>) childClass);
- }
-
- private DataContainerCodecPrototype<?> yangAugmentationChild(final AugmentationIdentifier arg) {
- final DataContainerCodecPrototype<?> firstTry = augmentations.byYang.get(arg);
- if (firstTry != null) {
- return firstTry;
- }
- if (possibleAugmentations.containsKey(arg)) {
- // Try to load augmentations, which will potentially update knownAugmentations, hence we re-load that field
- // again.
- reloadAllAugmentations();
- return augmentations.byYang.get(arg);
- }
- return null;
- }
-
- private @Nullable DataContainerCodecPrototype<?> augmentationByClass(final @NonNull Class<?> childClass) {
- DataContainerCodecPrototype<?> lookup = augmentationByClassOrEquivalentClass(childClass);
- if (lookup != null || !isPotentialAugmentation(childClass)) {
- return lookup;
- }
-
- // Attempt to reload all augmentations using TCCL and lookup again
- reloadAllAugmentations();
- lookup = augmentationByClassOrEquivalentClass(childClass);
- if (lookup != null) {
- return lookup;
- }
-
- // Still no result, this can be caused by TCCL not being set up properly -- try the class's ClassLoader
- // if it is present;
- final ClassLoader loader = childClass.getClassLoader();
- if (loader == null) {
- return null;
- }
-
- LOG.debug("Class {} not loaded via TCCL, attempting to recover", childClass);
- ClassLoaderUtils.runWithClassLoader(loader, this::reloadAllAugmentations);
- return augmentationByClassOrEquivalentClass(childClass);
- }
-
- private boolean isPotentialAugmentation(final Class<?> childClass) {
- final JavaTypeName name = JavaTypeName.create(childClass);
- for (Type type : possibleAugmentations.values()) {
- if (name.equals(type.getIdentifier())) {
- return true;
- }
- }
- return false;
- }
-
- private @Nullable DataContainerCodecPrototype<?> augmentationByClassOrEquivalentClass(
- final @NonNull Class<?> childClass) {
- // Perform a single load, so we can reuse it if we end up going to the reflection-based slow path
- final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> local = augmentations.byStream;
- final DataContainerCodecPrototype<?> childProto = local.get(childClass);
- if (childProto != null) {
- return childProto;
- }
-