Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Guava usage (again) #358

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions plugins/org.eclipse.sirius.common/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Bundle-SymbolicName: org.eclipse.sirius.common;singleton:=true
Bundle-Version: 7.4.3.qualifier
Bundle-Vendor: %providerName
Require-Bundle: org.eclipse.sirius.ecore.extender;bundle-version="2.0.0",
org.eclipse.emf.workspace;bundle-version="1.5.1",
com.google.guava;bundle-version="27.0"
org.eclipse.emf.workspace;bundle-version="1.5.1"
Export-Package: org.eclipse.sirius.common.tools;version="3.0.0",
org.eclipse.sirius.common.tools.api.constant;version="2.0.4",
org.eclipse.sirius.common.tools.api.contentassist;version="2.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2011 THALES GLOBAL SERVICES.
* Copyright (c) 2007, 2024 THALES GLOBAL SERVICES and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -13,6 +13,7 @@
package org.eclipse.sirius.common.tools.api.editing;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
Expand All @@ -39,10 +40,6 @@
import org.eclipse.sirius.common.tools.api.resource.FileModificationValidatorProvider;
import org.eclipse.sirius.common.tools.api.resource.IFileModificationValidator;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Iterables;

/**
* ResourceSetListener responsible for asking for file edit validation before
* commits.
Expand All @@ -59,7 +56,6 @@ public class FileStatusPrecommitListener extends ResourceSetListenerImpl {
* Create a new listener.
*/
public FileStatusPrecommitListener() {
super();
fileModificationValidators = FileModificationValidatorProvider.INSTANCE.getFileModificationValidator();
}

Expand All @@ -73,45 +69,22 @@ public boolean isAggregatePrecommitListener() {
return true;
}

