0

Does this question bring any value to the site?

It's been deleted, so here's what it looked like at the time it was closed and at the time it was deleted:

Why does this complex generics example not compile with Java 1.7 (does with Java 1.8)? How to solve it?

Here's the code. The errors and warnings, for both Java 1.7 and 1.8 are included in comments. Why does this happen? The question is also whether it is possible (and how) to change the signature of method takeCollection(...) so that callers have a way to call it and pass SubRoot/Foo/Bar collections and compile this both against 1.7 and 1.8, while NOT having to refer in the method signature specifically to SubRoot/Foo/Bar types via overloading (there may many of these, some in dependent projects not visible to this one)?

package generics;

import java.util.Collection;

// -------------------------------------------------------------------------------
// Cannot change this (start)
//-------------------------------------------------------------------------------
interface Root<F extends Foo<F,B>, B extends Bar<F,B>> {}
class     Foo <F extends Foo<F,B>, B extends Bar<F,B>> implements Root<F,B> {}
class     Bar <F extends Foo<F,B>, B extends Bar<F,B>> implements Root<F,B> {}

interface SubRoot extends Root<SubFoo, SubBar> {}
class     SubFoo  extends Foo <SubFoo, SubBar> implements SubRoot {}
class     SubBar  extends Bar <SubFoo, SubBar> implements SubRoot {}
//-------------------------------------------------------------------------------
// End of cannot change block.
//-------------------------------------------------------------------------------

public class GenericsTest {

  <R extends Root<F,B>, F extends Foo<F,B>, B extends Bar<F,B>>
  void takeOne(R one) {}

  // The following method signature can be changed a bit, but it has to accept a collection
  // of Roots, or ANY of its subtypes.
  <R extends Root<F,B>, F extends Foo<F,B>, B extends Bar<F,B>, C extends Collection<R>>
  void takeCollection(C collection) {}

  // ------------------------------------------
  // Test/illustration code that compiles well
  // ------------------------------------------

  @SuppressWarnings("rawtypes") // Warnings understood
  void testOneRoot1(Root root, Foo foo, Bar bar) {
    takeOne(root);
    // Java 1.7 and 1.8 agree about the warning, as expected.
    //
    // generics\GenericsTest.java:35: warning: [unchecked] unchecked method invocation: method takeOne in class GenericsTest is applied to given types
    //    takeOne(root);
    //           ^
    //  required: R
    //  found: Root
    //  where R,F,B are type-variables:
    //    R extends Root<F,B> declared in method <R,F,B>takeOne(R)
    //    F extends Foo<F,B> declared in method <R,F,B>takeOne(R)
    //    B extends Bar<F,B> declared in method <R,F,B>takeOne(R)

    takeOne(foo);
    // Java 1.7 and 1.8 agree about the warning, as expected.
    //
    // generics\GenericsTest.java:48: warning: [unchecked] unchecked method invocation: method takeOne in class GenericsTest is applied to given types
    //    takeOne(foo);
    //           ^
    //  required: R
    //  found: Foo
    //  where R,F,B are type-variables:
    //    R extends Root<F,B> declared in method <R,F,B>takeOne(R)
    //    F extends Foo<F,B> declared in method <R,F,B>takeOne(R)
    //    B extends Bar<F,B> declared in method <R,F,B>takeOne(R)

    takeOne(bar);
    // Java 1.7 and 1.8 agree about the warning, as expected.
    //
    // generics\GenericsTest.java:61: warning: [unchecked] unchecked method invocation: method takeOne in class GenericsTest is applied to given types
    //    takeOne(bar);
    //           ^
    //  required: R
    //  found: Bar
    //  where R,F,B are type-variables:
    //    R extends Root<F,B> declared in method <R,F,B>takeOne(R)
    //    F extends Foo<F,B> declared in method <R,F,B>takeOne(R)
    //    B extends Bar<F,B> declared in method <R,F,B>takeOne(R)
  }

  <R extends Root<F,B>, F extends Foo<F,B>, B extends Bar<F,B>>
  void testOneRoot2(R root, F foo, B bar) {
    takeOne(root); // All fine
    takeOne(foo);  // All fine
    takeOne(bar);  // All fine
  }

  void testOneSubRoot(SubRoot subRoot, SubFoo subFoo, SubBar subBar) {
    takeOne(subRoot); // All fine
    takeOne(subFoo);  // All fine
    takeOne(subBar);  // All fine
  }

