/** * Converts a tab delimited string into an object with given fields * Requires the object has public access for the specified fields * * @param objClass Class of object to be created * @param str string to convert * @param delimiterPattern delimiter * @param fieldNames fieldnames * @param <T> type to return * @return Object created from string */ public static <T> T columnStringToObject(Class<?> objClass, String str, Pattern delimiterPattern, String[] fieldNames) throws InstantiationException, IllegalAccessException, NoSuchMethodException, NoSuchFieldException, InvocationTargetException { String[] fields = delimiterPattern.split(str); T item = ErasureUtils.uncheckedCast(objClass.newInstance()); for (int i = 0; i < fields.length; i++) { try { Field field = objClass.getDeclaredField(fieldNames[i]); field.set(item, fields[i]); } catch (IllegalAccessException ex) { Method method = objClass.getDeclaredMethod("set" + StringUtils.capitalize(fieldNames[i]), String.class); method.invoke(item, fields[i]); } } return item; }
/** * Converts an object into a tab delimited string with given fields * Requires the object has public access for the specified fields * * @param object Object to convert * @param delimiter delimiter * @param fieldNames fieldnames * @return String representing object */ public static String objectToColumnString(Object object, String delimiter, String[] fieldNames) throws IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException { StringBuilder sb = new StringBuilder(); for (String fieldName : fieldNames) { if (sb.length() > 0) { sb.append(delimiter); } try { Field field = object.getClass().getDeclaredField(fieldName); sb.append(field.get(object)); } catch (IllegalAccessException ex) { Method method = object.getClass().getDeclaredMethod("get" + StringUtils.capitalize(fieldName)); sb.append(method.invoke(object)); } } return sb.toString(); }
/** * Converts an object into a tab delimited string with given fields * Requires the object has public access for the specified fields * * @param object Object to convert * @param delimiter delimiter * @param fieldNames fieldnames * @return String representing object */ public static String objectToColumnString(Object object, String delimiter, String[] fieldNames) throws IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException { StringBuilder sb = new StringBuilder(); for (String fieldName : fieldNames) { if (sb.length() > 0) { sb.append(delimiter); } try { Field field = object.getClass().getDeclaredField(fieldName); sb.append(field.get(object)); } catch (IllegalAccessException ex) { Method method = object.getClass().getDeclaredMethod("get" + StringUtils.capitalize(fieldName)); sb.append(method.invoke(object)); } } return sb.toString(); }
/** * Converts an object into a tab delimited string with given fields * Requires the object has public access for the specified fields * * @param object Object to convert * @param delimiter delimiter * @param fieldNames fieldnames * @return String representing object */ public static String objectToColumnString(Object object, String delimiter, String[] fieldNames) throws IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException { StringBuilder sb = new StringBuilder(); for (int i = 0; i < fieldNames.length; i++) { if (sb.length() > 0) { sb.append(delimiter); } try { Field field = object.getClass().getDeclaredField(fieldNames[i]); sb.append(field.get(object)) ; } catch (IllegalAccessException ex) { Method method = object.getClass().getDeclaredMethod("get" + StringUtils.capitalize(fieldNames[i])); sb.append(method.invoke(object)); } } return sb.toString(); }
/** * Converts an object into a tab delimited string with given fields * Requires the object has public access for the specified fields * * @param object Object to convert * @param delimiter delimiter * @param fieldNames fieldnames * @return String representing object */ public static String objectToColumnString(Object object, String delimiter, String[] fieldNames) throws IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException { StringBuilder sb = new StringBuilder(); for (String fieldName : fieldNames) { if (sb.length() > 0) { sb.append(delimiter); } try { Field field = object.getClass().getDeclaredField(fieldName); sb.append(field.get(object)); } catch (IllegalAccessException ex) { Method method = object.getClass().getDeclaredMethod("get" + StringUtils.capitalize(fieldName)); sb.append(method.invoke(object)); } } return sb.toString(); }
/** * Converts an object into a tab delimited string with given fields * Requires the object has public access for the specified fields * * @param object Object to convert * @param delimiter delimiter * @param fieldNames fieldnames * @return String representing object */ public static String objectToColumnString(Object object, String delimiter, String[] fieldNames) throws IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException { StringBuilder sb = new StringBuilder(); for (String fieldName : fieldNames) { if (sb.length() > 0) { sb.append(delimiter); } try { Field field = object.getClass().getDeclaredField(fieldName); sb.append(field.get(object)); } catch (IllegalAccessException ex) { Method method = object.getClass().getDeclaredMethod("get" + StringUtils.capitalize(fieldName)); sb.append(method.invoke(object)); } } return sb.toString(); }
/** * Converts a tab delimited string into an object with given fields * Requires the object has public access for the specified fields * * @param objClass Class of object to be created * @param str string to convert * @param delimiterPattern delimiter * @param fieldNames fieldnames * @param <T> type to return * @return Object created from string */ public static <T> T columnStringToObject(Class<?> objClass, String str, Pattern delimiterPattern, String[] fieldNames) throws InstantiationException, IllegalAccessException, NoSuchMethodException, NoSuchFieldException, InvocationTargetException { String[] fields = delimiterPattern.split(str); T item = ErasureUtils.uncheckedCast(objClass.newInstance()); for (int i = 0; i < fields.length; i++) { try { Field field = objClass.getDeclaredField(fieldNames[i]); field.set(item, fields[i]); } catch (IllegalAccessException ex) { Method method = objClass.getDeclaredMethod("set" + StringUtils.capitalize(fieldNames[i]), String.class); method.invoke(item, fields[i]); } } return item; }
/** * Converts a tab delimited string into an object with given fields * Requires the object has public access for the specified fields * * @param objClass Class of object to be created * @param str string to convert * @param delimiterPattern delimiter * @param fieldNames fieldnames * @param <T> type to return * @return Object created from string */ public static <T> T columnStringToObject(Class<?> objClass, String str, Pattern delimiterPattern, String[] fieldNames) throws InstantiationException, IllegalAccessException, NoSuchMethodException, NoSuchFieldException, InvocationTargetException { String[] fields = delimiterPattern.split(str); T item = ErasureUtils.uncheckedCast(objClass.newInstance()); for (int i = 0; i < fields.length; i++) { try { Field field = objClass.getDeclaredField(fieldNames[i]); field.set(item, fields[i]); } catch (IllegalAccessException ex) { Method method = objClass.getDeclaredMethod("set" + StringUtils.capitalize(fieldNames[i]), String.class); method.invoke(item, fields[i]); } } return item; }
/** * Converts a tab delimited string into an object with given fields * Requires the object has public access for the specified fields * * @param objClass Class of object to be created * @param str string to convert * @param delimiterPattern delimiter * @param fieldNames fieldnames * @param <T> type to return * @return Object created from string */ public static <T> T columnStringToObject(Class<?> objClass, String str, Pattern delimiterPattern, String[] fieldNames) throws InstantiationException, IllegalAccessException, NoSuchMethodException, NoSuchFieldException, InvocationTargetException { String[] fields = delimiterPattern.split(str); T item = ErasureUtils.<T>uncheckedCast(objClass.newInstance()); for (int i = 0; i < fields.length; i++) { try { Field field = objClass.getDeclaredField(fieldNames[i]); field.set(item, fields[i]); } catch (IllegalAccessException ex) { Method method = objClass.getDeclaredMethod("set" + StringUtils.capitalize(fieldNames[i]), String.class); method.invoke(item, fields[i]); } } return item; }
/** * Converts a tab delimited string into an object with given fields * Requires the object has public access for the specified fields * * @param objClass Class of object to be created * @param str string to convert * @param delimiterPattern delimiter * @param fieldNames fieldnames * @param <T> type to return * @return Object created from string */ public static <T> T columnStringToObject(Class<?> objClass, String str, Pattern delimiterPattern, String[] fieldNames) throws InstantiationException, IllegalAccessException, NoSuchMethodException, NoSuchFieldException, InvocationTargetException { String[] fields = delimiterPattern.split(str); T item = ErasureUtils.<T>uncheckedCast(objClass.newInstance()); for (int i = 0; i < fields.length; i++) { try { Field field = objClass.getDeclaredField(fieldNames[i]); field.set(item, fields[i]); } catch (IllegalAccessException ex) { Method method = objClass.getDeclaredMethod("set" + StringUtils.capitalize(fieldNames[i]), String.class); method.invoke(item, fields[i]); } } return item; }