/**
* {@inheritDoc}
*/
@Override
public Command transactionAboutToCommit(final ResourceSetChangeEvent event) throws RollbackException {
final boolean defensiveEditValidation = Platform.getPreferencesService().getBoolean("org.eclipse.sirius.common.ui", CommonPreferencesConstants.PREF_DEFENSIVE_EDIT_VALIDATION, true, null); //$NON-NLS-1$
final Command cmd = super.transactionAboutToCommit(event);
boolean defensiveEditValidation = Platform.getPreferencesService().getBoolean("org.eclipse.sirius.common.ui", CommonPreferencesConstants.PREF_DEFENSIVE_EDIT_VALIDATION, true, null); //$NON-NLS-1$
Command cmd = super.transactionAboutToCommit(event);
if (defensiveEditValidation) {
final Set<Resource> changedRes = new LinkedHashSet<>();
if (!event.getTransaction().isReadOnly()) {
for (org.eclipse.emf.common.notify.Notification notif : Iterables.filter(event.getNotifications(), org.eclipse.emf.common.notify.Notification.class)) {
if (notif.getNotifier() instanceof EObject) {
final Resource res = ((EObject) notif.getNotifier()).eResource();
if (resourceChange(res, notif)) {
changedRes.add(res);
}
}
}
}

final BiMap<IFile, Resource> files2Validate = HashBiMap.create();
final Iterator<Resource> it = changedRes.iterator();
while (it.hasNext()) {
final Resource nextResource = it.next();
final IFile file = WorkspaceSynchronizer.getFile(nextResource);
if (file != null && file.isReadOnly()) {
files2Validate.put(file, nextResource);
}
}

if (!files2Validate.isEmpty()) {
Set<Resource> changedRes = getModifiedResources(event);
Set<IFile> filesToValidate = getFilesToValidate(changedRes);
if (!filesToValidate.isEmpty()) {
final RollbackException cancelException = new RollbackException(new Status(IStatus.CANCEL, DslCommonPlugin.PLUGIN_ID, Messages.FileStatusPrecommitListener_fileModificationValidationStatus));
final MultiStatus status = new MultiStatus(DslCommonPlugin.PLUGIN_ID, IStatus.ERROR, Messages.FileStatusPrecommitListener_fileModificationValidationStatus, cancelException);
if (fileModificationValidators.isEmpty()) {
// No extension found, use the default process.
status.add(ResourcesPlugin.getWorkspace().validateEdit(files2Validate.keySet().toArray(new IFile[files2Validate.size()]), IWorkspace.VALIDATE_PROMPT));
status.add(ResourcesPlugin.getWorkspace().validateEdit(filesToValidate.toArray(new IFile[filesToValidate.size()]), IWorkspace.VALIDATE_PROMPT));
} else {
for (final IFileModificationValidator fileModificationValidator : fileModificationValidators) {
final IStatus validationStatus = fileModificationValidator.validateEdit(files2Validate.keySet());
final IStatus validationStatus = fileModificationValidator.validateEdit(filesToValidate);
if (validationStatus != null) {
status.add(validationStatus);
}
Expand All @@ -126,6 +99,34 @@ public Command transactionAboutToCommit(final ResourceSetChangeEvent event) thro
return cmd;
}

private Set<Resource> getModifiedResources(ResourceSetChangeEvent event) {
Set<Resource> changedRes = new LinkedHashSet<>();
if (!event.getTransaction().isReadOnly()) {
for (Notification notif : event.getNotifications()) {
if (notif.getNotifier() instanceof EObject eObject) {
Resource res = eObject.eResource();
if (resourceChange(res, notif)) {
changedRes.add(res);
}
}
}
}
return changedRes;
}

private Set<IFile> getFilesToValidate(Set<Resource> changedRes) {
Set<IFile> filesToValidate = new HashSet<>();
Iterator<Resource> it = changedRes.iterator();
while (it.hasNext()) {
Resource nextResource = it.next();
IFile file = WorkspaceSynchronizer.getFile(nextResource);
if (file != null && file.isReadOnly()) {
filesToValidate.add(file);
}
}
return filesToValidate;
}

/**
* Check if the notification is a resource change.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015, 2017 Obeo.
* Copyright (c) 2015, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -24,8 +24,6 @@
import org.eclipse.sirius.ecore.extender.business.api.accessor.EcoreMetamodelDescriptor;
import org.eclipse.sirius.ecore.extender.business.api.accessor.MetamodelDescriptor;

import com.google.common.collect.Sets;

/**
* Utility methods for working with IInterpreterContexts.
*
Expand Down Expand Up @@ -115,8 +113,8 @@ private static void collectProjectName(Resource eResource, Set<String> projectsO
* otherwise.
*/
public static boolean haveSameScopeDefinition(IInterpreterContext a, IInterpreterContext b) {
Set<String> aDependencies = Sets.newLinkedHashSet(a.getDependencies());
Set<String> bDependencies = Sets.newLinkedHashSet(b.getDependencies());
Set<String> aDependencies = new LinkedHashSet<>(a.getDependencies());
Set<String> bDependencies = new LinkedHashSet<>(b.getDependencies());
Set<String> aNSURI = collectNSUris(a);
Set<String> bNSURI = collectNSUris(b);
Set<String> aProjects = collectProjectsOrPlugins(a);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2013, 2017 Obeo.
* Copyright (c) 2013, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -12,14 +12,13 @@
*******************************************************************************/
package org.eclipse.sirius.common.tools.api.interpreter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand All @@ -46,10 +45,6 @@
import org.eclipse.sirius.common.tools.internal.interpreter.BundleClassLoading;
import org.eclipse.sirius.common.tools.internal.interpreter.ClassLoadingService;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;

/**
* The {@link JavaExtensionsManager} load and maintains {@link Class} instances
* based on the current search scope (of projects and/or plugins) and the
Expand All @@ -75,14 +70,14 @@ public final class JavaExtensionsManager {
*/
private Set<String> viewpointProjects = new LinkedHashSet<>();

private final Set<String> imports = new LinkedHashSet<String>();
private final Set<String> imports = new LinkedHashSet<>();

/**
* These are the imports which are registered as
* "not having been loaded so far", waiting for a change of scope or a
* recompilation which would make them loadable.
*/
private final Set<String> couldNotBeLoaded = new LinkedHashSet<String>();
private final Set<String> couldNotBeLoaded = new LinkedHashSet<>();

private final Map<String, Class<?>> loadedClasses = new LinkedHashMap<>();

Expand All @@ -96,21 +91,17 @@ public final class JavaExtensionsManager {

private boolean shouldLoadEPackages = true;

private ClasspathChangeCallback onWorkspaceChange = new ClasspathChangeCallback() {

@Override
public void classpathChanged(Set<String> updatedProjects) {
/*
* we get a notification if something in the classpath we used so
* far has changed.
*/
if (viewpointPlugins.size() > 0 || viewpointProjects.size() > 0) {
reload();
}
private ClasspathChangeCallback onWorkspaceChange = updatedProjects -> {
/*
* we get a notification if something in the classpath we used so
* far has changed.
*/
if (viewpointPlugins.size() > 0 || viewpointProjects.size() > 0) {
reload();
}
};

private Multimap<String, EPackage> lastDeclarerIDsToEPackages = HashMultimap.create();
private Map<String, List<EPackage>> lastDeclarerIDsToEPackages = new HashMap<>();

/**
* through this field we keep track fo the EPackage declarers which were
Expand Down Expand Up @@ -250,7 +241,7 @@ public synchronized void reloadIfNeeded() {
}

private void reloadEPackages() {
Multimap<String, EPackage> newDeclarations = HashMultimap.create();
Map<String, List<EPackage>> newDeclarations = new HashMap<>();
Set<String> newDeclarersAsBundles = new LinkedHashSet<>();
Collection<EPackageDeclarationSource> ecoreDeclarationSources = this.classLoading.findEcoreDeclarations(this.viewpointProjects, this.viewpointPlugins);
Collection<EPackageDeclarationSource> workspaceDeclarations = new ArrayList<>();
Expand All @@ -265,10 +256,10 @@ private void reloadEPackages() {
*/
EPackage pak = EPackage.Registry.INSTANCE.getEPackage(ePackageDeclaration.getNsURI());
if (pak != null) {
newDeclarations.put(declarer.getSymbolicName(), pak);
newDeclarations.putIfAbsent(declarer.getSymbolicName(), new ArrayList<>());
newDeclarations.get(declarer.getSymbolicName()).add(pak);
}
}

} else {
/*
* we keep that for later as we need to initialize a specific
Expand All @@ -278,17 +269,15 @@ private void reloadEPackages() {
workspaceDeclarations.add(declarer);
}
}
if (workspaceDeclarations.size() > 0) {
if (!workspaceDeclarations.isEmpty()) {
/*
* this resourceset is being used to load the genmodel instances
* from the workspace. It is setup with uri mappings so that other
* Ecore residing in the workspace are shadowing the ones from the
* targetplatform.
*/
ResourceSetImpl set = new ResourceSetImpl();

computePlatformURIMap(set);

/*
* the EPackage definition comes from a workspace project, right now
* we don't explicitely and fully support this use case where the
Expand All @@ -312,31 +301,35 @@ private void reloadEPackages() {
if (!StringUtil.isEmpty(nsURI)) {
EPackage loaded = ecorePackages.get(nsURI);
if (loaded != null) {
newDeclarations.put(nsURI, loaded);
newDeclarations.putIfAbsent(nsURI, new ArrayList<>());
newDeclarations.get(nsURI).add(loaded);
}
}
}
}

}

/*
* cleaning up previously registered EPackage which are not accessible
* any more.
*/
boolean firstRun = lastDeclarerIDsInBundles == null;
if (!firstRun) {
for (Entry<String, EPackage> entry : lastDeclarerIDsToEPackages.entries()) {
boolean changedType = lastDeclarerIDsInBundles.contains(entry.getKey()) != newDeclarersAsBundles.contains(entry.getKey());
if (changedType) {
unloadedEPackage(entry.getValue());
for (Entry<String, List<EPackage>> entry : lastDeclarerIDsToEPackages.entrySet()) {
for (EPackage ePackage : entry.getValue()) {
boolean changedType = lastDeclarerIDsInBundles.contains(entry.getKey()) != newDeclarersAsBundles.contains(entry.getKey());
if (changedType) {
unloadedEPackage(ePackage);
}
}
}
}
for (Entry<String, EPackage> entry : newDeclarations.entries()) {
boolean changedType = firstRun || lastDeclarerIDsInBundles.contains(entry.getKey()) != newDeclarersAsBundles.contains(entry.getKey());
if (changedType) {
loadedEPackage(entry.getValue());
for (Entry<String, List<EPackage>> entry : newDeclarations.entrySet()) {
for (EPackage ePackage : entry.getValue()) {
boolean changedType = firstRun || lastDeclarerIDsInBundles.contains(entry.getKey()) != newDeclarersAsBundles.contains(entry.getKey());
if (changedType) {
loadedEPackage(ePackage);
}
}
}

Expand All @@ -346,37 +339,9 @@ private void reloadEPackages() {
}

private void computePlatformURIMap(ResourceSetImpl set) {
Map<URI, URI> result = null;
/*
* We invoke computePlatformURIMap by reflection to keep being
* compatible with EMF 2.8 and still leverage the new capabilities
* regarding target platforms introduced in EMF 2.9.
*/
try {
Method computePlatformURIMap = EcorePlugin.class.getMethod("computePlatformURIMap", Boolean.TYPE); //$NON-NLS-1$
result = (Map<URI, URI>) computePlatformURIMap.invoke(null, true);
} catch (NoSuchMethodException e) {
/*
* result is still null, we'll call the old method.
*/
} catch (IllegalAccessException e) {
/*
* result is still null, we'll call the old method.
*/
} catch (IllegalArgumentException e) {
/*
* result is still null, we'll call the old method.
*/
} catch (InvocationTargetException e) {
/*
* result is still null, we'll call the old method.
*/
}
if (result == null) {
result = EcorePlugin.computePlatformURIMap();
}
if (result != null) {
set.getURIConverter().getURIMap().putAll(result);
Map<URI, URI> uriMap = EcorePlugin.computePlatformURIMap(true);
if (uriMap != null) {
set.getURIConverter().getURIMap().putAll(uriMap);
}
}

Expand Down Expand Up @@ -489,7 +454,7 @@ public synchronized void removeImport(String classQualifiedName) {
* @return the current list of class qualified name used as Java Extensions.
*/
public synchronized Collection<String> getImports() {
return ImmutableList.copyOf(this.imports);
return List.copyOf(this.imports);
}

/**
Expand Down
Loading