All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dxee.dject.extend.AbstractPropertySource Maven / Gradle / Ivy

Go to download

A collection of guice extensions, help to improve the developer experience of guice.

There is a newer version: 1.5.1
Show newest version
package com.github.dxee.dject.extend;

import com.github.dxee.dject.spi.PropertySource;

import java.lang.reflect.Method;

public abstract class AbstractPropertySource implements PropertySource {
    @Override
    public  T get(String key, Class type) {
        return get(key, type, null);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  T get(String key, Class type, T defaultValue) {
        String value = get(key);
        if (value == null) {
            return defaultValue;
        }
        Method method;
        try {
            method = type.getDeclaredMethod("valueOf", String.class);
        } catch (Exception e) {
            throw new RuntimeException("Unable to find method 'valueOf' of type '" + type.getName() + "'");
        }
        
        try {
            return (T) method.invoke(null, value);
        } catch (Exception e) {
            throw new RuntimeException("Unable to invoke method 'valueOf' of type '" + type.getName() + "'");
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy