Fix 17 SonarQube issues including string comparison, variable shadowing, and duplication#4
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
Conversation
Fixed issues: - AZ6wRBe2Qw83x_y7ZeLy for java:S1161 rule - AZ6wRBfHQw83x_y7ZeL2 for java:S1117 rule - AZ6wRBfNQw83x_y7ZeL5 for java:S1192 rule - AZ6wRBfNQw83x_y7ZeL6 for java:S1192 rule - AZ6wRBaXQw83x_y7ZeLD for java:S1117 rule - AZ6wRBfeQw83x_y7ZeMK for java:S1117 rule - AZ6wRBfeQw83x_y7ZeMH for java:S1192 rule - AZ6wRBfeQw83x_y7ZeMJ for java:S1117 rule - AZ6wRBfeQw83x_y7ZeML for java:S1117 rule - AZ6wRBf0Qw83x_y7ZeMT for java:S1117 rule - AZ6wRBf0Qw83x_y7ZeMU for java:S1117 rule - AZ6wRBf0Qw83x_y7ZeMW for java:S1161 rule - AZ6wRBf0Qw83x_y7ZeMV for java:S1117 rule - AZ6wRBeUQw83x_y7ZeLc for java:S4973 rule - AZ6wRBeUQw83x_y7ZeLf for java:S1192 rule - AZ6wRBeUQw83x_y7ZeLi for java:S1192 rule - AZ6wRBeUQw83x_y7ZeLd for java:S1700 rule Generated by SonarQube Agent (task: 9b9fa592-9c27-4363-9380-3055f012f5c4)
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This change resolves 17 SonarQube findings across multiple files by replacing unsafe string comparisons with equals(), eliminating variable shadowing issues through strategic renaming, and extracting duplicated string literals into reusable constants. These improvements enhance code quality, reduce maintenance burden, and prevent potential bugs caused by incorrect object comparison and variable name confusion.
View Project in SonarCloud
Fixed Issues
java:S4973 - Strings and Boxed types should be compared using "equals()". • MAJOR • View issue
Location:
src/main/java/land/oras/Registry.java:148Why is this an issue?
It’s almost always a mistake to compare two instances of
java.lang.Stringor boxed types likejava.lang.Integerusing reference equality==or!=, because it is not comparing actual value but locations in memory.What changed
Replaces the reference equality comparison (
==) between two String variablesauthHeaderSourceandauthHeaderTargetwith explicit null checks (authHeaderSource == null && authHeaderTarget == null). This fixes the bug where Strings were being compared using==instead ofequals(), which compares memory locations rather than actual values.java:S1117 - Rename "descriptor" which hides the field declared at line 66. • MAJOR • View issue
Location:
src/main/java/land/oras/Index.java:165Why is this an issue?
Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.
What changed
Renames the lambda parameter from 'descriptor' to 'manifestDescriptor' in the filter operation at line 165, eliminating the variable shadowing of the 'descriptor' field declared at line 66 of the Index class.
java:S1192 - Define a constant instead of duplicating this literal "%s://%s/%s" 3 times. • CRITICAL • View issue
Location:
src/main/java/land/oras/Registry.java:630Why is this an issue?
Duplicated string literals make the process of refactoring complex and error-prone, as any change would need to be propagated on all occurrences.
What changed
Defines two constants
HTTPSandSCHEME_REGISTRY_PATH_FORMATto replace duplicated string literals "https" (used 4 times) and "%s://%s/%s" (used 3 times). These constants are referenced by other hunks to eliminate the duplicated literals.java:S1192 - Define a constant instead of duplicating this literal "https" 4 times. • CRITICAL • View issue
Location:
src/main/java/land/oras/Registry.java:318Why is this an issue?
Duplicated string literals make the process of refactoring complex and error-prone, as any change would need to be propagated on all occurrences.
What changed
Defines two constants
HTTPSandSCHEME_REGISTRY_PATH_FORMATto replace duplicated string literals "https" (used 4 times) and "%s://%s/%s" (used 3 times). These constants are referenced by other hunks to eliminate the duplicated literals.java:S1117 - Rename "descriptor" which hides the field declared at line 66. • MAJOR • View issue
Location:
src/main/java/land/oras/Index.java:175Why is this an issue?
Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.
What changed
Renames the lambda parameter from 'descriptor' to 'manifestDescriptor' in the filter operation at line 175, eliminating the variable shadowing of the 'descriptor' field declared at line 66 of the Index class.
java:S1117 - Rename "namespace" which hides the field declared at line 74. • MAJOR • View issue
Location:
src/main/java/land/oras/ContainerRef.java:132Why is this an issue?
Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.
What changed
Renames the local variable
namespacetoresolvedNamespaceto avoid shadowing the fieldnamespacedeclared at the class level. This eliminates the confusion and potential bugs caused by a local variable hiding a field with the same name.java:S1117 - Rename "registry" which hides the field declared at line 64. • MAJOR • View issue
Location:
src/main/java/land/oras/ContainerRef.java:153Why is this an issue?
Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.
What changed
Renames the local variable
registrytoresolvedRegistryto avoid shadowing the fieldregistrydeclared at the class level, and replaces the string literal "docker.io" with the constantDOCKER_IOto eliminate string duplication.java:S1192 - Define a constant instead of duplicating this literal "docker.io" 3 times. • CRITICAL • View issue
Location:
src/main/java/land/oras/ContainerRef.java:154Why is this an issue?
Duplicated string literals make the process of refactoring complex and error-prone, as any change would need to be propagated on all occurrences.
What changed
Introduces the constant
DOCKER_IOto replace the duplicated string literal "docker.io" that appears 3 times in the file. This constant is referenced by other hunks to eliminate the duplication, fixing the code smell about duplicating the literal "docker.io".java:S1700 - Rename field "registry" • MAJOR • View issue
Location:
src/main/java/land/oras/Registry.java:98Why is this an issue?
It’s confusing to have a class member with the same name (case differences aside) as its enclosing class. This is particularly so when you consider the common practice of naming a class instance for the class itself.
What changed
Renames the field
registrytoregistryNameto avoid confusion with the enclosing class nameRegistry. This fixes the code smell where a class member has the same name (case differences aside) as its enclosing class.java:S1161 - Add the "@OverRide" annotation above this method signature • MAJOR • View issue
Location:
src/main/java/land/oras/Index.java:329Why is this an issue?
While not mandatory, using the
@Overrideannotation on compliant methods improves readability by making it explicit that methods are overridden.What changed
Adds the missing @OverRide annotation above the withJson method signature at line 329, making it explicit that this method overrides a parent method and improving code readability as recommended by the rule.
java:S1161 - Add the "@OverRide" annotation above this method signature • MAJOR • View issue
Location:
src/main/java/land/oras/Manifest.java:323Why is this an issue?
While not mandatory, using the
@Overrideannotation on compliant methods improves readability by making it explicit that methods are overridden.What changed
This hunk adds the @OverRide annotation above the withJson method in the Manifest class. The method overrides a parent method or implements an interface method, and the static analysis rule requires that such methods be explicitly annotated with @OverRide to improve readability and make the override relationship clear.
java:S1117 - Rename "path" which hides the field declared at line 55. • MAJOR • View issue
Location:
src/main/java/land/oras/OCILayout.java:257Why is this an issue?
Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.
What changed
This hunk renames the local variable
pathtoindexPathin the method at line 257 of OCILayout.java. The original local variablepathwas shadowing a field namedpathdeclared at line 55 of the same class. By renaming the local variable toindexPath, the shadowing is eliminated, making the code clearer and avoiding potential confusion between the local variable and the class field.java:S1117 - Rename "descriptor" which hides the field declared at line 66. • MAJOR • View issue
Location:
src/main/java/land/oras/Index.java:244Why is this an issue?
Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.
What changed
Renames the for-loop variable from 'descriptor' to 'existingDescriptor' at line 244, eliminating the variable shadowing of the 'descriptor' field declared at line 66 of the Index class.
java:S1117 - Rename "registry" which hides the field declared at line 64. • MAJOR • View issue
Location:
src/main/java/land/oras/ContainerRef.java:173Why is this an issue?
Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.
What changed
Eliminates the local variable
registryentirely by inlining the call togetRegistry(), which fixes the shadowing of the class fieldregistry. Also replaces the string literal "docker.io" with the constantDOCKER_IOto eliminate string duplication.java:S1192 - Define a constant instead of duplicating this literal "$manifest" 3 times. • CRITICAL • View issue
Location:
src/main/java/land/oras/Annotations.java:119Why is this an issue?
Duplicated string literals make the process of refactoring complex and error-prone, as any change would need to be propagated on all occurrences.
What changed
Defines two constants MANIFEST_KEY and CONFIG_KEY to replace the duplicated string literals "$manifest" and "$config" respectively. This is the foundational change that enables all other hunks to reference these constants instead of repeating the literal strings, directly addressing both duplicated string literal warnings.
java:S1192 - Define a constant instead of duplicating this literal "$config" 3 times. • CRITICAL • View issue
Location:
src/main/java/land/oras/Annotations.java:128Why is this an issue?
Duplicated string literals make the process of refactoring complex and error-prone, as any change would need to be propagated on all occurrences.
What changed
Defines two constants MANIFEST_KEY and CONFIG_KEY to replace the duplicated string literals "$manifest" and "$config" respectively. This is the foundational change that enables all other hunks to reference these constants instead of repeating the literal strings, directly addressing both duplicated string literal warnings.
java:S1117 - Rename "builder" which hides the field declared at line 89. • MAJOR • View issue
Location:
src/main/java/land/oras/auth/HttpClient.java:609Why is this an issue?
Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.
What changed
Renames the local variable 'builder' to 'requestBuilder' to avoid shadowing the class field 'builder' declared at line 89. This directly fixes the code smell where a local variable hides a field with the same name, eliminating potential confusion and unintended behavior.
SonarQube Remediation Agent uses AI. Check for mistakes.