Skip to content

fix: Define a constant instead of duplicating this literal "classpath:/" 4 ti#5

Open
ciadoh wants to merge 1 commit into
mainfrom
sonar-fix/a19cb449-33a4-47ee-88c1-7d8c610db92d-1782126794
Open

fix: Define a constant instead of duplicating this literal "classpath:/" 4 ti#5
ciadoh wants to merge 1 commit into
mainfrom
sonar-fix/a19cb449-33a4-47ee-88c1-7d8c610db92d-1782126794

Conversation

@ciadoh

@ciadoh ciadoh commented Jun 22, 2026

Copy link
Copy Markdown
Owner

SonarQube Issue

Key: a19cb449-33a4-47ee-88c1-7d8c610db92d
File: src/main/java/org/owasp/webgoat/container/AsciiDoctorTemplateResolver.java

AI Analysis & Fix

  1. Explanation:
    The SonarQube issue "java:S1192" is a code smell that indicates duplicated literal strings in the code. In this specific case, the string "classpath:/" is repeated 4 times in the file AsciiDoctorTemplateResolver.java. Code smells like this one increase the risk of introducing bugs when modifying or maintaining the code, since if the string needs to be changed, it would have to be updated in multiple places.

  2. Fix:
    A concrete fix for this issue would be to define a constant for the string "classpath:/" and use that constant throughout the code. Here's an example:

Before:

private String getTemplatePath(String templateName) {
    return "classpath:/" + templateName + ".adoc";
}

private String getTemplatePath(String templateName, String layoutName) {
    return "classpath:/" + templateName + "/" + layoutName + ".adoc";
}

private String getTemplatePath(String templateName, String layoutName, String partName) {
    return "classpath:/" + templateName + "/" + layoutName + "/" + partName + ".adoc";
}

private String getTemplatePath(String templateName, String layoutName, String partName, String extension) {
    return "classpath:/" + templateName + "/" + layoutName + "/" + partName + "." + extension;
}

After:

private static final String CLASSPATH_PREFIX = "classpath:/";

private String getTemplatePath(String templateName) {
    return CLASSPATH_PREFIX + templateName + ".adoc";
}

private String getTemplatePath(String templateName, String layoutName) {
    return CLASSPATH_PREFIX + templateName + "/" + layoutName + ".adoc";
}

private String getTemplatePath(String templateName, String layoutName, String partName) {
    return CLASSPATH_PREFIX + templateName + "/" + layoutName + "/" + partName + ".adoc";
}

private String getTemplatePath(String templateName, String layoutName, String partName, String extension) {
    return CLASSPATH_PREFIX + templateName + "/" + layoutName + "/" + partName + "." + extension;
}
  1. Caveats:
    When applying this fix, make sure that the constant is defined in the appropriate scope. In this example, since the constant is only used within the AsciiDoctorTemplateResolver class, it is defined as a private static final field. However, if the constant is used across multiple classes, it may be more appropriate to define it in an interface or a separate constants class.

Also, ensure that the constant is initialized with the correct value and that it is not changed elsewhere in the code. Changing the value of a constant after it has been defined can lead to unexpected behavior and bugs.


Generated by TechDebt AI

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.

1 participant