Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import java.lang.reflect.*;
- import java.lang.reflect.InvocationTargetException;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by bogdan on 27.11.2015.
- */
- public class Json {
- private static final Log LOG = LogFactory.getLog(Json.class.getSimpleName());
- private Map<String, String> json = new HashMap<>();
- public <T> Json(T value, Class<T> clazz) {
- LOG.info("Starting to create json object");
- try {
- Object object = clazz.newInstance();
- for (Field field : object.getClass().getFields()) {
- for (Method method : object.getClass().getMethods()) {
- String fieldName = field.getName();
- if (method.getName().equalsIgnoreCase(String.format("%s%s", "get", fieldName))) {
- LOG.info("Trying to append field " + fieldName);
- String fieldValue = String.valueOf(method.invoke(value));
- json.put(fieldName, fieldValue);
- LOG.info("Managed to append field " + fieldName + " value " + fieldValue);
- }
- }
- }
- } catch (InstantiationException e) {
- LOG.error("Can't instantiate class");
- throw new ServiceException(e.getMessage());
- } catch (IllegalAccessException e) {
- LOG.error("Can't access class");
- throw new ServiceException(e.getMessage());
- } catch (InvocationTargetException e) {
- LOG.error("Can't access class method");
- throw new ServiceException(e.getMessage());
- }
- }
- public <T> T getObject(String key) {
- return ((T) json.get(key));
- }
- public <T> T getObject(Class<T> clazz) {
- Object object;
- try {
- object = clazz.newInstance();
- for (String fieldName : json.keySet()) {
- LOG.info("Init field object");
- // Object fieldObject = object.getClass().getField(fieldName).get(object);
- Field fieldObject = object.getClass().getField(fieldName);
- for (Method method : object.getClass().getMethods()) {
- if (method.getName().equalsIgnoreCase(String.format("%s%s", "set", fieldName))) {
- LOG.info("Managed to find a set method " + method.getName());
- method.invoke(object, getFieldValue(fieldObject, json.get(fieldName)));
- }
- }
- }
- } catch (InstantiationException e) {
- LOG.error("Can't instantiate class");
- throw new ServiceException(e.getMessage());
- } catch (IllegalAccessException e) {
- LOG.error("Can't access class");
- throw new ServiceException(e.getMessage());
- } catch (NoSuchFieldException e) {
- LOG.error("Field doesn't exist");
- throw new ServiceException(e.getMessage());
- } catch (InvocationTargetException e) {
- LOG.error("Method can't be access");
- throw new ServiceException(e.getMessage());
- }
- return clazz.cast(object);
- }
- private Object getFieldValue(Field fieldObject, String value) {
- LOG.info("Field class is " + fieldObject.getType().getSimpleName() + " value is " + value);
- if (fieldObject.getType().getSimpleName().equalsIgnoreCase("string")) {
- return value;
- }
- if (fieldObject.getType().getSimpleName().equalsIgnoreCase("integer")) {
- return new Integer(value);
- }
- if (fieldObject.getType().getSimpleName().equalsIgnoreCase("double")) {
- return new Double(value);
- }
- return null;
- }
- @Override
- public String toString() {
- StringBuilder stringBuilder = new StringBuilder();
- for (String key : json.keySet()) {
- stringBuilder.append(key);
- stringBuilder.append(":");
- stringBuilder.append(json.get(key));
- stringBuilder.append(",");
- }
- return stringBuilder.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement