bsl-language-server icon indicating copy to clipboard operation
bsl-language-server copied to clipboard

Fix ClassCastException in MagicNumberDiagnostic with soft error handling

Open Copilot opened this issue 5 months ago • 11 comments

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:

  1. Type-safe configuration parsing: Added proper type checking to handle both String and List formats
  2. Soft error handling: Invalid configurations now log warnings and fall back to defaults instead of crashing
  3. Comprehensive error recovery: Try-catch block ensures no exceptions propagate from configuration
  4. Enhanced logging: Added detailed warning messages for troubleshooting invalid configurations

Changes Made

  • Added @Slf4j annotation and logging imports
  • Replaced unsafe cast with instanceof type 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 MagicNumberDiagnostic tests 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.

Copilot avatar Jun 14 '25 07:06 Copilot