You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] ExhaustiveSwitchHasDefault: False positive with final fields not initialized in constructor #5670
Description:
ExhaustiveSwitchHasDefault cannot be applied because the compiler complains that a final field was not initialized (for the impossible, missing default case).
Moreover, in combination with CheckStyle, which has a "MissingSwitchDefault" rule, this new PMD rule is impossible to apply.
Code Sample demonstrating the issue:
importjava.io.PrintStream;
publicclassTest {
privatefinalPrintStreamout;
publicenumProcessStream {
STDOUT,
STDERR
}
publicTest(ProcessStreamstream) {
switch (stream) {
caseSTDOUT:
this.out = System.out;
break;
caseSTDERR:
this.out = System.err;
break;
default:
// Without this "default" section, Eclipse complains with:// The blank final field out may not have been initialized.// Note that a problem regarding missing 'default:' on 'switch' has been suppressed,// which is perhaps related to this problemthrownewIllegalStateException();
}
}
}
Expected outcome:
Since the assumption that the compiler is doing its job right is wrong, I think we should at least work around this limitation, or disable the rule by default.
changed the title [-]ExhaustiveSwitchHasDefault issue with final fields not initialized in constructor[/-][+][java] ExhaustiveSwitchHasDefault issue with final fields not initialized in constructor[/+]on Apr 10, 2025
changed the title [-][java] ExhaustiveSwitchHasDefault issue with final fields not initialized in constructor[/-][+][java] ExhaustiveSwitchHasDefault: False positive with final fields not initialized in constructor[/+]on Aug 20, 2026
Activity
[-]ExhaustiveSwitchHasDefault issue with final fields not initialized in constructor[/-][+][java] ExhaustiveSwitchHasDefault issue with final fields not initialized in constructor[/+]adangel commentedon Apr 10, 2025
Thanks for reporting. This is related to #5514, but is a slightly different edge case.
[java] Fix #5670, #5514: ExhaustiveSwitchHasDefault when default is n…
[-][java] ExhaustiveSwitchHasDefault issue with final fields not initialized in constructor[/-][+][java] ExhaustiveSwitchHasDefault: False positive with final fields not initialized in constructor[/+]