Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8482] Use instanceof assignments to get rid of casting expressions #2018

Merged
merged 6 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private static boolean doGet(Map<String, ?> userProperties, String key, boolean
}

private static boolean doGet(Object val, boolean def) {
if (val instanceof Boolean) {
return (Boolean) val;
if (val instanceof Boolean bool) {
return bool;
} else if (val != null) {
return Boolean.parseBoolean(val.toString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ private String getFullClassName(TypeElement typeElement) {
StringBuilder className = new StringBuilder(typeElement.getSimpleName());
Element enclosingElement = typeElement.getEnclosingElement();

while (enclosingElement instanceof TypeElement) {
className.insert(0, "$").insert(0, ((TypeElement) enclosingElement).getSimpleName());
while (enclosingElement instanceof TypeElement enclosingTypeElement) {
className.insert(0, "$").insert(0, enclosingTypeElement.getSimpleName());
enclosingElement = enclosingElement.getEnclosingElement();
}

if (enclosingElement instanceof PackageElement) {
className.insert(0, ".").insert(0, ((PackageElement) enclosingElement).getQualifiedName());
if (enclosingElement instanceof PackageElement packageElement) {
className.insert(0, ".").insert(0, packageElement.getQualifiedName());
}

return className.toString();
Expand Down
10 changes: 4 additions & 6 deletions api/maven-api-model/src/main/mdo/maven.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -2465,8 +2465,7 @@
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object other) {
if (other instanceof Plugin) {
Plugin otherPlugin = (Plugin) other;
if (other instanceof Plugin otherPlugin) {
return getKey().equals(otherPlugin.getKey());
}
return false;
Expand Down Expand Up @@ -3249,13 +3248,12 @@
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof Extension)) {
return false;
} else {
Extension e = (Extension) o;
} else if (o instanceof Extension e) {
return java.util.Objects.equals(e.getArtifactId(), getArtifactId())
&& java.util.Objects.equals(e.getGroupId(), getGroupId())
&& java.util.Objects.equals(e.getVersion(), getVersion());
} else {
return false;
}
}

Expand Down
7 changes: 3 additions & 4 deletions api/maven-api-toolchain/src/main/mdo/toolchains.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,11 @@
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (!(other instanceof ToolchainModel)) {
return false;
} else {
ToolchainModel that = (ToolchainModel) other;
} else if (other instanceof ToolchainModel that) {
return java.util.Objects.equals(this.getType(), that.getType())
&& java.util.Objects.equals(this.getProvides(), that.getProvides());
} else {
return false;
}
} //-- boolean equals(Object)
]]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,8 @@ private String toListString() {
if (buffer.length() > 1) {
buffer.append(", ");
}
if (item instanceof ListItem) {
buffer.append(((ListItem) item).toListString());
if (item instanceof ListItem listItem) {
buffer.append(listItem.toListString());
} else {
buffer.append(item);
}
Expand Down Expand Up @@ -783,7 +783,7 @@ public String getCanonical() {

@Override
public boolean equals(Object o) {
return (o instanceof ComparableVersion) && items.equals(((ComparableVersion) o).items);
return o instanceof ComparableVersion comparableVersion && items.equals(comparableVersion.items);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ public boolean equals(Object other) {
return true;
}

if (!(other instanceof ArtifactVersion)) {
return false;
if (other instanceof ArtifactVersion artifactVersion) {
return compareTo(artifactVersion) == 0;
}

return compareTo((ArtifactVersion) other) == 0;
return false;
}

public int compareTo(ArtifactVersion otherVersion) {
if (otherVersion instanceof DefaultArtifactVersion) {
return this.comparable.compareTo(((DefaultArtifactVersion) otherVersion).comparable);
if (otherVersion instanceof DefaultArtifactVersion defaultArtifactVersion) {
return this.comparable.compareTo(defaultArtifactVersion.comparable);
} else {
return compareTo(new DefaultArtifactVersion(otherVersion.toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,32 +112,31 @@ public boolean equals(Object other) {
return true;
}

if (!(other instanceof Restriction)) {
return false;
}

Restriction restriction = (Restriction) other;
if (lowerBound != null) {
if (!lowerBound.equals(restriction.lowerBound)) {
if (other instanceof Restriction restriction) {
if (lowerBound != null) {
if (!lowerBound.equals(restriction.lowerBound)) {
return false;
}
} else if (restriction.lowerBound != null) {
return false;
}
} else if (restriction.lowerBound != null) {
return false;
}

if (lowerBoundInclusive != restriction.lowerBoundInclusive) {
return false;
}
if (lowerBoundInclusive != restriction.lowerBoundInclusive) {
return false;
}

if (upperBound != null) {
if (!upperBound.equals(restriction.upperBound)) {
if (upperBound != null) {
if (!upperBound.equals(restriction.upperBound)) {
return false;
}
} else if (restriction.upperBound != null) {
return false;
}
} else if (restriction.upperBound != null) {

return upperBoundInclusive == restriction.upperBoundInclusive;
} else {
return false;
}

return upperBoundInclusive == restriction.upperBoundInclusive;
}

public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,12 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof VersionRange)) {
if (obj instanceof VersionRange other) {
return Objects.equals(recommendedVersion, other.recommendedVersion)
&& Objects.equals(restrictions, other.restrictions);
} else {
return false;
}
VersionRange other = (VersionRange) obj;

return Objects.equals(recommendedVersion, other.recommendedVersion)
&& Objects.equals(restrictions, other.restrictions);
}

public int hashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public void deploy(
request.addMetadata(relatedMetadata.get(versionKey));

for (ArtifactMetadata metadata : artifact.getMetadataList()) {
if (metadata instanceof ProjectArtifactMetadata) {
if (metadata instanceof ProjectArtifactMetadata projectArtifactMetadata) {
org.eclipse.aether.artifact.Artifact pomArtifact = new SubArtifact(mainArtifact, "", "pom");
pomArtifact = pomArtifact.setFile(((ProjectArtifactMetadata) metadata).getFile());
pomArtifact = pomArtifact.setFile(projectArtifactMetadata.getFile());
request.addArtifact(pomArtifact);
} else if (metadata instanceof SnapshotArtifactRepositoryMetadata
|| metadata instanceof ArtifactRepositoryMetadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public void install(File source, Artifact artifact, ArtifactRepository localRepo
request.addArtifact(mainArtifact);

for (ArtifactMetadata metadata : artifact.getMetadataList()) {
if (metadata instanceof ProjectArtifactMetadata) {
if (metadata instanceof ProjectArtifactMetadata projectArtifactMetadata) {
org.eclipse.aether.artifact.Artifact pomArtifact = new SubArtifact(mainArtifact, "", "pom");
pomArtifact = pomArtifact.setFile(((ProjectArtifactMetadata) metadata).getFile());
pomArtifact = pomArtifact.setFile(projectArtifactMetadata.getFile());
request.addArtifact(pomArtifact);
} else if (metadata instanceof SnapshotArtifactRepositoryMetadata
|| metadata instanceof ArtifactRepositoryMetadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ private File getArtifactMetadataFromDeploymentRepository(
}
}
} finally {
if (metadata instanceof RepositoryMetadata) {
updateCheckManager.touch((RepositoryMetadata) metadata, remoteRepository, file);
if (metadata instanceof RepositoryMetadata repositoryMetadata) {
updateCheckManager.touch(repositoryMetadata, remoteRepository, file);
}
}
return file;
Expand All @@ -382,7 +382,7 @@ public void deploy(
ArtifactMetadata metadata, ArtifactRepository localRepository, ArtifactRepository deploymentRepository)
throws RepositoryMetadataDeploymentException {
File file;
if (metadata instanceof RepositoryMetadata) {
if (metadata instanceof RepositoryMetadata repositoryMetadata) {
getLogger().info("Retrieving previous metadata from " + deploymentRepository.getId());
try {
file = getArtifactMetadataFromDeploymentRepository(metadata, localRepository, deploymentRepository);
Expand All @@ -395,7 +395,7 @@ public void deploy(

if (file.isFile()) {
try {
fixTimestamp(file, readMetadata(file), ((RepositoryMetadata) metadata).getMetadata());
fixTimestamp(file, readMetadata(file), repositoryMetadata.getMetadata());
} catch (RepositoryMetadataReadException e) {
// will be reported via storeInlocalRepository
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public Path getPath() {
}

public Nature getNature() {
if (metadata instanceof RepositoryMetadata) {
return switch (((RepositoryMetadata) metadata).getNature()) {
if (metadata instanceof RepositoryMetadata repositoryMetadata) {
return switch (repositoryMetadata.getNature()) {
case RepositoryMetadata.RELEASE_OR_SNAPSHOT -> Nature.RELEASE_OR_SNAPSHOT;
case RepositoryMetadata.SNAPSHOT -> Nature.SNAPSHOT;
default -> Nature.RELEASE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,8 @@ public void run() {

@Override
public void dispose() {
if (executor instanceof ExecutorService) {
((ExecutorService) executor).shutdownNow();
if (executor instanceof ExecutorService executorService) {
executorService.shutdownNow();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ public boolean equals(Object obj) {
return true;
}

if (!(obj instanceof InversionArtifactFilter)) {
if (obj instanceof InversionArtifactFilter other) {
return toInvert.equals(other.toInvert);
} else {
return false;
}

InversionArtifactFilter other = (InversionArtifactFilter) obj;

return toInvert.equals(other.toInvert);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ public boolean equals(Object obj) {
return true;
}

if (!(obj instanceof OrArtifactFilter)) {
if (obj instanceof OrArtifactFilter other) {
return filters.equals(other.filters);
} else {
return false;
}

OrArtifactFilter other = (OrArtifactFilter) obj;

return filters.equals(other.filters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ public boolean equals(Object obj) {
return true;
}

if (!(obj instanceof TypeArtifactFilter)) {
if (obj instanceof TypeArtifactFilter other) {
return type.equals(other.type);
} else {
return false;
}

TypeArtifactFilter other = (TypeArtifactFilter) obj;

return type.equals(other.type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ private List<ArtifactRepository> normalizeToArtifactRepositories(
List<ArtifactRepository> repos = new ArrayList<>(repositories.size());

for (Object repository : repositories) {
if (repository instanceof Repository) {
if (repository instanceof Repository repositoryInstance) {
try {
ArtifactRepository repo = repositorySystem.buildArtifactRepository((Repository) repository);
ArtifactRepository repo = repositorySystem.buildArtifactRepository(repositoryInstance);
repositorySystem.injectMirror(request.getRepositorySession(), Arrays.asList(repo));
repositorySystem.injectProxy(request.getRepositorySession(), Arrays.asList(repo));
repositorySystem.injectAuthentication(request.getRepositorySession(), Arrays.asList(repo));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,24 +311,22 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof Artifact)) {
return false;
}

Artifact a = (Artifact) o;

if (!a.getGroupId().equals(getGroupId())) {
return false;
} else if (!a.getArtifactId().equals(getArtifactId())) {
return false;
} else if (!a.getVersion().equals(getVersion())) {
return false;
} else if (!a.getType().equals(getType())) {
return false;
if (o instanceof Artifact a) {
if (!a.getGroupId().equals(getGroupId())) {
return false;
} else if (!a.getArtifactId().equals(getArtifactId())) {
return false;
} else if (!a.getVersion().equals(getVersion())) {
return false;
} else if (!a.getType().equals(getType())) {
return false;
} else {
return a.getClassifier() == null
? getClassifier() == null
: a.getClassifier().equals(getClassifier());
}
} else {
return a.getClassifier() == null
? getClassifier() == null
: a.getClassifier().equals(getClassifier());
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,14 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof CacheKey)) {
if (o instanceof CacheKey other) {
return pomHash == other.pomHash
&& artifactEquals(artifact, other.artifact)
&& resolveManagedVersions == other.resolveManagedVersions
&& repositoriesEquals(repositories, other.repositories);
} else {
return false;
}

CacheKey other = (CacheKey) o;

return pomHash == other.pomHash
&& artifactEquals(artifact, other.artifact)
&& resolveManagedVersions == other.resolveManagedVersions
&& repositoriesEquals(repositories, other.repositories);
}
}

Expand Down
Loading
Loading