Use pattern matching for instanceof where appropriate

See gh-31475
This commit is contained in:
dreis2211
2022-06-20 18:14:16 +02:00
committed by Andy Wilkinson
parent a7b98e7312
commit 5db04da275
278 changed files with 1049 additions and 1126 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -239,8 +239,8 @@ public class CommandRunner implements Iterable<Command> {
private int handleError(boolean debug, Exception ex) {
Set<CommandException.Option> options = NO_EXCEPTION_OPTIONS;
if (ex instanceof CommandException) {
options = ((CommandException) ex).getOptions();
if (ex instanceof CommandException commandException) {
options = commandException.getOptions();
if (options.contains(CommandException.Option.RETHROW)) {
throw (CommandException) ex;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -281,8 +281,8 @@ abstract class ArchiveCommand extends OptionParsingCommand {
@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
for (ASTNode node : nodes) {
if (node instanceof ModuleNode) {
visitModule((ModuleNode) node);
if (node instanceof ModuleNode moduleNode) {
visitModule(moduleNode);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -163,8 +163,7 @@ class InitializrServiceMetadata {
while (keys.hasNext()) {
String key = (String) keys.next();
Object o = root.get(key);
if (o instanceof JSONObject) {
JSONObject child = (JSONObject) o;
if (o instanceof JSONObject child) {
if (child.has(DEFAULT_ATTRIBUTE)) {
result.put(key, child.getString(DEFAULT_ATTRIBUTE));
}
@@ -212,8 +211,8 @@ class InitializrServiceMetadata {
for (Iterator<?> iterator = json.keys(); iterator.hasNext();) {
String key = (String) iterator.next();
Object value = json.get(key);
if (value instanceof String) {
result.put(key, (String) value);
if (value instanceof String string) {
result.put(key, string);
}
}
return result;

View File

@@ -56,8 +56,8 @@ class ServiceCapabilitiesReportGenerator {
*/
String generate(String url) throws IOException {
Object content = this.initializrService.loadServiceCapabilities(url);
if (content instanceof InitializrServiceMetadata) {
return generateHelp(url, (InitializrServiceMetadata) content);
if (content instanceof InitializrServiceMetadata metadata) {
return generateHelp(url, metadata);
}
return content.toString();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -76,8 +76,7 @@ public class SourceOptions {
List<String> sources = new ArrayList<>();
int sourceArgCount = 0;
for (Object option : nonOptionArguments) {
if (option instanceof String) {
String filename = (String) option;
if (option instanceof String filename) {
if ("--".equals(filename)) {
break;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -221,8 +221,8 @@ public class Shell {
boolean handleSigInt() {
Command command = this.lastCommand;
if (command instanceof RunProcessCommand) {
return ((RunProcessCommand) command).handleSigInt();
if (command instanceof RunProcessCommand runProcessCommand) {
return runProcessCommand.handleSigInt();
}
return false;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,8 +57,7 @@ public abstract class AnnotatedNodeASTTransformation implements ASTTransformatio
List<AnnotationNode> annotationNodes = new ArrayList<>();
ClassVisitor classVisitor = new ClassVisitor(source, annotationNodes);
for (ASTNode node : nodes) {
if (node instanceof ModuleNode) {
ModuleNode module = (ModuleNode) node;
if (node instanceof ModuleNode module) {
visitAnnotatedNode(module.getPackage(), annotationNodes);
for (ImportNode importNode : module.getImports()) {
visitAnnotatedNode(importNode, annotationNodes);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -149,8 +149,8 @@ public abstract class AstUtils {
public static ClosureExpression getClosure(BlockStatement block, String name, boolean remove) {
for (ExpressionStatement statement : getExpressionStatements(block)) {
Expression expression = statement.getExpression();
if (expression instanceof MethodCallExpression) {
ClosureExpression closure = getClosure(name, (MethodCallExpression) expression);
if (expression instanceof MethodCallExpression methodCallExpression) {
ClosureExpression closure = getClosure(name, methodCallExpression);
if (closure != null) {
if (remove) {
block.getStatements().remove(statement);
@@ -165,8 +165,8 @@ public abstract class AstUtils {
private static List<ExpressionStatement> getExpressionStatements(BlockStatement block) {
List<ExpressionStatement> statements = new ArrayList<>();
for (Statement statement : block.getStatements()) {
if (statement instanceof ExpressionStatement) {
statements.add((ExpressionStatement) statement);
if (statement instanceof ExpressionStatement expressionStatement) {
statements.add(expressionStatement);
}
}
return statements;
@@ -174,7 +174,7 @@ public abstract class AstUtils {
private static ClosureExpression getClosure(String name, MethodCallExpression expression) {
Expression method = expression.getMethod();
if (method instanceof ConstantExpression && name.equals(((ConstantExpression) method).getValue())) {
if (method instanceof ConstantExpression constantExpression && name.equals(constantExpression.getValue())) {
return (ClosureExpression) ((ArgumentListExpression) expression.getArguments()).getExpression(0);
}
return null;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -62,8 +62,8 @@ public class DependencyAutoConfigurationTransformation implements ASTTransformat
@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
for (ASTNode astNode : nodes) {
if (astNode instanceof ModuleNode) {
visitModule((ModuleNode) astNode);
if (astNode instanceof ModuleNode moduleNode) {
visitModule(moduleNode);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -107,8 +107,8 @@ public class DependencyManagementBomTransformation extends AnnotatedNodeASTTrans
List<Map<String, String>> dependencies = new ArrayList<>(constantExpressions.size());
for (ConstantExpression expression : constantExpressions) {
Object value = expression.getValue();
if (value instanceof String) {
String[] components = ((String) expression.getValue()).split(":");
if (value instanceof String string) {
String[] components = string.split(":");
if (components.length == 3) {
dependency = new HashMap<>();
dependency.put("group", components[0]);
@@ -126,12 +126,12 @@ public class DependencyManagementBomTransformation extends AnnotatedNodeASTTrans
}
private List<ConstantExpression> getConstantExpressions(Expression valueExpression) {
if (valueExpression instanceof ListExpression) {
return getConstantExpressions((ListExpression) valueExpression);
if (valueExpression instanceof ListExpression listExpression) {
return getConstantExpressions(listExpression);
}
if (valueExpression instanceof ConstantExpression
&& ((ConstantExpression) valueExpression).getValue() instanceof String) {
return Arrays.asList((ConstantExpression) valueExpression);
if (valueExpression instanceof ConstantExpression constantExpression
&& constantExpression.getValue() instanceof String) {
return Arrays.asList(constantExpression);
}
reportError("@DependencyManagementBom requires an inline constant that is a string or a string array",
valueExpression);
@@ -141,9 +141,9 @@ public class DependencyManagementBomTransformation extends AnnotatedNodeASTTrans
private List<ConstantExpression> getConstantExpressions(ListExpression valueExpression) {
List<ConstantExpression> expressions = new ArrayList<>();
for (Expression expression : valueExpression.getExpressions()) {
if (expression instanceof ConstantExpression
&& ((ConstantExpression) expression).getValue() instanceof String) {
expressions.add((ConstantExpression) expression);
if (expression instanceof ConstantExpression constantExpression
&& constantExpression.getValue() instanceof String) {
expressions.add(constantExpression);
}
else {
reportError("Each entry in the array must be an inline string constant", expression);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -188,8 +188,8 @@ public class ExtendedGroovyClassLoader extends GroovyClassLoader {
private void findGroovyJarsDirectly(ClassLoader classLoader, Set<URL> urls) {
while (classLoader != null) {
if (classLoader instanceof URLClassLoader) {
for (URL url : ((URLClassLoader) classLoader).getURLs()) {
if (classLoader instanceof URLClassLoader urlClassLoader) {
for (URL url : urlClassLoader.getURLs()) {
if (isGroovyJar(url.toString())) {
urls.add(url);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -58,8 +58,8 @@ public abstract class GenericBomAstTransformation implements SpringBootAstTransf
@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
for (ASTNode astNode : nodes) {
if (astNode instanceof ModuleNode) {
visitModule((ModuleNode) astNode, getBomModule());
if (astNode instanceof ModuleNode moduleNode) {
visitModule(moduleNode, getBomModule());
}
}
}
@@ -107,12 +107,12 @@ public abstract class GenericBomAstTransformation implements SpringBootAstTransf
}
private List<ConstantExpression> getConstantExpressions(Expression valueExpression) {
if (valueExpression instanceof ListExpression) {
return getConstantExpressions((ListExpression) valueExpression);
if (valueExpression instanceof ListExpression listExpression) {
return getConstantExpressions(listExpression);
}
if (valueExpression instanceof ConstantExpression
&& ((ConstantExpression) valueExpression).getValue() instanceof String) {
return Arrays.asList((ConstantExpression) valueExpression);
if (valueExpression instanceof ConstantExpression constantExpression
&& constantExpression.getValue() instanceof String) {
return Arrays.asList(constantExpression);
}
return Collections.emptyList();
}
@@ -120,9 +120,9 @@ public abstract class GenericBomAstTransformation implements SpringBootAstTransf
private List<ConstantExpression> getConstantExpressions(ListExpression valueExpression) {
List<ConstantExpression> expressions = new ArrayList<>();
for (Expression expression : valueExpression.getExpressions()) {
if (expression instanceof ConstantExpression
&& ((ConstantExpression) expression).getValue() instanceof String) {
expressions.add((ConstantExpression) expression);
if (expression instanceof ConstantExpression constantExpression
&& constantExpression.getValue() instanceof String) {
expressions.add(constantExpression);
}
}
return expressions;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,8 +52,7 @@ public class GroovyBeansTransformation implements ASTTransformation {
@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
for (ASTNode node : nodes) {
if (node instanceof ModuleNode) {
ModuleNode module = (ModuleNode) node;
if (node instanceof ModuleNode module) {
for (ClassNode classNode : new ArrayList<>(module.getClasses())) {
if (classNode.isScript()) {
classNode.visitContents(new ClassVisitor(source, classNode));

View File

@@ -153,8 +153,8 @@ public class GroovyCompiler {
private URL[] getExistingUrls() {
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
if (tccl instanceof ExtendedGroovyClassLoader) {
return ((ExtendedGroovyClassLoader) tccl).getURLs();
if (tccl instanceof ExtendedGroovyClassLoader groovyClassLoader) {
return groovyClassLoader.getURLs();
}
else {
return new URL[0];

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -73,9 +73,9 @@ public class ResolveDependencyCoordinatesTransformation extends AnnotatedNodeAST
private String getValue(AnnotationNode annotation) {
Expression expression = annotation.getMember("value");
if (expression instanceof ConstantExpression) {
Object value = ((ConstantExpression) expression).getValue();
return (value instanceof String) ? (String) value : null;
if (expression instanceof ConstantExpression constantExpression) {
Object value = constantExpression.getValue();
return (value instanceof String string) ? string : null;
}
return null;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -184,8 +184,8 @@ public class CliTester implements BeforeEachCallback, AfterEachCallback {
@Override
public void afterEach(ExtensionContext extensionContext) {
for (AbstractCommand command : this.commands) {
if (command != null && command instanceof RunCommand) {
((RunCommand) command).stop();
if (command instanceof RunCommand runCommand) {
runCommand.stop();
}
}
System.clearProperty("disableSpringSnapshotRepos");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -91,9 +91,9 @@ final class GenericBomAstTransformationTests {
private List<String> getValue() {
Expression expression = findAnnotation().getMember("value");
if (expression instanceof ListExpression) {
if (expression instanceof ListExpression listExpression) {
List<String> list = new ArrayList<>();
for (Expression ex : ((ListExpression) expression).getExpressions()) {
for (Expression ex : listExpression.getExpressions()) {
list.add((String) ((ConstantExpression) ex).getValue());
}
return list;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -225,8 +225,8 @@ final class ResolveDependencyCoordinatesTransformationTests {
private Object getGrabAnnotationMemberAsString(String memberName) {
Expression expression = this.grabAnnotation.getMember(memberName);
if (expression instanceof ConstantExpression) {
return ((ConstantExpression) expression).getValue();
if (expression instanceof ConstantExpression constantExpression) {
return constantExpression.getValue();
}
else if (expression == null) {
return null;