  void testRootCollectionRaw(@SuppressWarnings("rawtypes") Collection<? extends Root> collection) {
    takeCollection(collection);
    // Java 1.7 quiet (not expected). Java 1.8 produces a warning, as expected:
    //
    //  generics\GenericsTest.java:89: warning: [unchecked] unchecked method invocation: method takeCollection in class GenericsTest is applied to given types
    //      takeCollection(collection);
    //                    ^
    //    required: C
    //    found: Collection<CAP#1>
    //    where C,R,F,B are type-variables:
    //      C extends Collection<R> declared in method <R,F,B,C>takeCollection(C)
    //      R extends Root<F,B> declared in method <R,F,B,C>takeCollection(C)
    //      F extends Foo<F,B> declared in method <R,F,B,C>takeCollection(C)
    //      B extends Bar<F,B> declared in method <R,F,B,C>takeCollection(C)
    //    where CAP#1 is a fresh type-variable:
    //      CAP#1 extends Root from capture of ? extends Root
 }

  // --------------------------------------------
  // Test/illustration code that does NOT compile
  // --------------------------------------------

  <R extends Root<F,B>, F extends Foo<F,B>, B extends Bar<F,B>>
  void testRootCollection1(Collection<? extends R> collection) {
    takeCollection(collection);
    //  JDK 1.8 quiet (as expected). JDK 1.7 yields an error (not expected):
    //
    //  generics\GenericsTest.java:112: error: invalid inferred types for R#1,F#1,B#1; inferred type does not conform to declared bound(s)
    //      takeCollection(collection);
    //                    ^
    //      inferred: CAP#1
    //      bound(s): Root<CAP#2,CAP#3>
    //    where R#1,F#1,B#1,C,R#2,F#2,B#2 are type-variables:
    //      R#1 extends Root<F#1,B#1> declared in method <R#1,F#1,B#1,C>takeCollection(C)
    //      F#1 extends Foo<F#1,B#1> declared in method <R#1,F#1,B#1,C>takeCollection(C)
    //      B#1 extends Bar<F#1,B#1> declared in method <R#1,F#1,B#1,C>takeCollection(C)
    //      C extends Collection<R#1> declared in method <R#1,F#1,B#1,C>takeCollection(C)
    //      R#2 extends Root<F#2,B#2> declared in method <R#2,F#2,B#2>testRootCollection1(Collection<? extends R#2>)
    //      F#2 extends Foo<F#2,B#2> declared in method <R#2,F#2,B#2>testRootCollection1(Collection<? extends R#2>)
    //      B#2 extends Bar<F#2,B#2> declared in method <R#2,F#2,B#2>testRootCollection1(Collection<? extends R#2>)
    //    where CAP#1,CAP#2,CAP#3 are fresh type-variables:
    //      CAP#1 extends R#2 from capture of ? extends R#2
    //      CAP#2 extends Foo<CAP#2,CAP#3> from capture of ?
    //      CAP#3 extends Bar<CAP#2,CAP#3> from capture of ?
  }

  <R extends Root<F,B>, F extends Foo<F,B>, B extends Bar<F,B>>
  void testRootCollection2(Collection<R> collection) {
    takeCollection(collection);
    //  JDK 1.8 quiet (as expected). JDK 1.7 yields an error (not expected):
    //
    //  generics\GenericsTest.java:136: error: invalid inferred types for R#1,F#1,B#1; inferred type does not conform to declared bound(s)
    //      takeCollection(collection);
    //                    ^
    //      inferred: R#2
    //      bound(s): Root<CAP#1,CAP#2>
    //    where R#1,F#1,B#1,C,R#2,F#2,B#2 are type-variables:
    //      R#1 extends Root<F#1,B#1> declared in method <R#1,F#1,B#1,C>takeCollection(C)
    //      F#1 extends Foo<F#1,B#1> declared in method <R#1,F#1,B#1,C>takeCollection(C)
    //      B#1 extends Bar<F#1,B#1> declared in method <R#1,F#1,B#1,C>takeCollection(C)
    //      C extends Collection<R#1> declared in method <R#1,F#1,B#1,C>takeCollection(C)
    //      R#2 extends Root<F#2,B#2> declared in method <R#2,F#2,B#2>testRootCollection2(Collection<R#2>)
    //      F#2 extends Foo<F#2,B#2> declared in method <R#2,F#2,B#2>testRootCollection2(Collection<R#2>)
    //      B#2 extends Bar<F#2,B#2> declared in method <R#2,F#2,B#2>testRootCollection2(Collection<R#2>)
    //    where CAP#1,CAP#2 are fresh type-variables:
    //      CAP#1 extends Foo<CAP#1,CAP#2> from capture of ?
    //      CAP#2 extends Bar<CAP#1,CAP#2> from capture of ?
  }

