From 099fcbf1f7058739ac076b04ad897e9768aa3e06 Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 12 Nov 2024 11:46:41 -0800 Subject: [PATCH] Refactor instanceof expressions to use new Java 21 name syntax. PiperOrigin-RevId: 695816069 Change-Id: Icd911c319f15d4ba2ed58318a51ba368c16bc16c --- .../java/net/starlark/java/lib/json/Json.java | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/starlark/java/lib/json/Json.java b/src/main/java/net/starlark/java/lib/json/Json.java index 608a0464db560c..a21338b6dc7ea2 100644 --- a/src/main/java/net/starlark/java/lib/json/Json.java +++ b/src/main/java/net/starlark/java/lib/json/Json.java @@ -110,8 +110,8 @@ private void encode(Object x) throws EvalException { return; } - if (x instanceof String) { - appendQuoted((String) x); + if (x instanceof String s) { + appendQuoted(s); return; } @@ -120,8 +120,8 @@ private void encode(Object x) throws EvalException { return; } - if (x instanceof StarlarkFloat) { - if (!Double.isFinite(((StarlarkFloat) x).toDouble())) { + if (x instanceof StarlarkFloat fl) { + if (!Double.isFinite(fl.toDouble())) { throw Starlark.errorf("cannot encode non-finite float %s", x); } out.append(x.toString()); // always contains a decimal point or exponent @@ -136,9 +136,7 @@ private void encode(Object x) throws EvalException { } // e.g. dict (must have string keys) - if (x instanceof Map) { - Map m = (Map) x; - + if (x instanceof Map m) { // Sort keys for determinism. Object[] keys = m.keySet().toArray(); for (Object key : keys) { @@ -169,11 +167,11 @@ private void encode(Object x) throws EvalException { } // e.g. tuple, list - if (x instanceof StarlarkIterable) { + if (x instanceof StarlarkIterable it) { out.append('['); String sep = ""; int i = 0; - for (Object elem : (StarlarkIterable) x) { + for (Object elem : it) { out.append(sep); sep = ","; try { @@ -188,9 +186,7 @@ private void encode(Object x) throws EvalException { } // e.g. struct - if (x instanceof Structure) { - Structure obj = (Structure) x; - + if (x instanceof Structure obj) { // Sort keys for determinism. String[] fields = obj.getFieldNames().toArray(new String[0]); Arrays.sort(fields);