1616import org .emfjson .jackson .module .EMFModule ;
1717
1818import java .util .*;
19+ import java .util .function .Consumer ;
1920import java .util .stream .Collectors ;
2021
2122import static java .util .Spliterator .ORDERED ;
2223import static java .util .Spliterators .spliteratorUnknownSize ;
2324import static java .util .stream .StreamSupport .stream ;
2425import static org .emfjson .jackson .annotations .JsonAnnotations .getAliases ;
2526import static org .emfjson .jackson .annotations .JsonAnnotations .getElementName ;
26- import static org .emfjson .jackson .module .EMFModule .Feature .OPTION_SERIALIZE_TYPE ;
2727import static org .emfjson .jackson .module .EMFModule .Feature .OPTION_USE_ID ;
2828
2929public class EObjectPropertyMap {
@@ -48,84 +48,84 @@ public static Builder from(EMFModule module, int features) {
4848 return new Builder (module .getIdentityInfo (), module .getTypeInfo (), module .getReferenceInfo (), features );
4949 }
5050
51- private void buildCache (DatabindContext ctxt ) {
52- ResourceSet resourceSet = EMFContext .getResourceSet (ctxt );
53-
54- Set <EClass > types = resourceSet .getPackageRegistry ().values ().stream ()
55- .flatMap (model -> stream (spliteratorUnknownSize (((EPackage ) model ).eAllContents (), ORDERED ), false ))
56- .filter (e -> e instanceof EClass )
57- .map (e -> (EClass ) e )
58- .collect (Collectors .toSet ());
59-
60- types .forEach (type -> cache .put (type , construct (ctxt , type )));
61- }
62-
6351 public EObjectPropertyMap construct (DatabindContext ctxt , EClass type ) {
6452 if (type == null ) {
6553 buildCache (ctxt );
6654 }
6755
68- Map <String , EObjectProperty > propertiesMap = new HashMap <>();
69- Set <EObjectProperty > properties = new HashSet <>();
7056 EObjectPropertyMap propertyMap = type == null ? null : cache .get (type );
7157
7258 if (propertyMap == null ) {
73- collectProperties (ctxt , type , propertiesMap , properties );
74- propertyMap = new EObjectPropertyMap (type , propertiesMap , properties );
59+ propertyMap = createPropertyMap (ctxt , type );
7560 if (type != null ) {
7661 cache .put (type , propertyMap );
7762 }
7863 }
7964 return propertyMap ;
8065 }
8166
82- private void collectProperties (DatabindContext ctxt , EClass type , Map <String , EObjectProperty > propertiesMap , Set <EObjectProperty > properties ) {
83- final EcoreTypeFactory factory = EMFContext .getTypeFactory (ctxt );
84- EObjectProperty property ;
67+ private void buildCache (DatabindContext ctxt ) {
68+ ResourceSet resourceSet = EMFContext .getResourceSet (ctxt );
8569
86- if ( OPTION_USE_ID . enabledIn ( features )) {
87- property = new EObjectIdentityProperty ( identityInfo );
88- properties . add ( property );
89- propertiesMap . put ( identityInfo . getProperty (), property );
90- }
70+ Set < EClass > types = resourceSet . getPackageRegistry (). values (). stream ()
71+ . flatMap ( model -> stream ( spliteratorUnknownSize ((( EPackage ) model ). eAllContents (), ORDERED ), false ))
72+ . filter ( e -> e instanceof EClass )
73+ . map ( e -> ( EClass ) e )
74+ . collect ( Collectors . toSet ());
9175
92- if (OPTION_SERIALIZE_TYPE .enabledIn (features )) {
93- property = getTypeProperty (type );
94- properties .add (property );
95- propertiesMap .put (property .getFieldName (), property );
96- }
76+ types .forEach (type -> cache .put (type , construct (ctxt , type )));
77+ }
78+
79+ private EObjectPropertyMap createPropertyMap (DatabindContext ctxt , EClass type ) {
80+ EcoreTypeFactory factory = EMFContext .getTypeFactory (ctxt );
81+ HashMap <String , EObjectProperty > propertiesMap = new HashMap <>();
82+ Set <EObjectProperty > properties = new LinkedHashSet <>();
9783
98- property = new EObjectReferenceProperty (referenceInfo );
99- properties .add (property );
100- propertiesMap .put (referenceInfo .getProperty (), property );
84+ Consumer <EObjectProperty > add = p -> {
85+ properties .add (p );
86+ propertiesMap .put (p .getFieldName (), p );
87+ };
88+
89+ add .accept (new EObjectReferenceProperty (referenceInfo ));
90+ add .accept (getTypeProperty (type , features ));
91+
92+ if (OPTION_USE_ID .enabledIn (features )) {
93+ add .accept (new EObjectIdentityProperty (identityInfo ));
94+ }
10195
10296 if (type != null ) {
10397 for (EStructuralFeature feature : type .getEAllStructuralFeatures ()) {
104- if (isCandidate (feature )) {
105- JavaType javaType = factory .typeOf (ctxt , type , feature );
106-
107- if (javaType != null ) {
108- property = new EObjectFeatureProperty (feature , javaType , features );
109- properties .add (property );
110- propertiesMap .put (getElementName (feature ), property );
98+ createFeatureProperty (ctxt , factory , type , feature ).ifPresent (property -> {
99+ add .accept (property );
111100
112- for (String alias : getAliases (feature )) {
113- propertiesMap .put (alias , property );
114- }
101+ for (String alias : getAliases (feature )) {
102+ propertiesMap .put (alias , property );
115103 }
116- }
104+ });
117105 }
118106
119107 for (EOperation operation : type .getEAllOperations ()) {
120108 EAnnotation annotation = operation .getEAnnotation ("JsonProperty" );
121109
122110 if (annotation != null && operation .getEParameters ().isEmpty ()) {
123- property = new EObjectOperationProperty (getElementName (operation ), operation );
124- properties .add (property );
125- propertiesMap .put (getElementName (operation ), property );
111+ add .accept (new EObjectOperationProperty (getElementName (operation ), operation ));
126112 }
127113 }
128114 }
115+
116+ return new EObjectPropertyMap (type , propertiesMap , properties );
117+ }
118+
119+ private Optional <EObjectFeatureProperty > createFeatureProperty (DatabindContext ctxt , EcoreTypeFactory factory ,
120+ EClass type , EStructuralFeature feature ) {
121+ if (isCandidate (feature )) {
122+ JavaType javaType = factory .typeOf (ctxt , type , feature );
123+ if (javaType != null ) {
124+ return Optional .of (new EObjectFeatureProperty (feature , javaType , features ));
125+ }
126+ }
127+
128+ return Optional .empty ();
129129 }
130130
131131 boolean isFeatureMapEntry (EStructuralFeature feature ) {
@@ -164,17 +164,18 @@ boolean isCandidate(EReference eReference) {
164164 return !(opposite != null && opposite .isContainment ());
165165 }
166166
167- private EObjectProperty getTypeProperty (EClass type ) {
167+ private EObjectProperty getTypeProperty (EClass type , int features ) {
168+ EcoreTypeInfo currentTypeInfo = null ;
169+
168170 if (type != null && !JsonAnnotations .shouldIgnoreType (type )) {
169- EcoreTypeInfo currentTypeInfo = JsonAnnotations .getTypeProperty (type );
170- if (currentTypeInfo != null ) {
171- return new EObjectTypeProperty (currentTypeInfo );
172- } else {
173- return new EObjectTypeProperty (typeInfo );
174- }
175- } else {
176- return new EObjectTypeProperty (typeInfo );
171+ currentTypeInfo = JsonAnnotations .getTypeProperty (type );
177172 }
173+
174+ if (currentTypeInfo == null ) {
175+ currentTypeInfo = typeInfo ;
176+ }
177+
178+ return new EObjectTypeProperty (currentTypeInfo , features );
178179 }
179180
180181 public EObjectPropertyMap constructDefault (DatabindContext ctxt ) {
@@ -185,7 +186,7 @@ public EObjectPropertyMap find(DeserializationContext ctxt, EClass defaultType,
185186 List <EClass > types = EMFContext .allSubTypes (ctxt , defaultType );
186187 Map <String , EClass > properties = new HashMap <>();
187188 for (EClass type : types ) {
188- EObjectProperty p = getTypeProperty (type );
189+ EObjectProperty p = getTypeProperty (type , features );
189190 properties .put (p .getFieldName (), type );
190191 }
191192
@@ -208,7 +209,7 @@ public EObjectPropertyMap find(DeserializationContext ctxt, EClass defaultType,
208209
209210 private EObjectTypeProperty typeProperty ;
210211
211- EObjectPropertyMap (EClass type , Map <String , EObjectProperty > propertiesMap , Set <EObjectProperty > properties ) {
212+ private EObjectPropertyMap (EClass type , Map <String , EObjectProperty > propertiesMap , Set <EObjectProperty > properties ) {
212213 this .type = type ;
213214 this .propertiesMap = propertiesMap ;
214215 this .properties = properties ;
0 commit comments