That is correct behaviour, because the target of copyTo must support all
interfaces implemented (see docs). So you cannot copy a Token to a
TermAttribute (because it would be a loss in information), but the other way
round is possible.

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: u...@thetaphi.de

  _____  

From: Shai Erera [mailto:ser...@gmail.com] 
Sent: Monday, November 23, 2009 12:19 PM
To: java-dev@lucene.apache.org
Subject: Bug in Token.copyTo?

 

Hi

I think that Token.copyTo is buggy. It checks if the AttributeImpl is
instanceof Token, and if so reinits. However if it's not, it casts
AttributeImpl to *ALL* of TermAttribute, OffsetAttribute,
PositionIncrementAttribute etc. and sets them w/ the proper values. However,
one can pass an AttributeImpl which is not Token, but one of its
implementing interfaces, such as TermAttribute, and then the code breaks w/
a ClassCastException.

There's nothing in the API which prevents me from passing a non-Token
instance, and more than that - it looks as if the code tries to optimize for
a Token instance, but does not handle properly other cases. I think the
'else' clause should include instanceof checks, because obviously I cannot
pass an instance that implements all of the interfaces and is not Token
(well I can, but what's the point, just use Token).

Here is a short example which reproduces:

      Token token = new Token("hello", 0, 5);
      TermAttributeImpl term = new TermAttributeImpl();
      
      token.copyTo(term);
      System.out.println(term);

Shai

Reply via email to