Skip to content

Commit

Permalink
Refactor instanceof expressions to use new Java 21 name syntax.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 696187570
Change-Id: I5f1acd358aeefaf4a35e574968bebad8818e9b5c
  • Loading branch information
katre authored and copybara-github committed Nov 13, 2024
1 parent aa5d821 commit 72a9d49
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,7 @@ public void addAspect(StarlarkAspect starlarkAspect) throws EvalException {

private void addAspect(StarlarkAspect starlarkAspect, @Nullable String requiredByAspect)
throws EvalException {
if (starlarkAspect instanceof StarlarkDefinedAspect) {
StarlarkDefinedAspect starlarkDefinedAspect = (StarlarkDefinedAspect) starlarkAspect;
if (starlarkAspect instanceof StarlarkDefinedAspect starlarkDefinedAspect) {
if (!starlarkDefinedAspect.isExported()) {
throw Starlark.errorf(
"Aspects should be top-level values in extension files that define them.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,7 @@ public final boolean equals(Object that) {
if (this == that) {
return true;
}
if (that instanceof AutoloadSymbols) {
AutoloadSymbols other = (AutoloadSymbols) that;
if (that instanceof AutoloadSymbols other) {
// These fields are used to generate all other private fields.
// Thus, other fields don't need to be included in comparison.
return this.bzlmodEnabled == other.bzlmodEnabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public static void visitTarget(
return;
}

if (target instanceof Rule) {
Rule rule = (Rule) target;
if (target instanceof Rule rule) {
visitRuleVisibility(rule, edgeFilter, labelProcessor);
visitRule(rule, edgeFilter, labelProcessor);
visitRuleToolchains(rule, edgeFilter, labelProcessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ private void fieldElement(String name, Object v) throws EvalException {
indent--;
emitLine("}");

} else if (v instanceof String) {
String s = (String) v;
} else if (v instanceof String s) {
emitLine(
name, ": \"", s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n"), "\"");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2429,8 +2429,7 @@ private void populateDefaultRuleAttributeValues(
// be discovered and propagated here.
Object valueToSet;
Object defaultValue = attr.getDefaultValue(null);
if (defaultValue instanceof StarlarkComputedDefaultTemplate) {
StarlarkComputedDefaultTemplate template = (StarlarkComputedDefaultTemplate) defaultValue;
if (defaultValue instanceof StarlarkComputedDefaultTemplate template) {
valueToSet = template.computePossibleValues(attr, rule, pkgBuilder.getLocalEventHandler());
} else if (defaultValue instanceof ComputedDefault) {
// Compute all possible values to verify that the ComputedDefault is well-defined. This was
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,8 +730,7 @@ private static Object starlarkifyValue(Mutability mu, Object val, Package pkg) {
};
}

if (val instanceof Label) {
Label l = (Label) val;
if (val instanceof Label l) {
if (l.getPackageName().equals(pkg.getName())) {
// TODO(https://github.com/bazelbuild/bazel/issues/13828): do not ignore the repo component
// of the label.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ public static Location getLocationMaybe(Target target) {
public static String formatMissingEdge(
@Nullable Target target, Label label, NoSuchThingException e, @Nullable Attribute attr) {
// instanceof returns false if target is null (which is exploited here)
if (target instanceof Rule) {
Rule rule = (Rule) target;
if (target instanceof Rule rule) {
if (isExplicitDependency(rule, label)) {
return String.format("%s and referenced by '%s'", e.getMessage(), target.getLabel());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ public String toString() {
@Override
public StarlarkInt convert(Object x, Object what, LabelConverter labelConverter)
throws ConversionException {
if (x instanceof StarlarkInt) {
StarlarkInt i = (StarlarkInt) x;
if (x instanceof StarlarkInt i) {
try {
i.toIntUnchecked(); // assert signed 32-bit
} catch (
Expand Down

0 comments on commit 72a9d49

Please sign in to comment.