1
2
3
4
5
6 package gov.nist.secauto.metaschema.databind.model.impl;
7
8 import gov.nist.secauto.metaschema.core.model.IBoundObject;
9 import gov.nist.secauto.metaschema.core.model.util.ModuleUtils;
10 import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
11 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
12 import gov.nist.secauto.metaschema.databind.IBindingContext;
13 import gov.nist.secauto.metaschema.databind.io.BindingException;
14 import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex;
15 import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag;
16 import gov.nist.secauto.metaschema.databind.model.IBoundModule;
17
18 import java.lang.annotation.Annotation;
19 import java.lang.reflect.Method;
20
21 import edu.umd.cs.findbugs.annotations.NonNull;
22 import edu.umd.cs.findbugs.annotations.Nullable;
23 import nl.talsmasoftware.lazy4j.Lazy;
24
25 public abstract class AbstractBoundDefinitionModelComplex<A extends Annotation>
26 implements IBoundDefinitionModelComplex {
27 @NonNull
28 private final Class<? extends IBoundObject> clazz;
29 @NonNull
30 private final A annotation;
31 @NonNull
32 private final IBindingContext bindingContext;
33 @NonNull
34 private final IBoundModule module;
35 @NonNull
36 private final Lazy<IEnhancedQName> qname;
37 @NonNull
38 private final Lazy<IEnhancedQName> definitionQName;
39 @Nullable
40 private final Method beforeDeserializeMethod;
41 @Nullable
42 private final Method afterDeserializeMethod;
43
44 protected AbstractBoundDefinitionModelComplex(
45 @NonNull Class<? extends IBoundObject> clazz,
46 @NonNull A annotation,
47 @NonNull IBoundModule module,
48 @NonNull IBindingContext bindingContext) {
49 this.clazz = clazz;
50 this.annotation = annotation;
51 this.bindingContext = bindingContext;
52 this.module = module;
53 this.qname = ObjectUtils.notNull(Lazy.lazy(() -> ModuleUtils.parseModelName(
54 getContainingModule(),
55 getEffectiveName())));
56 this.definitionQName = ObjectUtils.notNull(Lazy.lazy(() -> ModuleUtils.parseModelName(
57 getContainingModule(),
58 getName())));
59 this.beforeDeserializeMethod = ClassIntrospector.getMatchingMethod(
60 clazz,
61 "beforeDeserialize",
62 Object.class);
63 this.afterDeserializeMethod = ClassIntrospector.getMatchingMethod(
64 clazz,
65 "afterDeserialize",
66 Object.class);
67 }
68
69 @Override
70 public Class<? extends IBoundObject> getBoundClass() {
71 return clazz;
72 }
73
74 @NonNull
75 public A getAnnotation() {
76 return annotation;
77 }
78
79 @Override
80 @NonNull
81 public IBoundModule getContainingModule() {
82 return module;
83 }
84
85 @Override
86 @NonNull
87 public IBindingContext getBindingContext() {
88 return bindingContext;
89 }
90
91 @SuppressWarnings("null")
92 @Override
93 public final IEnhancedQName getQName() {
94 return qname.get();
95 }
96
97 @SuppressWarnings("null")
98 @Override
99 public final IEnhancedQName getDefinitionQName() {
100 return definitionQName.get();
101 }
102
103 @Override
104 public Method getBeforeDeserializeMethod() {
105 return beforeDeserializeMethod;
106 }
107
108 @Override
109 public Method getAfterDeserializeMethod() {
110 return afterDeserializeMethod;
111 }
112
113
114
115
116
117
118
119 @Override
120 public IBoundObject deepCopyItem(IBoundObject item, IBoundObject parentInstance) throws BindingException {
121 IBoundObject instance = newInstance(item::getMetaschemaData);
122
123 callBeforeDeserialize(instance, parentInstance);
124
125 deepCopyItemInternal(item, instance);
126
127 callAfterDeserialize(instance, parentInstance);
128
129 return instance;
130 }
131
132 protected void deepCopyItemInternal(@NonNull IBoundObject fromObject, @NonNull IBoundObject toObject)
133 throws BindingException {
134 for (IBoundInstanceFlag instance : getFlagInstances()) {
135 instance.deepCopy(fromObject, toObject);
136 }
137 }
138 }