8000 Refactor instanceof expressions to use new Java 21 name syntax. · coderabbit-test/bazel@12dc0b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12dc0b9

Browse files
katrecopybara-github
authored andcommitted
Refactor instanceof expressions to use new Java 21 name syntax.
PiperOrigin-RevId: 696532159 Change-Id: Iabd92b1f7015b79254a071efc9c623a6e58583db
1 parent 1ddb323 commit 12dc0b9

File tree

5 files changed

+43
-67
lines changed

5 files changed

+43
-67
lines changed

src/main/java/net/starlark/java/eval/Eval.java

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,21 @@ private static TokenKind execStatements(
8989
// We enable it only for statements outside any function (isToplevelFunction)
9090
// and outside any if- or for- statements (!indented).
9191
if (isToplevelFunction && !indented && fr.thread.postAssignHook != null) {
92-
if (stmt instanceof AssignmentStatement) {
93-
AssignmentStatement assign = (AssignmentStatement) stmt;
92+
if (stmt instanceof AssignmentStatement assign) {
9493
for (Identifier id : Identifier.boundIdentifiers(assign.getLHS())) {
9594
Object value = fn(fr).getGlobal(id.getBinding().getIndex());
9695
// TODO(bazel-team): Instead of special casing StarlarkFunction, make it implement
9796
// StarlarkExportable.
98-
if (value instanceof StarlarkFunction) {
97+
if (value instanceof StarlarkFunction func) {
9998
// Optimization: The id token of a StarlarkFunction should be based on its global
10099
// identifier when available. This enables an name-based lookup on deserialization.
101-
((StarlarkFunction) value).export(fr.thread, id.getName());
100+
func.export(fr.thread, id.getName());
102101
} else {
103102
fr.thread.postAssignHook.assign(id.getName(), value);
104103
}
105104
}
106-
} else if (stmt instanceof DefStatement) {
107-
Identifier id = ((DefStatement) stmt).getIdentifier();
105+
} else if (stmt instanceof DefStatement def) {
106+
Identifier id = def.getIdentifier();
108107
((StarlarkFunction) fn(fr).getGlobal(id.getBinding().getIndex()))
109108
.export(fr.thread, id.getName());
110109
}
@@ -314,24 +313,22 @@ private static TokenKind exec(StarlarkThread.Frame fr, Statement st)
314313
*/
315314
private static void assign(StarlarkThread.Frame fr, Expression lhs, Object value)
316315
throws EvalException, InterruptedException {
317-
if (lhs instanceof Identifier) {
316+
if (lhs instanceof Identifier ident) {
318317
// x = ...
319-
assignIdentifier(fr, (Identifier) lhs, value);
318+
assignIdentifier(fr, ident, value);
320319

321-
} else if (lhs instanceof IndexExpression) {
320+
} else if (lhs instanceof IndexExpression index) {
322321
// x[i] = ...
323-
Object object = eval(fr, ((IndexExpression) lhs).getObject());
324-
Object key = eval(fr, ((IndexExpression) lhs).getKey());
322+
Object object = eval(fr, index.getObject());
323+
Object key = eval(fr, index.getKey());
325324
EvalUtils.setIndex(object, key, value);
326325

327-
} else if (lhs instanceof ListExpression) {
326+
} else if (lhs instanceof ListExpression list) {
328327
// a, b, c = ...
329-
ListExpression list = (ListExpression) lhs;
330328
assignSequence(fr, list.getElements(), value);
331329

332-
} else if (lhs instanceof DotExpression) {
330+
} else if (lhs instanceof DotExpression dot) {
333331
// x.f = ...
334-
DotExpression dot = (DotExpression) lhs;
335332
Object object = eval(fr, dot.getObject());
336333
String field = dot.getField().getName();
337334
try {
@@ -398,7 +395,7 @@ private static void execAugmentedAssignment(StarlarkThread.Frame fr, AssignmentS
398395
TokenKind op = stmt.getOperator();
399396
Expression rhs = stmt.getRHS();
400397

401-
if (lhs instanceof Identifier) {
398+
if (lhs instanceof Identifier ident) {
402399
// x op= y (lhs must be evaluated only once)
403400
Object x = eval(fr, lhs);
404401
Object y = eval(fr, rhs);
@@ -409,12 +406,11 @@ private static void execAugmentedAssignment(StarlarkThread.Frame fr, AssignmentS
409406
fr.setErrorLocation(stmt.getOperatorLocation());
410407
throw ex;
411408
}
412-
assignIdentifier(fr, (Identifier) lhs, z);
409+
assignIdentifier(fr, ident, z);
413410

414-
} else if (lhs instanceof IndexExpression) {
411+
} else if (lhs instanceof IndexExpression index) {
415412
// object[index] op= y
416413
// The object and key should be evaluated only once, so we don't use lhs.eval().
417-
IndexExpression index = (IndexExpression) lhs;
418414
Object object = eval(fr, index.getObject());
419415
Object key = eval(fr, index.getKey());
420416
Object x = EvalUtils.index(fr.thread, object, key);
@@ -434,9 +430,8 @@ private static void execAugmentedAssignment(StarlarkThread.Frame fr, AssignmentS
434430
throw ex;
435431
}
436432

437-
} else if (lhs instanceof DotExpression) {
433+
} else if (lhs instanceof DotExpression dot) {
438434
// object.field op= y (lhs must be evaluated only once)
439-
DotExpression dot = (DotExpression) lhs;
440435
Object object = eval(fr, dot.getObject());
441436
String field = dot.getField().getName();
442437
try {
@@ -474,10 +469,9 @@ private static Object inplaceBinaryOp(StarlarkThread.Frame fr, TokenKind op, Obj
474469
case PLUS:
475470
// list += iterable behaves like list.extend(iterable)
476471
// TODO(b/141263526): following Python, allow list+=iterable (but not list+iterable).
477-
if (x instanceof StarlarkList && y instanceof StarlarkList) {
478-
StarlarkList<?> list = (StarlarkList) x;
479-
list.extend(y);
480-
return list;
472+
if (x instanceof StarlarkList<?> xList && y instanceof StarlarkList<?> yList) {
473+
xList.extend(yList);
474+
return xList;
481475
}
482476
break;
483477

@@ -490,41 +484,33 @@ private static Object inplaceBinaryOp(StarlarkThread.Frame fr, TokenKind op, Obj
490484
Map<Object, Object> yMap = (Map<Object, Object>) y;
491485
xDict.putEntries(yMap);
492486
return xDict;
493-
} else if (x instanceof StarlarkSet && y instanceof Set) {
487+
} else if (x instanceof StarlarkSet<?> xSet && y instanceof Set<?> ySet) {
494488
// set |= set merges the contents of the second operand into the first.
495-
@SuppressWarnings("unchecked")
496-
StarlarkSet<Object> xSet = (StarlarkSet<Object>) x;
497-
xSet.update(Tuple.of(y));
489+
xSet.update(Tuple.of(ySet));
498490
return xSet;
499491
}
500492
break;
501493

502494
case AMPERSAND:
503-
if (x instanceof StarlarkSet && y instanceof Set) {
495+
if (x instanceof StarlarkSet<?> xSet && y instanceof Set<?> ySet) {
504496
// set &= set replaces the first set with the intersection of the two sets.
505-
@SuppressWarnings("unchecked")
506-
StarlarkSet<Object> xSet = (StarlarkSet<Object>) x;
507-
xSet.intersectionUpdate(Tuple.of(y));
497+
xSet.intersectionUpdate(Tuple.of(ySet));
508498
return xSet;
509499
}
510500
break;
511501

512502
case CARET:
513-
if (x instanceof StarlarkSet && y instanceof Set) {
503+
if (x instanceof StarlarkSet<?> xSet && y instanceof Set<?> ySet) {
514504
// set ^= set replaces the first set with the symmetric difference of the two sets.
515-
@SuppressWarnings("unchecked")
516-
StarlarkSet<Object> xSet = (StarlarkSet<Object>) x;
517-
xSet.symmetricDifferenceUpdate(y);
505+
xSet.symmetricDifferenceUpdate(ySet);
518506
return xSet;
519507
}
520508
break;
521509

522510
case MINUS:
523-
if (x instanceof StarlarkSet && y instanceof Set) {
511+
if (x instanceof StarlarkSet<?> xSet && y instanceof Set<?> ySet) {
524512
// set -= set removes all elements of the second set from the first set.
525-
@SuppressWarnings("unchecked")
526-
StarlarkSet<Object> xSet = (StarlarkSet<Object>) x;
527-
xSet.differenceUpdate(Tuple.of(y));
513+
xSet.differenceUpdate(Tuple.of(ySet));
528514
return xSet;
529515
}
530516
break;
@@ -568,10 +554,10 @@ private static Object eval(StarlarkThread.Frame fr, Expression expr)
568554
// the StarlarkInt in the IntLiteral (a temporary hack
569555
// until we use a compiled representation).
570556
Number n = ((IntLiteral) expr).getValue();
571-
if (n instanceof Integer) {
572-
return StarlarkInt.of((Integer) n);
573-
} else if (n instanceof Long) {
574-
return StarlarkInt.of((Long) n);
557+
if (n instanceof Integer nInt) {
558+
return StarlarkInt.of(nInt);
559+
} else if (n instanceof Long nLong) {
560+
return StarlarkInt.of(nLong);
575561
} else {
576562
return StarlarkInt.of((BigInteger) n);
577563
}
@@ -707,14 +693,14 @@ private static Object evalCall(StarlarkThread.Frame fr, CallExpression call)
707693
// f(*args) -- varargs
708694
if (star != null) {
709695
Object value = eval(fr, star.getValue());
710-
if (!(value instanceof StarlarkIterable)) {
696+
if (!(value instanceof StarlarkIterable<?> iter)) {
711697
fr.setErrorLocation(star.getStartLocation());
712698
throw Starlark.errorf("argument after * must be an iterable, not %s", Starlark.type(value));
713699
}
714700
// TODO(adonovan): opt: if value.size is known, preallocate (and skip if empty).
715701
ArrayList<Object> list = new ArrayList<>();
716702
Collections.addAll(list, positional);
717-
Iterables.addAll(list, ((Iterable<?>) value));
703+
Iterables.addAll(list, iter);
718704
positional = list.toArray();
719705
}
720706

@@ -723,11 +709,10 @@ private static Object evalCall(StarlarkThread.Frame fr, CallExpression call)
723709
Object value = eval(fr, starstar.getValue());
724710
// Unlike *args, we don't have a Starlark-specific mapping interface to check for in **kwargs,
725711
// so check for Java's Map instead.
726-
if (!(value instanceof Map)) {
712+
if (!(value instanceof Map<?, ?> kwargs)) {
727713
fr.setErrorLocation(starstar.getStartLocation());
728714
throw Starlark.errorf("argument after ** must be a dict, not %s", Starlark.type(value));
729715
}
730-
Map<?, ?> kwargs = (Map<?, ?>) value;
731716
int j = named.length;
732717
named = Arrays.copyOf(named, j + 2 * kwargs.size());
733718
for (Map.Entry<?, ?> e : kwargs.entrySet()) {
@@ -847,8 +832,7 @@ void execClauses(int index) throws EvalException, InterruptedException {
847832
// recursive case: one or more clauses
848833
if (index < comp.getClauses().size()) {
849834
Comprehension.Clause clause = comp.getClauses().get(index);
850-
if (clause instanceof Comprehension.For) {
851-
Comprehension.For forClause = (Comprehension.For) clause;
835+
if (clause instanceof Comprehension.For forClause) {
852836

853837
Iterable<?> seq = evalAsIterable(fr, forClause.getIterable());
854838
EvalUtils.addIterator(seq);

src/main/java/net/starlark/java/eval/EvalUtils.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ static Object binaryOp(TokenKind op, Object x, Object y, StarlarkThread starlark
217217
break;
218218

219219
case STAR:
220-
if (x instanceof StarlarkInt) {
221-
StarlarkInt xi = (StarlarkInt) x;
220+
if (x instanceof StarlarkInt xi) {
222221
if (y instanceof StarlarkInt) {
223222
// int * int
224223
return StarlarkInt.multiply(xi, (StarlarkInt) y);
@@ -327,9 +326,8 @@ static Object binaryOp(TokenKind op, Object x, Object y, StarlarkThread starlark
327326
return StarlarkFloat.mod(xf, yf);
328327
}
329328

330-
} else if (x instanceof String) {
329+
} else if (x instanceof String xs) {
331330
// string % any
332-
String xs = (String) x;
333331
try {
334332
if (y instanceof Tuple) {
335333
return Starlark.formatWithList(semantics, xs, (Tuple) y);
@@ -487,8 +485,7 @@ static Object index(StarlarkThread starlarkThread, Object object, Object key)
487485
// it should go in the implementations of StarlarkIndexable#getIndex that produce non-Starlark
488486
// values.
489487
return result == null ? null : Starlark.fromJava(result, mu);
490-
} else if (object instanceof String) {
491-
String string = (String) object;
488+
} else if (object instanceof String string) {
492489
int index = Starlark.toInt(key, "string index");
493490
index = getSequenceIndex(index, string.length());
494491
return StringModule.memoizedCharToString(string.charAt(index));

src/main/java/net/starlark/java/eval/MethodLibrary.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ private static final class KeyCallException extends RuntimeException {
196196
doc = "A number (int or float)")
197197
})
198198
public Object abs(Object x) throws EvalException {
199-
if (x instanceof StarlarkInt) {
200-
StarlarkInt starlarkInt = (StarlarkInt) x;
199+
if (x instanceof StarlarkInt starlarkInt) {
201200
if (starlarkInt.signum() < 0) {
202201
return StarlarkInt.uminus(starlarkInt);
203202
}
@@ -474,8 +473,7 @@ public boolean bool(Object x) throws EvalException {
474473
@Param(name = "x", doc = "The value to convert.", defaultValue = "unbound"),
475474
})
476475
public StarlarkFloat floatForStarlark(Object x) throws EvalException {
477-
if (x instanceof String) {
478-
String s = (String) x;
476+
if (x instanceof String s) {
479477
if (s.isEmpty()) {
480478
throw Starlark.errorf("empty string");
481479
}

src/main/java/net/starlark/java/eval/MutableStarlarkList.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,13 @@ public void addElementAt(int index, E element) throws EvalException {
146146
@Override
147147
public void addElements(Iterable<? extends E> elements) throws EvalException {
148148
Starlark.checkMutable(this);
149-
if (elements instanceof MutableStarlarkList) {
150-
MutableStarlarkList<?> that = (MutableStarlarkList) elements;
149+
if (elements instanceof MutableStarlarkList<?> that) {
151150
// (safe even if this == that)
152151
growAdditional(that.size);
153152
System.arraycopy(that.elems, 0, this.elems, this.size, that.size);
154153
this.size += that.size;
155-
} else if (elements instanceof Collection) {
154+
} else if (elements instanceof Collection<?> that) {
156155
// collection of known size
157-
Collection<?> that = (Collection) elements;
158156
growAdditional(that.size());
159157
for (Object x : that) {
160158
elems[size++] = x;

src/main/java/net/starlark/java/eval/Starlark.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,7 @@ public static Object getattr(
901901
}
902902

903903
// user-defined field?
904-
if (x instanceof Structure) {
905-
Structure struct = (Structure) x;
904+
if (x instanceof Structure struct) {
906905
Object field = struct.getValue(semantics, name);
907906
if (field != null) {
908907
return Starlark.checkValid(field);

0 commit comments

Comments
 (0)
0