Skip to content

Fix Sonar S2259 potential NPEs to restore reliability rating (#231) - #234

Merged
AndreasIgel merged 1 commit into
mainfrom
fix/sonar-s2259-npe
Aug 6, 2026
Merged

Fix Sonar S2259 potential NPEs to restore reliability rating (#231)#234
AndreasIgel merged 1 commit into
mainfrom
fix/sonar-s2259-npe

Conversation

@AndreasIgel

Copy link
Copy Markdown
Collaborator

Summary

Fixes the two javabugs:S2259 ("access that may throw a NullPointerException") findings that push the SonarCloud new-code Reliability rating on main to C and fail the quality gate. Restores the reliability rating to A.

Fixes #231.

Changes

JavaLangMapper.isConcreteClass — real missing null-guard. Sonar's interprocedural flow (mapsetEmptyConstructorInfoIfAvailableisConcreteClass) shows typeElement may be null, so typeElement.getKind() could NPE. Guarded the parameter:

return typeElement != null
    && typeElement.getKind() == ElementKind.CLASS
    && !typeElement.getModifiers().contains(Modifier.ABSTRACT);

ImportCollector.collectImports — defensive guard for a Sonar dataflow false positive. GenerationTargetClassDto.generics is a final field initialised to new LinkedList<>() with no setter, so getGenerics() is never null; Sonar cannot prove that across the mapper. Assigned to a local and guarded:

List<GenericParameterDto> generics = classDef.getGenerics();
if (generics != null) {
  generics.forEach(generic -> generic.getUpperBounds().forEach(this::addTypeImports));
}

No behavior change.

Verification

  • mvn -B install -Pmetrics --file pom.xml → BUILD SUCCESS, 311 processor tests pass (0 failures/errors), google-java-format clean.
  • Generated-source gate clean: after rm -rf example/generated-example-builder && mvn -B -q -DskipTests -pl example clean compile, git status --porcelain -- example/generated-example-builder is empty.

Out of scope (tracked separately)

Guard nullable type elements before inspecting their kind and modifiers. Guard nullable generic lists before collecting upper-bound imports.

Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
@sonarqubecloud

sonarqubecloud Bot commented Aug 5, 2026

Copy link
Copy Markdown

@AndreasIgel
AndreasIgel merged commit 3b9ed8f into main Aug 6, 2026
7 checks passed
@AndreasIgel
AndreasIgel deleted the fix/sonar-s2259-npe branch August 6, 2026 17:45
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.

Fix Sonar S2259 potential NPEs (reliability rating) in ImportCollector and JavaLangMapper

1 participant