org.webpieces.plugins.properties.beans.PropertyInvoker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plugin-properties Show documentation
Show all versions of plugin-properties Show documentation
Webpieces database backed property store
package org.webpieces.plugins.properties.beans;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.webpieces.router.api.extensions.ObjectStringConverter;
import org.webpieces.router.impl.params.ObjectTranslator;
@Singleton
public class PropertyInvoker {
private ObjectTranslator objectTranslator;
@Inject
public PropertyInvoker(
ObjectTranslator objectTranslator
) {
this.objectTranslator = objectTranslator;
}
public String readPropertyAsString(PropertyInfo p) {
Object value = getValue(p);
return objectTranslator.getConverterFor(value).objectToString(value);
}
private Object getValue(PropertyInfo p) {
Method method = p.getGetter();
try {
return method.invoke(p.getInjectee());
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
public ObjectStringConverter> fetchConverter(PropertyInfo info) {
Class> returnType = info.getGetter().getReturnType();
ObjectStringConverter> converter = objectTranslator.getConverter(returnType);
if(converter == null) {
//since we checked before when loading, there should be a converter, or code was changed and we have new bug
throw new RuntimeException("Odd, this shouldn't be possible, bug. return type="+returnType.getName());
}
return converter;
}
public void writeProperty(PropertyInfo info, String valueAsStr) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class> returnType = info.getGetter().getReturnType();
ObjectStringConverter> converter = objectTranslator.getConverter(returnType);
if(converter == null) {
//since we checked before when loading, there should be a converter, or code was changed and we have new bug
throw new RuntimeException("Odd, this shouldn't be possible, bug. return type="+returnType.getName());
}
Object val = converter.stringToObject(valueAsStr);
Object injectee = info.getInjectee();
info.getSetter().invoke(injectee, val);
}
}