diff --git a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectDeserializer.java b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectDeserializer.java index aad6f77..8a3e413 100644 --- a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectDeserializer.java +++ b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectDeserializer.java @@ -139,6 +139,8 @@ public class GsonEObjectDeserializer implements JsonDeserializer> private IJsonResourceProcessor jsonResourceProcessor; + private final Map typeCache = new HashMap<>(); + /** * The constructor. * @@ -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 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 = 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 = 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; } /**