antlr/antlr4 · error · IllegalArgumentException

Could not read path: "+path

Error message

Could not read path: "+path

What it means

XPath.split(path) wraps the ANTLRInputStream over a StringReader for the XPath path string. An IOException from a StringReader is essentially impossible in practice (reading from an in-memory string), so this IllegalArgumentException('Could not read path: ...', ioe) is a defensive wrapper that almost never fires.

Source

Thrown at runtime/Java/src/org/antlr/v4/runtime/tree/xpath/XPath.java:91

	protected XPathElement[] elements;
	protected Parser parser;

	public XPath(Parser parser, String path) {
		this.parser = parser;
		this.path = path;
		elements = split(path);
//		System.out.println(Arrays.toString(elements));
	}

	// TODO: check for invalid token/rule names, bad syntax

	public XPathElement[] split(String path) {
		ANTLRInputStream in;
		try {
			in = new ANTLRInputStream(new StringReader(path));
		}
		catch (IOException ioe) {
			throw new IllegalArgumentException("Could not read path: "+path, ioe);
		}
		XPathLexer lexer = new XPathLexer(in) {
			@Override
			public void recover(LexerNoViableAltException e) { throw e;	}
		};
		lexer.removeErrorListeners();
		lexer.addErrorListener(new XPathLexerErrorListener());
		CommonTokenStream tokenStream = new CommonTokenStream(lexer);
		try {
			tokenStream.fill();
		}
		catch (LexerNoViableAltException e) {
			int pos = lexer.getCharPositionInLine();
			String msg = "Invalid tokens or characters at index "+pos+" in path '"+path+"'";
			throw new IllegalArgumentException(msg, e);
		}

		List<Token> tokens = tokenStream.getTokens();

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Treat occurrence as a runtime environment anomaly: verify the antlr4 runtime jar is unmodified
  2. Ensure the path string is a plain java.lang.String, not wrapped by any custom Reader machinery
Defensive patterns

Strategy: try-catch

Try / catch

try { new XPath(parser, path); } catch (IllegalArgumentException e) { /* inspect cause for IOException; normally unreachable */ }

Prevention

When it happens

Trigger: Constructing new XPath(parser, path) or calling split(path) where the StringReader throws IOException; theoretically only via a subclassed Reader or corrupted JVM state.

Common situations: Practically unreachable; if seen, it indicates a custom/patched runtime rather than normal XPath usage.

Related errors


AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14). Data as JSON: /api/errors/da87a281833176ae. Report an issue: GitHub.