Index: src/test/org/apache/lucene/document/TestDocument.java =================================================================== --- src/test/org/apache/lucene/document/TestDocument.java (revision 753376) +++ src/test/org/apache/lucene/document/TestDocument.java (working copy) @@ -260,4 +260,23 @@ dir.close(); assertEquals("did not see all IDs", 7, result); } + + public void testFieldSetValueChangeBinary() { + Field field1 = new Field("field1", new byte[0], + Field.Store.YES); + Field field2 = new Field("field2", "", + Field.Store.YES, Field.Index.ANALYZED); + try { + field1.setValue("abc"); + fail("did not hit expected exception"); + } catch (IllegalArgumentException iae) { + // expected + } + try { + field2.setValue(new byte[0]); + fail("did not hit expected exception"); + } catch (IllegalArgumentException iae) { + // expected + } + } } Index: src/java/org/apache/lucene/document/Field.java =================================================================== --- src/java/org/apache/lucene/document/Field.java (revision 753376) +++ src/java/org/apache/lucene/document/Field.java (working copy) @@ -207,16 +207,28 @@ * href="http://wiki.apache.org/lucene-java/ImproveIndexingSpeed">ImproveIndexingSpeed * for details.
*/ public void setValue(String value) { + if (isBinary) { + throw new IllegalArgumentException("cannot set a String value on a binary field"); + } fieldsData = value; } /** Expert: change the value of this field. See setValue(String). */ public void setValue(Reader value) { + if (isBinary) { + throw new IllegalArgumentException("cannot set a Reader value on a binary field"); + } + if (isStored) { + throw new IllegalArgumentException("cannot set a Reader value on a stored field"); + } fieldsData = value; } /** Expert: change the value of this field. See setValue(String). */ public void setValue(byte[] value) { + if (!isBinary) { + throw new IllegalArgumentException("cannot set a byte[] value on a non-binary field"); + } fieldsData = value; binaryLength = value.length; binaryOffset = 0; @@ -224,6 +236,9 @@ /** Expert: change the value of this field. See setValue(String). */ public void setValue(byte[] value, int offset, int length) { + if (!isBinary) { + throw new IllegalArgumentException("cannot set a byte[] value on a non-binary field"); + } fieldsData = value; binaryLength = length; binaryOffset = offset; @@ -232,7 +247,14 @@ /** Expert: change the value of this field. See setValue(String). */ public void setValue(TokenStream value) { + if (isBinary) { + throw new IllegalArgumentException("cannot set a TokenStream value on a binary field"); + } + if (isStored) { + throw new IllegalArgumentException("cannot set a TokenStream value on a stored field"); + } fieldsData = value; + isBinary = false; } /**