You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Shai Erera <se...@gmail.com> on 2009/11/23 12:19:10 UTC

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

Re: Bug in Token.copyTo?

Posted by Shai Erera <se...@gmail.com>.
Oops, I should have read the documentation better :)

On Mon, Nov 23, 2009 at 1:22 PM, Uwe Schindler <uw...@thetaphi.de> wrote:

>  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: uwe@thetaphi.de
>   ------------------------------
>
> *From:* Shai Erera [mailto:serera@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
>

RE: Bug in Token.copyTo?

Posted by Uwe Schindler <uw...@thetaphi.de>.
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: uwe@thetaphi.de

  _____  

From: Shai Erera [mailto:serera@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