Skip to content

Conversation

@benzonico
Copy link
Contributor

@benzonico benzonico commented Nov 12, 2025

SONARJAVA-5718

Union types in multi-catch blocks now have unique and accurate fully qualified names.

Previously, union types (e.g., MatchException | NumberFormatException in multi-catch) returned a non-unique FQN of their common supertype (e.g., "java.lang.RuntimeException"), which conflicted with actual RuntimeException types.

Changes:

  • Added isUnionType() and getUnionTypes() methods to the Type API
  • Introduced union type alternatives tracking in JSema via a new cache
  • Modified JParser to capture ITypeBinding for each union type alternative
  • Updated fullyQualifiedName() to return sorted, pipe-separated alternatives (e.g., "java.lang.MatchException | java.lang.NumberFormatException")
  • Implemented proper union type detection and alternative retrieval in JType
  • Added default implementations in Symbols for non-union types
  • Updated tests to verify correct union type handling

This enables rules to accurately distinguish between actual exception types and union types in catch clauses.

Part of

benzonico and others added 2 commits November 12, 2025 10:49
Union types in multi-catch blocks now have unique and accurate fully qualified names.

Previously, union types (e.g., `MatchException | NumberFormatException` in multi-catch)
returned a non-unique FQN of their common supertype (e.g., "java.lang.RuntimeException"),
which conflicted with actual RuntimeException types.

Changes:
- Added `isUnionType()` and `getUnionTypes()` methods to the Type API
- Introduced union type alternatives tracking in JSema via a new cache
- Modified JParser to capture ITypeBinding for each union type alternative
- Updated fullyQualifiedName() to return sorted, pipe-separated alternatives
  (e.g., "java.lang.MatchException | java.lang.NumberFormatException")
- Implemented proper union type detection and alternative retrieval in JType
- Added default implementations in Symbols for non-union types
- Updated tests to verify correct union type handling

This enables rules to accurately distinguish between actual exception types
and union types in catch clauses.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
- Add comprehensive test coverage for union type implementation with 4 new tests:
  * Test union types with 3+ alternatives
  * Verify FQN alphabetical ordering for union types
  * Test non-union types return false for isUnionType()
  * Test primitive types are not union types
- Reduce cognitive complexity of fullyQualifiedName() method by extracting logic into separate helper methods:
  * baseQualifiedName() for base type name resolution
  * buildIntersectionTypeName() for intersection type handling
  * buildUnionTypeName() for union type handling
- Fix visibility of alternativeBindings field from public to package-private in JavaTree.UnionTypeTreeImpl

All 91 tests pass successfully.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@sonarqube-next
Copy link

Quality Gate failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 90%)

See analysis details on SonarQube

@github-actions
Copy link
Contributor

This PR is stale because it has been open 7 days with no activity. If there is no activity in the next 7 days it will be closed automatically

@github-actions github-actions bot added the stale label Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant