com.raynigon.unit_api.jackson.annotation.JsonQuantityHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-module Show documentation
Show all versions of jackson-module Show documentation
The jackson-module is a part of the unit-api
The newest version!
package com.raynigon.unit_api.jackson.annotation;
import com.raynigon.unit_api.core.io.QuantityReader;
import com.raynigon.unit_api.core.io.QuantityWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class JsonQuantityHelper {
public static QuantityReader getReaderInstance(JsonQuantityReader readerWrapper) {
if (readerWrapper == null) return null;
return createInstace(readerWrapper.value(), "Reader");
}
public static QuantityWriter getWriterInstance(JsonQuantityWriter writerWrapper) {
if (writerWrapper == null) return null;
return createInstace(writerWrapper.value(), "Writer");
}
private static T createInstace(Class extends T> readerType, String type) {
try {
Constructor extends T> ctor = readerType.getConstructor();
return ctor.newInstance();
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Unable to create Quantity " + type + ":" + readerType, e);
}
}
}