LUCENE-2088: AttributeSource.addAttribute should only accept interfaces, the missing test leads to problems with Token.TOKEN_ATTRIBUTE_FACTORY

This also fixes the package.html error Shai mentioned.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@883074 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2009-11-22 13:18:10 +00:00
parent d838d7bf3a
commit 5e307da9d3
4 changed files with 23 additions and 1 deletions

View File

@ -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

View File

@ -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();
}
</PRE>
</p>

View File

@ -224,6 +224,12 @@ public class AttributeSource {
public <A extends Attribute> A addAttribute(Class<A> 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);

View File

@ -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) {}
}
}