Skip to content

Commit 7b0570b

Browse files
blicklycopybara-github
authored andcommitted
Use Java 14 pattern matching instanceof where applicable
See docs.oracle.com/en/java/javase/14/language/pattern-matching-instanceof-operator.html PiperOrigin-RevId: 715097270
1 parent e88380c commit 7b0570b

26 files changed

+105
-123
lines changed

src/com/google/javascript/jscomp/AbstractCommandLineRunner.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1117,11 +1117,11 @@ private Appendable createDefaultOutput() throws IOException {
11171117
}
11181118

11191119
private static void closeAppendable(Appendable output) throws IOException {
1120-
if (output instanceof Flushable) {
1121-
((Flushable) output).flush();
1120+
if (output instanceof Flushable flushable) {
1121+
flushable.flush();
11221122
}
1123-
if (output instanceof Closeable) {
1124-
((Closeable) output).close();
1123+
if (output instanceof Closeable closeable) {
1124+
closeable.close();
11251125
}
11261126
}
11271127

src/com/google/javascript/jscomp/CommandLineRunner.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -1148,10 +1148,9 @@ private void printCategoryUsage(
11481148
stringWriter,
11491149
null,
11501150
(optionHandler) -> {
1151-
if (optionHandler.option instanceof NamedOptionDef) {
1151+
if (optionHandler.option instanceof NamedOptionDef namedOptionDef) {
11521152
return !optionHandler.option.hidden()
1153-
&& optionName.equals(
1154-
((NamedOptionDef) optionHandler.option).name().replaceFirst("^--", ""));
1153+
&& optionName.equals(namedOptionDef.name().replaceFirst("^--", ""));
11551154
}
11561155
return false;
11571156
});
@@ -1179,10 +1178,9 @@ private void printCategoryUsage(
11791178
outputStream,
11801179
null,
11811180
(optionHandler) -> {
1182-
if (optionHandler.option instanceof NamedOptionDef) {
1181+
if (optionHandler.option instanceof NamedOptionDef namedOptionDef) {
11831182
return !optionHandler.option.hidden()
1184-
&& options.contains(
1185-
((NamedOptionDef) optionHandler.option).name().replaceFirst("^--", ""));
1183+
&& options.contains(namedOptionDef.name().replaceFirst("^--", ""));
11861184
}
11871185
return false;
11881186
});

src/com/google/javascript/jscomp/CompilerOptions.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1437,12 +1437,12 @@ public ImmutableMap<String, Node> getDefineReplacements() {
14371437
for (Map.Entry<String, Object> entry : this.defineReplacements.entrySet()) {
14381438
String name = entry.getKey();
14391439
Object value = entry.getValue();
1440-
if (value instanceof Boolean) {
1441-
map.put(name, NodeUtil.booleanNode(((Boolean) value).booleanValue()));
1442-
} else if (value instanceof Number) {
1443-
map.put(name, NodeUtil.numberNode(((Number) value).doubleValue(), null));
1444-
} else if (value instanceof String) {
1445-
map.put(name, IR.string((String) value));
1440+
if (value instanceof Boolean b) {
1441+
map.put(name, NodeUtil.booleanNode(b.booleanValue()));
1442+
} else if (value instanceof Number number) {
1443+
map.put(name, NodeUtil.numberNode(number.doubleValue(), null));
1444+
} else if (value instanceof String string) {
1445+
map.put(name, IR.string(string));
14461446
} else {
14471447
throw new IllegalStateException(String.valueOf(value));
14481448
}

src/com/google/javascript/jscomp/ComposeWarningsGuard.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public ComposeWarningsGuard(WarningsGuard... guards) {
8484
}
8585

8686
void addGuard(WarningsGuard guard) {
87-
if (guard instanceof ComposeWarningsGuard) {
88-
ComposeWarningsGuard composeGuard = (ComposeWarningsGuard) guard;
87+
if (guard instanceof ComposeWarningsGuard composeGuard) {
8988
if (composeGuard.demoteErrors) {
9089
this.demoteErrors = composeGuard.demoteErrors;
9190
}

src/com/google/javascript/jscomp/ConformanceRules.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1469,8 +1469,8 @@ private Rule createRule(AbstractCompiler compiler, Requirement requirement)
14691469
rule = (Rule) (ctor.newInstance(compiler, requirement));
14701470
} catch (InvocationTargetException e) {
14711471
Throwable cause = e.getCause();
1472-
if (cause instanceof InvalidRequirementSpec) {
1473-
throw (InvalidRequirementSpec) cause;
1472+
if (cause instanceof InvalidRequirementSpec invalidRequirementSpec) {
1473+
throw invalidRequirementSpec;
14741474
}
14751475
throw new RuntimeException(cause);
14761476
}

src/com/google/javascript/jscomp/CrossChunkMethodMotion.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ private void moveMethods(Collection<NameInfo> allNameInfo) {
121121
Iterator<Symbol> declarations = nameInfo.getDeclarations().descendingIterator();
122122
while (declarations.hasNext()) {
123123
Symbol symbol = declarations.next();
124-
if (symbol instanceof PrototypeProperty) {
125-
tryToMovePrototypeMethod(nameInfo, deepestCommonModuleRef, (PrototypeProperty) symbol);
126-
} else if (symbol instanceof ClassMemberFunction) {
127-
tryToMoveMemberFunction(nameInfo, deepestCommonModuleRef, (ClassMemberFunction) symbol);
124+
if (symbol instanceof PrototypeProperty prototypeProperty) {
125+
tryToMovePrototypeMethod(nameInfo, deepestCommonModuleRef, prototypeProperty);
126+
} else if (symbol instanceof ClassMemberFunction classMemberFunction) {
127+
tryToMoveMemberFunction(nameInfo, deepestCommonModuleRef, classMemberFunction);
128128
} // else it's a variable definition, and we don't move those.
129129
}
130130
}
@@ -554,8 +554,7 @@ private void moveClassInstanceMethodWithStub(
554554

555555
static boolean hasUnmovableRedeclaration(NameInfo nameInfo, Property prop) {
556556
for (Symbol symbol : nameInfo.getDeclarations()) {
557-
if (symbol instanceof Property) {
558-
Property otherProp = (Property) symbol;
557+
if (symbol instanceof Property otherProp) {
559558
// It is possible to do better here if the dependencies are well defined
560559
// but redefinitions are usually in optional chunks so it isn't likely
561560
// worth the effort to check.

src/com/google/javascript/jscomp/GlobalNamespace.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ static class AstChange {
283283

284284
@Override
285285
public boolean equals(Object obj) {
286-
if (obj instanceof AstChange) {
287-
AstChange other = (AstChange) obj;
286+
if (obj instanceof AstChange other) {
288287
return Objects.equals(this.scope, other.scope) && Objects.equals(this.node, other.node);
289288
}
290289
return false;
@@ -1392,9 +1391,8 @@ private void checkNoExistingRefsForNode(Node node) {
13921391
if (this.refsForNode == null) {
13931392
return;
13941393
}
1395-
if (this.refsForNode instanceof Ref) {
1396-
checkState(
1397-
((Ref) this.refsForNode).node != node, "Ref already exists for node: %s", refsForNode);
1394+
if (this.refsForNode instanceof Ref ref) {
1395+
checkState(ref.node != node, "Ref already exists for node: %s", refsForNode);
13981396
return;
13991397
}
14001398
Ref refForNode = castRefsForNodeMap().get(node);
@@ -1658,8 +1656,8 @@ Collection<Ref> getRefs() {
16581656
if (refsForNode == null) {
16591657
return ImmutableSet.of();
16601658
}
1661-
if (refsForNode instanceof Ref) {
1662-
return ImmutableSet.of((Ref) refsForNode);
1659+
if (refsForNode instanceof Ref ref) {
1660+
return ImmutableSet.of(ref);
16631661
}
16641662
return castRefsForNodeMap().values();
16651663
}
@@ -1675,17 +1673,16 @@ Collection<Ref> getRefs() {
16751673
if (refsForNode == null) {
16761674
return null;
16771675
}
1678-
if (refsForNode instanceof Ref) {
1679-
Ref ref = (Ref) refsForNode;
1676+
if (refsForNode instanceof Ref ref) {
16801677
return ref.getNode() == node ? ref : null;
16811678
}
16821679
return castRefsForNodeMap().get(node);
16831680
}
16841681

16851682
Ref getFirstRef() {
16861683
checkNotNull(refsForNode, "no first Ref to get");
1687-
if (refsForNode instanceof Ref) {
1688-
return (Ref) refsForNode;
1684+
if (refsForNode instanceof Ref ref) {
1685+
return ref;
16891686
}
16901687
return castRefsForNodeMap().values().iterator().next();
16911688
}

src/com/google/javascript/jscomp/NodeTraversal.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ public ControlFlowGraph<Node> getControlFlowGraph(AbstractCompiler compiler) {
200200
ControlFlowGraph<Node> result;
201201
Object o = cfgs.peek();
202202
checkState(o != null);
203-
if (o instanceof Node) {
204-
Node cfgRoot = (Node) o;
203+
if (o instanceof Node cfgRoot) {
205204
result =
206205
ControlFlowAnalysis.builder()
207206
.setCompiler(compiler)
@@ -755,15 +754,15 @@ public void visit(NodeTraversal t, Node n, Node parent) {
755754

756755
@Override
757756
public void enterScope(NodeTraversal t) {
758-
if (insideScopeNode && cb instanceof ScopedCallback) {
759-
((ScopedCallback) cb).enterScope(t);
757+
if (insideScopeNode && cb instanceof ScopedCallback scopedCallback) {
758+
scopedCallback.enterScope(t);
760759
}
761760
}
762761

763762
@Override
764763
public void exitScope(NodeTraversal t) {
765-
if (insideScopeNode && cb instanceof ScopedCallback) {
766-
((ScopedCallback) cb).exitScope(t);
764+
if (insideScopeNode && cb instanceof ScopedCallback scopedCallback) {
765+
scopedCallback.exitScope(t);
767766
}
768767
}
769768
}
@@ -1234,10 +1233,10 @@ public TypedScope getTypedScope() {
12341233
private AbstractScope<?, ?> getAbstractScope(int rootDepth) {
12351234

12361235
Object o = scopes.get(rootDepth);
1237-
if (o instanceof Node) {
1236+
if (o instanceof Node node) {
12381237
// The root scope has a null parent.
12391238
AbstractScope<?, ?> parentScope = (rootDepth > 0) ? getAbstractScope(rootDepth - 1) : null;
1240-
AbstractScope<?, ?> scope = scopeCreator.createScope((Node) o, parentScope);
1239+
AbstractScope<?, ?> scope = scopeCreator.createScope(node, parentScope);
12411240
scopes.set(rootDepth, scope);
12421241
return scope;
12431242
} else {

src/com/google/javascript/jscomp/SourceMap.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ public String toString() {
123123

124124
@Override
125125
public boolean equals(Object other) {
126-
if (other instanceof PrefixLocationMapping) {
127-
return ((PrefixLocationMapping) other).prefix.equals(prefix)
128-
&& ((PrefixLocationMapping) other).replacement.equals(replacement);
126+
if (other instanceof PrefixLocationMapping prefixLocationMapping) {
127+
return prefixLocationMapping.prefix.equals(prefix)
128+
&& prefixLocationMapping.replacement.equals(replacement);
129129
} else {
130130
return false;
131131
}

src/com/google/javascript/jscomp/SymbolTable.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -2368,22 +2368,22 @@ private int getLexicalScopeDepth(SymbolScope scope) {
23682368
}
23692369

23702370
private @Nullable JSType getType(StaticSlot sym) {
2371-
if (sym instanceof StaticTypedSlot) {
2372-
return ((StaticTypedSlot) sym).getType();
2371+
if (sym instanceof StaticTypedSlot staticTypedSlot) {
2372+
return staticTypedSlot.getType();
23732373
}
23742374
return null;
23752375
}
23762376

23772377
private @Nullable JSType getTypeOfThis(StaticScope s) {
2378-
if (s instanceof StaticTypedScope) {
2379-
return ((StaticTypedScope) s).getTypeOfThis();
2378+
if (s instanceof StaticTypedScope staticTypedScope) {
2379+
return staticTypedScope.getTypeOfThis();
23802380
}
23812381
return null;
23822382
}
23832383

23842384
private boolean isTypeInferred(StaticSlot sym) {
2385-
if (sym instanceof StaticTypedSlot) {
2386-
return ((StaticTypedSlot) sym).isTypeInferred();
2385+
if (sym instanceof StaticTypedSlot staticTypedSlot) {
2386+
return staticTypedSlot.isTypeInferred();
23872387
}
23882388
return true;
23892389
}

src/com/google/javascript/jscomp/Timeline.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ private static class Time {
5252

5353
@Override
5454
public boolean equals(Object anObject) {
55-
if (anObject instanceof Time) {
56-
return name.equals(((Time) anObject).name);
55+
if (anObject instanceof Time time) {
56+
return name.equals(time.name);
5757
}
5858
return false;
5959
}

src/com/google/javascript/jscomp/TypeCheck.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3375,8 +3375,8 @@ private boolean isReasonableObjectPropertyKey(JSType type) {
33753375

33763376
// Named types are usually @typedefs. For such types we need to check underlying type specified
33773377
// in @typedef annotation.
3378-
if (type instanceof NamedType) {
3379-
return isReasonableObjectPropertyKey(((NamedType) type).getReferencedType());
3378+
if (type instanceof NamedType namedType) {
3379+
return isReasonableObjectPropertyKey(namedType.getReferencedType());
33803380
}
33813381

33823382
// For union type every alternate must be stringifiable.

src/com/google/javascript/jscomp/TypeInference.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2350,11 +2350,11 @@ private FlowScope traverseInstantiation(Node n, JSType ctorType, FlowScope scope
23502350
ctorType = ctorType.restrictByNotNullOrUndefined();
23512351

23522352
FunctionType ctorFnType = ctorType.toMaybeFunctionType();
2353-
if (ctorFnType == null && ctorType instanceof FunctionType) {
2353+
if (ctorFnType == null && ctorType instanceof FunctionType functionType) {
23542354
// If ctorType is a NoObjectType, then toMaybeFunctionType will
23552355
// return null. But NoObjectType implements the FunctionType
23562356
// interface, precisely because it can validly construct objects.
2357-
ctorFnType = (FunctionType) ctorType;
2357+
ctorFnType = functionType;
23582358
}
23592359

23602360
if (ctorFnType == null || !ctorFnType.isConstructor()) {

src/com/google/javascript/jscomp/ant/CompileTask.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -566,31 +566,31 @@ private boolean setDefine(CompilerOptions options,
566566
String key, Object value) {
567567
boolean success = false;
568568

569-
if (value instanceof String) {
569+
if (value instanceof String string) {
570570
final boolean isTrue = "true".equals(value);
571571
final boolean isFalse = "false".equals(value);
572572

573573
if (isTrue || isFalse) {
574574
options.setDefineToBooleanLiteral(key, isTrue);
575575
} else {
576576
try {
577-
double dblTemp = Double.parseDouble((String) value);
577+
double dblTemp = Double.parseDouble(string);
578578
options.setDefineToDoubleLiteral(key, dblTemp);
579579
} catch (NumberFormatException nfe) {
580580
// Not a number, assume string
581-
options.setDefineToStringLiteral(key, (String) value);
581+
options.setDefineToStringLiteral(key, string);
582582
}
583583
}
584584

585585
success = true;
586-
} else if (value instanceof Boolean) {
587-
options.setDefineToBooleanLiteral(key, (Boolean) value);
586+
} else if (value instanceof Boolean b) {
587+
options.setDefineToBooleanLiteral(key, b);
588588
success = true;
589-
} else if (value instanceof Integer) {
590-
options.setDefineToNumberLiteral(key, (Integer) value);
589+
} else if (value instanceof Integer i) {
590+
options.setDefineToNumberLiteral(key, i);
591591
success = true;
592-
} else if (value instanceof Double) {
593-
options.setDefineToDoubleLiteral(key, (Double) value);
592+
} else if (value instanceof Double d) {
593+
options.setDefineToDoubleLiteral(key, d);
594594
success = true;
595595
}
596596

@@ -706,16 +706,14 @@ private long getLastModifiedTime(List<?> fileLists) {
706706
long lastModified = 0;
707707

708708
for (Object entry : fileLists) {
709-
if (entry instanceof FileList) {
710-
FileList list = (FileList) entry;
709+
if (entry instanceof FileList list) {
711710

712711
for (String fileName : list.getFiles(this.getProject())) {
713712
File path = list.getDir(this.getProject());
714713
File file = new File(path, fileName);
715714
lastModified = max(getLastModifiedTime(file), lastModified);
716715
}
717-
} else if (entry instanceof Path) {
718-
Path path = (Path) entry;
716+
} else if (entry instanceof Path path) {
719717
for (String src : path.list()) {
720718
File file = new File(src);
721719
lastModified = max(getLastModifiedTime(file), lastModified);

src/com/google/javascript/jscomp/base/format/SimpleFormat.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,8 @@ private CharSequence padding(CharSequence source, int startIndex) {
573573
int length = source.length();
574574
if (precision >= 0) {
575575
length = min(length, precision);
576-
if (source instanceof StringBuilder) {
577-
((StringBuilder) source).setLength(length);
576+
if (source instanceof StringBuilder stringBuilder) {
577+
stringBuilder.setLength(length);
578578
} else {
579579
source = source.subSequence(0, length);
580580
}
@@ -633,14 +633,14 @@ private CharSequence transformFromInteger() {
633633
char currentConversionType = formatToken.getConversionType();
634634

635635
long value;
636-
if (arg instanceof Long) {
637-
value = ((Long) arg).longValue();
638-
} else if (arg instanceof Integer) {
639-
value = ((Integer) arg).longValue();
640-
} else if (arg instanceof Short) {
641-
value = ((Short) arg).longValue();
642-
} else if (arg instanceof Byte) {
643-
value = ((Byte) arg).longValue();
636+
if (arg instanceof Long l) {
637+
value = l.longValue();
638+
} else if (arg instanceof Integer i) {
639+
value = i.longValue();
640+
} else if (arg instanceof Short s) {
641+
value = s.longValue();
642+
} else if (arg instanceof Byte b) {
643+
value = b.longValue();
644644
} else {
645645
throw badArgumentType();
646646
}
@@ -742,8 +742,8 @@ private CharSequence transformFromDateTime() {
742742
// this is a total hack... as we don't care...
743743
Date date = null;
744744
{
745-
if (arg instanceof Long) {
746-
date = new Date(((Long) arg).longValue());
745+
if (arg instanceof Long l) {
746+
date = new Date(l.longValue());
747747
} else if (arg instanceof Date) {
748748
date = (Date) arg;
749749
} else {

src/com/google/javascript/jscomp/bundle/TranspilationException.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ private static String format(
8484
}
8585

8686
private static @Nullable TranspilationException tryCastToTranspilationException(Throwable t) {
87-
if (t instanceof TranspilationException) {
88-
return (TranspilationException) t;
87+
if (t instanceof TranspilationException transpilationException) {
88+
return transpilationException;
8989
}
9090
return null;
9191
}

0 commit comments

Comments
 (0)