{"record":{"id":"f578d3d24ba3216a","repo":"antlr/antlr4","slug":"there-is-no-serialized-atn","errorCode":null,"errorMessage":"there is no serialized ATN","messagePattern":"there is no serialized ATN","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/Recognizer.java","lineNumber":129,"sourceCode":"\t\t\treturn result;\n\t\t}\n\t}\n\n\tpublic int getTokenType(String tokenName) {\n\t\tInteger ttype = getTokenTypeMap().get(tokenName);\n\t\tif ( ttype!=null ) return ttype;\n\t\treturn Token.INVALID_TYPE;\n\t}\n\n\t/**\n\t * If this recognizer was generated, it will have a serialized ATN\n\t * representation of the grammar.\n\t *\n\t * <p>For interpreters, we don't know their serialized ATN despite having\n\t * created the interpreter from it.</p>\n\t */\n\tpublic String getSerializedATN() {\n\t\tthrow new UnsupportedOperationException(\"there is no serialized ATN\");\n\t}\n\n\t/** For debugging and other purposes, might want the grammar name.\n\t *  Have ANTLR generate an implementation for this method.\n\t */\n\tpublic abstract String getGrammarFileName();\n\n\t/**\n\t * Get the {@link ATN} used by the recognizer for prediction.\n\t *\n\t * @return The {@link ATN} used by the recognizer for prediction.\n\t */\n\tpublic abstract ATN getATN();\n\n\t/**\n\t * Get the ATN interpreter used by the recognizer for prediction.\n\t *\n\t * @return The ATN interpreter used by the recognizer for prediction.","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java#L111-L147","documentation":"Thrown by Recognizer.getSerializedATN(), which the base class implements by throwing because only generated recognizers embed the serialized ATN string. Interpreter-based recognizers are built from a deserialized ATN and deliberately do not retain the serialization. Any tooling that asks a recognizer for its serialized ATN will fail on non-generated instances.","triggerScenarios":"Calling recognizer.getSerializedATN() on a Recognizer, LexerInterpreter, or ParserInterpreter instance (the method is only overridden in generated Lexer/Parser subclasses); serialization or tooling code that reflects over arbitrary recognizers.","commonSituations":"Grammar-inspection utilities, cache keys built from the ATN string, or porting generated-code assumptions to interpreter-based parsing of arbitrary grammars at runtime.","solutions":["Call getSerializedATN() only on generated recognizer classes, which override it","If you built the interpreter yourself, keep the original serialized ATN string alongside it instead of re-asking the recognizer","Guard with an instanceof/generated check before calling (see validation below)"],"exampleFix":"// before\nString atn = recognizer.getSerializedATN(); // throws for interpreters\n\n// after\nString atn = (recognizer instanceof ParserInterpreter)\n    ? savedSerializedAtn   // you kept it when constructing the interpreter\n    : recognizer.getSerializedATN();","handlingStrategy":"validation","validationCode":"boolean isGenerated = recognizer.getClass().getPackage() != null\n    && recognizer.getGrammarFileName().endsWith(\".g4\");\n// simplest reliable check: generated classes override getSerializedATN without throwing\nif (!(recognizer instanceof LexerInterpreter || recognizer instanceof ParserInterpreter)) {\n    String atn = recognizer.getSerializedATN(); // safe for generated classes\n}","typeGuard":"static boolean canSupplySerializedAtn(Recognizer<?, ?> r) {\n    return !(r instanceof LexerInterpreter) && !(r instanceof ParserInterpreter);\n}","tryCatchPattern":null,"preventionTips":["Keep the serialized ATN string you used to build an interpreter","Only call getSerializedATN() on generated classes","Abstract ATN access behind your own interface that knows the provenance"],"tags":["antlr","atn","interpreter","serialization"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}