Add RuleContext#getPrerequisitesIf and AnalysisUtils#filterByProvider overloads for BuiltinProvider. PiperOrigin-RevId: 251515875
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/AnalysisUtils.java b/src/main/java/com/google/devtools/build/lib/analysis/AnalysisUtils.java index 1e37e93..2bc6b77 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/AnalysisUtils.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/AnalysisUtils.java
@@ -136,6 +136,11 @@ return Iterables.filter(prerequisites, target -> target.get(provider) != null); } + /** Returns the iterable of collections that have the specified provider. */ + public static <S extends TransitiveInfoCollection, C extends InfoInterface> + Iterable<S> filterByProvider(Iterable<S> prerequisites, final BuiltinProvider<C> provider) { + return Iterables.filter(prerequisites, target -> target.get(provider) != null); + } /** * Returns the path of the associated manifest file for the path of a Fileset. Works for both
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java index 7779744..b4ab0d7 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
@@ -1072,6 +1072,15 @@ } /** + * Returns all the providers of the specified type that are listed under the specified attribute + * of this target in the BUILD file, and that contain the specified provider. + */ + public <C extends Info> Iterable<? extends TransitiveInfoCollection> getPrerequisitesIf( + String attributeName, Mode mode, final BuiltinProvider<C> classType) { + return AnalysisUtils.filterByProvider(getPrerequisites(attributeName, mode), classType); + } + + /** * Returns the prerequisite referred to by the specified attribute. Also checks whether * the attribute is marked as executable and that the target referred to can actually be * executed.