@Test public void testAnnotate_googleApiClientAndGrpcConflict() throws IOException { ClassPathBuilder builder = new ClassPathBuilder(); ClassPathResult classPathResult = builder.resolve( ImmutableList.of( new DefaultArtifact("com.google.api-client:google-api-client:1.27.0"), new DefaultArtifact("io.grpc:grpc-core:1.17.1")), false); LinkageChecker linkageChecker = LinkageChecker.create(classPathResult.getClassPath()); ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems(); Optional<LinkageProblem> foundProblem = linkageProblems.stream() .filter(problem -> problem instanceof SymbolNotFoundProblem) .findFirst(); SymbolNotFoundProblem problem = (SymbolNotFoundProblem) foundProblem.get(); assertEquals("verify", ((MethodSymbol) problem.getSymbol()).getName()); LinkageProblemCauseAnnotator.annotate( classPathBuilder, classPathResult, ImmutableSet.of(problem)); LinkageProblemCause cause = problem.getCause(); assertTrue(cause instanceof DependencyConflict); DependencyPath pathToSelectedArtifact = ((DependencyConflict) cause).getPathToSelectedArtifact(); Artifact selectedLeaf = pathToSelectedArtifact.getLeaf(); assertEquals("guava", selectedLeaf.getArtifactId()); assertEquals("20.0", selectedLeaf.getVersion()); Artifact firstElementInSelected = pathToSelectedArtifact.get(1); assertEquals("google-api-client", firstElementInSelected.getArtifactId()); DependencyPath pathToUnselectedArtifact = ((DependencyConflict) cause).getPathToArtifactThruSource(); Artifact unselectedLeaf = pathToUnselectedArtifact.getLeaf(); assertEquals("guava", unselectedLeaf.getArtifactId()); assertEquals("26.0-android", unselectedLeaf.getVersion()); Artifact firstElementInUnselected = pathToUnselectedArtifact.get(1); assertEquals("grpc-core", firstElementInUnselected.getArtifactId()); }
public static void annotate( ClassPathBuilder classPathBuilder, ClassPathResult rootResult, Iterable<LinkageProblem> linkageProblems) throws IOException { checkNotNull(classPathBuilder); checkNotNull(rootResult); checkNotNull(linkageProblems); Map<Artifact, ClassPathResult> cache = new HashMap<>(); for (LinkageProblem linkageProblem : linkageProblems) { ClassFile sourceClass = linkageProblem.getSourceClass(); ClassPathEntry sourceEntry = sourceClass.getClassPathEntry(); Artifact sourceArtifact = sourceEntry.getArtifact(); ClassPathResult subtreeResult = cache.get(sourceArtifact); if (subtreeResult == null) { subtreeResult = classPathBuilder.resolveWithMaven(sourceArtifact); cache.put(sourceArtifact, subtreeResult); } Symbol symbol = linkageProblem.getSymbol(); ClassPathEntry entryInSubtree = subtreeResult.findEntryBySymbol(symbol); if (entryInSubtree == null) { linkageProblem.setCause(UnknownCause.getInstance()); } else { Artifact artifactInSubtree = entryInSubtree.getArtifact(); DependencyPath pathToSourceEntry = rootResult.getDependencyPaths(sourceEntry).get(0); DependencyPath pathFromSourceEntryToUnselectedEntry = subtreeResult.getDependencyPaths(entryInSubtree).get(0); DependencyPath pathToUnselectedEntry = pathToSourceEntry.concat(pathFromSourceEntryToUnselectedEntry); ClassPathEntry selectedEntry = rootResult.findEntryById( artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId()); if (selectedEntry != null) { Artifact selectedArtifact = selectedEntry.getArtifact(); if (!selectedArtifact.getVersion().equals(artifactInSubtree.getVersion())) { linkageProblem.setCause( new DependencyConflict( linkageProblem, rootResult.getDependencyPaths(selectedEntry).get(0), pathToUnselectedEntry)); } else { linkageProblem.setCause(UnknownCause.getInstance()); } } else { Artifact excludingArtifact = pathToSourceEntry.findExclusion( artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId()); if (excludingArtifact != null) { linkageProblem.setCause( new ExcludedDependency(pathToUnselectedEntry, excludingArtifact)); } else { linkageProblem.setCause(new MissingDependency(pathToUnselectedEntry)); } } } } }
LinkageProblemCauseAnnotator { public static void annotate( ClassPathBuilder classPathBuilder, ClassPathResult rootResult, Iterable<LinkageProblem> linkageProblems) throws IOException { checkNotNull(classPathBuilder); checkNotNull(rootResult); checkNotNull(linkageProblems); Map<Artifact, ClassPathResult> cache = new HashMap<>(); for (LinkageProblem linkageProblem : linkageProblems) { ClassFile sourceClass = linkageProblem.getSourceClass(); ClassPathEntry sourceEntry = sourceClass.getClassPathEntry(); Artifact sourceArtifact = sourceEntry.getArtifact(); ClassPathResult subtreeResult = cache.get(sourceArtifact); if (subtreeResult == null) { subtreeResult = classPathBuilder.resolveWithMaven(sourceArtifact); cache.put(sourceArtifact, subtreeResult); } Symbol symbol = linkageProblem.getSymbol(); ClassPathEntry entryInSubtree = subtreeResult.findEntryBySymbol(symbol); if (entryInSubtree == null) { linkageProblem.setCause(UnknownCause.getInstance()); } else { Artifact artifactInSubtree = entryInSubtree.getArtifact(); DependencyPath pathToSourceEntry = rootResult.getDependencyPaths(sourceEntry).get(0); DependencyPath pathFromSourceEntryToUnselectedEntry = subtreeResult.getDependencyPaths(entryInSubtree).get(0); DependencyPath pathToUnselectedEntry = pathToSourceEntry.concat(pathFromSourceEntryToUnselectedEntry); ClassPathEntry selectedEntry = rootResult.findEntryById( artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId()); if (selectedEntry != null) { Artifact selectedArtifact = selectedEntry.getArtifact(); if (!selectedArtifact.getVersion().equals(artifactInSubtree.getVersion())) { linkageProblem.setCause( new DependencyConflict( linkageProblem, rootResult.getDependencyPaths(selectedEntry).get(0), pathToUnselectedEntry)); } else { linkageProblem.setCause(UnknownCause.getInstance()); } } else { Artifact excludingArtifact = pathToSourceEntry.findExclusion( artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId()); if (excludingArtifact != null) { linkageProblem.setCause( new ExcludedDependency(pathToUnselectedEntry, excludingArtifact)); } else { linkageProblem.setCause(new MissingDependency(pathToUnselectedEntry)); } } } } } }
LinkageProblemCauseAnnotator { public static void annotate( ClassPathBuilder classPathBuilder, ClassPathResult rootResult, Iterable<LinkageProblem> linkageProblems) throws IOException { checkNotNull(classPathBuilder); checkNotNull(rootResult); checkNotNull(linkageProblems); Map<Artifact, ClassPathResult> cache = new HashMap<>(); for (LinkageProblem linkageProblem : linkageProblems) { ClassFile sourceClass = linkageProblem.getSourceClass(); ClassPathEntry sourceEntry = sourceClass.getClassPathEntry(); Artifact sourceArtifact = sourceEntry.getArtifact(); ClassPathResult subtreeResult = cache.get(sourceArtifact); if (subtreeResult == null) { subtreeResult = classPathBuilder.resolveWithMaven(sourceArtifact); cache.put(sourceArtifact, subtreeResult); } Symbol symbol = linkageProblem.getSymbol(); ClassPathEntry entryInSubtree = subtreeResult.findEntryBySymbol(symbol); if (entryInSubtree == null) { linkageProblem.setCause(UnknownCause.getInstance()); } else { Artifact artifactInSubtree = entryInSubtree.getArtifact(); DependencyPath pathToSourceEntry = rootResult.getDependencyPaths(sourceEntry).get(0); DependencyPath pathFromSourceEntryToUnselectedEntry = subtreeResult.getDependencyPaths(entryInSubtree).get(0); DependencyPath pathToUnselectedEntry = pathToSourceEntry.concat(pathFromSourceEntryToUnselectedEntry); ClassPathEntry selectedEntry = rootResult.findEntryById( artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId()); if (selectedEntry != null) { Artifact selectedArtifact = selectedEntry.getArtifact(); if (!selectedArtifact.getVersion().equals(artifactInSubtree.getVersion())) { linkageProblem.setCause( new DependencyConflict( linkageProblem, rootResult.getDependencyPaths(selectedEntry).get(0), pathToUnselectedEntry)); } else { linkageProblem.setCause(UnknownCause.getInstance()); } } else { Artifact excludingArtifact = pathToSourceEntry.findExclusion( artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId()); if (excludingArtifact != null) { linkageProblem.setCause( new ExcludedDependency(pathToUnselectedEntry, excludingArtifact)); } else { linkageProblem.setCause(new MissingDependency(pathToUnselectedEntry)); } } } } } private  LinkageProblemCauseAnnotator(); }
LinkageProblemCauseAnnotator { public static void annotate( ClassPathBuilder classPathBuilder, ClassPathResult rootResult, Iterable<LinkageProblem> linkageProblems) throws IOException { checkNotNull(classPathBuilder); checkNotNull(rootResult); checkNotNull(linkageProblems); Map<Artifact, ClassPathResult> cache = new HashMap<>(); for (LinkageProblem linkageProblem : linkageProblems) { ClassFile sourceClass = linkageProblem.getSourceClass(); ClassPathEntry sourceEntry = sourceClass.getClassPathEntry(); Artifact sourceArtifact = sourceEntry.getArtifact(); ClassPathResult subtreeResult = cache.get(sourceArtifact); if (subtreeResult == null) { subtreeResult = classPathBuilder.resolveWithMaven(sourceArtifact); cache.put(sourceArtifact, subtreeResult); } Symbol symbol = linkageProblem.getSymbol(); ClassPathEntry entryInSubtree = subtreeResult.findEntryBySymbol(symbol); if (entryInSubtree == null) { linkageProblem.setCause(UnknownCause.getInstance()); } else { Artifact artifactInSubtree = entryInSubtree.getArtifact(); DependencyPath pathToSourceEntry = rootResult.getDependencyPaths(sourceEntry).get(0); DependencyPath pathFromSourceEntryToUnselectedEntry = subtreeResult.getDependencyPaths(entryInSubtree).get(0); DependencyPath pathToUnselectedEntry = pathToSourceEntry.concat(pathFromSourceEntryToUnselectedEntry); ClassPathEntry selectedEntry = rootResult.findEntryById( artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId()); if (selectedEntry != null) { Artifact selectedArtifact = selectedEntry.getArtifact(); if (!selectedArtifact.getVersion().equals(artifactInSubtree.getVersion())) { linkageProblem.setCause( new DependencyConflict( linkageProblem, rootResult.getDependencyPaths(selectedEntry).get(0), pathToUnselectedEntry)); } else { linkageProblem.setCause(UnknownCause.getInstance()); } } else { Artifact excludingArtifact = pathToSourceEntry.findExclusion( artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId()); if (excludingArtifact != null) { linkageProblem.setCause( new ExcludedDependency(pathToUnselectedEntry, excludingArtifact)); } else { linkageProblem.setCause(new MissingDependency(pathToUnselectedEntry)); } } } } } private  LinkageProblemCauseAnnotator(); static void annotate(
      ClassPathBuilder classPathBuilder,
      ClassPathResult rootResult,
      Iterable<LinkageProblem> linkageProblems); }
