Skip to content

Exception received when Lambda expressions used inside @Provides method. #6

@ryonday

Description

@ryonday

Let's make a module:

public class TestModule extends AbstractModule {

@Override
    protected void configure() {

        Config config = ConfigFactory.load().getConfig("here.we.go");

        install(TypesafeConfigModule.fromConfigWithPackage(config, "stuff.package"));
    }
}

For some reason, we want to provide a String based on what's in the config (stupid example):

    @Provides
    @Singleton
    private String someString( @TypesafeConfig("one") String one,
                                   @TypesafeConfig("two") String two,
                                   @TypesafeConfig("three") String three) {

        return Optional.ofNullable(one)
            .map( s -> two)
            .orElse(three);
    }

This will result in the following exception:

Jun 23, 2017 1:48:01 PM com.google.inject.internal.MessageProcessor visit
INFO: An exception was caught and reported. Message: org.reflections.ReflectionsException: Can't resolve member named 0 for class package.for.TestModule.lambda$someString
org.reflections.ReflectionsException: Can't resolve member named 0 for class package.for.TestModule..lambda$someString

Apparently the Reflections package counts the lambdas in the method as separate classes (which they are).

This, however works:

    @Provides
    @Singleton
    private String someString( @TypesafeConfig("one") String one,
                                   @TypesafeConfig("two") String two,
                                   @TypesafeConfig("three") String three) {

         if(one == null) {
            return two;
        } else {
            return three;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions