Changing the compiler in Java 7

In Java 6, I was able to use:

public static <T, UK extends T, US extends T> T getCountrySpecificComponent(UK uk, US us) {
    Country country = CountryContext.getCountry();
    if (country == Country.US) {
        return us;
    } else if (country == Country.UK) {
        return uk;
    } else {
        throw new IllegalStateException("Unhandled country returned: "+country);
    }
}

With these repositories:

public interface Repository{
   List<User> findAll();
}

public interface RepositoryUS extends Repository{}

public interface RepositoryUK extends Repository{}

When using these:

RepositoryUK uk = ...
RepositoryUS us = ...

This line compiles in Java6, but does not work in Java7 (the error cannot find the character - because the compiler is looking for findAll () for the class object)

List<User> users = getCountrySpecificComponent(uk, us).findAll();

Compiles in Java 7

List<User> users = ((Repository)getCountrySpecificComponent(uk, us)).findAll();

I know this is a rather unusual use case, but is there a reason for this change? Or is there a way to tell the compiler a little smarter?

+5
source share
3 answers

I think it Tshould be limited to expand Repository. Thus, the compiler knows that it getCountrySpecificComponentreturns some repository.

EDIT:

It should also be good to write: public static <T extends Repository> T getCountrySpecificComponent(T uk, T us)

+3
source

, , . , - :

public interface UserFindingComponent{
   List<User> findAll(); 
}

public interface Repository extends UserFindingComponent{ }

public interface RepositoryUS extends Repository{}

public interface RepositoryUK extends Repository{}

...

public static <T extends UserFindingComponent, UK extends T, US extends T> T getCountrySpecificComponent(UK uk, US us) {
    Country country = CountryContext.getCountry();
    if (country == Country.US) {
        return us;
    } else if (country == Country.UK) {
        return uk;
    } else {
        throw new IllegalStateException("Unhandled country returned: "+country);
    }
}
0

In this case, the compiler could not deduce the type parameters, which probably should have been the case in Java 6. You can tell the compiler that, however, the general types using the syntax below:

import java.util.List;

class User {
}

interface Repository {
  List<User> findAll();
}

interface RepositoryUS extends Repository {
}

interface RepositoryUK extends Repository {
}

class Test {
  public static <T, UK extends T, US extends T> T getCountrySpecificComponent(UK uk, US us) {
    Country country = CountryContext.getCountry();
    if (country == Country.US) {
      return us;
    } else if (country == Country.UK) {
      return uk;
    } else {
      throw new IllegalStateException("Unhandled country returned: " + country);
    }
    return us;
  }

  public static void main(String... args) {
    RepositoryUK uk = null;
    RepositoryUS us = null;
    List<User> users = Test.<Repository, RepositoryUK, RepositoryUS>getCountrySpecificComponent(uk, us).findAll(); 
  }
}
0
source

All Articles