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);
}
7
  • What is your question? Commented Jun 19, 2017 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. Commented Jun 19, 2017 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? Commented Jun 19, 2017 at 10:59
  • 1
    Just convert it to an anonymous class, for example: stackoverflow.com/a/43145782/829571 Commented Jun 19, 2017 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. Commented Jun 19, 2017 at 11:04

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Basically, the forRepository method comes from Spring Data REST - link
Do I need to update the interfaces in my answer or can you do the transfer?
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()??
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.