diff --git a/edexOsgi/com.raytheon.uf.common.util/.classpath b/edexOsgi/com.raytheon.uf.common.util/.classpath index 1fa3e6803d..919c1e858c 100644 --- a/edexOsgi/com.raytheon.uf.common.util/.classpath +++ b/edexOsgi/com.raytheon.uf.common.util/.classpath @@ -3,5 +3,6 @@ + diff --git a/edexOsgi/com.raytheon.uf.common.util/build.properties b/edexOsgi/com.raytheon.uf.common.util/build.properties index 34d2e4d2da..2b7d65d0b4 100644 --- a/edexOsgi/com.raytheon.uf.common.util/build.properties +++ b/edexOsgi/com.raytheon.uf.common.util/build.properties @@ -2,3 +2,4 @@ source.. = src/ output.. = bin/ bin.includes = META-INF/,\ . +src.excludes = test/src/ diff --git a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/ConvertUtil.java b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/ConvertUtil.java index 884aa32fab..b26119d94d 100644 --- a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/ConvertUtil.java +++ b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/ConvertUtil.java @@ -37,6 +37,7 @@ import org.apache.commons.beanutils.Converter; * Mar 19, 2009 njensen Initial creation * Mar 13, 2013 1789 bsteffen Move Calendar and Date parsing out of * ConvertUtil and also fix date parsing. + * Jun 11, 2013 2091 bclement added getFields utility/javadoc * * * @@ -76,7 +77,7 @@ public class ConvertUtil { * If the string value cannot be converted to the desired class * type */ - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public static Object convertObject(String value, Class desiredClass) { if (value == null || value.equals("null")) { return null; @@ -94,12 +95,36 @@ public class ConvertUtil { return ConvertUtils.convert(obj); } + /** + * Convert value to same class as field of entity. + * + * @param value + * string representation of object + * @param entity + * @param fieldName + * @return + * @throws SecurityException + * @throws NoSuchFieldException + */ public static Object convertAsType(String value, Class entity, String fieldName) throws SecurityException, NoSuchFieldException { Field field = getField(fieldName, entity); return convertObject(value, field.getType()); } + /** + * Convert value to same class as addressed field of entity. + * + * @param value + * string representation of object + * @param entity + * @param fieldPath + * path that addresses the target field for conversion. First + * field name on path is a field of entity. + * @return + * @throws SecurityException + * @throws NoSuchFieldException + */ public static Object convertAsType(String value, Class entity, String[] fieldPath) throws SecurityException, NoSuchFieldException { Field f = getField(fieldPath[0], entity); @@ -109,7 +134,39 @@ public class ConvertUtil { return convertObject(value, f.getType()); } - protected static Field getField(String fieldName, Class c) + /** + * Get array of Field objects for nested field path. + * + * @param entity + * @param fieldPath + * name of fields in each nested object starting with a field in + * entity + * @return + * @throws SecurityException + * @throws NoSuchFieldException + */ + public static Field[] getFields(Class entity, String[] fieldPath) + throws SecurityException, NoSuchFieldException { + Field[] rval = new Field[fieldPath.length]; + rval[0] = getField(fieldPath[0], entity); + for (int i = 1; i < fieldPath.length; ++i) { + rval[i] = getField(fieldPath[i], rval[i - 1].getType()); + } + return rval; + } + + /** + * Recursive method to get Field object from class. + * + * @param fieldName + * name of field in c + * @param c + * entity that has field + * @return + * @throws SecurityException + * @throws NoSuchFieldException + */ + public static Field getField(String fieldName, Class c) throws SecurityException, NoSuchFieldException { Field rval; try { diff --git a/edexOsgi/com.raytheon.uf.common.util/test/src/com/raytheon/uf/common/util/ConvertUtilsTest.java b/edexOsgi/com.raytheon.uf.common.util/test/src/com/raytheon/uf/common/util/ConvertUtilsTest.java new file mode 100644 index 0000000000..28be7f2c29 --- /dev/null +++ b/edexOsgi/com.raytheon.uf.common.util/test/src/com/raytheon/uf/common/util/ConvertUtilsTest.java @@ -0,0 +1,125 @@ +// header placeholder 2a20 c8e1 +package com.raytheon.uf.common.util; + +import java.lang.reflect.Field; +import java.util.Date; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for convert utils + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * Jun 11, 2013            bclement     Initial creation
+ * 
+ * 
+ * + * @author bclement + * @version 1.0 + */ +public class ConvertUtilsTest { + + public static class TestEnity { + + protected int number; + + protected NestedTestEnity nested; + + /** + * @param number + * @param nested + */ + public TestEnity(int number, NestedTestEnity nested) { + this.number = number; + this.nested = nested; + } + + /** + * @return the number + */ + public int getNumber() { + return number; + } + + /** + * @param number + * the number to set + */ + public void setNumber(int number) { + this.number = number; + } + + /** + * @return the nested + */ + public NestedTestEnity getNested() { + return nested; + } + + /** + * @param nested + * the nested to set + */ + public void setNested(NestedTestEnity nested) { + this.nested = nested; + } + + } + + public static class NestedTestEnity { + + protected Date time; + + /** + * @param time + */ + public NestedTestEnity(Date time) { + this.time = time; + } + + /** + * @return the time + */ + public Date getTime() { + return time; + } + + /** + * @param time + * the time to set + */ + public void setTime(Date time) { + this.time = time; + } + + } + + /** + * @throws NoSuchFieldException + * @throws SecurityException + * @throws IllegalAccessException + * @throws IllegalArgumentException + * + */ + @Test + public void getFieldsTest() throws SecurityException, NoSuchFieldException, + IllegalArgumentException, IllegalAccessException { + final Date now = new Date(); + + final String[] path = { "nested", "time" }; + final TestEnity ent = new TestEnity(42, new NestedTestEnity(now)); + Field[] res = ConvertUtil.getFields(TestEnity.class, path); + Assert.assertEquals(path.length, res.length); + + NestedTestEnity resNested = (NestedTestEnity) res[0].get(ent); + Date resDate = (Date) res[1].get(resNested); + Assert.assertEquals(now, resDate); + } + +}