Add TargetPattern.parseConstant(@CompileTimeConstant String) for constant patterns.
This function is permitted to throw a RuntimeException to facilitate its usage in
static initializers.
PiperOrigin-RevId: 281863421
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java
index 3b314a4..f6f9215 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java
@@ -30,6 +30,8 @@
import com.google.devtools.build.lib.concurrent.ThreadSafeBatchCallback;
import com.google.devtools.build.lib.util.StringUtilities;
import com.google.devtools.build.lib.vfs.PathFragment;
+import com.google.errorprone.annotations.CheckReturnValue;
+import com.google.errorprone.annotations.CompileTimeConstant;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
@@ -881,15 +883,24 @@
}
/**
- * Absolutizes the target pattern to the offset.
- * Patterns starting with "//" are absolute and not modified.
- * Assumes the given pattern is not invalid wrt leading "/"s.
+ * Parses a constant string TargetPattern, throwing IllegalStateException on invalid pattern.
+ */
+ @CheckReturnValue
+ public TargetPattern parseConstantUnchecked(@CompileTimeConstant String pattern) {
+ try {
+ return parse(pattern);
+ } catch (TargetParsingException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ /**
+ * Absolutizes the target pattern to the offset. Patterns starting with "//" are absolute and
+ * not modified. Assumes the given pattern is not invalid wrt leading "/"s.
*
- * If the offset is "foo":
- * absolutize(":bar") --> "//foo:bar"
- * absolutize("bar") --> "//foo/bar"
- * absolutize("//biz/bar") --> "//biz/bar" (absolute)
- * absolutize("biz:bar") --> "//foo/biz:bar"
+ * <p>If the offset is "foo": absolutize(":bar") --> "//foo:bar" absolutize("bar") -->
+ * "//foo/bar" absolutize("//biz/bar") --> "//biz/bar" (absolute) absolutize("biz:bar") -->
+ * "//foo/biz:bar"
*
* @param pattern The target pattern to parse.
* @return the pattern, absolutized to the offset if approprate.
diff --git a/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java
index 0048197..50db0b9c 100644
--- a/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java
+++ b/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java
@@ -102,17 +102,8 @@
private static final Function<SkyKey, ConfiguredTargetKey> SKYKEY_TO_CTKEY =
skyKey -> (ConfiguredTargetKey) skyKey.argument();
- private static final ImmutableList<TargetPattern> ALL_PATTERNS;
-
- static {
- TargetPattern targetPattern;
- try {
- targetPattern = TargetPattern.defaultParser().parse("//...");
- } catch (TargetParsingException e) {
- throw new IllegalStateException(e);
- }
- ALL_PATTERNS = ImmutableList.of(targetPattern);
- }
+ private static final ImmutableList<TargetPattern> ALL_PATTERNS =
+ ImmutableList.of(TargetPattern.defaultParser().parseConstantUnchecked("//..."));
protected RecursivePackageProviderBackedTargetPatternResolver resolver;