Skip to content

Commit

Permalink
[TP] Remove declarations of Exception that are never thrown and clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Oct 12, 2024
1 parent d2f62b7 commit 848ecf9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ protected TargetBundle[] resolveBundles(ITargetDefinition definition, IProgressM
private void cacheIUs() throws CoreException {
IProfile profile = fSynchronizer.getProfile();
List<IInstallableUnit> result = new ArrayList<>();
MultiStatus status = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories, null);
MultiStatus status = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories);
for (IVersionedId unit : fIUs) {
IQuery<IInstallableUnit> query = QueryUtil.createIUQuery(unit);
addQueryResult(profile, query, unit, result, status);
Expand Down Expand Up @@ -571,14 +571,13 @@ public boolean isFollowRepositoryReferences() {
* Returns the installable units defined by this container
*
* @return the discovered IUs
* @exception CoreException if unable to retrieve IU's
*/
public List<IInstallableUnit> getInstallableUnits() throws CoreException {
public List<IInstallableUnit> getInstallableUnits() {
return fUnits;
}

/** Returns the declared installable unit identifiers and versions. */
Collection<IVersionedId> getUnits() {
Collection<IVersionedId> getDeclaredUnits() {
return Collections.unmodifiableSet(fIUs);
}

Expand Down Expand Up @@ -682,10 +681,10 @@ public String serialize() {
}
}

IInstallableUnit[] getRootIUs(IProgressMonitor monitor) throws CoreException {
Collection<IInstallableUnit> getRootIUs(IProgressMonitor monitor) throws CoreException {
IQueryable<IInstallableUnit> repos = P2TargetUtils.getQueryableMetadata(getRepositories(),
isFollowRepositoryReferences(), monitor);
MultiStatus status = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories, null);
MultiStatus status = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories);
List<IInstallableUnit> result = new ArrayList<>();
for (IVersionedId iu : fIUs) {
// For versions such as 0.0.0, the IU query may return multiple IUs, so we check which is the latest version
Expand All @@ -696,7 +695,7 @@ IInstallableUnit[] getRootIUs(IProgressMonitor monitor) throws CoreException {
fResolutionStatus = status;
throw new CoreException(status);
}
return result.toArray(new IInstallableUnit[0]);
return result;
}

private void addQueryResult(IQueryable<IInstallableUnit> queryable, IQuery<IInstallableUnit> query, IVersionedId iu,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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.LinkedHashSet;
Expand Down Expand Up @@ -503,25 +502,20 @@ private boolean checkProfile(ITargetDefinition target, final IProfile profile) {
return false;
}
}

// ensure environment & NL settings are still the same (else we need a new profile)
if (!all && !generateEnvironmentProperties(target).equals(profile.getProperty(IProfile.PROP_ENVIRONMENTS))) {
return false;
}
if (!generateNLProperty(target).equals(profile.getProperty(IProfile.PROP_NL))) {
return false;
}

// check provisioning mode: slice versus plan
if (!getProvisionMode().equals(profile.getProperty(PROP_PROVISION_MODE))) {
return false;
}

// check that the include source flag matches what the profile represents
if (getIncludeSource() != Boolean.parseBoolean(profile.getProperty(PROP_AUTO_INCLUDE_SOURCE))) {
return false;
}

if (getIncludeConfigurePhase() != Boolean.parseBoolean(profile.getProperty(PROP_INCLUDE_CONFIGURE_PHASE))) {
return false;
}
Expand All @@ -530,17 +524,17 @@ private boolean checkProfile(ITargetDefinition target, final IProfile profile) {
// still in the profile, we need to recreate (rather than uninstall)
IUProfilePropertyQuery propertyQuery = new IUProfilePropertyQuery(PROP_INSTALLED_IU, Boolean.toString(true));
IQueryResult<IInstallableUnit> queryResult = profile.query(propertyQuery, null);
ITargetLocation[] containers = target.getTargetLocations();
if (containers == null) {
return queryResult.isEmpty();
}
Set<NameVersionDescriptor> installedIUs = new HashSet<>();
for (IInstallableUnit unit : queryResult) {
installedIUs.add(new NameVersionDescriptor(unit.getId(), unit.getVersion().toString()));
}
ITargetLocation[] containers = target.getTargetLocations();
if (containers == null) {
return installedIUs.isEmpty();
}
for (ITargetLocation container : containers) {
if (container instanceof IUBundleContainer bc) {
for (IVersionedId iu : bc.getUnits()) {
for (IVersionedId iu : bc.getDeclaredUnits()) {
// if there is something in a container but not in the profile, recreate
if (!installedIUs.remove(new NameVersionDescriptor(iu.getId(), iu.getVersion().toString()))) {
return false;
Expand Down Expand Up @@ -951,7 +945,7 @@ static IQueryable<IInstallableUnit> getQueryableMetadata(Collection<URI> repos,
Set<IRepositoryReference> seen = new HashSet<>();
List<IMetadataRepository> result = new ArrayList<>(repos.size());
List<IMetadataRepository> additional = new ArrayList<>();
MultiStatus repoStatus = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories, null);
MultiStatus repoStatus = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories);
for (URI location : repos) {
try {
IMetadataRepository repository = manager.loadRepository(location, subMonitor.split(1));
Expand Down Expand Up @@ -1007,15 +1001,15 @@ private void resolveWithPlanner(ITargetDefinition target, IProfile profile, IPro
SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.IUBundleContainer_0, 220);

// Get the root IUs for every relevant container in the target definition
IInstallableUnit[] units = getRootIUs(target, subMonitor.split(20));
Set<IInstallableUnit> units = getRootIUs(target, subMonitor.split(20));

// create the provisioning plan
IPlanner planner = getPlanner();
IProfileChangeRequest request = planner.createChangeRequest(profile);
// first remove everything that was explicitly installed. Then add it back. This has the net effect of
// removing everything that is no longer needed.
computeRemovals(profile, request, getIncludeSource());
request.addAll(Arrays.asList(units));
request.addAll(units);
for (IInstallableUnit unit : units) {
request.setInstallableUnitProfileProperty(unit, PROP_INSTALLED_IU, Boolean.toString(true));
}
Expand Down Expand Up @@ -1223,7 +1217,7 @@ private void resolveWithSlicer(ITargetDefinition target, IProfile profile, IProg
SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.IUBundleContainer_0, 110);

// resolve IUs
IInstallableUnit[] units = getRootIUs(target, subMonitor.split(40));
Set<IInstallableUnit> units = getRootIUs(target, subMonitor.split(40));

Collection<URI> repositories = getMetadataRepositories(target);
if (repositories.isEmpty()) {
Expand All @@ -1243,9 +1237,8 @@ private void resolveWithSlicer(ITargetDefinition target, IProfile profile, IProg
if (getIncludeSource()) {
// Build an IU that represents all the source bundles and slice again to add them in if available
IInstallableUnit sourceIU = createSourceIU(queryResult, Version.createOSGi(1, 0, 0));
IInstallableUnit[] units2 = new IInstallableUnit[units.length + 1];
System.arraycopy(units, 0, units2, 0, units.length);
units2[units.length] = sourceIU;
List<IInstallableUnit> units2 = new ArrayList<>(units);
units2.add(sourceIU);

queryResult = slice(units2, allMetadata, target, subMonitor.split(5));
if (queryResult == null || queryResult.isEmpty()) {
Expand Down Expand Up @@ -1298,7 +1291,9 @@ private void resolveWithSlicer(ITargetDefinition target, IProfile profile, IProg
* @return the result of the slice operation
* @throws CoreException if a problem occurs during the slice operation that should stop this location from resolving
*/
private IQueryResult<IInstallableUnit> slice(IInstallableUnit[] units, IQueryable<IInstallableUnit> allMetadata, ITargetDefinition definition, IProgressMonitor monitor) throws CoreException {
private IQueryResult<IInstallableUnit> slice(Collection<IInstallableUnit> units,
IQueryable<IInstallableUnit> allMetadata, ITargetDefinition definition, IProgressMonitor monitor)
throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
// slice IUs and all prerequisites
PermissiveSlicer slicer = null;
Expand All @@ -1313,7 +1308,7 @@ private IQueryResult<IInstallableUnit> slice(IInstallableUnit[] units, IQueryabl
props.put(IProfile.PROP_INSTALL_FEATURES, Boolean.TRUE.toString());
slicer = new PermissiveSlicer(allMetadata, props, true, false, false, true, false);
}
IQueryable<IInstallableUnit> slice = slicer.slice(Arrays.asList(units), subMonitor.split(50));
IQueryable<IInstallableUnit> slice = slicer.slice(units, subMonitor.split(50));
IStatus sliceStatus = slicer.getStatus();
// If the slicer encounters an error, stop the operation
if (sliceStatus.getSeverity() == IStatus.ERROR) {
Expand Down Expand Up @@ -1454,19 +1449,20 @@ private void findProfileRepos(Set<URI> additionalRepos) {
* @return the discovered IUs
* @exception CoreException if unable to retrieve IU's
*/
private IInstallableUnit[] getRootIUs(ITargetDefinition definition, IProgressMonitor monitor) throws CoreException {
private Set<IInstallableUnit> getRootIUs(ITargetDefinition definition, IProgressMonitor monitor)
throws CoreException {

HashSet<IInstallableUnit> result = new HashSet<>();
ITargetLocation[] containers = definition.getTargetLocations();
if (containers == null) {
return new IInstallableUnit[0];
return Set.of();
}
SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.IUBundleContainer_0, containers.length * 10);
MultiStatus status = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories, null);
SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.IUBundleContainer_0, containers.length);
MultiStatus status = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories);
Set<IInstallableUnit> result = new HashSet<>();
for (ITargetLocation container : containers) {
if (container instanceof IUBundleContainer iuContainer) {
try {
Collections.addAll(result, iuContainer.getRootIUs(subMonitor.split(10)));
result.addAll(iuContainer.getRootIUs(subMonitor.split(1)));
} catch (CoreException e) {
status.add(e.getStatus());
}
Expand All @@ -1475,7 +1471,7 @@ private IInstallableUnit[] getRootIUs(ITargetDefinition definition, IProgressMon
if (!status.isOK()) {
throw new CoreException(status);
}
return result.toArray(new IInstallableUnit[result.size()]);
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -372,7 +372,7 @@ public IStatus resolve(IProgressMonitor monitor) {
fResolutionStatus = null;
SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.TargetDefinition_1, targetLocations.length * 100);
try {
MultiStatus status = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.TargetDefinition_2, null);
MultiStatus status = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.TargetDefinition_2);
Map<P2TargetUtils, List<ITargetLocation>> synchronizers = new HashMap<>();
// clear all previous maps
P2TargetUtils.fgTargetArtifactRepo.clear();
Expand All @@ -396,17 +396,17 @@ public IStatus resolve(IProgressMonitor monitor) {
List<ITargetLocation> delayedLocations = synchronizers.values().stream().flatMap(Collection::stream)
.toList();
subMonitor.setWorkRemaining(synchronizers.size() * 100 + delayedLocations.size());
for (Entry<P2TargetUtils, List<ITargetLocation>> entry : synchronizers.entrySet()) {
synchronizers.forEach((synchronizer, locations) -> {
subMonitor.checkCanceled();
try {
entry.getKey().synchronize(this, subMonitor.split(100));
entry.getValue().stream().map(loc -> loc.getStatus()).filter(Objects::nonNull)
.filter(s -> !s.isOK()).forEach(status::add);
synchronizer.synchronize(this, subMonitor.split(100));
locations.stream().map(ITargetLocation::getStatus).filter(s -> s != null && !s.isOK())
.forEach(status::add);
} catch (CoreException e) {
PDECore.log(e.getStatus());
status.add(e.getStatus());
}
}
});
for (ITargetLocation location : delayedLocations) {
subMonitor.checkCanceled();
IStatus s = location.resolve(this, subMonitor.split(1));
Expand Down Expand Up @@ -450,7 +450,7 @@ public IStatus getStatus() {
ITargetLocation[] containers = getTargetLocations();
if (containers != null) {
// Check if the containers have any resolution problems
MultiStatus result = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.TargetDefinition_5, null);
MultiStatus result = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.TargetDefinition_5);
for (ITargetLocation container : containers) {
IStatus containerStatus = container.getStatus();
if (containerStatus != null && !containerStatus.isOK()) {
Expand Down Expand Up @@ -827,7 +827,7 @@ public void setImplicitDependencies(NameVersionDescriptor[] bundles) {
}
fImplicit = bundles;
if (fRoot != null && bundles != null && bundles.length > 0) {
Element implicitDependenciesElement = TargetDefinitionDocumentTools.getChildElement(fRoot,
Element implicitDependencies = TargetDefinitionDocumentTools.getChildElement(fRoot,
TargetDefinitionPersistenceHelper.IMPLICIT);
List<Element> descriptorElements = new ArrayList<>();
for (NameVersionDescriptor descriptor : bundles) {
Expand All @@ -839,9 +839,7 @@ public void setImplicitDependencies(NameVersionDescriptor[] bundles) {
descriptorElements.add(plugin);
}

TargetDefinitionDocumentTools.updateElements(implicitDependenciesElement, null, descriptorElements,
(Element o1, Element o2) -> o1.getAttribute(TargetDefinitionPersistenceHelper.ATTR_ID)
.compareTo(o2.getAttribute(TargetDefinitionPersistenceHelper.ATTR_ID)));
TargetDefinitionDocumentTools.updateElements(implicitDependencies, null, descriptorElements, BY_ATTR_ID);
} else {
removeElement(TargetDefinitionPersistenceHelper.IMPLICIT);
}
Expand Down Expand Up @@ -1205,10 +1203,9 @@ private void serializeBundleContainers(ITargetLocation[] targetLocations, Elemen

NodeList nodes = containersElement.getChildNodes();
for (int j = 0; j < nodes.getLength(); j++) {
Node node = nodes.item(j);
if (node instanceof Element element
&& node.getNodeName().equalsIgnoreCase(TargetDefinitionPersistenceHelper.LOCATION)) {
String type = (element).getAttribute(TargetDefinitionPersistenceHelper.ATTR_LOCATION_TYPE);
if (nodes.item(j) instanceof Element element
&& element.getNodeName().equalsIgnoreCase(TargetDefinitionPersistenceHelper.LOCATION)) {
String type = element.getAttribute(TargetDefinitionPersistenceHelper.ATTR_LOCATION_TYPE);
switch (type) {
case IUBundleContainer.TYPE:
oldIUContainers.add(element);
Expand Down Expand Up @@ -1252,13 +1249,15 @@ private void updateContainerElements(Element containersElement, List<Element> ol
.compareTo(o2.getAttribute(TargetDefinitionPersistenceHelper.ATTR_LOCATION_PATH));
int idCompare = 0;
if (o1 instanceof FeatureBundleContainer) {
idCompare = o1.getAttribute(TargetDefinitionPersistenceHelper.ATTR_ID)
.compareTo(o2.getAttribute(TargetDefinitionPersistenceHelper.ATTR_ID));
idCompare = BY_ATTR_ID.compare(o1, o2);
}
return typeCompare == 0 && pathCompare == 0 && idCompare == 0 ? 0 : 1;
});
}

private static final Comparator<Element> BY_ATTR_ID = Comparator
.comparing(o -> o.getAttribute(TargetDefinitionPersistenceHelper.ATTR_ID));

private void updateIUContainerElements(Element containersElement, List<Element> oldContainers,
List<Element> newContainers) {
Map<String, List<Element>> oldContainersByRepo = new HashMap<>();
Expand All @@ -1268,18 +1267,15 @@ private void updateIUContainerElements(Element containersElement, List<Element>
List<Element> units = new ArrayList<>();
String repoURL = null;
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element) {
if (nodes.item(i) instanceof Element element) {
if (repoURL == null
&& node.getNodeName().equalsIgnoreCase(TargetDefinitionPersistenceHelper.REPOSITORY)) {
repoURL = ((Element) node).getAttribute(TargetDefinitionPersistenceHelper.LOCATION);
if (!oldContainersByRepo.containsKey(repoURL)) {
oldContainersByRepo.put(repoURL, new ArrayList<>());
}
oldContainersByRepo.get(repoURL).add(container);
} else if (node.getNodeName()
&& element.getNodeName().equalsIgnoreCase(TargetDefinitionPersistenceHelper.REPOSITORY)) {
repoURL = element.getAttribute(TargetDefinitionPersistenceHelper.LOCATION);

oldContainersByRepo.computeIfAbsent(repoURL, u -> new ArrayList<>()).add(container);
} else if (element.getNodeName()
.equalsIgnoreCase(TargetDefinitionPersistenceHelper.INSTALLABLE_UNIT)) {
units.add((Element) node);
units.add(element);
}
}
}
Expand All @@ -1295,24 +1291,21 @@ private void updateIUContainerElements(Element containersElement, List<Element>
List<Element> units = new ArrayList<>();
String repoURL = null;
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element) {
if (nodes.item(i) instanceof Element element) {
if (repoURL == null
&& node.getNodeName().equalsIgnoreCase(TargetDefinitionPersistenceHelper.REPOSITORY)) {
repoURL = ((Element) node).getAttribute(TargetDefinitionPersistenceHelper.LOCATION);
} else if (node.getNodeName()
&& element.getNodeName().equalsIgnoreCase(TargetDefinitionPersistenceHelper.REPOSITORY)) {
repoURL = element.getAttribute(TargetDefinitionPersistenceHelper.LOCATION);
} else if (element.getNodeName()
.equalsIgnoreCase(TargetDefinitionPersistenceHelper.INSTALLABLE_UNIT)) {
units.add((Element) node);
units.add(element);
}
}
}
if (repoURL != null) {
if (oldContainersByRepo.containsKey(repoURL)) {
Element oldContainer = oldContainersByRepo.get(repoURL).get(0);
TargetDefinitionDocumentTools.updateElements(oldContainer, oldUnitsByContainer.get(oldContainer),
units,
(Element o1, Element o2) -> o1.getAttribute(TargetDefinitionPersistenceHelper.ATTR_ID)
.compareTo(o2.getAttribute(TargetDefinitionPersistenceHelper.ATTR_ID)));
units, BY_ATTR_ID);
if (oldContainersByRepo.get(repoURL).size() == 1) {
oldContainersByRepo.remove(repoURL);
} else {
Expand All @@ -1325,8 +1318,8 @@ private void updateIUContainerElements(Element containersElement, List<Element>
}
}

for (Entry<String, List<Element>> entry : oldContainersByRepo.entrySet()) {
entry.getValue().forEach(TargetDefinitionDocumentTools::removeChildAndWhitespace);
for (List<Element> containers : oldContainersByRepo.values()) {
containers.forEach(TargetDefinitionDocumentTools::removeChildAndWhitespace);
}
}

Expand All @@ -1351,8 +1344,6 @@ private void serializeBundles(Element parent, NameVersionDescriptor[] bundles) {
bundlElements.add(includedBundle);
}
}
TargetDefinitionDocumentTools.updateElements(parent, null, bundlElements,
(Element o1, Element o2) -> o1.getAttribute(TargetDefinitionPersistenceHelper.ATTR_ID)
.compareTo(o2.getAttribute(TargetDefinitionPersistenceHelper.ATTR_ID)));
}
TargetDefinitionDocumentTools.updateElements(parent, null, bundlElements, BY_ATTR_ID);
}
}
Loading

0 comments on commit 848ecf9

Please sign in to comment.