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

@@ -120,10 +120,10 @@ class AsciidoctorConventions {
configureOptions(asciidoctorTask);
asciidoctorTask.baseDirFollowsSourceDir();
createSyncDocumentationSourceTask(project, asciidoctorTask);
if (asciidoctorTask instanceof AsciidoctorTask) {
boolean pdf = asciidoctorTask.getName().toLowerCase().contains("pdf");
if (asciidoctorTask instanceof AsciidoctorTask task) {
boolean pdf = task.getName().toLowerCase().contains("pdf");
String backend = (!pdf) ? "spring-html" : "spring-pdf";
((AsciidoctorTask) asciidoctorTask).outputOptions((outputOptions) -> outputOptions.backends(backend));
task.outputOptions((outputOptions) -> outputOptions.backends(backend));
}
}

View File

@@ -300,7 +300,7 @@ public class BomExtension {
public void setModules(List<Object> modules) {
this.modules = modules.stream()
.map((input) -> (input instanceof Module) ? (Module) input : new Module((String) input))
.map((input) -> (input instanceof Module module) ? module : new Module((String) input))
.collect(Collectors.toList());
}
@@ -315,9 +315,8 @@ public class BomExtension {
public Object methodMissing(String name, Object args) {
if (args instanceof Object[] && ((Object[]) args).length == 1) {
Object arg = ((Object[]) args)[0];
if (arg instanceof Closure) {
if (arg instanceof Closure closure) {
ModuleHandler moduleHandler = new ModuleHandler();
Closure<?> closure = (Closure<?>) arg;
closure.setResolveStrategy(Closure.DELEGATE_FIRST);
closure.setDelegate(moduleHandler);
closure.call(moduleHandler);

View File

@@ -255,9 +255,8 @@ public class BomPlugin implements Plugin<Project> {
private Node findChild(Node parent, String name) {
for (Object child : parent.children()) {
if (child instanceof Node) {
Node node = (Node) child;
if ((node.name() instanceof QName) && name.equals(((QName) node.name()).getLocalPart())) {
if (child instanceof Node node) {
if ((node.name() instanceof QName qname) && name.equals(qname.getLocalPart())) {
return node;
}
if (name.equals(node.name())) {
@@ -276,9 +275,8 @@ public class BomPlugin implements Plugin<Project> {
}
private boolean isNodeWithName(Object candidate, String name) {
if (candidate instanceof Node) {
Node node = (Node) candidate;
if ((node.name() instanceof QName) && name.equals(((QName) node.name()).getLocalPart())) {
if (candidate instanceof Node node) {
if ((node.name() instanceof QName qname) && name.equals(qname.getLocalPart())) {
return true;
}
if (name.equals(node.name())) {

View File

@@ -63,8 +63,8 @@ final class StandardGitHubRepository implements GitHubRepository {
return (Integer) response.getBody().get("number");
}
catch (RestClientException ex) {
if (ex instanceof Forbidden) {
System.out.println("Received 403 response with headers " + ((Forbidden) ex).getResponseHeaders());
if (ex instanceof Forbidden forbidden) {
System.out.println("Received 403 response with headers " + forbidden.getResponseHeaders());
}
throw ex;
}

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.
@@ -33,8 +33,8 @@ abstract class AbstractDependencyVersion implements DependencyVersion {
@Override
public int compareTo(DependencyVersion other) {
ComparableVersion otherComparable = (other instanceof AbstractDependencyVersion)
? ((AbstractDependencyVersion) other).comparableVersion : new ComparableVersion(other.toString());
ComparableVersion otherComparable = (other instanceof AbstractDependencyVersion otherVersion)
? otherVersion.comparableVersion : new ComparableVersion(other.toString());
return this.comparableVersion.compareTo(otherComparable);
}

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.
@@ -83,8 +83,8 @@ class ArtifactVersionDependencyVersion extends AbstractDependencyVersion {
protected Optional<ArtifactVersionDependencyVersion> extractArtifactVersionDependencyVersion(
DependencyVersion other) {
ArtifactVersionDependencyVersion artifactVersion = null;
if (other instanceof ArtifactVersionDependencyVersion) {
artifactVersion = (ArtifactVersionDependencyVersion) other;
if (other instanceof ArtifactVersionDependencyVersion otherVersion) {
artifactVersion = otherVersion;
}
return Optional.ofNullable(artifactVersion);
}

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.
@@ -47,10 +47,9 @@ final class ReleaseTrainDependencyVersion implements DependencyVersion {
@Override
public int compareTo(DependencyVersion other) {
if (!(other instanceof ReleaseTrainDependencyVersion)) {
if (!(other instanceof ReleaseTrainDependencyVersion otherReleaseTrain)) {
return -1;
}
ReleaseTrainDependencyVersion otherReleaseTrain = (ReleaseTrainDependencyVersion) other;
int comparison = this.releaseTrain.compareTo(otherReleaseTrain.releaseTrain);
if (comparison != 0) {
return comparison;
@@ -67,10 +66,9 @@ final class ReleaseTrainDependencyVersion implements DependencyVersion {
if (other instanceof CalendarVersionDependencyVersion) {
return false;
}
if (!(other instanceof ReleaseTrainDependencyVersion)) {
if (!(other instanceof ReleaseTrainDependencyVersion otherReleaseTrain)) {
return true;
}
ReleaseTrainDependencyVersion otherReleaseTrain = (ReleaseTrainDependencyVersion) other;
return otherReleaseTrain.compareTo(this) < 0;
}
@@ -84,10 +82,9 @@ final class ReleaseTrainDependencyVersion implements DependencyVersion {
if (other instanceof CalendarVersionDependencyVersion) {
return false;
}
if (!(other instanceof ReleaseTrainDependencyVersion)) {
if (!(other instanceof ReleaseTrainDependencyVersion otherReleaseTrain)) {
return true;
}
ReleaseTrainDependencyVersion otherReleaseTrain = (ReleaseTrainDependencyVersion) other;
return otherReleaseTrain.releaseTrain.equals(this.releaseTrain) && isNewerThan(other);
}

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.
@@ -79,8 +79,8 @@ public class CheckClasspathForUnnecessaryExclusions extends DefaultTask {
}
private void processDependency(Dependency dependency) {
if (dependency instanceof ModuleDependency) {
processDependency((ModuleDependency) dependency);
if (dependency instanceof ModuleDependency moduleDependency) {
processDependency(moduleDependency);
}
}

View File

@@ -373,9 +373,7 @@ public class MavenPluginPlugin implements Plugin<Project> {
@TaskAction
public void createRepository() {
for (ResolvedArtifactResult result : this.runtimeClasspath.getIncoming().getArtifacts()) {
if (result.getId().getComponentIdentifier() instanceof ModuleComponentIdentifier) {
ModuleComponentIdentifier identifier = (ModuleComponentIdentifier) result.getId()
.getComponentIdentifier();
if (result.getId().getComponentIdentifier() instanceof ModuleComponentIdentifier identifier) {
String fileName = result.getFile().getName()
.replace(identifier.getVersion() + "-" + identifier.getVersion(), identifier.getVersion());
File repositoryLocation = this.outputDirectory.dir(identifier.getGroup().replace('.', '/') + "/"