Details

    • Type: Task Task
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 3.0
    • Fix Version/s: 4.0-ALPHA
    • Component/s: core/other
    • Labels:
      None

      Description

      Paul Cowan wrote in LUCENE-1257:

      OK, thought I'd jump in and help out here with one of my Java 5 favourites. Haven't seen anyone discuss this, and don't believe any of the patches address this, so thought I'd throw a patch out there (against SVN HEAD @ revision 827821) which uses Java 5 covariant return types for (almost) all of the Object#clone() implementations in core.
      i.e. this:

      public Object clone() {
      changes to:
      public SpanNotQuery clone() {

      which lets us get rid of a whole bunch of now-unnecessary casts, so e.g.

      if (clone == null) clone = (SpanNotQuery) this.clone();
      becomes
      if (clone == null) clone = this.clone();

      Almost everything has been done and all downcasts removed, in core, with the exception of

      Some SpanQuery stuff, where it's assumed that it's safe to cast the clone() of a SpanQuery to a SpanQuery - this can't be made covariant without declaring "abstract SpanQuery clone()" in SpanQuery itself, which breaks those SpanQuerys that don't declare their own clone()
      Some IndexReaders, e.g. DirectoryReader - we can't be more specific than changing .clone() to return IndexReader, because it returns the result of IndexReader.clone(boolean). We could use covariant types for THAT, which would work fine, but that didn't follow the pattern of the others so that could be a later commit.
      Two changes were also made in contrib/, where not making the changes would have broken code by trying to widen IndexInput#clone() back out to returning Object, which is not permitted. contrib/ was otherwise left untouched.

      Let me know what you think, or if you have any other questions.

      1. LUCENE-2000-clone_covariance.patch
        42 kB
        Ryan McKinley
      2. LUCENE-2000-clone_covariance.patch
        51 kB
        Uwe Schindler

        Issue Links

          Activity

          Hide
          Uwe Schindler added a comment - - edited

          I moved this to an extra issue, because there is some discussion needed.

          I am strongly against this for various reasons:

          • Java 5 itsself does not override clone() with covariant return type (nowhere!). So e.g. String.clone() always returns jl.Object.
          • This is because of backwards problems (which are not easy to explain) – it has something to do, if a subclass compiled against Java 1.4 version of Lucene overrides clone and calls super.clone(). Because of this, the JDK does not provide String.clone() retrurning String. javac does its best to prevent problems here, but for APIs that need to be backwards compatible, it should return Object as always.
          • Covariant clone return types need, that all subclasses of a class, that originally implemented a covariant clone() also override it covariant to be consistent. And because of this you have consistency problems (see your IndexReader problem). This is not possible for backwards compatibility. Because of this, covariant clone should only be done for internal classes (package-private, private) or final classes. Another example of this problem is AttributeImpl which defines a clone() method. Subclasses would need to override this covariant clone() method. Custom Attributes compiled against Lucene 2.9 would fail to do this -> MethodNotFoundException (I tried it out, it breaks)

          Because of all this problems, I prefer to always cast the return value of clone(). This is not unsafe (and because of this you get no unchecked warning), because you always know how to cast the clone result. By the way: You still have to always clone() the super.clone() call, so you do not get any pros of using covariant return types.

          I do not want to start a flame war here, but we should not do this.

          Show
          Uwe Schindler added a comment - - edited I moved this to an extra issue, because there is some discussion needed. I am strongly against this for various reasons: Java 5 itsself does not override clone() with covariant return type (nowhere!). So e.g. String.clone() always returns jl.Object. This is because of backwards problems (which are not easy to explain) – it has something to do, if a subclass compiled against Java 1.4 version of Lucene overrides clone and calls super.clone(). Because of this, the JDK does not provide String.clone() retrurning String. javac does its best to prevent problems here, but for APIs that need to be backwards compatible, it should return Object as always. Covariant clone return types need, that all subclasses of a class, that originally implemented a covariant clone() also override it covariant to be consistent. And because of this you have consistency problems (see your IndexReader problem). This is not possible for backwards compatibility. Because of this, covariant clone should only be done for internal classes (package-private, private) or final classes. Another example of this problem is AttributeImpl which defines a clone() method. Subclasses would need to override this covariant clone() method. Custom Attributes compiled against Lucene 2.9 would fail to do this -> MethodNotFoundException (I tried it out, it breaks) Because of all this problems, I prefer to always cast the return value of clone(). This is not unsafe (and because of this you get no unchecked warning), because you always know how to cast the clone result. By the way: You still have to always clone() the super.clone() call, so you do not get any pros of using covariant return types. I do not want to start a flame war here, but we should not do this.
          Hide
          Earwin Burrfoot added a comment -

          I believe we should do this at our next "we're breaking backcompat" release.
          Any compile errors that could bring to clients are fixed like, exceptionally easy. And the code comes out cleaner -> we have way more callees of clone() than overrides.

          Show
          Earwin Burrfoot added a comment - I believe we should do this at our next "we're breaking backcompat" release. Any compile errors that could bring to clients are fixed like, exceptionally easy. And the code comes out cleaner -> we have way more callees of clone() than overrides.
          Hide
          Ryan McKinley added a comment -

          updating to /trunk

          But if Uwe thinks this is a bad idea... i can accept that

          Show
          Ryan McKinley added a comment - updating to /trunk But if Uwe thinks this is a bad idea... i can accept that
          Hide
          Uwe Schindler added a comment -

          In Lucene trunk we break backwards, be free to change it! I am fine with it.

          (this is why I kept it open)

          Show
          Uwe Schindler added a comment - In Lucene trunk we break backwards, be free to change it! I am fine with it. (this is why I kept it open)
          Hide
          Ryan McKinley added a comment -

          added to trunk in #1306626

          This won't be ported to 3.x

          Show
          Ryan McKinley added a comment - added to trunk in #1306626 This won't be ported to 3.x
          Hide
          Michael McCandless added a comment -

          We now get a bunch of "redundant cast" warnings from this ... are there plans to fix that...?

          Show
          Michael McCandless added a comment - We now get a bunch of "redundant cast" warnings from this ... are there plans to fix that...?
          Hide
          Uwe Schindler added a comment -

          We should fix this in trunk! I wanted to ask the same question!

          Show
          Uwe Schindler added a comment - We should fix this in trunk! I wanted to ask the same question!
          Hide
          Ryan McKinley added a comment -

          I just checked in a bunch of stuff... kept removing things until:

          ant compile | grep "redundant cast"
          

          was empty

          Show
          Ryan McKinley added a comment - I just checked in a bunch of stuff... kept removing things until: ant compile | grep "redundant cast " was empty
          Hide
          Michael McCandless added a comment -

          Thanks Ryan!

          Show
          Michael McCandless added a comment - Thanks Ryan!
          Hide
          Uwe Schindler added a comment -

          Hi,
          in revision 1375428 (trunk) and 1375429 (4.x), I also added a covariant override for IndexInput.clone(), removing tons of useless casts.

          Show
          Uwe Schindler added a comment - Hi, in revision 1375428 (trunk) and 1375429 (4.x), I also added a covariant override for IndexInput.clone(), removing tons of useless casts.

            People

            • Assignee:
              Ryan McKinley
              Reporter:
              Uwe Schindler
            • Votes:
              2 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development