diff options
author | 2017-12-13 20:15:11 -0800 | |
---|---|---|
committer | 2017-12-13 20:17:12 -0800 | |
commit | 1446210debe015946729a71ee2e6992b449bba30 (patch) | |
tree | 3fd9d4aa126ba7b728b8317e681519aa2b07c7e5 /src/main/java/com/google/devtools/build/lib/cmdline | |
parent | 70b6e5d7df8130f4e622e1ff9c1cd224d208d00e (diff) |
Initial @AutoCodec implementation.
PiperOrigin-RevId: 178994972
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/cmdline')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/cmdline/Label.java | 116 |
1 files changed, 64 insertions, 52 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java index 6bb0e5f829..caa5c5ec25 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java @@ -61,15 +61,16 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu * Package names that aren't made relative to the current repository because they mean special * things to Bazel. */ - public static final ImmutableSet<PathFragment> ABSOLUTE_PACKAGE_NAMES = ImmutableSet.of( - // Used for select - PathFragment.create("conditions"), - // dependencies that are a function of the configuration - PathFragment.create("tools/defaults"), - // Visibility is labels aren't actually targets - PathFragment.create("visibility"), - // There is only one //external package - Label.EXTERNAL_PACKAGE_NAME); + public static final ImmutableSet<PathFragment> ABSOLUTE_PACKAGE_NAMES = + ImmutableSet.of( + // Used for select + PathFragment.create("conditions"), + // dependencies that are a function of the configuration + PathFragment.create("tools/defaults"), + // Visibility is labels aren't actually targets + PathFragment.create("visibility"), + // There is only one //external package + Label.EXTERNAL_PACKAGE_NAME); public static final PackageIdentifier EXTERNAL_PACKAGE_IDENTIFIER = PackageIdentifier.createInMainRepo(EXTERNAL_PACKAGE_NAME); @@ -78,10 +79,13 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu public static final SkyFunctionName TRANSITIVE_TRAVERSAL = SkyFunctionName.create("TRANSITIVE_TRAVERSAL"); + public static final LabelCodec CODEC = LabelCodec.INSTANCE; + private static final Interner<Label> LABEL_INTERNER = BlazeInterners.newWeakInterner(); /** * Factory for Labels from absolute string form. e.g. + * * <pre> * //foo/bar * //foo/bar:quux @@ -98,6 +102,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu /** * Factory for Labels from absolute string form. e.g. + * * <pre> * //foo/bar * //foo/bar:quux @@ -106,8 +111,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu * {@literal @}foo//bar:baz * </pre> * - * @param defaultToMain Treat labels in the default repository as being in the main - * one instead. + * @param defaultToMain Treat labels in the default repository as being in the main one instead. */ public static Label parseAbsolute(String absName, boolean defaultToMain) throws LabelSyntaxException { @@ -156,11 +160,10 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu /** * Factory for Labels from separate components. * - * @param packageName The name of the package. The package name does - * <b>not</b> include {@code //}. Must be valid according to - * {@link LabelValidator#validatePackageName}. - * @param targetName The name of the target within the package. Must be - * valid according to {@link LabelValidator#validateTargetName}. + * @param packageName The name of the package. The package name does <b>not</b> include {@code + * //}. Must be valid according to {@link LabelValidator#validatePackageName}. + * @param targetName The name of the target within the package. Must be valid according to {@link + * LabelValidator#validateTargetName}. * @throws LabelSyntaxException if either of the arguments was invalid. */ public static Label create(String packageName, String targetName) throws LabelSyntaxException { @@ -168,8 +171,8 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu } /** - * Similar factory to above, but takes a package identifier to allow external repository labels - * to be created. + * Similar factory to above, but takes a package identifier to allow external repository labels to + * be created. */ public static Label create(PackageIdentifier packageId, String targetName) throws LabelSyntaxException { @@ -182,7 +185,6 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu * <p>Only call this method if you know what you're doing; in particular, don't call it on * arbitrary {@code targetName} inputs */ - public static Label createUnvalidated(PackageIdentifier packageId, String targetName) { return LABEL_INTERNER.intern(new Label(packageId, StringCanonicalizer.intern(targetName))); } @@ -190,6 +192,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu /** * Resolves a relative label using a workspace-relative path to the current working directory. The * method handles these cases: + * * <ul> * <li>The label is absolute. * <li>The label starts with a colon. @@ -246,8 +249,8 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu } /** - * Validates the given package name and returns a canonical {@link PackageIdentifier} instance - * if it is valid. Otherwise it throws a SyntaxException. + * Validates the given package name and returns a canonical {@link PackageIdentifier} instance if + * it is valid. Otherwise it throws a SyntaxException. */ private static PackageIdentifier validatePackageName(String packageIdentifier, String name) throws LabelSyntaxException { @@ -285,9 +288,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu this.hashCode = hashCode(this.name, this.packageIdentifier); } - /** - * A specialization of Arrays.HashCode() that does not require constructing a 2-element array. - */ + /** A specialization of Arrays.HashCode() that does not require constructing a 2-element array. */ private static final int hashCode(Object obj1, Object obj2) { int result = 31 + (obj1 == null ? 0 : obj1.hashCode()); return 31 * result + (obj2 == null ? 0 : obj2.hashCode()); @@ -309,10 +310,14 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu * Returns the name of the package in which this rule was declared (e.g. {@code * //file/base:fileutils_test} returns {@code file/base}). */ - @SkylarkCallable(name = "package", structField = true, - doc = "The package part of this label. " - + "For instance:<br>" - + "<pre class=language-python>Label(\"//pkg/foo:abc\").package == \"pkg/foo\"</pre>") + @SkylarkCallable( + name = "package", + structField = true, + doc = + "The package part of this label. " + + "For instance:<br>" + + "<pre class=language-python>Label(\"//pkg/foo:abc\").package == \"pkg/foo\"</pre>" + ) public String getPackageName() { return packageIdentifier.getPackageFragment().getPathString(); } @@ -322,11 +327,15 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu * {@code @repo//pkg:b}, it will returns {@code external/repo/pkg} and for label {@code //pkg:a}, * it will returns an empty string. */ - @SkylarkCallable(name = "workspace_root", structField = true, - doc = "Returns the execution root for the workspace of this label, relative to the execroot. " - + "For instance:<br>" - + "<pre class=language-python>Label(\"@repo//pkg/foo:abc\").workspace_root ==" - + " \"external/repo\"</pre>") + @SkylarkCallable( + name = "workspace_root", + structField = true, + doc = + "Returns the execution root for the workspace of this label, relative to the execroot. " + + "For instance:<br>" + + "<pre class=language-python>Label(\"@repo//pkg/foo:abc\").workspace_root ==" + + " \"external/repo\"</pre>" + ) public String getWorkspaceRoot() { return packageIdentifier.getRepository().getSourceRoot().toString(); } @@ -343,21 +352,23 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu return packageIdentifier.getPackageFragment(); } - /** - * Returns the label as a path fragment, using the package and the label name. - */ + /** Returns the label as a path fragment, using the package and the label name. */ public PathFragment toPathFragment() { return packageIdentifier.getPackageFragment().getRelative(name); } /** - * Returns the name by which this rule was declared (e.g. {@code //foo/bar:baz} - * returns {@code baz}). + * Returns the name by which this rule was declared (e.g. {@code //foo/bar:baz} returns {@code + * baz}). */ - @SkylarkCallable(name = "name", structField = true, - doc = "The name of this label within the package. " - + "For instance:<br>" - + "<pre class=language-python>Label(\"//pkg/foo:abc\").name == \"abc\"</pre>") + @SkylarkCallable( + name = "name", + structField = true, + doc = + "The name of this label within the package. " + + "For instance:<br>" + + "<pre class=language-python>Label(\"//pkg/foo:abc\").name == \"abc\"</pre>" + ) public String getName() { return name; } @@ -382,13 +393,16 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu } public String getUnambiguousCanonicalForm() { - return packageIdentifier.getRepository() + "//" + packageIdentifier.getPackageFragment() - + ":" + name; + return packageIdentifier.getRepository() + + "//" + + packageIdentifier.getPackageFragment() + + ":" + + name; } /** - * Renders this label in canonical form, except with labels in the main and default - * repositories conflated. + * Renders this label in canonical form, except with labels in the main and default repositories + * conflated. */ public String getDefaultCanonicalForm() { String repository; @@ -397,8 +411,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu } else { repository = packageIdentifier.getRepository().getName(); } - return repository + "//" + packageIdentifier.getPackageFragment() - + ":" + name; + return repository + "//" + packageIdentifier.getPackageFragment() + ":" + name; } /** @@ -517,9 +530,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu return hashCode; } - /** - * Two labels are equal iff both their name and their package name are equal. - */ + /** Two labels are equal iff both their name and their package name are equal. */ @Override public boolean equals(Object other) { if (!(other instanceof Label)) { @@ -527,7 +538,8 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu } Label otherLabel = (Label) other; // Perform the equality comparisons in order from least likely to most likely. - return hashCode == otherLabel.hashCode && name.equals(otherLabel.name) + return hashCode == otherLabel.hashCode + && name.equals(otherLabel.name) && packageIdentifier.equals(otherLabel.packageIdentifier); } |