Index: CHANGES.txt
===================================================================
--- CHANGES.txt (revision 882977)
+++ CHANGES.txt (working copy)
@@ -184,6 +184,9 @@
* 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
Index: src/java/org/apache/lucene/util/AttributeSource.java
===================================================================
--- src/java/org/apache/lucene/util/AttributeSource.java (revision 882918)
+++ src/java/org/apache/lucene/util/AttributeSource.java (working copy)
@@ -224,6 +224,12 @@
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);
Index: src/test/org/apache/lucene/util/TestAttributeSource.java
===================================================================
--- src/test/org/apache/lucene/util/TestAttributeSource.java (revision 882918)
+++ src/test/org/apache/lucene/util/TestAttributeSource.java (working copy)
@@ -141,4 +141,18 @@
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) {}
+ }
}