1

I'm working on an application which is working on Java 1.7. I need to rewrite some code which has been writing with Java 1.8 with SpringFramework. Unfortunately, I'm not familiar with a newer version and I don't know how to rewrite this code to work with Java 7...

Below part of the code.

ConfigRepo:

public class ConfigRepo extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repoRestConfig) {
    repoRestConfig.withEntityLookup().forRepository(IConfigRepo.class, (Config config) -> {
        ConfigPK pk = new ConfigPK();
        pk.setScope(config.getId().getScope());
        pk.setKey(config.getId().getKey());
        return pk;
    }, IConfigRepo::findOne);
}

IConfigRepo:

public interface IConfigRepo extends CrudRepository<Config, ConfigPK> {}

EDIT: Added my code.

I'm not sure if this what I've done part of it correctly. I don't know how this Config config should be passed. Also I don't know what I should do with this method reference...

My version:

public class ConfigRepo extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repoRestConfig) {
    repoRestConfig.withEntityLookup().forRepository(IConfigRepo.class, new Config() {
        public ConfigPK prepareConfigPK(Config config) {
            ConfigPK pk = new ConfigPK();
            pk.setScope(config.getId().getScope());
            pk.setKey(config.getId().getKey());
            return pk;
        }, IConfigRepo::findOne);
}
Lui
  • 594
  • 3
  • 10
  • 23
  • What is your question? – C-Otto Jun 19 '17 at 10:53
  • I think it's quite clear... I'm asking for help to rewrite this code to Java 7, because I can't handle with this. – Lui Jun 19 '17 at 10:55
  • 1
    I realize you want help, but where are you struggling? What did you try? What does not work? Where's your code? – C-Otto Jun 19 '17 at 10:59
  • 1
    Just convert it to an anonymous class, for example: https://stackoverflow.com/a/43145782/829571 – assylias Jun 19 '17 at 11:01
  • 3
    @C-Otto If you're not familiar with how Java 8 works, you might have a lot of troubles understanding how to rewrite that. For instance, `IConfigRepo::findOne` I'd have to first understand what is this *thing* then sort out how to revert it since it's not obvious (the other way around is much, much more obvious). I think that this whole situation is properly explained by Lui. It's not trivial and I'd be lost too. – Olivier Grégoire Jun 19 '17 at 11:04
  • @C-Otto added my part of conversion... But I'm not sure if I understood the lambda correctly in this case. With the method reference I don't know how to handle this. – Lui Jun 19 '17 at 11:09
  • @Lui extract methods for lambda or method reference expression, and then press `ALT+ Enter` replace them with anonymous class. – holi-java Jun 19 '17 at 12:19

1 Answers1

2

The function forRepository seems to accept three arguments:

  1. A Class<IConfigRepo>
  2. An instance of the interface Converter<Config, ConfigPK>:

    public interface Converter<Config, ConfigPK> {
        ConfigPK convert(Config config);
    }
    

    It's actually a generic interface but I inserted the types you use there.

  3. An instance of another functional interface Lookup<IConfigRepo, ID>

    public interface Lookup {
        Object lookup(IConfigRepo repository, ID identifier)
    }
    

    Again a generic interface, but I inserted the types you use (except ID).

So the both functional interface arguments can be rewritten as instances of anonymous classes:

// Java 8
(Config config) -> {
    ConfigPK pk = new ConfigPK();
    pk.setScope(config.getId().getScope());
    pk.setKey(config.getId().getKey());
    return pk;
}

//Java 7
new Converter<Config, ConfigPK>() {
    @Override
    public ConfigPK convert(Config config) {
        ConfigPK pk = new ConfigPK();
        pk.setScope(config.getId().getScope());
        pk.setKey(config.getId().getKey());
        return pk;
    }
}

and

// Java 8
IConfigRepo::findOne


// Java 7
// ??? because I don't know your type for ID
new Lookup<IConfigRepo, ???>() {
     @Override
     public Object lookup(IConfigRepo repository, ??? identifier) {
          return repo.findOne();
     }
}

In your code you can replace the Java8 style lambda and method reference as parameters with what I wrote there

danielspaniol
  • 2,228
  • 21
  • 38
  • Basically, the forRepository method comes from Spring Data REST - [link](http://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest/core/config/EntityLookupRegistrar.html#forRepository-java.lang.Class-org.springframework.core.convert.converter.Converter-org.springframework.data.rest.core.config.EntityLookupRegistrar.LookupRegistrar.Lookup-) – Lui Jun 19 '17 at 11:52
  • Do I need to update the interfaces in my answer or can you do the transfer? – danielspaniol Jun 19 '17 at 11:53
  • If you could do this, specially for last parameter... Because this method\s declaration is from CrudRepository interface and implementation is in SimpleJpaRepository. And to be honest it's quite hard for me... What is more, what about those names for "new MethodName()"? For second argument it should be new Config()? And for third parameter new CrudRepository()?? – Lui Jun 19 '17 at 12:04
  • Ahh, so it should look like this... Thank you for your help, I really appreciate it! :) But I'm not sure also what type should be for ID... At least for now, because I've just started with this application. But I'm wondering... How Java 8 knows what should be used in this method reference?? Because in Java 8 we are not passing any object to this function... So somehow it must know what should be used... – Lui Jun 19 '17 at 14:52