From c638e2ad6916e54686a77c7e4adf081fa9717f9a Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sat, 23 Apr 2022 18:56:14 +0200 Subject: [PATCH] Use specific P2 types --- .../tycho/p2/impl/test/ArtifactMock.java | 7 +++--- .../p2/publisher/P2GeneratorImplTest.java | 5 ++-- .../p2/impl/publisher/DependencyMetadata.java | 4 ++-- .../p2/impl/publisher/P2GeneratorImpl.java | 4 +--- .../tycho/p2/resolver/P2ResolverImpl.java | 24 +++++++++---------- .../publisher/PublishProductToolTest.java | 7 +++--- .../tools/publisher/PublisherServiceTest.java | 2 +- .../core/osgitools/DefaultReactorProject.java | 8 +++---- .../p2/extras/CompareWithBaselineMojo.java | 12 ++++------ .../p2/resolver/P2DependencyResolver.java | 24 +++++++------------ .../tycho/plugins/p2/P2MetadataMojo.java | 7 ++---- 11 files changed, 43 insertions(+), 61 deletions(-) diff --git a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl.test/src/test/java/org/eclipse/tycho/p2/impl/test/ArtifactMock.java b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl.test/src/test/java/org/eclipse/tycho/p2/impl/test/ArtifactMock.java index bbc7c620dc..8930968f53 100644 --- a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl.test/src/test/java/org/eclipse/tycho/p2/impl/test/ArtifactMock.java +++ b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl.test/src/test/java/org/eclipse/tycho/p2/impl/test/ArtifactMock.java @@ -16,6 +16,7 @@ import java.util.LinkedHashSet; import java.util.Set; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.tycho.IDependencyMetadata; import org.eclipse.tycho.IDependencyMetadata.DependencyMetadataType; import org.eclipse.tycho.p2.metadata.IArtifactFacade; @@ -33,9 +34,9 @@ public class ArtifactMock implements IArtifactFacade { private String classifier; - private Set dependencyMetadata = new LinkedHashSet<>(); + private Set dependencyMetadata = new LinkedHashSet<>(); - private Set secondaryDependencyMetadata = new LinkedHashSet<>(); + private Set secondaryDependencyMetadata = new LinkedHashSet<>(); public ArtifactMock(File location, String groupId, String artifactId, String version, String packagingType, String classifier) { @@ -90,7 +91,7 @@ public void setClassifier(String classifier) { this.classifier = classifier; } - public Set getDependencyMetadata(boolean primary) { + public Set getDependencyMetadata(boolean primary) { return primary ? dependencyMetadata : secondaryDependencyMetadata; } diff --git a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl.test/src/test/java/org/eclipse/tycho/p2/publisher/P2GeneratorImplTest.java b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl.test/src/test/java/org/eclipse/tycho/p2/publisher/P2GeneratorImplTest.java index 5970c82551..1a52d6cc11 100644 --- a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl.test/src/test/java/org/eclipse/tycho/p2/publisher/P2GeneratorImplTest.java +++ b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl.test/src/test/java/org/eclipse/tycho/p2/publisher/P2GeneratorImplTest.java @@ -95,9 +95,8 @@ public void generateSourceBundleMetadataForProjectWithP2Inf() throws Exception { assertEquals(0, unit.getRequirements().size()); } - private IInstallableUnit getUnit(String id, Set units) { - for (Object obj : units) { - IInstallableUnit unit = (IInstallableUnit) obj; + private IInstallableUnit getUnit(String id, Set units) { + for (IInstallableUnit unit : units) { if (id.equals(unit.getId())) { return unit; } diff --git a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/impl/publisher/DependencyMetadata.java b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/impl/publisher/DependencyMetadata.java index 8e589017bd..8a3c2d288f 100644 --- a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/impl/publisher/DependencyMetadata.java +++ b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/impl/publisher/DependencyMetadata.java @@ -36,14 +36,14 @@ public Set getDependencyMetadata(DependencyMetadataType type) @Override public Set getDependencyMetadata() { - LinkedHashSet result = new LinkedHashSet<>(); + Set result = new LinkedHashSet<>(); result.addAll(getDependencyMetadata(DependencyMetadataType.SEED)); result.addAll(getDependencyMetadata(DependencyMetadataType.RESOLVE)); return result; } + @Override public void setDependencyMetadata(DependencyMetadataType type, Collection units) { - typeMap.put(type, new LinkedHashSet<>(units)); } diff --git a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/impl/publisher/P2GeneratorImpl.java b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/impl/publisher/P2GeneratorImpl.java index 5d0cba5d4c..691a847661 100644 --- a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/impl/publisher/P2GeneratorImpl.java +++ b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/impl/publisher/P2GeneratorImpl.java @@ -184,9 +184,7 @@ public void persistMetadata(Map metadata, File unitsXml, Fi Set units = new LinkedHashSet<>(); Set artifactDescriptors = new LinkedHashSet<>(); for (IP2Artifact artifact : metadata.values()) { - for (IInstallableUnit unit : artifact.getInstallableUnits()) { - units.add(unit); - } + units.addAll(artifact.getInstallableUnits()); artifactDescriptors.add(artifact.getArtifactDescriptor()); } new MetadataIO().writeXML(units, unitsXml); diff --git a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/resolver/P2ResolverImpl.java b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/resolver/P2ResolverImpl.java index b9514e6f18..83b4d17875 100644 --- a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/resolver/P2ResolverImpl.java +++ b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/resolver/P2ResolverImpl.java @@ -123,11 +123,11 @@ public Map resolveTargetDependencies(Targ // we need a linked hashmap to maintain iteration-order, some of the code relies on it! Map results = new LinkedHashMap<>(); Set usedTargetPlatformUnits = new LinkedHashSet<>(); - Set metadata = project != null ? project.getDependencyMetadata(DependencyMetadataType.SEED) + Set metadata = project != null ? project.getDependencyMetadata(DependencyMetadataType.SEED) : Collections.emptySet(); for (TargetEnvironment environment : environments) { if (isMatchingEnv(metadata, environment, logger::debug)) { - results.put(environment, resolveDependencies(Collections. emptySet(), project, + results.put(environment, resolveDependencies(Collections.emptySet(), project, new ProjectorResolutionStrategy(logger), environment, targetPlatform, usedTargetPlatformUnits)); } else { logger.info(MessageFormat.format( @@ -505,19 +505,17 @@ private static void addArtifactFile(DefaultP2ResolutionResult result, IInstallab * @param environment * @return */ - private static boolean isMatchingEnv(Set metadata, TargetEnvironment environment, + private static boolean isMatchingEnv(Set metadata, TargetEnvironment environment, Consumer debugConsumer) { if (metadata != null) { - for (Object meta : metadata) { - if (meta instanceof IInstallableUnit) { - IMatchExpression filter = ((IInstallableUnit) meta).getFilter(); - if (filter != null) { - boolean match = filter.isMatch(InstallableUnit.contextIU(environment.toFilterProperties())); - debugConsumer.accept(MessageFormat.format("{0}: {1} (matches {2})", filter, - Arrays.toString(filter.getParameters()), match)); - if (!match) { - return false; - } + for (IInstallableUnit meta : metadata) { + IMatchExpression filter = meta.getFilter(); + if (filter != null) { + boolean match = filter.isMatch(InstallableUnit.contextIU(environment.toFilterProperties())); + debugConsumer.accept(MessageFormat.format("{0}: {1} (matches {2})", filter, + Arrays.toString(filter.getParameters()), match)); + if (!match) { + return false; } } } diff --git a/tycho-bundles/org.eclipse.tycho.p2.tools.tests/src/test/java/org/eclipse/tycho/p2/tools/publisher/PublishProductToolTest.java b/tycho-bundles/org.eclipse.tycho.p2.tools.tests/src/test/java/org/eclipse/tycho/p2/tools/publisher/PublishProductToolTest.java index 4e3b3846a2..47ef7d445e 100644 --- a/tycho-bundles/org.eclipse.tycho.p2.tools.tests/src/test/java/org/eclipse/tycho/p2/tools/publisher/PublishProductToolTest.java +++ b/tycho-bundles/org.eclipse.tycho.p2.tools.tests/src/test/java/org/eclipse/tycho/p2/tools/publisher/PublishProductToolTest.java @@ -278,8 +278,7 @@ public void testPublishingWithRootFeatures() { assertThat(productUnit.getRequirements(), not(hasItem(requirement("org.eclipse.egit.feature.group", "2.0")))); assertEquals("org.eclipse.help", seeds.get(1).getId()); - assertThat((IInstallableUnit) seeds.get(1).getInstallableUnit(), - is(unitWithId("org.eclipse.help.feature.group"))); + assertThat(seeds.get(1).getInstallableUnit(), is(unitWithId("org.eclipse.help.feature.group"))); assertEquals("org.eclipse.egit", seeds.get(2).getId()); assertThat((IInstallableUnit) seeds.get(2).getInstallableUnit(), is(unitWithId("org.eclipse.egit.feature.group"))); @@ -291,13 +290,13 @@ public void testPublishingWithRootFeatures() { */ private static IInstallableUnit getUnit(Collection seeds) { assertEquals(1, seeds.size()); - return (IInstallableUnit) seeds.iterator().next().getInstallableUnit(); + return seeds.iterator().next().getInstallableUnit(); } private Set unitsIn(Collection seeds) { Set result = new HashSet<>(); for (DependencySeed seed : seeds) { - result.add((IInstallableUnit) seed.getInstallableUnit()); + result.add(seed.getInstallableUnit()); } return result; } diff --git a/tycho-bundles/org.eclipse.tycho.p2.tools.tests/src/test/java/org/eclipse/tycho/p2/tools/publisher/PublisherServiceTest.java b/tycho-bundles/org.eclipse.tycho.p2.tools.tests/src/test/java/org/eclipse/tycho/p2/tools/publisher/PublisherServiceTest.java index cddd69fdb1..0ccdb923b4 100644 --- a/tycho-bundles/org.eclipse.tycho.p2.tools.tests/src/test/java/org/eclipse/tycho/p2/tools/publisher/PublisherServiceTest.java +++ b/tycho-bundles/org.eclipse.tycho.p2.tools.tests/src/test/java/org/eclipse/tycho/p2/tools/publisher/PublisherServiceTest.java @@ -130,7 +130,7 @@ public void testValidateProfileFile() throws Exception { private static Map unitsById(Collection seeds) { Map result = new HashMap<>(); for (DependencySeed seed : seeds) { - IInstallableUnit iu = (IInstallableUnit) seed.getInstallableUnit(); + IInstallableUnit iu = seed.getInstallableUnit(); result.put(iu.getId(), iu); } return result; diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultReactorProject.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultReactorProject.java index 4409c6ec17..cfee928b8e 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultReactorProject.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultReactorProject.java @@ -186,16 +186,16 @@ public void setDependencyMetadata(DependencyMetadataType type, Collection getDependencyMetadata() { - LinkedHashSet result = new LinkedHashSet<>( - getDependencyMetadata(DependencyMetadataType.SEED)); + Set result = new LinkedHashSet<>(getDependencyMetadata(DependencyMetadataType.SEED)); result.addAll(getDependencyMetadata(DependencyMetadataType.RESOLVE)); return result; } @Override public Set getDependencyMetadata(DependencyMetadataType type) { - return Objects.requireNonNullElse((Set) getContextValue(getDependencyMetadataKey(type)), - Collections.emptySet()); + @SuppressWarnings("unchecked") + Set contextValue = (Set) getContextValue(getDependencyMetadataKey(type)); + return Objects.requireNonNullElse(contextValue, Collections.emptySet()); } private static String getDependencyMetadataKey(DependencyMetadataType type) { diff --git a/tycho-extras/tycho-p2-extras-plugin/src/main/java/org/eclipse/tycho/plugins/p2/extras/CompareWithBaselineMojo.java b/tycho-extras/tycho-p2-extras-plugin/src/main/java/org/eclipse/tycho/plugins/p2/extras/CompareWithBaselineMojo.java index 1363b72283..7b3c209012 100644 --- a/tycho-extras/tycho-p2-extras-plugin/src/main/java/org/eclipse/tycho/plugins/p2/extras/CompareWithBaselineMojo.java +++ b/tycho-extras/tycho-p2-extras-plugin/src/main/java/org/eclipse/tycho/plugins/p2/extras/CompareWithBaselineMojo.java @@ -13,7 +13,6 @@ package org.eclipse.tycho.plugins.p2.extras; import java.io.File; -import java.lang.reflect.Method; import java.net.URI; import java.util.Collections; import java.util.List; @@ -30,6 +29,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.logging.Logger; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.sisu.equinox.EquinoxServiceFactory; import org.eclipse.tycho.IDependencyMetadata.DependencyMetadataType; import org.eclipse.tycho.ReactorProject; @@ -112,7 +112,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { return; } ReactorProject reactorProject = DefaultReactorProject.adapt(project); - Set dependencyMetadata = reactorProject.getDependencyMetadata(DependencyMetadataType.SEED); + Set dependencyMetadata = reactorProject.getDependencyMetadata(DependencyMetadataType.SEED); if (dependencyMetadata == null || dependencyMetadata.isEmpty()) { getLog().debug("Skipping baseline version comparison, no p2 artifacts created in build."); return; @@ -134,12 +134,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { TargetPlatform baselineTP = resolverFactory.getTargetPlatformFactory().createTargetPlatform(baselineTPStub, TychoProjectUtils.getExecutionEnvironmentConfiguration(reactorProject), null); - for (Object item : dependencyMetadata) { + for (IInstallableUnit item : dependencyMetadata) { try { - Method getIdMethod = item.getClass().getMethod("getId"); - Method getVersionMethod = item.getClass().getMethod("getVersion"); - String id = (String) getIdMethod.invoke(item); - Version version = new Version(getVersionMethod.invoke(item).toString()); + String id = item.getId(); + Version version = new Version(item.getVersion().toString()); P2ResolutionResult res = resolver.resolveInstallableUnit(baselineTP, id, "0.0.0"); for (Entry foundInBaseline : res.getArtifacts()) { diff --git a/tycho-p2/tycho-p2-facade/src/main/java/org/eclipse/tycho/p2/resolver/P2DependencyResolver.java b/tycho-p2/tycho-p2-facade/src/main/java/org/eclipse/tycho/p2/resolver/P2DependencyResolver.java index 2ec71943be..3ca711113e 100644 --- a/tycho-p2/tycho-p2-facade/src/main/java/org/eclipse/tycho/p2/resolver/P2DependencyResolver.java +++ b/tycho-p2/tycho-p2-facade/src/main/java/org/eclipse/tycho/p2/resolver/P2DependencyResolver.java @@ -92,7 +92,6 @@ import org.eclipse.tycho.osgi.adapters.MavenLoggerAdapter; import org.eclipse.tycho.p2.facade.internal.AttachedArtifact; import org.eclipse.tycho.p2.metadata.DependencyMetadataGenerator; -import org.eclipse.tycho.p2.metadata.IArtifactFacade; import org.eclipse.tycho.p2.metadata.PublisherOptions; import org.eclipse.tycho.p2.repository.LocalRepositoryP2Indices; import org.eclipse.tycho.p2.resolver.facade.P2ResolutionResult; @@ -150,15 +149,13 @@ public void setupProjects(final MavenSession session, final MavenProject project typeMap.put(type, new LinkedHashSet<>()); } for (IDependencyMetadata metadata : metadataMap.values()) { - for (Entry> map : typeMap.entrySet()) { - map.getValue().addAll(metadata.getDependencyMetadata(map.getKey())); - } + typeMap.forEach((key, value) -> value.addAll(metadata.getDependencyMetadata(key))); } Set initial = new HashSet<>(); - for (Entry> entry : typeMap.entrySet()) { - reactorProject.setDependencyMetadata(entry.getKey(), entry.getValue()); - initial.addAll(entry.getValue()); - } + typeMap.forEach((key, value) -> { + reactorProject.setDependencyMetadata(key, value); + initial.addAll(value); + }); reactorProject.setDependencyMetadata(DependencyMetadataType.INITIAL, initial); } @@ -239,8 +236,7 @@ private ReactorProject getThisReactorProject(MavenSession session, MavenProject Map dependencyMetadata = getDependencyMetadata(session, project, environments, optionalAction); Map> typeMap = new TreeMap<>(); - for (Map.Entry entry : dependencyMetadata.entrySet()) { - IDependencyMetadata value = entry.getValue(); + for (IDependencyMetadata value : dependencyMetadata.values()) { for (DependencyMetadataType type : DependencyMetadataType.values()) { typeMap.computeIfAbsent(type, t -> new LinkedHashSet<>()).addAll(value.getDependencyMetadata(type)); } @@ -343,11 +339,7 @@ public PomDependencyCollector resolvePomDependencies(MavenSession session, Maven if (dependencyArtifacts instanceof ArtifactCollection) { //enhance the dependency set ArtifactCollection collection = (ArtifactCollection) dependencyArtifacts; - Map mavenInstallableUnits = dependencyCollector - .getMavenInstallableUnits(); - for (var entry : mavenInstallableUnits.entrySet()) { - IInstallableUnit key = entry.getKey(); - IArtifactFacade val = entry.getValue(); + dependencyCollector.getMavenInstallableUnits().forEach((key, val) -> { ArtifactKey artifactKey = dependencyCollector.getArtifactKey(val); File location = val.getLocation(); try { @@ -355,7 +347,7 @@ public PomDependencyCollector resolvePomDependencies(MavenSession session, Maven } catch (IllegalStateException e) { //already contained in the collection --> ignore it... } - } + }); } return dependencyCollector; } diff --git a/tycho-p2/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/P2MetadataMojo.java b/tycho-p2/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/P2MetadataMojo.java index 7e338e8671..c0f4cbb3b8 100644 --- a/tycho-p2/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/P2MetadataMojo.java +++ b/tycho-p2/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/P2MetadataMojo.java @@ -191,10 +191,7 @@ protected void attachP2Metadata() throws MojoExecutionException { ReactorProject reactorProject = DefaultReactorProject.adapt(project); Set installableUnits = new LinkedHashSet<>(); - for (Map.Entry entry : generatedMetadata.entrySet()) { - String classifier = entry.getKey(); - IP2Artifact p2artifact = entry.getValue(); - + generatedMetadata.forEach((classifier, p2artifact) -> { installableUnits.addAll(p2artifact.getInstallableUnits()); // attach any new classified artifacts, like feature root files for example @@ -202,7 +199,7 @@ protected void attachP2Metadata() throws MojoExecutionException { projectHelper.attachArtifact(project, getExtension(p2artifact.getLocation()), classifier, p2artifact.getLocation()); } - } + }); // TODO 353889 distinguish between dependency resolution seed units ("primary") and other units of the project reactorProject.setDependencyMetadata(DependencyMetadataType.SEED, installableUnits);