Search in sources :

Example 16 with ClassPathEntry

use of com.google.cloud.tools.opensource.classpath.ClassPathEntry in project cloud-opensource-java by GoogleCloudPlatform.

the class MaximumLinkageErrorsTest method hasLinkageProblemFromArtifactId.

private boolean hasLinkageProblemFromArtifactId(LinkageProblem problem, String artifactId) {
    ClassPathEntry sourceClassPathEntry = problem.getSourceClass().getClassPathEntry();
    Artifact sourceArtifact = sourceClassPathEntry.getArtifact();
    return artifactId.equals(sourceArtifact.getArtifactId());
}
Also used : ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) Artifact(org.eclipse.aether.artifact.Artifact)

Example 17 with ClassPathEntry

use of com.google.cloud.tools.opensource.classpath.ClassPathEntry in project java-cloud-bom by googleapis.

the class BomContentTest method findNoDowngradeViolation.

/**
 * Returns messages describing the violation of the no-downgrade rule by {@code artifact} against
 * the BOM containing {@code bomArtifacts}. An empty list if there is no violations.
 */
private static ImmutableList<String> findNoDowngradeViolation(Map<String, Artifact> bomArtifacts, Artifact artifact) throws InvalidVersionSpecificationException {
    ImmutableList.Builder<String> violations = ImmutableList.builder();
    ClassPathBuilder classPathBuilder = new ClassPathBuilder();
    ClassPathResult result = classPathBuilder.resolve(ImmutableList.of(artifact), false, DependencyMediation.MAVEN);
    for (ClassPathEntry entry : result.getClassPath()) {
        Artifact transitiveDependency = entry.getArtifact();
        String key = Artifacts.makeKey(transitiveDependency);
        Artifact bomArtifact = bomArtifacts.get(key);
        if (bomArtifact == null) {
            // transitiveDependency is not part of the BOM
            continue;
        }
        Version versionInBom = versionScheme.parseVersion(bomArtifact.getVersion());
        Version versionInTransitiveDependency = versionScheme.parseVersion(transitiveDependency.getVersion());
        if (versionInTransitiveDependency.compareTo(versionInBom) <= 0) {
            // the no-downgrade rule.
            continue;
        }
        // Filter by scopes that are invisible to library users
        ImmutableList<DependencyPath> dependencyPaths = result.getDependencyPaths(entry);
        Verify.verify(!dependencyPaths.isEmpty(), "The class path entry should have at least one dependency path from the root");
        boolean dependencyVisibleToUsers = false;
        for (DependencyPath dependencyPath : dependencyPaths) {
            int length = dependencyPath.size();
            // As the root element is an empty node, the last element is at "length - 2".
            Dependency dependency = dependencyPath.getDependency(length - 2);
            if (dependencyScopesVisibleToUsers.contains(dependency.getScope())) {
                dependencyVisibleToUsers = true;
                break;
            }
        }
        if (!dependencyVisibleToUsers) {
            // provided scope.
            continue;
        }
        // A violation of the no-downgrade rule is found.
        violations.add(artifact + " has a transitive dependency " + transitiveDependency + ". This is higher version than " + bomArtifact + " in the BOM. Example dependency path: " + dependencyPaths.get(0));
    }
    return violations.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Dependency(org.eclipse.aether.graph.Dependency) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) Artifact(org.eclipse.aether.artifact.Artifact) Version(org.eclipse.aether.version.Version)

Aggregations

ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)17 Artifact (org.eclipse.aether.artifact.Artifact)12 ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)10 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)8 ClassPathBuilder (com.google.cloud.tools.opensource.classpath.ClassPathBuilder)7 LinkageProblem (com.google.cloud.tools.opensource.classpath.LinkageProblem)7 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)7 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)4 ImmutableList (com.google.common.collect.ImmutableList)4 Path (java.nio.file.Path)4 ClassFile (com.google.cloud.tools.opensource.classpath.ClassFile)3 LinkageChecker (com.google.cloud.tools.opensource.classpath.LinkageChecker)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)2 Dependency (org.eclipse.aether.graph.Dependency)2