racc / typesafeconfig-guice

Allows Guice Injection of configuration values from Typesafe Config
Apache License 2.0
72 stars 18 forks source link

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

Open ryonday opened 7 years ago

ryonday commented 7 years ago

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;
        }
    }
ryonday commented 7 years ago

P.S. Awesome library!

racc commented 7 years ago

Thanks @ryonday. Seems like a weird bug, but at least you have a workaround :) Happy to take a patch if you have one, but it might be a little while before I can take a look at it otherwise...