1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.databind.model;
7   
8   import java.lang.reflect.Constructor;
9   import java.lang.reflect.InvocationTargetException;
10  import java.net.URI;
11  import java.util.Collection;
12  import java.util.List;
13  
14  import dev.metaschema.core.model.IModuleExtended;
15  import dev.metaschema.core.util.ObjectUtils;
16  import dev.metaschema.databind.IBindingContext;
17  import edu.umd.cs.findbugs.annotations.NonNull;
18  
19  /**
20   * Represents a bound Metaschema module that provides access to its field and
21   * assembly definitions through Java class bindings.
22   */
23  public interface IBoundModule
24      extends IModuleExtended<
25          IBoundModule,
26          IBoundDefinitionModelComplex,
27          IBoundDefinitionFlag,
28          IBoundDefinitionModelField<?>,
29          IBoundDefinitionModelAssembly> {
30  
31    /**
32     * Create a new instance of a bound module using reflection.
33     *
34     * @param clazz
35     *          the bound module class to instantiate
36     * @param bindingContext
37     *          the binding context for the module
38     * @param importedModules
39     *          the list of modules imported by this module
40     * @return the new module instance
41     * @throws IllegalArgumentException
42     *           if the module cannot be instantiated
43     */
44    @NonNull
45    static IBoundModule newInstance(
46        @NonNull Class<? extends IBoundModule> clazz,
47        @NonNull IBindingContext bindingContext,
48        @NonNull List<? extends IBoundModule> importedModules) {
49  
50      Constructor<? extends IBoundModule> constructor;
51      try {
52        constructor = clazz.getDeclaredConstructor(List.class, IBindingContext.class);
53      } catch (NoSuchMethodException ex) {
54        throw new IllegalArgumentException(ex);
55      }
56  
57      try {
58        return ObjectUtils.notNull(constructor.newInstance(importedModules, bindingContext));
59      } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
60        throw new IllegalArgumentException(ex);
61      }
62    }
63  
64    /**
65     * Get the Module binding context.
66     *
67     * @return the context
68     */
69    @NonNull
70    IBindingContext getBindingContext();
71  
72    @Override
73    default URI getLocation() {
74      // not known
75      return null;
76    }
77  
78    @Override
79    Collection<IBoundDefinitionModelAssembly> getAssemblyDefinitions();
80  
81    @Override
82    IBoundDefinitionModelAssembly getAssemblyDefinitionByName(@NonNull Integer name);
83  
84    @Override
85    Collection<IBoundDefinitionModelField<?>> getFieldDefinitions();
86  
87    @Override
88    IBoundDefinitionModelField<?> getFieldDefinitionByName(@NonNull Integer name);
89  }