throws Exception
{
// construct instance
Object instance = null;
ThriftFieldMetadata fieldMetadata = null;
if (data != null) {
fieldMetadata = metadataMap.get(data.getKey());
if (fieldMetadata != null && fieldMetadata.getConstructorInjection().isPresent()) {
ThriftConstructorInjection constructor = fieldMetadata.getConstructorInjection().get();
Object[] parametersValues = new Object[] { data.getValue() };
try {
instance = constructor.getConstructor().newInstance(parametersValues);
}
catch (InvocationTargetException e) {
if (e.getTargetException() != null) {
Throwables.propagateIfInstanceOf(e.getTargetException(), Exception.class);
}
throw e;
}
}
}
if (instance == null && metadata.getConstructorInjection().isPresent()) {
ThriftConstructorInjection constructor = metadata.getConstructorInjection().get();
// must be no-args
Object[] parametersValues = new Object[0];
try {
instance = constructor.getConstructor().newInstance(parametersValues);
}
catch (InvocationTargetException e) {
if (e.getTargetException() != null) {
Throwables.propagateIfInstanceOf(e.getTargetException(), Exception.class);
}
throw e;
}
}
if (fieldMetadata != null) {
// inject fields
for (ThriftInjection injection : fieldMetadata.getInjections()) {
if (injection instanceof ThriftFieldInjection) {
ThriftFieldInjection fieldInjection = (ThriftFieldInjection) injection;
if (data.getValue() != null) {
fieldInjection.getField().set(instance, data.getValue());
}
}
}
if (fieldMetadata.getMethodInjection().isPresent()) {
Object[] parametersValues = new Object[] { data.getValue() };
if (data.getValue() != null) {
try {
fieldMetadata.getMethodInjection().get().getMethod().invoke(instance, parametersValues);
}
catch (InvocationTargetException e) {
if (e.getTargetException() != null) {
Throwables.propagateIfInstanceOf(e.getTargetException(), Exception.class);
}