diff --git a/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageChecker.java b/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageChecker.java index 12dfc715de..cac9c0c054 100644 --- a/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageChecker.java +++ b/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageChecker.java @@ -36,6 +36,7 @@ import java.util.Queue; import java.util.Set; import java.util.logging.Logger; +import java.util.stream.Collectors; import javax.annotation.Nullable; import org.apache.bcel.classfile.Field; import org.apache.bcel.classfile.FieldOrMethod; @@ -54,6 +55,7 @@ public class LinkageChecker { private final ImmutableList classPath; private final SymbolReferences symbolReferences; private final ClassReferenceGraph classReferenceGraph; + private final Artifact sourceFilter; private final ExcludedErrors excludedErrors; @VisibleForTesting @@ -66,7 +68,7 @@ public ClassReferenceGraph getClassReferenceGraph() { } public static LinkageChecker create(List classPath) throws IOException { - return create(classPath, ImmutableSet.copyOf(classPath), null); + return create(classPath, ImmutableSet.copyOf(classPath), null, null); } /** @@ -79,6 +81,7 @@ public static LinkageChecker create(List classPath) throws IOExc public static LinkageChecker create( List classPath, Iterable entryPoints, + Artifact sourceFilter, @Nullable Path exclusionFile) throws IOException { Preconditions.checkArgument(!classPath.isEmpty(), "The linkage classpath is empty."); @@ -93,6 +96,7 @@ public static LinkageChecker create( classPath, symbolReferenceMaps, classReferenceGraph, + sourceFilter, ExcludedErrors.create(exclusionFile)); } @@ -129,13 +133,13 @@ public static LinkageChecker create(Bom bom, Path exclusionFile) List artifactsInBom = classpath.subList(0, managedDependencies.size()); ImmutableSet entryPoints = ImmutableSet.copyOf(artifactsInBom); - return LinkageChecker.create(classpath, entryPoints, exclusionFile); + return LinkageChecker.create(classpath, entryPoints, null, exclusionFile); } @VisibleForTesting LinkageChecker cloneWith(SymbolReferences newSymbolMaps) { return new LinkageChecker( - classDumper, classPath, newSymbolMaps, classReferenceGraph, excludedErrors); + classDumper, classPath, newSymbolMaps, classReferenceGraph, null, excludedErrors); } private LinkageChecker( @@ -143,11 +147,13 @@ private LinkageChecker( List classPath, SymbolReferences symbolReferenceMaps, ClassReferenceGraph classReferenceGraph, + Artifact sourceFilter, ExcludedErrors excludedErrors) { this.classDumper = Preconditions.checkNotNull(classDumper); this.classPath = ImmutableList.copyOf(classPath); this.classReferenceGraph = Preconditions.checkNotNull(classReferenceGraph); this.symbolReferences = Preconditions.checkNotNull(symbolReferenceMaps); + this.sourceFilter = sourceFilter; this.excludedErrors = Preconditions.checkNotNull(excludedErrors); } @@ -161,7 +167,13 @@ public ImmutableSet findLinkageProblems() throws IOException { ImmutableSet.Builder problemToClass = ImmutableSet.builder(); // This sourceClassFile is a source of references to other symbols. - for (ClassFile classFile : symbolReferences.getClassFiles()) { + Set classFiles = symbolReferences.getClassFiles(); + if (sourceFilter != null) { + classFiles = classFiles.stream() + .filter(x -> x.getClassPathEntry().getArtifact().toString().equals(sourceFilter.toString())) + .collect(Collectors.toSet()); + } + for (ClassFile classFile : classFiles) { ImmutableSet classSymbols = symbolReferences.getClassSymbols(classFile); for (ClassSymbol classSymbol : classSymbols) { if (classSymbol instanceof SuperClassSymbol) { @@ -202,7 +214,7 @@ public ImmutableSet findLinkageProblems() throws IOException { } } - for (ClassFile classFile : symbolReferences.getClassFiles()) { + for (ClassFile classFile : classFiles) { ImmutableSet methodSymbols = symbolReferences.getMethodSymbols(classFile); ImmutableSet classFileNames = classFile.getClassPathEntry().getFileNames(); for (MethodSymbol methodSymbol : methodSymbols) { @@ -215,7 +227,7 @@ public ImmutableSet findLinkageProblems() throws IOException { } } - for (ClassFile classFile : symbolReferences.getClassFiles()) { + for (ClassFile classFile : classFiles) { ImmutableSet fieldSymbols = symbolReferences.getFieldSymbols(classFile); ImmutableSet classFileNames = classFile.getClassPathEntry().getFileNames(); for (FieldSymbol fieldSymbol : fieldSymbols) { diff --git a/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageCheckerArguments.java b/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageCheckerArguments.java index 04a5556f86..d264e3efb4 100644 --- a/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageCheckerArguments.java +++ b/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageCheckerArguments.java @@ -25,6 +25,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.List; + import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; @@ -124,6 +126,14 @@ private static Options configureOptions() { .build(); inputGroup.addOption(jarOption); + Option sourceFilter = + Option.builder("s") + .longOpt("source-filter") + .hasArg(true) + .desc("Source Filter") + .build(); + options.addOption(sourceFilter); + Option repositoryOption = Option.builder("m") .longOpt("maven-repositories") @@ -277,4 +287,12 @@ Path getOutputExclusionFile() { } return null; } + + Artifact getSourceFilterArtifact() { + if (commandLine.hasOption("s")) { + String mavenCoordinatesOption = commandLine.getOptionValue('s'); + return new DefaultArtifact(mavenCoordinatesOption); + } + return null; + } } diff --git a/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageCheckerMain.java b/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageCheckerMain.java index 8bd7d7b094..f8639b2860 100644 --- a/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageCheckerMain.java +++ b/dependencies/src/main/java/com/google/cloud/tools/opensource/classpath/LinkageCheckerMain.java @@ -133,7 +133,7 @@ private static Problems checkJarFiles( ImmutableSet entryPoints = ImmutableSet.copyOf(inputClassPath); LinkageChecker linkageChecker = LinkageChecker.create( - inputClassPath, entryPoints, linkageCheckerArguments.getInputExclusionFile()); + inputClassPath, entryPoints, null, linkageCheckerArguments.getInputExclusionFile()); ImmutableSet linkageProblems = findLinkageProblems(linkageChecker, linkageCheckerArguments.getReportOnlyReachable()); @@ -161,7 +161,7 @@ private static Problems checkArtifacts( LinkageChecker linkageChecker = LinkageChecker.create( - inputClassPath, entryPoints, linkageCheckerArguments.getInputExclusionFile()); + inputClassPath, entryPoints, linkageCheckerArguments.getSourceFilterArtifact(), linkageCheckerArguments.getInputExclusionFile()); ImmutableSet linkageProblems = findLinkageProblems(linkageChecker, linkageCheckerArguments.getReportOnlyReachable()); diff --git a/enforcer-rules/src/main/java/com/google/cloud/tools/dependencies/enforcer/LinkageCheckerRule.java b/enforcer-rules/src/main/java/com/google/cloud/tools/dependencies/enforcer/LinkageCheckerRule.java index 81f102ad41..fca5081c76 100644 --- a/enforcer-rules/src/main/java/com/google/cloud/tools/dependencies/enforcer/LinkageCheckerRule.java +++ b/enforcer-rules/src/main/java/com/google/cloud/tools/dependencies/enforcer/LinkageCheckerRule.java @@ -224,7 +224,7 @@ public void execute() throws EnforcerRuleException { // findLinkageProblems immediately after create. Path exclusionFile = this.exclusionFile == null ? null : Paths.get(this.exclusionFile); - LinkageChecker linkageChecker = LinkageChecker.create(classPath, entryPoints, exclusionFile); + LinkageChecker linkageChecker = LinkageChecker.create(classPath, entryPoints, null, exclusionFile); ImmutableSet linkageProblems = linkageChecker.findLinkageProblems(); if (reportOnlyReachable) { ClassReferenceGraph classReferenceGraph = linkageChecker.getClassReferenceGraph(); diff --git a/linkage-monitor/src/main/java/com/google/cloud/tools/dependencies/linkagemonitor/LinkageMonitor.java b/linkage-monitor/src/main/java/com/google/cloud/tools/dependencies/linkagemonitor/LinkageMonitor.java index 9e492a83d4..b2823790d9 100644 --- a/linkage-monitor/src/main/java/com/google/cloud/tools/dependencies/linkagemonitor/LinkageMonitor.java +++ b/linkage-monitor/src/main/java/com/google/cloud/tools/dependencies/linkagemonitor/LinkageMonitor.java @@ -294,7 +294,7 @@ private ImmutableSet run(String groupId, String artifactId) List entryPointJars = classpath.subList(0, snapshotManagedDependencies.size()); ImmutableSet problemsInSnapshot = - LinkageChecker.create(classpath, ImmutableSet.copyOf(entryPointJars), null) + LinkageChecker.create(classpath, ImmutableSet.copyOf(entryPointJars), null, null) .findLinkageProblems(); if (problemsInBaseline.equals(problemsInSnapshot)) {