Skip to content

Commit

Permalink
[38] Cache resolved types during deserialization
Browse files Browse the repository at this point in the history
Bug: #38
Signed-off-by: Pierre-Charles David <[email protected]>
  • Loading branch information
pcdavid authored and sbegaudeau committed Jun 5, 2024
1 parent d735322 commit 785ad7f
Showing 1 changed file with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public class GsonEObjectDeserializer implements JsonDeserializer<List<EObject>>

private IJsonResourceProcessor jsonResourceProcessor;

private final Map<String, EClass> typeCache = new HashMap<>();

/**
* The constructor.
*
Expand Down Expand Up @@ -579,44 +581,59 @@ private EObject createProxyEObject(String id, String qualifiedType, EReference e
EObject proxyEObject = null;

URI toResolveURI = URI.createURI(id);

if (qualifiedType != null) {
EClass eType = this.typeCache.computeIfAbsent(qualifiedType, this::resolveType);
if (eType != null) {
proxyEObject = eType.getEPackage().getEFactoryInstance().create(eType);
URI resolvedURI = toResolveURI.resolve(this.resourceURI);
((InternalEObject) proxyEObject).eSetProxyURI(resolvedURI);
}
} else {
EClass eType = eReference.getEReferenceType();
if (!eType.isAbstract()) {
proxyEObject = eType.getEPackage().getEFactoryInstance().create(eType);
if (proxyEObject != null) {
URI resolvedURI = toResolveURI.resolve(this.resourceURI);
((InternalEObject) proxyEObject).eSetProxyURI(resolvedURI);
}
}
}
return proxyEObject;
}

/**
* Resolves a type from a qualified name (e.g. "flow:System") into the corresponding EClass (or null) using the
* resource set's package registry.
*
* @param qualifiedType
* the qualified name of the type to resolve.
* @return the corresponding EClass, or null.
*/
private EClass resolveType(String qualifiedType) {
if (qualifiedType != null) {
String[] splitType = qualifiedType.split(":"); //$NON-NLS-1$
if (splitType.length == 2) {
String packageName = splitType[0];
String eClassName = splitType[1];
// @formatter:off
Optional<EPackage> packageOpt = this.resourceSet.getPackageRegistry().values().stream()
.filter(EPackage.class::isInstance)
.map(EPackage.class::cast)
.filter(pkg-> pkg.getName().equals(packageName))
.filter(pkg -> pkg.getName().equals(packageName))
.findFirst();
if (packageOpt.isPresent()) {
Optional<EObject> eObject = packageOpt.get().getEClassifiers().stream()
.filter(clazz-> eClassName.equals(clazz.getName()))
.filter(EClass.class::isInstance)
.map(EClass.class::cast)
.map(eClass -> eClass.getEPackage().getEFactoryInstance().create(eClass))
.findFirst();
if (eObject.isPresent()) {
proxyEObject = eObject.get();
URI resolvedURI = toResolveURI;
resolvedURI = toResolveURI.resolve(this.resourceURI);
((InternalEObject) proxyEObject).eSetProxyURI(resolvedURI);
Optional<EClass> eClass = packageOpt.get().getEClassifiers().stream()
.filter(clazz -> eClassName.equals(clazz.getName()))
.filter(EClass.class::isInstance)
.map(EClass.class::cast)
.findFirst();
if (eClass.isPresent()) {
return eClass.get();
}
// @formatter:on
}
}
} else {
EClass eType = eReference.getEReferenceType();
if (!eType.isAbstract()) {
proxyEObject = eType.getEPackage().getEFactoryInstance().create(eType);
if (proxyEObject != null) {
URI resolvedURI = toResolveURI.resolve(this.resourceURI);
((InternalEObject) proxyEObject).eSetProxyURI(resolvedURI);
}
}
}
return proxyEObject;
return null;
}

/**
Expand Down

0 comments on commit 785ad7f

Please sign in to comment.