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

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
ghillairet committed May 7, 2018
1 parent a57375b commit 66b8e05
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ public class EObjectDeserializer extends JsonDeserializer<EObject> {
private final EObjectPropertyMap.Builder builder;
private final Class<?> currentType;

public EObjectDeserializer(EObjectPropertyMap.Builder builder) {
this.builder = builder;
this.currentType = null;
}

public EObjectDeserializer(EObjectPropertyMap.Builder builder, Class<?> currentType) {
this.builder = builder;
this.currentType = currentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import com.fasterxml.jackson.databind.DatabindContext;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.emfjson.jackson.handlers.URIHandler;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import org.emfjson.jackson.module.EMFModule;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static java.util.Spliterator.ORDERED;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.StreamSupport.stream;
import static org.emfjson.jackson.annotations.JsonAnnotations.getAliases;
import static org.emfjson.jackson.annotations.JsonAnnotations.getElementName;
import static org.emfjson.jackson.module.EMFModule.Feature.OPTION_SERIALIZE_TYPE;
import static org.emfjson.jackson.module.EMFModule.Feature.OPTION_USE_ID;

public class EObjectPropertyMap {
Expand All @@ -48,84 +48,84 @@ public static Builder from(EMFModule module, int features) {
return new Builder(module.getIdentityInfo(), module.getTypeInfo(), module.getReferenceInfo(), features);
}

private void buildCache(DatabindContext ctxt) {
ResourceSet resourceSet = EMFContext.getResourceSet(ctxt);

Set<EClass> types = resourceSet.getPackageRegistry().values().stream()
.flatMap(model -> stream(spliteratorUnknownSize(((EPackage) model).eAllContents(), ORDERED), false))
.filter(e -> e instanceof EClass)
.map(e -> (EClass) e)
.collect(Collectors.toSet());

types.forEach(type -> cache.put(type, construct(ctxt, type)));
}

public EObjectPropertyMap construct(DatabindContext ctxt, EClass type) {
if (type == null) {
buildCache(ctxt);
}

Map<String, EObjectProperty> propertiesMap = new HashMap<>();
Set<EObjectProperty> properties = new HashSet<>();
EObjectPropertyMap propertyMap = type == null ? null: cache.get(type);

if (propertyMap == null) {
collectProperties(ctxt, type, propertiesMap, properties);
propertyMap = new EObjectPropertyMap(type, propertiesMap, properties);
propertyMap = createPropertyMap(ctxt, type);
if (type != null) {
cache.put(type, propertyMap);
}
}
return propertyMap;
}

private void collectProperties(DatabindContext ctxt, EClass type, Map<String, EObjectProperty> propertiesMap, Set<EObjectProperty> properties) {
final EcoreTypeFactory factory = EMFContext.getTypeFactory(ctxt);
EObjectProperty property;
private void buildCache(DatabindContext ctxt) {
ResourceSet resourceSet = EMFContext.getResourceSet(ctxt);

if (OPTION_USE_ID.enabledIn(features)) {
property = new EObjectIdentityProperty(identityInfo);
properties.add(property);
propertiesMap.put(identityInfo.getProperty(), property);
}
Set<EClass> types = resourceSet.getPackageRegistry().values().stream()
.flatMap(model -> stream(spliteratorUnknownSize(((EPackage) model).eAllContents(), ORDERED), false))
.filter(e -> e instanceof EClass)
.map(e -> (EClass) e)
.collect(Collectors.toSet());

if (OPTION_SERIALIZE_TYPE.enabledIn(features)) {
property = getTypeProperty(type);
properties.add(property);
propertiesMap.put(property.getFieldName(), property);
}
types.forEach(type -> cache.put(type, construct(ctxt, type)));
}

private EObjectPropertyMap createPropertyMap(DatabindContext ctxt, EClass type) {
EcoreTypeFactory factory = EMFContext.getTypeFactory(ctxt);
HashMap<String, EObjectProperty> propertiesMap = new HashMap<>();
Set<EObjectProperty> properties = new LinkedHashSet<>();

property = new EObjectReferenceProperty(referenceInfo);
properties.add(property);
propertiesMap.put(referenceInfo.getProperty(), property);
Consumer<EObjectProperty> add = p -> {
properties.add(p);
propertiesMap.put(p.getFieldName(), p);
};

add.accept(new EObjectReferenceProperty(referenceInfo));
add.accept(getTypeProperty(type, features));

if (OPTION_USE_ID.enabledIn(features)) {
add.accept(new EObjectIdentityProperty(identityInfo));
}

if (type != null) {
for (EStructuralFeature feature : type.getEAllStructuralFeatures()) {
if (isCandidate(feature)) {
JavaType javaType = factory.typeOf(ctxt, type, feature);

if (javaType != null) {
property = new EObjectFeatureProperty(feature, javaType, features);
properties.add(property);
propertiesMap.put(getElementName(feature), property);
createFeatureProperty(ctxt, factory, type, feature).ifPresent(property -> {
add.accept(property);

for (String alias : getAliases(feature)) {
propertiesMap.put(alias, property);
}
for (String alias : getAliases(feature)) {
propertiesMap.put(alias, property);
}
}
});
}

for (EOperation operation : type.getEAllOperations()) {
EAnnotation annotation = operation.getEAnnotation("JsonProperty");

if (annotation != null && operation.getEParameters().isEmpty()) {
property = new EObjectOperationProperty(getElementName(operation), operation);
properties.add(property);
propertiesMap.put(getElementName(operation), property);
add.accept(new EObjectOperationProperty(getElementName(operation), operation));
}
}
}

return new EObjectPropertyMap(type, propertiesMap, properties);
}

private Optional<EObjectFeatureProperty> createFeatureProperty(DatabindContext ctxt, EcoreTypeFactory factory,
EClass type, EStructuralFeature feature) {
if (isCandidate(feature)) {
JavaType javaType = factory.typeOf(ctxt, type, feature);
if (javaType != null) {
return Optional.of(new EObjectFeatureProperty(feature, javaType, features));
}
}

return Optional.empty();
}

boolean isFeatureMapEntry(EStructuralFeature feature) {
Expand Down Expand Up @@ -164,17 +164,18 @@ boolean isCandidate(EReference eReference) {
return !(opposite != null && opposite.isContainment());
}

private EObjectProperty getTypeProperty(EClass type) {
private EObjectProperty getTypeProperty(EClass type, int features) {
EcoreTypeInfo currentTypeInfo = null;

if (type != null && !JsonAnnotations.shouldIgnoreType(type)) {
EcoreTypeInfo currentTypeInfo = JsonAnnotations.getTypeProperty(type);
if (currentTypeInfo != null) {
return new EObjectTypeProperty(currentTypeInfo);
} else {
return new EObjectTypeProperty(typeInfo);
}
} else {
return new EObjectTypeProperty(typeInfo);
currentTypeInfo = JsonAnnotations.getTypeProperty(type);
}

if (currentTypeInfo == null) {
currentTypeInfo = typeInfo;
}

return new EObjectTypeProperty(currentTypeInfo, features);
}

public EObjectPropertyMap constructDefault(DatabindContext ctxt) {
Expand All @@ -185,7 +186,7 @@ public EObjectPropertyMap find(DeserializationContext ctxt, EClass defaultType,
List<EClass> types = EMFContext.allSubTypes(ctxt, defaultType);
Map<String, EClass> properties = new HashMap<>();
for (EClass type : types) {
EObjectProperty p = getTypeProperty(type);
EObjectProperty p = getTypeProperty(type, features);
properties.put(p.getFieldName(), type);
}

Expand All @@ -208,7 +209,7 @@ public EObjectPropertyMap find(DeserializationContext ctxt, EClass defaultType,

private EObjectTypeProperty typeProperty;

EObjectPropertyMap(EClass type, Map<String, EObjectProperty> propertiesMap, Set<EObjectProperty> properties) {
private EObjectPropertyMap(EClass type, Map<String, EObjectProperty> propertiesMap, Set<EObjectProperty> properties) {
this.type = type;
this.propertiesMap = propertiesMap;
this.properties = properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,31 @@

import java.io.IOException;

import static org.emfjson.jackson.module.EMFModule.Feature.OPTION_SERIALIZE_TYPE;

public class EObjectTypeProperty extends EObjectProperty {

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

private final ValueReader<String, EClass> valueReader;
private final ValueWriter<EClass, String> valueWriter;
private final int features;

public EObjectTypeProperty(EcoreTypeInfo info) {
public EObjectTypeProperty(EcoreTypeInfo info, int features) {
super(info.getProperty());

this.valueReader = info.getValueReader();
this.valueWriter = info.getValueWriter();
this.features = features;
}

@Override
public void serialize(EObject bean, JsonGenerator jg, SerializerProvider provider) throws IOException {
if (!OPTION_SERIALIZE_TYPE.enabledIn(features)) {
return;
}

EClass objectType = bean.eClass();
EReference containment = bean.eContainmentFeature();

Expand Down

0 comments on commit 66b8e05

Please sign in to comment.