Home > OS >  Guice, Type Conversion When Getting Values from Properties
Guice, Type Conversion When Getting Values from Properties

Time:09-26

The following Guice module binds a property file to the @Named annotation.

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

// Omitted: other imports

public class ExampleModule extends AbstractModule {
    @Override
    protected void configure() {
        Names.bindProperties(binder(), getProperties());
    }

    private Properties getProperties() {
        // Omitted: return the application.properties file
    }
}

I can now inject properties directly into my classes.

public class Example {
    @Inject
    @Named("com.example.title")
    private String title;

    @Inject
    @Named("com.example.panel-height")
    private int panelHeight;
}

The values read from a properties file are strings but, as you can see in the example above, Guice is capable of doing type conversion for int fields.

Now, given the property com.example.background-color=0x333333 I would like to able to get the same type conversion for an arbitrary class, like:

public class Example {
    @Inject
    @Named("com.example.background-color")
    private Color color;
}

Let's say that the Color class contains a static method decode() and I can obtain a new Color instance by calling Color.decode("0x333333").

How can I configure Guice to do this automatically and behind the scenes for me?

CodePudding user response:

Guice can't do that for you.

I suppose the conversion from String to int happens upon injection and not when you call Names.bindProperties(...)

See the bindProperties methods:

  /** Creates a constant binding to {@code @Named(key)} for each entry in {@code properties}. */
  public static void bindProperties(Binder binder, Map<String, String> properties) {
    binder = binder.skipSources(Names.class);
    for (Map.Entry<String, String> entry : properties.entrySet()) {
      String key = entry.getKey();
      String value = entry.getValue();
      binder.bind(Key.get(String.class, new NamedImpl(key))).toInstance(value);
    }
  }

  /**
   * Creates a constant binding to {@code @Named(key)} for each property. This method binds all
   * properties including those inherited from {@link Properties#defaults defaults}.
   */
  public static void bindProperties(Binder binder, Properties properties) {
    binder = binder.skipSources(Names.class);

    // use enumeration to include the default properties
    for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); ) {
      String propertyName = (String) e.nextElement();
      String value = properties.getProperty(propertyName);
      binder.bind(Key.get(String.class, new NamedImpl(propertyName))).toInstance(value);
    }
  }

They are just binding strings.

You could just copy one of them and create your own binding. If the property value is in a color format, bind it additionally as Color.

As an example:

public class GuiceColors {

    public static class GameModule extends AbstractModule {
        @Override
        protected void configure() {
            Properties props = new Properties();
            try {
                props.load(getClass().getResourceAsStream("application.properties"));
            } catch (IOException e) {
                e.printStackTrace();
            }

            bindPropertiesWithColors(props);
        }

        private void bindPropertiesWithColors(Properties properties) {
            Binder binder2 = binder().skipSources(Names.class);

            // use enumeration to include the default properties
            for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements();) {
                String propertyName = (String) e.nextElement();
                String value = properties.getProperty(propertyName);
                try {
                    Color decodedColor = Color.decode(value);
                    binder2.bind(Key.get(Color.class, Names.named(propertyName)))
                            .toInstance(decodedColor);
                } catch (NumberFormatException ex) {
                    // property value cannot be decoded as color, ignore the exception
                }
                binder2.bind(Key.get(String.class, Names.named(propertyName))).toInstance(value);
            }
        }

    }

    public static class Example {
        @Inject
        @Named("com.example.background-color")
        private Color color;

        @Inject
        @Named("com.example.background-color")
        private String colorString;

    }

    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new GameModule());
        System.out.println(injector.getInstance(Example.class).color);
        System.out.println(injector.getInstance(Example.class).colorString);
    }
}

with application.properties being:

com.example.background-color = 0x333333

CodePudding user response:

I found a solution by myself looking into the Guice sources, although I have to say it's not the prettiest (more on this later on).

First of all, we need to create a TypeConverter.

import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeConverter;

// Omitted: other imports

public class ColorTypeConverter implements TypeConverter {
    @Override
    public Object convert(String value, TypeLiteral<?> toType) {
        if (!toType.getRawType().isAssignableFrom(Color.class)) {
            throw new IllegalArgumentException("Cannot convert type "   toType.getType().getTypeName());
        }

        if (value == null || value.isBlank()) {
            return null;
        }

        return Color.decode(value);
    }
}

Then, a Matcher. I generalized.

import com.google.inject.TypeLiteral;
import com.google.inject.matcher.AbstractMatcher;

// Omitted: other imports

public class SubclassMatcher extends AbstractMatcher<TypeLiteral<?>> {
    private final Class<?> type;

    public SubclassMatcher(Class<?> type) {
        this.type = type;
    }

    @Override
    public boolean matches(TypeLiteral<?> toType) {
        return toType.getRawType().isAssignableFrom(type);
    }
}

Finally, add the following line to the Guice module.

import com.google.inject.AbstractModule;

// Omitted: other imports

public class ExampleModule extends AbstractModule {
    @Override
    protected void configure() {
        binder().convertToTypes(new SubclassMatcher(Color.class), new ColorTypeConverter());
        // Omitted: other configurations
    }
}

Now, the following injection works.

public class Example {
    @Inject
    @Named("com.example.background-color")
    private Color backgroundColor;
}

It could be prettier. There exists a com.google.inject.matcher.Matchers API which I wasn't able use and could have solved my problem without constructing my personal SubclassMatcher class. See, Matchers.subclassesOf(Class<?>). It's for sure my fault as I don't believe Google wouldn't think of this pretty common use-case. If you find a way to make it work, please leave a comment.

  • Related