1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.model;
7   
8   import gov.nist.secauto.metaschema.core.model.IBoundObject;
9   import gov.nist.secauto.metaschema.databind.io.BindingException;
10  import gov.nist.secauto.metaschema.databind.model.info.IFeatureComplexItemValueHandler;
11  
12  import java.lang.reflect.InvocationTargetException;
13  import java.lang.reflect.Method;
14  import java.util.Map;
15  import java.util.function.Predicate;
16  
17  import edu.umd.cs.findbugs.annotations.NonNull;
18  import edu.umd.cs.findbugs.annotations.Nullable;
19  
20  /**
21   * Represents a field or assembly instance bound to Java class.
22   */
23  public interface IBoundDefinitionModelComplex
24      extends IBoundDefinitionModel<IBoundObject>, IFeatureComplexItemValueHandler {
25  
26    @NonNull
27    Map<String, IBoundProperty<?>> getJsonProperties(@Nullable Predicate<IBoundInstanceFlag> flagFilter);
28  
29    @Override
30    default boolean isInline() {
31      return getBoundClass().getEnclosingClass() != null;
32    }
33  
34    @Nullable
35    Method getBeforeDeserializeMethod();
36  
37    /**
38     * Calls the method named "beforeDeserialize" on each class in the object's
39     * hierarchy if the method exists on the class.
40     * <p>
41     * These methods can be used to set the initial state of the target bound object
42     * before data is read and applied during deserialization.
43     *
44     * @param targetObject
45     *          the data object target to call the method(s) on
46     * @param parentObject
47     *          the object target's parent object, which is used as the method
48     *          argument
49     * @throws BindingException
50     *           if an error occurs while calling the method
51     */
52    @Override
53    default void callBeforeDeserialize(IBoundObject targetObject, IBoundObject parentObject) throws BindingException {
54      Method beforeDeserializeMethod = getBeforeDeserializeMethod();
55      if (beforeDeserializeMethod != null) {
56        try {
57          beforeDeserializeMethod.invoke(targetObject, parentObject);
58        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
59          throw new BindingException(ex);
60        }
61      }
62    }
63  
64    @Nullable
65    Method getAfterDeserializeMethod();
66  
67    /**
68     * Calls the method named "afterDeserialize" on each class in the object's
69     * hierarchy if the method exists.
70     * <p>
71     * These methods can be used to modify the state of the target bound object
72     * after data is read and applied during deserialization.
73     *
74     * @param targetObject
75     *          the data object target to call the method(s) on
76     * @param parentObject
77     *          the object target's parent object, which is used as the method
78     *          argument
79     * @throws BindingException
80     *           if an error occurs while calling the method
81     */
82    @Override
83    default void callAfterDeserialize(IBoundObject targetObject, IBoundObject parentObject) throws BindingException {
84      Method afterDeserializeMethod = getAfterDeserializeMethod();
85      if (afterDeserializeMethod != null) {
86        try {
87          afterDeserializeMethod.invoke(targetObject, parentObject);
88        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
89          throw new BindingException(ex);
90        }
91      }
92    }
93  
94    // @Override
95    // public String getJsonKeyFlagName() {
96    // // definition items never have a JSON key
97    // return null;
98    // }
99  
100 }