|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Christoph Läubrich and others. |
| 3 | + * This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Christoph Läubrich - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.tycho.baseline.analyze; |
| 14 | + |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.TreeSet; |
| 24 | +import java.util.jar.JarFile; |
| 25 | +import java.util.jar.Manifest; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +import org.apache.maven.plugin.MojoFailureException; |
| 29 | +import org.apache.maven.plugin.logging.Log; |
| 30 | +import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 31 | +import org.eclipse.osgi.util.ManifestElement; |
| 32 | +import org.eclipse.tycho.artifacts.ArtifactVersion; |
| 33 | +import org.eclipse.tycho.core.MarkdownBuilder; |
| 34 | +import org.eclipse.tycho.core.resolver.target.ArtifactMatcher; |
| 35 | +import org.eclipse.tycho.model.manifest.MutableBundleManifest; |
| 36 | +import org.osgi.framework.BundleException; |
| 37 | +import org.osgi.framework.Constants; |
| 38 | +import org.osgi.framework.Version; |
| 39 | +import org.osgi.framework.VersionRange; |
| 40 | + |
| 41 | +/** |
| 42 | + * Checker for Require-Bundle dependencies. |
| 43 | + */ |
| 44 | +public class RequireBundleChecker extends DependencyChecker { |
| 45 | + |
| 46 | + private final Collection<IInstallableUnit> units; |
| 47 | + private final List<ClassUsage> usages; |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a new Require-Bundle checker. |
| 51 | + * |
| 52 | + * @param context the check context |
| 53 | + * @param units the installable units |
| 54 | + * @param usages the class usages from the project |
| 55 | + */ |
| 56 | + public RequireBundleChecker(CheckContext context, Collection<IInstallableUnit> units, List<ClassUsage> usages) { |
| 57 | + super(context); |
| 58 | + this.units = units; |
| 59 | + this.usages = usages; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Checks a Require-Bundle dependency. |
| 64 | + * |
| 65 | + * @param bundleName the symbolic name of the required bundle |
| 66 | + * @param bundleVersionStr the version range string |
| 67 | + * @throws MojoFailureException if checks failed |
| 68 | + */ |
| 69 | + public void check(String bundleName, String bundleVersionStr) throws MojoFailureException { |
| 70 | + Log log = context.getLog(); |
| 71 | + Optional<IInstallableUnit> bundleProvidingUnit = ArtifactMatcher.findBundle(bundleName, units); |
| 72 | + if (bundleProvidingUnit.isEmpty()) { |
| 73 | + return; |
| 74 | + } |
| 75 | + IInstallableUnit unit = bundleProvidingUnit.get(); |
| 76 | + org.eclipse.equinox.p2.metadata.Version matchedBundleVersion = unit.getVersion(); |
| 77 | + if (matchedBundleVersion.isOSGiCompatible()) { |
| 78 | + Version current = new Version(matchedBundleVersion.toString()); |
| 79 | + allVersions.computeIfAbsent(bundleName, nil -> new TreeSet<>()).add(current); |
| 80 | + lowestVersion.put(bundleName, current); |
| 81 | + } |
| 82 | + VersionRange versionRange = VersionRange.valueOf(bundleVersionStr); |
| 83 | + List<ArtifactVersion> list = context.getVersionProviders().stream() |
| 84 | + .flatMap(avp -> avp.getBundleVersions(unit, bundleName, versionRange, context.getProject())).toList(); |
| 85 | + if (log.isDebugEnabled()) { |
| 86 | + log.debug("== Bundle " + bundleName + " " + bundleVersionStr + " is provided by " + unit |
| 87 | + + " with version range " + versionRange + ", matching versions: " |
| 88 | + + list.stream().map(av -> av.getVersion()).map(String::valueOf).collect(Collectors.joining(", "))); |
| 89 | + } |
| 90 | + for (ArtifactVersion v : list) { |
| 91 | + Version version = v.getVersion(); |
| 92 | + if (version == null) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + if (!allVersions.computeIfAbsent(bundleName, nil -> new TreeSet<>()).add(version)) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + Path artifact = v.getArtifact(); |
| 99 | + log.debug(v + "=" + artifact); |
| 100 | + if (artifact == null) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + // Determine exported packages for THIS specific version from the JAR manifest |
| 104 | + Set<String> exportedPackages = getExportedPackagesFromJar(artifact); |
| 105 | + if (exportedPackages.isEmpty()) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + // Collect methods our code uses from these exported packages |
| 109 | + Set<MethodSignature> bundleMethods = new TreeSet<>(); |
| 110 | + Map<MethodSignature, Collection<String>> references = new HashMap<>(); |
| 111 | + for (String packageName : exportedPackages) { |
| 112 | + bundleMethods.addAll(collectMethodsForPackage(usages, packageName)); |
| 113 | + references.putAll(collectReferencesForPackage(usages, packageName)); |
| 114 | + } |
| 115 | + if (bundleMethods.isEmpty()) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + if (log.isDebugEnabled()) { |
| 119 | + for (MethodSignature signature : bundleMethods) { |
| 120 | + log.debug("Referenced from bundle " + bundleName + " version " + version + ": " + signature.id()); |
| 121 | + } |
| 122 | + } |
| 123 | + ClassCollection collection = context.getClassCollection(artifact); |
| 124 | + // For Require-Bundle, pass null as packageNameFilter since methods can come |
| 125 | + // from different packages |
| 126 | + boolean ok = checkMethodsInCollection(collection, bundleMethods, bundleName, null, version, references, v, |
| 127 | + unit, bundleVersionStr, matchedBundleVersion, "Require-Bundle"); |
| 128 | + if (ok) { |
| 129 | + lowestVersion.merge(bundleName, version, (v1, v2) -> v1.compareTo(v2) > 0 ? v2 : v1); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private Set<String> getExportedPackagesFromJar(Path jarPath) { |
| 135 | + Set<String> packages = new HashSet<>(); |
| 136 | + try (JarFile jar = new JarFile(jarPath.toFile())) { |
| 137 | + Manifest manifest = jar.getManifest(); |
| 138 | + if (manifest != null) { |
| 139 | + String exportPackage = manifest.getMainAttributes().getValue(Constants.EXPORT_PACKAGE); |
| 140 | + if (exportPackage != null) { |
| 141 | + ManifestElement[] elements = ManifestElement.parseHeader(Constants.EXPORT_PACKAGE, exportPackage); |
| 142 | + if (elements != null) { |
| 143 | + for (ManifestElement element : elements) { |
| 144 | + packages.add(element.getValue()); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } catch (BundleException | java.io.IOException e) { |
| 150 | + context.getLog().debug("Could not read exported packages from " + jarPath + ": " + e); |
| 151 | + } |
| 152 | + return packages; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public boolean applySuggestions(MutableBundleManifest manifest) { |
| 157 | + if (withError.isEmpty()) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + Map<String, String> requiredBundleVersions = manifest.getRequiredBundleVersions(); |
| 161 | + Map<String, String> bundleUpdates = new HashMap<>(); |
| 162 | + Map<String, Version> lowestBundleVersion = getLowestVersions(); |
| 163 | + for (String bundleName : withError) { |
| 164 | + Version lowestVersion = lowestBundleVersion.getOrDefault(bundleName, Version.emptyVersion); |
| 165 | + String current = requiredBundleVersions.get(bundleName); |
| 166 | + if (current == null) { |
| 167 | + bundleUpdates.put(bundleName, |
| 168 | + String.format("[%s,%d)", lowestVersion, (lowestVersion.getMajor() + 1))); |
| 169 | + } else { |
| 170 | + VersionRange range = VersionRange.valueOf(current); |
| 171 | + Version right = range.getRight(); |
| 172 | + bundleUpdates.put(bundleName, |
| 173 | + String.format("[%s,%s%c", lowestVersion, right, range.getRightType())); |
| 174 | + } |
| 175 | + } |
| 176 | + manifest.updateRequiredBundleVersions(bundleUpdates); |
| 177 | + return true; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void reportSuggestions(MarkdownBuilder results, Log log) { |
| 182 | + reportVersionContrainSuggestions("bundle", results, log); |
| 183 | + } |
| 184 | +} |
0 commit comments