Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 8 compilation error with generic types #164

Open
aramk opened this issue Apr 14, 2014 · 0 comments
Open

Java 8 compilation error with generic types #164

aramk opened this issue Apr 14, 2014 · 0 comments

Comments

@aramk
Copy link

aramk commented Apr 14, 2014

I've encountered an issue when using Java 1.8 (JDK 8).

import static org.fest.assertions.api.Assertions.assertThat;

public class FestTest {

  public static <T> T someMethod() {
    return (T) "123";
  }

  public static void main(String[] args) {
    assertThat(someMethod()).isEqualTo("123");
  }

}

This runs fine in Java 7 but not Java 8, which gives these compile errors:

java: reference to assertThat is ambiguous
  both method assertThat(java.lang.Boolean) in org.fest.assertions.api.Assertions and method assertThat(java.math.BigDecimal) in org.fest.assertions.api.Assertions match

java: no suitable method found for isEqualTo(java.lang.String)
    method org.fest.assertions.api.AbstractAssert.isEqualTo(java.lang.Boolean) is not applicable
      (argument mismatch; java.lang.String cannot be converted to java.lang.Boolean)
    method org.fest.assertions.api.BooleanAssert.isEqualTo(boolean) is not applicable
      (argument mismatch; java.lang.String cannot be converted to boolean)

The issue is also reported here:

http://openjdk.5641.n7.nabble.com/Re-Strange-covariant-generics-downcasting-issue-with-JDK8-td158942.html

Basically Java 8 is more strict in its generic typing than Java 7 and considered more correct, so the code in https://github.com/alexruiz/fest-assert-2.x/tree/fest-assert-core-2.0M10 is considered invalid by the compiler. Is there any hope of an update resolving this issue? A simple fix to this is to cast the return to an appropriate type:

assertThat((Object) someMethod()).isEqualTo("123");

But this is quite tedious if we have hundreds of such lines in our code with different types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant