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    @Nullable
30    Method getBeforeDeserializeMethod();
31  
32    /**
33     * Calls the method named "beforeDeserialize" on each class in the object's
34     * hierarchy if the method exists on the class.
35     * <p>
36     * These methods can be used to set the initial state of the target bound object
37     * before data is read and applied during deserialization.
38     *
39     * @param targetObject
40     *          the data object target to call the method(s) on
41     * @param parentObject
42     *          the object target's parent object, which is used as the method
43     *          argument
44     * @throws BindingException
45     *           if an error occurs while calling the method
46     */
47    @Override
48    default void callBeforeDeserialize(IBoundObject targetObject, IBoundObject parentObject) throws BindingException {
49      Method beforeDeserializeMethod = getBeforeDeserializeMethod();
50      if (beforeDeserializeMethod != null) {
51        try {
52          beforeDeserializeMethod.invoke(targetObject, parentObject);
53        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
54          throw new BindingException(ex);
55        }
56      }
57    }
58  
59    @Nullable
60    Method getAfterDeserializeMethod();
61  
62    /**
63     * Calls the method named "afterDeserialize" on each class in the object's
64     * hierarchy if the method exists.
65     * <p>
66     * These methods can be used to modify the state of the target bound object
67     * after data is read and applied during deserialization.
68     *
69     * @param targetObject
70     *          the data object target to call the method(s) on
71     * @param parentObject
72     *          the object target's parent object, which is used as the method
73     *          argument
74     * @throws BindingException
75     *           if an error occurs while calling the method
76     */
77    @Override
78    default void callAfterDeserialize(IBoundObject targetObject, IBoundObject parentObject) throws BindingException {
79      Method afterDeserializeMethod = getAfterDeserializeMethod();
80      if (afterDeserializeMethod != null) {
81        try {
82          afterDeserializeMethod.invoke(targetObject, parentObject);
83        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
84          throw new BindingException(ex);
85        }
86      }
87    }
88  
89    // @Override
90    // public String getJsonKeyFlagName() {
91    // // definition items never have a JSON key
92    // return null;
93    // }
94  
95  }