{"record":{"id":"8e2329053d4fba26","repo":"antlr/antlr4","slug":"this-atn-simulator-does-not-support-clearing-the-d","errorCode":null,"errorMessage":"This ATN simulator does not support clearing the DFA.","messagePattern":"This ATN simulator does not support clearing the DFA\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"warning","filePath":"runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java","lineNumber":70,"sourceCode":"\t\tthis.atn = atn;\n\t\tthis.sharedContextCache = sharedContextCache;\n\t}\n\n\tpublic abstract void reset();\n\n\t/**\n\t * Clear the DFA cache used by the current instance. Since the DFA cache may\n\t * be shared by multiple ATN simulators, this method may affect the\n\t * performance (but not accuracy) of other parsers which are being used\n\t * concurrently.\n\t *\n\t * @throws UnsupportedOperationException if the current instance does not\n\t * support clearing the DFA.\n\t *\n\t * @since 4.3\n\t */\n\tpublic void clearDFA() {\n\t\tthrow new UnsupportedOperationException(\"This ATN simulator does not support clearing the DFA.\");\n\t}\n\n\tpublic PredictionContextCache getSharedContextCache() {\n\t\treturn sharedContextCache;\n\t}\n\n\tpublic PredictionContext getCachedContext(PredictionContext context) {\n\t\tif ( sharedContextCache==null ) return context;\n\n\t\tsynchronized (sharedContextCache) {\n\t\t\tIdentityHashMap<PredictionContext, PredictionContext> visited =\n\t\t\t\tnew IdentityHashMap<PredictionContext, PredictionContext>();\n\t\t\treturn PredictionContext.getCachedContext(context,\n\t\t\t\t\t\t\t\t\t\t\t\t\t  sharedContextCache,\n\t\t\t\t\t\t\t\t\t\t\t\t\t  visited);\n\t\t}\n\t}\n}","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java#L52-L88","documentation":"ATNSimulator.clearDFA() is a base-class stub: only the concrete simulators (ParserATNSimulator and LexerATNSimulator) override it to drop their DFA cache. Calling it on the abstract base type (or on a subclass such as a profiling simulator that does not override it) throws UnsupportedOperationException. The DFA cache only affects speed, never correctness, so clearing is an optional optimization.","triggerScenarios":"Invoking clearDFA() on a variable statically typed as ATNSimulator, or on a custom ATNSimulator subclass (e.g. a profiling or wrapper simulator) that inherits the base implementation. Typical call sites are long-running services that reset the cache to reclaim memory or after loading new grammars.","commonSituations":"Memory-leak workarounds in long-lived parser services. Test harnesses that want a cold DFA between runs. Upgrading to ANTLR 4.3+ where clearDFA was introduced and calling it through the base type.","solutions":["Call clearDFA() on the concrete type: cast to ParserATNSimulator or LexerATNSimulator first (instanceof check)","If you own the subclass, override clearDFA() and clear whatever DFA state it holds (or delegate to the wrapped simulator)","Otherwise create a fresh parser/lexer instance; a new simulator starts with an empty DFA"],"exampleFix":"// before\nATNSimulator sim = parser.getInterpreter();\nsim.clearDFA(); // throws if not overridden\n// after\nATNSimulator sim = parser.getInterpreter();\nif (sim instanceof ParserATNSimulator) {\n    ((ParserATNSimulator) sim).clearDFA();\n}","handlingStrategy":"type-guard","validationCode":"if (sim instanceof ParserATNSimulator) ((ParserATNSimulator) sim).clearDFA();\nelse if (sim instanceof LexerATNSimulator) ((LexerATNSimulator) sim).clearDFA();\nelse { /* create fresh parser/lexer instead */ }","typeGuard":"static boolean canClearDfa(ATNSimulator sim) {\n    return sim instanceof ParserATNSimulator || sim instanceof LexerATNSimulator;\n}","tryCatchPattern":null,"preventionTips":["Hold simulators by their concrete type when you need cache management","Prefer constructing a new parser/lexer over clearing a shared DFA","Wrap custom ATNSimulator subclasses so they delegate clearDFA"],"tags":["antlr","java","atn","dfa","unsupported-operation"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}