@@ -89,22 +89,21 @@ private static TokenKind execStatements(
89
89
// We enable it only for statements outside any function (isToplevelFunction)
90
90
// and outside any if- or for- statements (!indented).
91
91
if (isToplevelFunction && !indented && fr .thread .postAssignHook != null ) {
92
- if (stmt instanceof AssignmentStatement ) {
93
- AssignmentStatement assign = (AssignmentStatement ) stmt ;
92
+ if (stmt instanceof AssignmentStatement assign ) {
94
93
for (Identifier id : Identifier .boundIdentifiers (assign .getLHS ())) {
95
94
Object value = fn (fr ).getGlobal (id .getBinding ().getIndex ());
96
95
// TODO(bazel-team): Instead of special casing StarlarkFunction, make it implement
97
96
// StarlarkExportable.
98
- if (value instanceof StarlarkFunction ) {
97
+ if (value instanceof StarlarkFunction func ) {
99
98
// Optimization: The id token of a StarlarkFunction should be based on its global
100
99
// 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 ());
102
101
} else {
103
102
fr .thread .postAssignHook .assign (id .getName (), value );
104
103
}
105
104
}
106
- } else if (stmt instanceof DefStatement ) {
107
- Identifier id = (( DefStatement ) stmt ) .getIdentifier ();
105
+ } else if (stmt instanceof DefStatement def ) {
106
+ Identifier id = def .getIdentifier ();
108
107
((StarlarkFunction ) fn (fr ).getGlobal (id .getBinding ().getIndex ()))
109
108
.export (fr .thread , id .getName ());
110
109
}
@@ -314,24 +313,22 @@ private static TokenKind exec(StarlarkThread.Frame fr, Statement st)
314
313
*/
315
314
private static void assign (StarlarkThread .Frame fr , Expression lhs , Object value )
316
315
throws EvalException , InterruptedException {
317
- if (lhs instanceof Identifier ) {
316
+ if (lhs instanceof Identifier ident ) {
318
317
// x = ...
319
- assignIdentifier (fr , ( Identifier ) lhs , value );
318
+ assignIdentifier (fr , ident , value );
320
319
321
- } else if (lhs instanceof IndexExpression ) {
320
+ } else if (lhs instanceof IndexExpression index ) {
322
321
// 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 ());
325
324
EvalUtils .setIndex (object , key , value );
326
325
327
- } else if (lhs instanceof ListExpression ) {
326
+ } else if (lhs instanceof ListExpression list ) {
328
327
// a, b, c = ...
329
- ListExpression list = (ListExpression ) lhs ;
330
328
assignSequence (fr , list .getElements (), value );
331
329
332
- } else if (lhs instanceof DotExpression ) {
330
+ } else if (lhs instanceof DotExpression dot ) {
333
331
// x.f = ...
334
- DotExpression dot = (DotExpression ) lhs ;
335
332
Object object = eval (fr , dot .getObject ());
336
333
String field = dot .getField ().getName ();
337
334
try {
@@ -398,7 +395,7 @@ private static void execAugmentedAssignment(StarlarkThread.Frame fr, AssignmentS
398
395
TokenKind op = stmt .getOperator ();
399
396
Expression rhs = stmt .getRHS ();
400
397
401
- if (lhs instanceof Identifier ) {
398
+ if (lhs instanceof Identifier ident ) {
402
399
// x op= y (lhs must be evaluated only once)
403
400
Object x = eval (fr , lhs );
404
401
Object y = eval (fr , rhs );
@@ -409,12 +406,11 @@ private static void execAugmentedAssignment(StarlarkThread.Frame fr, AssignmentS
409
406
fr .setErrorLocation (stmt .getOperatorLocation ());
410
407
throw ex ;
411
408
}
412
- assignIdentifier (fr , ( Identifier ) lhs , z );
409
+ assignIdentifier (fr , ident , z );
413
410
414
- } else if (lhs instanceof IndexExpression ) {
411
+ } else if (lhs instanceof IndexExpression index ) {
415
412
// object[index] op= y
416
413
// The object and key should be evaluated only once, so we don't use lhs.eval().
417
- IndexExpression index = (IndexExpression ) lhs ;
418
414
Object object = eval (fr , index .getObject ());
419
415
Object key = eval (fr , index .getKey ());
420
416
Object x = EvalUtils .index (fr .thread , object , key );
@@ -434,9 +430,8 @@ private static void execAugmentedAssignment(StarlarkThread.Frame fr, AssignmentS
434
430
throw ex ;
435
431
}
436
432
437
- } else if (lhs instanceof DotExpression ) {
433
+ } else if (lhs instanceof DotExpression dot ) {
438
434
// object.field op= y (lhs must be evaluated only once)
439
- DotExpression dot = (DotExpression ) lhs ;
440
435
Object object = eval (fr , dot .getObject ());
441
436
String field = dot .getField ().getName ();
442
437
try {
@@ -474,10 +469,9 @@ private static Object inplaceBinaryOp(StarlarkThread.Frame fr, TokenKind op, Obj
474
469
case PLUS :
475
470 // list += iterable behaves like list.extend(iterable)
476
471
// 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 ;
481
475
}
482
476
break ;
483
477
@@ -490,41 +484,33 @@ private static Object inplaceBinaryOp(StarlarkThread.Frame fr, TokenKind op, Obj
490
484
Map <Object , Object > yMap = (Map <Object , Object >) y ;
491
485
xDict .putEntries (yMap );
492
486
return xDict ;
493
- } else if (x instanceof StarlarkSet && y instanceof Set ) {
487
+ } else if (x instanceof StarlarkSet <?> xSet && y instanceof Set <?> ySet ) {
494
488
// 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 ));
498
490
return xSet ;
499
491
}
500
492
break ;
501
493
502
494
case AMPERSAND :
503
- if (x instanceof StarlarkSet && y instanceof Set ) {
495
+ if (x instanceof StarlarkSet <?> xSet && y instanceof Set <?> ySet ) {
504
496
// 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 ));
508
498
return xSet ;
509
499
}
510
500
break ;
511
501
512
502
case CARET :
513
- if (x instanceof StarlarkSet && y instanceof Set ) {
503
+ if (x instanceof StarlarkSet <?> xSet && y instanceof Set <?> ySet ) {
514
504
// 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 );
518
506
return xSet ;
519
507
}
520
508
break ;
521
509
522
510
case MINUS :
523
- if (x instanceof StarlarkSet && y instanceof Set ) {
511
+ if (x instanceof StarlarkSet <?> xSet && y instanceof Set <?> ySet ) {
524
512
// 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 ));
528
514
return xSet ;
529
515
}
530
516
break ;
@@ -568,10 +554,10 @@ private static Object eval(StarlarkThread.Frame fr, Expression expr)
568
554
// the StarlarkInt in the IntLiteral (a temporary hack
569
555
// until we use a compiled representation).
570
556
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 );
575
561
} else {
576
562
return StarlarkInt .of ((BigInteger ) n );
577
563
}
@@ -707,14 +693,14 @@ private static Object evalCall(StarlarkThread.Frame fr, CallExpression call)
707
693
// f(*args) -- varargs
708
694
if (star != null ) {
709
695
Object value = eval (fr , star .getValue ());
710
- if (!(value instanceof StarlarkIterable )) {
696
+ if (!(value instanceof StarlarkIterable <?> iter )) {
711
697
fr .setErrorLocation (star .getStartLocation ());
712
698
throw Starlark .errorf ("argument after * must be an iterable, not %s" , Starlark .type (value ));
713
699
}
714
700
// TODO(adonovan): opt: if value.size is known, preallocate (and skip if empty).
715
701
ArrayList <Object > list = new ArrayList <>();
716
702
Collections .addAll (list , positional );
717
- Iterables .addAll (list , (( Iterable <?>) value ) );
703
+ Iterables .addAll (list , iter );
718
704
positional = list .toArray ();
719
705
}
720
706
@@ -723,11 +709,10 @@ private static Object evalCall(StarlarkThread.Frame fr, CallExpression call)
723
709
Object value = eval (fr , starstar .getValue ());
724
710
// Unlike *args, we don't have a Starlark-specific mapping interface to check for in **kwargs,
725
711
// so check for Java's Map instead.
726
- if (!(value instanceof Map )) {
712
+ if (!(value instanceof Map <?, ?> kwargs )) {
727
713
fr .setErrorLocation (starstar .getStartLocation ());
728
714
throw Starlark .errorf ("argument after ** must be a dict, not %s" , Starlark .type (value ));
729
715
}
730
- Map <?, ?> kwargs = (Map <?, ?>) value ;
731
716
int j = named .length ;
732
717
named = Arrays .copyOf (named , j + 2 * kwargs .size ());
733
718
for (Map .Entry <?, ?> e : kwargs .entrySet ()) {
@@ -847,8 +832,7 @@ void execClauses(int index) throws EvalException, InterruptedException {
847
832
// recursive case: one or more clauses
848
833
if (index < comp .getClauses ().size ()) {
849
834
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 ) {
852
836
853
837
Iterable <?> seq = evalAsIterable (fr , forClause .getIterable ());
854
838
EvalUtils .addIterator (seq );
0 commit comments