  void testSubRootCollection1(Collection<SubRoot> collection) {
    takeCollection(collection);
    //  JDK 1.8 quiet (as expected). JDK 1.7 yields an error (not expected):
    //
    //  generics\GenericsTest.java:158: error: invalid inferred types for R,F,B; inferred type does not conform to declared bound(s)
    //      takeCollection(collection);
    //                    ^
    //      inferred: SubRoot
    //      bound(s): Root<CAP#1,CAP#2>
    //    where R,F,B,C are type-variables:
    //      R extends Root<F,B> declared in method <R,F,B,C>takeCollection(C)
    //      F extends Foo<F,B> declared in method <R,F,B,C>takeCollection(C)
    //      B extends Bar<F,B> declared in method <R,F,B,C>takeCollection(C)
    //      C extends Collection<R> declared in method <R,F,B,C>takeCollection(C)
    //    where CAP#1,CAP#2 are fresh type-variables:
    //      CAP#1 extends Foo<CAP#1,CAP#2> from capture of ?
    //      CAP#2 extends Bar<CAP#1,CAP#2> from capture of ?
  }

  void testSubRootCollection2(Collection<? extends SubRoot> collection) {
    takeCollection(collection);
    //  JDK 1.8 quiet. JDK 1.7 yields an error:
    //
    //  generics\GenericsTest.java:177: error: invalid inferred types for R,F,B; inferred type does not conform to declared bound(s)
    //      takeCollection(collection);
    //                    ^
    //      inferred: CAP#1
    //      bound(s): Root<CAP#2,CAP#3>
    //    where R,F,B,C are type-variables:
    //      R extends Root<F,B> declared in method <R,F,B,C>takeCollection(C)
    //      F extends Foo<F,B> declared in method <R,F,B,C>takeCollection(C)
    //      B extends Bar<F,B> declared in method <R,F,B,C>takeCollection(C)
    //      C extends Collection<R> declared in method <R,F,B,C>takeCollection(C)
    //    where CAP#1,CAP#2,CAP#3 are fresh type-variables:
    //      CAP#1 extends SubRoot from capture of ? extends SubRoot
    //      CAP#2 extends Foo<CAP#2,CAP#3> from capture of ?
    //      CAP#3 extends Bar<CAP#2,CAP#3> from capture of ?
  }
}

A couple of close-voters voted to close it as lacking a minimal, complete, and verifiable example. I believe the rest, including myself, voted to close as too broad. Some of us also voted to delete it.

I voted to close and delete because I thought the question was extremely convoluted, very unlikely to be of any searchable value, and really too localized.

The accepted answer, Andy's, gave a solution but did not explain why. And the other answer basically boiled down to

Java 7 is not as advanced as in Java 8

There's no value lost here, in my opinion.

Some of us were contacted by the author, up in arms, telling us our behavior was unacceptable. I'd like to know how the community feels.

4
  • 4
    If the OP is up in arms, should they not be asking a meta question, given that they're the one that feels that a different action should be taken (and can thus justify their position)?
    – Servy
    Mar 13, 2017 at 16:06
  • 2
    @Servy Maybe. I'm asking for my continued efforts cleaning up. Mar 13, 2017 at 16:07
  • 1
    I have no real idea what the [java] community thinks is an interesting technical question. But it looks like you are an experienced Java programmer and don't know the answer to this question. Are you not even a little bit interested in a well-written correct answer? Nothing lost, perhaps. Nothing gained. Mar 13, 2017 at 16:51
  • @HansPassant: Perhaps the ChuckNorrisException passes as interesting? It's one of the highest voted questions I recall seeing in years' past...
    – Makoto
    Mar 13, 2017 at 16:58

1 Answer 1

15

Let me try to pare down the question a bit.

The OP is trying to compare the behavior of generics between Java 8 and Java 7, and is puzzled why certain generics work in Java 8 as opposed to Java 7. They would like a solution which works with Java 7 specifically to ensure that their code is compatible with it.

In the question, the OP has provided:

  • code to reproduce the error
  • comments in the code to illustrate behaviors, what's expected and what actually resulted
  • clear criteria for "success", in that an approach exists in Java 7 to make generics work, or not - a negative answer is also "success" in this scenario, too

Based on that, I don't agree with:

  • The question lacking "minimal" code; the code provided does everything the OP says it does and illustrates what their expectations are. If you remove all of the comments, you get the code snippet down to about 59 lines, which isn't that much to look at at all. Naturally, the comments inflate this, but the comments also provide very valuable context.
  • The question being "too broad". There's a clear question being posed - why does this behave this way in Java 8 as opposed to Java 7, and how can I get around it - and there's existing precedent with a verifiable source which can explain why and how this happens. Working from this, one could also build a solution - either in the affirmative or negative - as to how the OP could address their code problem in Java 7.
  • Its deletion. Value was lost in that there were no negatively voted answers, and even the question itself wasn't net-negative. Even if this information could've been pared down to, "Java 7 isn't as advanced as Java 8", that's still a useful answer to provide, since there are still companies stuck on Java 7 for their own reasons.

At best, this question should've been edited to address concerns about clarity or broadness, not deleted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .