diff --git a/CHANGES.txt b/CHANGES.txt index 5e171c2e581..051601f2b17 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -184,6 +184,9 @@ Bug fixes * LUCENE-2013: SpanRegexQuery does not work with QueryScorer. (Benjamin Keil via Mark Miller) +* LUCENE-2088: addAttribute() should only accept interfaces that + extend Attribute. (Shai Erera, Uwe Schindler) + New features * LUCENE-1933: Provide a convenience AttributeFactory that creates a diff --git a/src/java/org/apache/lucene/analysis/package.html b/src/java/org/apache/lucene/analysis/package.html index 490101d2bee..dd944ad5ea1 100644 --- a/src/java/org/apache/lucene/analysis/package.html +++ b/src/java/org/apache/lucene/analysis/package.html @@ -140,7 +140,6 @@ There are many post tokenization steps that can be done, including (but not limi TokenStream ts = analyzer.tokenStream("myfield",new StringReader("some text goes here")); while (ts.incrementToken()) { System.out.println("token: "+ts)); - t = ts.next(); }
diff --git a/src/java/org/apache/lucene/util/AttributeSource.java b/src/java/org/apache/lucene/util/AttributeSource.java index 1a194aeaa78..8dfeb46f311 100644 --- a/src/java/org/apache/lucene/util/AttributeSource.java +++ b/src/java/org/apache/lucene/util/AttributeSource.java @@ -224,6 +224,12 @@ public class AttributeSource { public A addAttribute(Class attClass) { AttributeImpl attImpl = attributes.get(attClass); if (attImpl == null) { + if (!(attClass.isInterface() && Attribute.class.isAssignableFrom(attClass))) { + throw new IllegalArgumentException( + "addAttribute() only accepts an interface that extends Attribute, but " + + attClass.getName() + " does not fulfil this contract." + ); + } addAttributeImpl(attImpl = this.factory.createAttributeInstance(attClass)); } return attClass.cast(attImpl); diff --git a/src/test/org/apache/lucene/util/TestAttributeSource.java b/src/test/org/apache/lucene/util/TestAttributeSource.java index 113d2ff8a11..709a835749e 100644 --- a/src/test/org/apache/lucene/util/TestAttributeSource.java +++ b/src/test/org/apache/lucene/util/TestAttributeSource.java @@ -141,4 +141,18 @@ public class TestAttributeSource extends LuceneTestCase { assertTrue("TypeAttribute is not implemented by TypeAttributeImpl", src.addAttribute(TypeAttribute.class) instanceof TypeAttributeImpl); } + + public void testInvalidArguments() throws Exception { + try { + AttributeSource src = new AttributeSource(); + src.addAttribute(Token.class); + fail("Should throw IllegalArgumentException"); + } catch (IllegalArgumentException iae) {} + + try { + AttributeSource src = new AttributeSource(Token.TOKEN_ATTRIBUTE_FACTORY); + src.addAttribute(Token.class); + fail("Should throw IllegalArgumentException"); + } catch (IllegalArgumentException iae) {} + } }