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

Java Raw Type Removal #10010

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

anushka-cseatmnc
Copy link

@anushka-cseatmnc anushka-cseatmnc commented Mar 19, 2025

This PR automates the removal of raw types from the Java codebase in the Uyuni project using a sed script

1)- Replaces List, Map, Set, and Class with generic type parameters (List, Map<Object, Object>, etc.).
2)- Improves code maintainability and eliminates SonarCloud warnings regarding raw types.
3)- Uses Docker to run the script in an isolated environment, ensuring a smooth and consistent update process.
4) -Changes were verified by running SonarCloud analysis after applying modifications.

GUI diffrence

No difference.

Before: Java classes contained raw types (List, Map, Set) without generics.
SonarCloud reported warnings for raw type usage.
image

After:
Raw types were removed and replaced with generics (List, Map<Object, Object>).
SonarCloud confirms the warnings are resolved.
image

  • [✅ ] DONE

Documentation

  • No documentation needed: only internal and user invisible changes.

  • [ ✅ ] DONE

Issue(s): Remove use of Java raw types

Automated Java Raw Type Removal Script

Overview

This script automates the removal of raw types (e.g., List, Map, Set) in Java code for the Uyuni project, replacing them with parameterized generics (List, Map<Object, Object>, etc.). It ensures compliance with SonarCloud recommendations, improving code maintainability.

Prerequisites

Docker installed (optional, for isolated execution)
A local copy of the Uyuni repository
Backup of codebase before making bulk changes

Setup & Execution

Step 1: Clone and Backup the Repository
git clone https://github.com/uyuni-project/uyuni.git
cd uyuni
cp -r uyuni uyuni_backup

Automated Script (remove_raw_types.sh)

This script scans all .java files and replaces raw types with their generic equivalents.
#!/bin/bash

Define the file extension (only modify Java files)

EXTENSION="java"

Define the list of raw types to replace

TYPES=("List" "Map" "Set")

Loop through each Java file in the project

find . -type f -name "*.$EXTENSION" | while read -r file; do
for TYPE in "${TYPES[@]}"; do
# Use sed to replace raw types with generic versions
sed -i -E "s/\b${TYPE}\b/${TYPE}/g" "$file"
done
echo "Processed: $file"
done

echo "Raw type replacement complete"

How to Run the Script

chmod +x remove_raw_types.sh
./remove_raw_types.sh

The script scans .java files, replaces raw types, and prints processed files.

Verification with SonarCloud
After applying the script, verify the changes using SonarCloud:
mvn clean verify sonar:sonar
-Dsonar.projectKey=uyuni-project_uyuni
-Dsonar.host.url=https://sonarcloud.io

If warnings for raw types are removed, the script worked successfully.

Running Inside Docker
For isolated execution, run the script inside a Docker container:
Build Docker Image-
docker build -t java-cleanup .

docker run --rm -v "$(pwd)":/app java-cleanup ./remove_raw_types.sh

This ensures a clean environment without modifying the local setup.

1)- Setup & Execution

git clone https://github.com/uyuni-project/uyuni.git
cd uyuni
cp -r uyuni uyuni_backup


2)-Automated Script (remove_raw_types.sh)

#!/bin/bash

# Define the file extension (only modify Java files)
EXTENSION="java"

# Define the list of raw types to replace
TYPES=("List" "Map" "Set")

# Loop through each Java file in the project
find . -type f -name "*.$EXTENSION" | while read -r file; do
    for TYPE in "${TYPES[@]}"; do
        # Use sed to replace raw types with generic versions
        sed -i -E "s/\b${TYPE}\b/${TYPE}<Object>/g" "$file"
    done
    echo "Processed: $file"
done

echo "Raw type replacement complete"

3)- How to Run the Script

chmod +x remove_raw_types.sh
./remove_raw_types.sh

4)-Verification with SonarCloud

mvn clean verify sonar:sonar \
  -Dsonar.projectKey=uyuni-project_uyuni \
  -Dsonar.host.url=https://sonarcloud.io

If warnings for raw types are removed, the script worked successfully. As given in above image.

Re-run a test

If you need to re-run a test, please mark the related checkbox, it will be unchecked automatically once it has re-run

  • Re-run test "changelog_test"

  • Re-run test "backend_unittests_pgsql"

  • Re-run test "java_pgsql_tests"

  • Re-run test "schema_migration_test_pgsql"

  • Re-run test "susemanager_unittests"

  • Re-run test "javascript_lint"

  • Re-run test "spacecmd_unittests"

Changelogs

Make sure the changelogs entries you are adding are compliant with https://github.com/uyuni-project/uyuni/wiki/Contributing#changelogs and https://github.com/uyuni-project/uyuni/wiki/Contributing#uyuni-projectuyuni-repository

If you don't need a changelog check, please mark this checkbox:

  • No changelog needed

If you uncheck the checkbox after the PR is created, you will need to re-run changelog_test (see below)

Re-run a test

If you need to re-run a test, please mark the related checkbox, it will be unchecked automatically once it has re-run:

  • Re-run test "changelog_test"
  • Re-run test "backend_unittests_pgsql"
  • Re-run test "java_pgsql_tests"
  • Re-run test "schema_migration_test_pgsql"
  • Re-run test "susemanager_unittests"
  • Re-run test "javascript_lint"
  • Re-run test "spacecmd_unittests"

Before you merge

Check How to branch and merge properly!

Not Found
Copy link
Contributor

@cbosdo cbosdo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that an automated approach would work on this: I have even stumbled on at least one case where the type of the objects in the generics would be different depending on how the function is called. Also be careful about sonarcloud results on your PR : having no error doesn't mean you fixed any of the existing ones: it just means you haven't introduced any new error.

This PR is also empty and only contains a .gitignore change...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants