bsl-language-server
bsl-language-server copied to clipboard
Fix ClassCastException in MagicNumberDiagnostic with soft error handling
The MagicNumberDiagnostic.configure() method was throwing a ClassCastException when the configuration system provided an ArrayList instead of a String for the authorizedNumbers parameter. This caused the entire diagnostic system to fail during bean creation.
Root Cause
The issue occurred at line 72 in MagicNumberDiagnostic.java:
var authorizedNumbersString =
(String) configuration.getOrDefault("authorizedNumbers", DEFAULT_AUTHORIZED_NUMBERS);
When the configuration contained an ArrayList (e.g., ["-1", "0", "1"]), the unsafe cast to String failed with:
ClassCastException: class java.util.ArrayList cannot be cast to class java.lang.String
Solution
Implemented robust configuration handling with soft error recovery:
- Type-safe configuration parsing: Added proper type checking to handle both String and List formats
- Soft error handling: Invalid configurations now log warnings and fall back to defaults instead of crashing
- Comprehensive error recovery: Try-catch block ensures no exceptions propagate from configuration
- Enhanced logging: Added detailed warning messages for troubleshooting invalid configurations
Changes Made
- Added
@Slf4jannotation and logging imports - Replaced unsafe cast with
instanceoftype checking - Added support for both String (
"-1,0,1") and List (["-1","0","1"]) formats - Added comprehensive error handling with fallback to default values
- Added warning logs for invalid configuration types
Testing
- Added test case
testConfigureWithArrayListShouldNotThrowClassCastException()to verify ArrayList handling - Added test case
testConfigureWithInvalidTypeShouldUseDefaults()to verify error recovery - Verified all existing
MagicNumberDiagnostictests continue to pass - Confirmed warning logs are properly generated for invalid configurations
The fix ensures diagnostic configuration errors result in warning messages and graceful fallbacks rather than application crashes, as requested by @nixel2007.
Fixes #3485.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.