Skip to content
This repository was archived by the owner on Jun 26, 2021. It is now read-only.

Commit 66b8e05

Browse files
committed
code cleaning
1 parent a57375b commit 66b8e05

File tree

4 files changed

+68
-65
lines changed

4 files changed

+68
-65
lines changed

src/main/java/org/emfjson/jackson/databind/deser/EObjectDeserializer.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ public class EObjectDeserializer extends JsonDeserializer<EObject> {
4040
private final EObjectPropertyMap.Builder builder;
4141
private final Class<?> currentType;
4242

43-
public EObjectDeserializer(EObjectPropertyMap.Builder builder) {
44-
this.builder = builder;
45-
this.currentType = null;
46-
}
47-
4843
public EObjectDeserializer(EObjectPropertyMap.Builder builder, Class<?> currentType) {
4944
this.builder = builder;
5045
this.currentType = currentType;

src/main/java/org/emfjson/jackson/databind/deser/ReferenceEntries.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import com.fasterxml.jackson.databind.DatabindContext;
1515
import org.eclipse.emf.ecore.EObject;
16-
import org.eclipse.emf.ecore.resource.ResourceSet;
1716
import org.emfjson.jackson.handlers.URIHandler;
1817

1918
import java.util.ArrayList;

src/main/java/org/emfjson/jackson/databind/property/EObjectPropertyMap.java

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
import org.emfjson.jackson.module.EMFModule;
1717

1818
import java.util.*;
19+
import java.util.function.Consumer;
1920
import java.util.stream.Collectors;
2021

2122
import static java.util.Spliterator.ORDERED;
2223
import static java.util.Spliterators.spliteratorUnknownSize;
2324
import static java.util.stream.StreamSupport.stream;
2425
import static org.emfjson.jackson.annotations.JsonAnnotations.getAliases;
2526
import static org.emfjson.jackson.annotations.JsonAnnotations.getElementName;
26-
import static org.emfjson.jackson.module.EMFModule.Feature.OPTION_SERIALIZE_TYPE;
2727
import static org.emfjson.jackson.module.EMFModule.Feature.OPTION_USE_ID;
2828

2929
public 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;

src/main/java/org/emfjson/jackson/databind/property/EObjectTypeProperty.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,31 @@
1818

1919
import java.io.IOException;
2020

21+
import static org.emfjson.jackson.module.EMFModule.Feature.OPTION_SERIALIZE_TYPE;
22+
2123
public class EObjectTypeProperty extends EObjectProperty {
2224

2325
private final JsonSerializer<Object> serializer = new StringSerializer();
2426
private final JsonDeserializer<String> deserializer = StringDeserializer.instance;
2527

2628
private final ValueReader<String, EClass> valueReader;
2729
private final ValueWriter<EClass, String> valueWriter;
30+
private final int features;
2831

29-
public EObjectTypeProperty(EcoreTypeInfo info) {
32+
public EObjectTypeProperty(EcoreTypeInfo info, int features) {
3033
super(info.getProperty());
3134

3235
this.valueReader = info.getValueReader();
3336
this.valueWriter = info.getValueWriter();
37+
this.features = features;
3438
}
3539

3640
@Override
3741
public void serialize(EObject bean, JsonGenerator jg, SerializerProvider provider) throws IOException {
42+
if (!OPTION_SERIALIZE_TYPE.enabledIn(features)) {
43+
return;
44+
}
45+
3846
EClass objectType = bean.eClass();
3947
EReference containment = bean.eContainmentFeature();
4048

0 commit comments

Comments
 (0)