From cc045fbaf31ebc8ef678c726f40c4770ea80f608 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 23 Jan 2014 07:23:55 -0600 Subject: [PATCH] Improve CommonToken cloning performance, and update documentation --- .../Java/src/org/antlr/v4/runtime/CommonToken.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/CommonToken.java b/runtime/Java/src/org/antlr/v4/runtime/CommonToken.java index 050255caf..9c63393ec 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/CommonToken.java +++ b/runtime/Java/src/org/antlr/v4/runtime/CommonToken.java @@ -139,10 +139,17 @@ public class CommonToken implements WritableToken, Serializable { /** * Constructs a new {@link CommonToken} as a copy of another {@link Token}. * + *

+ * If {@code oldToken} is also a {@link CommonToken} instance, the newly + * constructed token will share a reference to the {@link #text} field and + * the {@link Pair} stored in {@link #source}. Otherwise, {@link #text} will + * be assigned the result of calling {@link #getText}, and {@link #source} + * will be constructed from the result of {@link Token#getTokenSource} and + * {@link Token#getInputStream}.

+ * * @param oldToken The token to copy. */ public CommonToken(@NotNull Token oldToken) { - text = oldToken.getText(); type = oldToken.getType(); line = oldToken.getLine(); index = oldToken.getTokenIndex(); @@ -152,9 +159,11 @@ public class CommonToken implements WritableToken, Serializable { stop = oldToken.getStopIndex(); if (oldToken instanceof CommonToken) { + text = ((CommonToken)oldToken).text; source = ((CommonToken)oldToken).source; } else { + text = oldToken.getText(); source = new Pair(oldToken.getTokenSource(), oldToken.getInputStream()); } }