mirror of https://github.com/apache/lucene.git
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:
parent
d838d7bf3a
commit
5e307da9d3
|
@ -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
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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) {}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue