@Test public void testAnnotate_autoServiceAnnotationsExclusion() throws IOException { ClassPathBuilder builder = new ClassPathBuilder(); ClassPathResult classPathResult = builder.resolve( ImmutableList.of(new DefaultArtifact("com.google.auto.value:auto-value:1.7.3")), false); Optional<ClassPathEntry> foundAutoService = classPathResult.getClassPath().stream() .filter(entry -> entry.getArtifact().getArtifactId().equals("auto-service")) .findFirst(); ClassPathEntry autoServiceEntry = foundAutoService.get(); LinkageProblem problem = new ClassNotFoundProblem( new ClassFile( autoServiceEntry, "com.google.auto.service.processor.AutoServiceProcessor"), new ClassSymbol("com.google.auto.service.AutoService")); LinkageProblemCauseAnnotator.annotate( classPathBuilder, classPathResult, ImmutableSet.of(problem)); LinkageProblemCause cause = problem.getCause(); assertEquals(ExcludedDependency.class, cause.getClass()); ExcludedDependency excludedDependency = (ExcludedDependency) cause; DependencyPath pathToMissingArtifact = excludedDependency.getPathToMissingArtifact(); Artifact leaf = pathToMissingArtifact.getLeaf(); assertEquals("auto-service-annotations", leaf.getArtifactId()); assertEquals("auto-value", excludedDependency.getExcludingArtifact().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);  }