feat: add null-safe navigation operator (?.) for Java 8 compatibility#497
Merged
lukaszlenart merged 2 commits intoognl-3-4-xfrom Nov 29, 2025
Merged
feat: add null-safe navigation operator (?.) for Java 8 compatibility#497lukaszlenart merged 2 commits intoognl-3-4-xfrom
lukaszlenart merged 2 commits intoognl-3-4-xfrom
Conversation
Add SAFE_DOT token and modify navigationChain production rule to support null-safe navigation syntax (user?.profile?.city). The parser now recognizes the ?. operator and sets a nullSafe flag on ASTChain nodes, enabling null-safe property navigation that returns null instead of throwing exceptions when encountering null intermediate values. Ported from main branch PRs #484 and #496. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Add nullSafe field and null-checking logic in getValueBody().
Returns null when encountering null intermediate values instead
of throwing exceptions.
Update toString() to output ?. operator when nullSafe flag is set.
Add helper method shouldAppendNavigationOperator() to reduce
cognitive complexity.
This enables expressions like:
- user?.profile?.city (returns null if any part is null)
- items?.{? #this > 0} (null-safe collection operations)
- #var?.method() (null-safe method calls)
Ported from main branch PRs #484 and #496, adapted for Java 8
compatibility (ognl-3-4-x branch).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
|
Collaborator
Author
|
@FHannes ported into 3.4.x |
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.




Summary
Port the null-safe navigation operator (
?.) from main branch to ognl-3-4-x branch, enabling safer null handling for Java 8 users.Key Features:
?.operator for null-safe property navigationnullinstead of throwing exceptions when encountering null intermediate valuesPorted from:
Changes
Grammar/Parser (src/main/javacc/ognl.jj)
SAFE_DOTtoken for?.operatornavigationChain()production rule to detect and flag null-safe navigationCore Implementation (src/main/java/ognl/ASTChain.java)
nullSafefield with getter/settergetValueBody()to return null on encountering null intermediate valuestoString()to output?.when nullSafe is trueshouldAppendNavigationOperator()helper methodTest Suite (61+ tests)
NullSafeOperatorTest.java- 26 tests for basic functionalityNullSafeCollectionTest.java- 13 tests for collectionsNullSafeCompilationTest.java- 6 tests for compilationNullSafeIntegrationTest.java- 16 tests for edge casesExamples
Test Results
✅ All 947 tests pass (including 61+ new null-safe operator tests)
Differences from Main Branch
shortCircuitsystem property, so that logic was intentionally skippedArrays.asList()instead ofList.of(), traditional casting instead of pattern matchingTest Plan
?.syntax?.operator🤖 Generated with Claude Code