{"record":{"id":"70bcc083eef58e3e","repo":"languagetool-org/languagetool","slug":"spanish-language-expected-got-language","errorCode":null,"errorMessage":"Spanish language expected, got \" + language","messagePattern":"Spanish language expected, got \" \\+ language","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"languagetool-language-modules/es/src/main/java/org/languagetool/language/Spanish.java","lineNumber":406,"sourceCode":"          ruleMatch.setShortMessage(\"Mayúsculas y minúsculas\");\n          ruleMatch.getRule().setLocQualityIssueType(ITSIssueType.Typographical);\n          ruleMatch.getRule().setCategory(Categories.CASING.getCategory(ResourceBundleTools.getMessageBundle(this)));\n          ruleMatch.setSpecificRuleId(ruleMatch.getRule().getId().replace(\"ORTHOGRAPHY\", \"CASING\"));\n          //ruleMatch.getRule().setTags(Arrays.asList(Tag.picky));\n        }\n      }\n      results.add(ruleMatch);\n    }\n\n    return results;\n  }\n\n  public static @NotNull Spanish getInstance() {\n    Language language = Objects.requireNonNull(Languages.getLanguageForShortCode(LANGUAGE_SHORT_CODE));\n    if (language instanceof Spanish spanish) {\n      return spanish;\n    }\n    throw new RuntimeException(\"Spanish language expected, got \" + language);\n  }\n}\n","sourceCodeStart":388,"sourceCodeEnd":409,"githubUrl":"https://github.com/languagetool-org/languagetool/blob/2e990059ce67d5e2a0f7f7ca5d31160c6709df4b/languagetool-language-modules/es/src/main/java/org/languagetool/language/Spanish.java#L388-L409","documentation":"Spanish.getInstance() resolves the language for the Spanish short code and returns it only if it is actually an instance of Spanish. If Languages.getLanguageForShortCode returns a different Language implementation for that code (variant or override registered elsewhere), the invariant is broken and a RuntimeException is thrown naming the returned language.","triggerScenarios":"Calling Spanish.getInstance() when the registered language for \"es\" resolves to a non-Spanish Language object — e.g. another variant of Spanish registered under the same short code, or a custom language registered as \"es\".","commonSituations":"Environments where a custom/contrib language overrides the built-in \"es\" code; test setups registering mock languages; running with a language variant list where Spanish is represented by a subclass-free different class.","solutions":["Check which language is registered for \"es\" via Languages.getLanguageForShortCode(\"es\") and remove conflicting registrations.","Avoid registering custom languages under the built-in \"es\" short code.","Use the returned Language object directly instead of downcasting when a variant is intentional."],"exampleFix":"// before\nSpanish es = Spanish.getInstance(); // throws if \"es\" overridden\n// after\nLanguage lang = Languages.getLanguageForShortCode(\"es\");\nSpanish es = lang instanceof Spanish s ? s : null;","handlingStrategy":"type-guard","validationCode":"Language l = Languages.getLanguageForShortCode(\"es\");\nif (!(l instanceof Spanish)) { /* handle variant/override before using getInstance */ }","typeGuard":"if (language instanceof Spanish spanish) { return spanish; } else { /* not the built-in Spanish class */ }","tryCatchPattern":"try {\n  Spanish es = Spanish.getInstance();\n} catch (RuntimeException e) {\n  if (e.getMessage().startsWith(\"Spanish language expected\")) { /* fall back to the Language interface */ }\n}","preventionTips":["Do not register custom languages or variants under the \"es\" short code","Check Languages.getLanguageForShortCode(\"es\") result type before downcasting","Prefer programming against the Language interface when variants may be present"],"tags":["languagetool","spanish","language-registry","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"2e990059ce67d5e2a0f7f7ca5d31160c6709df4b","analyzedAt":"2026-09-06T09:20:17.015Z","contentChangedAt":"2026-09-06T09:20:17.015Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}