diff --git a/src/java/org/apache/commons/lang/text/StrTokenizer.java b/src/java/org/apache/commons/lang/text/StrTokenizer.java index 618621069..a14900a2d 100644 --- a/src/java/org/apache/commons/lang/text/StrTokenizer.java +++ b/src/java/org/apache/commons/lang/text/StrTokenizer.java @@ -1048,22 +1048,32 @@ public class StrTokenizer implements ListIterator, Cloneable { //----------------------------------------------------------------------- /** - * Creates a new instance of this Tokenizer. - * The new instance is reset so that it will be at the start of the token list. + * Creates a new instance of this Tokenizer. The new instance is reset so that it will be at the start of the token + * list. If a {@link CloneNotSupportedException} is caught, return null. + * * @return a new instance of this Tokenizer which has been reset. */ public Object clone() { try { - StrTokenizer cloned = (StrTokenizer) super.clone(); - if (cloned.chars != null) { - cloned.chars = (char[]) cloned.chars.clone(); - } - cloned.reset(); - return cloned; - + return cloneReset(); } catch (CloneNotSupportedException ex) { return null; } } + /** + * Creates a new instance of this Tokenizer. The new instance is reset so that it will be at the start of the token + * list. + * + * @return a new instance of this Tokenizer which has been reset. + */ + protected Object cloneReset() throws CloneNotSupportedException { + StrTokenizer cloned = (StrTokenizer) super.clone(); + if (cloned.chars != null) { + cloned.chars = (char[]) cloned.chars.clone(); + } + cloned.reset(); + return cloned; + } + } diff --git a/src/test/org/apache/commons/lang/text/StrTokenizerTest.java b/src/test/org/apache/commons/lang/text/StrTokenizerTest.java index 0ecab5358..3a5403756 100644 --- a/src/test/org/apache/commons/lang/text/StrTokenizerTest.java +++ b/src/test/org/apache/commons/lang/text/StrTokenizerTest.java @@ -542,6 +542,19 @@ public class StrTokenizerTest extends TestCase { assertEquals(tok, tok.setIgnoreEmptyTokens(false)); } + /** + * Tests that the {@link StrTokenizer#clone()} clone method catches {@link CloneNotSupportedException} and returns + * null. + */ + public void testCloneNotSupportedException() { + Object notCloned = (new StrTokenizer() { + public Object cloneReset() throws CloneNotSupportedException { + throw new CloneNotSupportedException("test"); + } + }).clone(); + assertNull(notCloned); + } + public void testCloneNull() { StrTokenizer tokenizer = new StrTokenizer((char[]) null); // Start sanity check