@Test public void testLoadModelCastToSupertype() { String resourceURI = toFullPath("Test1.ecore"); Optional<EObject> result = resourceManager.loadModel(resourceURI, resourceSet, EObject.class); assertNotNull(result); assertTrue(result.isPresent()); }
public <T extends EObject> Optional<T> loadModel(final URI resourceURI, final ResourceSet rs, final Class<T> clazz) { Optional<Resource> res = loadResource(resourceURI, rs); if (res.isPresent() && !res.get().getContents().isEmpty()) { EObject root = res.get().getContents().get(0); if (clazz.isInstance(root)) { return Optional.of(clazz.cast(root)); } LOG.info(String.format("Root element of resource \"%s\" is not an instance of %s", resourceURI, clazz)); } LOG.info("Resource appears to be empty: " + resourceURI); return Optional.empty(); }
ResourceManager { public <T extends EObject> Optional<T> loadModel(final URI resourceURI, final ResourceSet rs, final Class<T> clazz) { Optional<Resource> res = loadResource(resourceURI, rs); if (res.isPresent() && !res.get().getContents().isEmpty()) { EObject root = res.get().getContents().get(0); if (clazz.isInstance(root)) { return Optional.of(clazz.cast(root)); } LOG.info(String.format("Root element of resource \"%s\" is not an instance of %s", resourceURI, clazz)); } LOG.info("Resource appears to be empty: " + resourceURI); return Optional.empty(); } }
ResourceManager { public <T extends EObject> Optional<T> loadModel(final URI resourceURI, final ResourceSet rs, final Class<T> clazz) { Optional<Resource> res = loadResource(resourceURI, rs); if (res.isPresent() && !res.get().getContents().isEmpty()) { EObject root = res.get().getContents().get(0); if (clazz.isInstance(root)) { return Optional.of(clazz.cast(root)); } LOG.info(String.format("Root element of resource \"%s\" is not an instance of %s", resourceURI, clazz)); } LOG.info("Resource appears to be empty: " + resourceURI); return Optional.empty(); } @Inject  ResourceManager(final Set<EPackageConfiguration> configurations); }
ResourceManager { public <T extends EObject> Optional<T> loadModel(final URI resourceURI, final ResourceSet rs, final Class<T> clazz) { Optional<Resource> res = loadResource(resourceURI, rs); if (res.isPresent() && !res.get().getContents().isEmpty()) { EObject root = res.get().getContents().get(0); if (clazz.isInstance(root)) { return Optional.of(clazz.cast(root)); } LOG.info(String.format("Root element of resource \"%s\" is not an instance of %s", resourceURI, clazz)); } LOG.info("Resource appears to be empty: " + resourceURI); return Optional.empty(); } @Inject  ResourceManager(final Set<EPackageConfiguration> configurations); void initialize(); @SuppressWarnings("checkstyle:IllegalCatch") Optional<Resource> loadResource(final URI resourceURI, final ResourceSet rs); Optional<Resource> loadResource(final String resourceURI, final ResourceSet rs); Optional<T> loadModel(final URI resourceURI, final ResourceSet rs, final Class<T> clazz); Optional<T> loadModel(final String resourceURI, final ResourceSet rs,
      final Class<T> clazz); boolean save(final ResourceSet rs); }
ResourceManager { public <T extends EObject> Optional<T> loadModel(final URI resourceURI, final ResourceSet rs, final Class<T> clazz) { Optional<Resource> res = loadResource(resourceURI, rs); if (res.isPresent() && !res.get().getContents().isEmpty()) { EObject root = res.get().getContents().get(0); if (clazz.isInstance(root)) { return Optional.of(clazz.cast(root)); } LOG.info(String.format("Root element of resource \"%s\" is not an instance of %s", resourceURI, clazz)); } LOG.info("Resource appears to be empty: " + resourceURI); return Optional.empty(); } @Inject  ResourceManager(final Set<EPackageConfiguration> configurations); void initialize(); @SuppressWarnings("checkstyle:IllegalCatch") Optional<Resource> loadResource(final URI resourceURI, final ResourceSet rs); Optional<Resource> loadResource(final String resourceURI, final ResourceSet rs); Optional<T> loadModel(final URI resourceURI, final ResourceSet rs, final Class<T> clazz); Optional<T> loadModel(final String resourceURI, final ResourceSet rs,
      final Class<T> clazz); boolean save(final ResourceSet rs);  }