com.github.dxee.dject.extend.AbstractPropertySource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dject-core Show documentation
Show all versions of dject-core Show documentation
A collection of guice extensions, help to improve the developer experience of guice.
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() + "'");
}
}
}