LinkageProblemCauseAnnotator { public static void annotate( ClassPathBuilder classPathBuilder, ClassPathResult rootResult, Iterable<LinkageProblem> linkageProblems) throws IOException { checkNotNull(classPathBuilder); checkNotNull(rootResult); checkNotNull(linkageProblems); Map<Artifact, ClassPathResult> cache = new HashMap<>(); for (LinkageProblem linkageProblem : linkageProblems) { ClassFile sourceClass = linkageProblem.getSourceClass(); ClassPathEntry sourceEntry = sourceClass.getClassPathEntry(); Artifact sourceArtifact = sourceEntry.getArtifact(); ClassPathResult subtreeResult = cache.get(sourceArtifact); if (subtreeResult == null) { subtreeResult = classPathBuilder.resolveWithMaven(sourceArtifact); cache.put(sourceArtifact, subtreeResult); } Symbol symbol = linkageProblem.getSymbol(); ClassPathEntry entryInSubtree = subtreeResult.findEntryBySymbol(symbol); if (entryInSubtree == null) { linkageProblem.setCause(UnknownCause.getInstance()); } else { Artifact artifactInSubtree = entryInSubtree.getArtifact(); DependencyPath pathToSourceEntry = rootResult.getDependencyPaths(sourceEntry).get(0); DependencyPath pathFromSourceEntryToUnselectedEntry = subtreeResult.getDependencyPaths(entryInSubtree).get(0); DependencyPath pathToUnselectedEntry = pathToSourceEntry.concat(pathFromSourceEntryToUnselectedEntry); ClassPathEntry selectedEntry = rootResult.findEntryById( artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId()); if (selectedEntry != null) { Artifact selectedArtifact = selectedEntry.getArtifact(); if (!selectedArtifact.getVersion().equals(artifactInSubtree.getVersion())) { linkageProblem.setCause( new DependencyConflict( linkageProblem, rootResult.getDependencyPaths(selectedEntry).get(0), pathToUnselectedEntry)); } else { linkageProblem.setCause(UnknownCause.getInstance()); } } else { Artifact excludingArtifact = pathToSourceEntry.findExclusion( artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId()); if (excludingArtifact != null) { linkageProblem.setCause( new ExcludedDependency(pathToUnselectedEntry, excludingArtifact)); } else { linkageProblem.setCause(new MissingDependency(pathToUnselectedEntry)); } } } } } private  LinkageProblemCauseAnnotator(); static void annotate(
      ClassPathBuilder classPathBuilder,
      ClassPathResult rootResult,
      Iterable<LinkageProblem> linkageProblems);  }