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.