Merge "Issue #2091 added getFields method to ConvertUtil" into development

Former-commit-id: 8600b56f3a83eda25c004262a98999d84ab01b89
This commit is contained in:
Dustin Johnson 2013-06-11 17:16:32 -05:00 committed by Gerrit Code Review
commit 182ae795fc
4 changed files with 186 additions and 2 deletions

View file

@ -3,5 +3,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="src" output="test/bin" path="test/src"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View file

@ -2,3 +2,4 @@ source.. = src/
output.. = bin/ output.. = bin/
bin.includes = META-INF/,\ bin.includes = META-INF/,\
. .
src.excludes = test/src/

View file

@ -37,6 +37,7 @@ import org.apache.commons.beanutils.Converter;
* Mar 19, 2009 njensen Initial creation * Mar 19, 2009 njensen Initial creation
* Mar 13, 2013 1789 bsteffen Move Calendar and Date parsing out of * Mar 13, 2013 1789 bsteffen Move Calendar and Date parsing out of
* ConvertUtil and also fix date parsing. * ConvertUtil and also fix date parsing.
* Jun 11, 2013 2091 bclement added getFields utility/javadoc
* *
* </pre> * </pre>
* *
@ -76,7 +77,7 @@ public class ConvertUtil {
* If the string value cannot be converted to the desired class * If the string value cannot be converted to the desired class
* type * type
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings({ "unchecked", "rawtypes" })
public static Object convertObject(String value, Class<?> desiredClass) { public static Object convertObject(String value, Class<?> desiredClass) {
if (value == null || value.equals("null")) { if (value == null || value.equals("null")) {
return null; return null;
@ -94,12 +95,36 @@ public class ConvertUtil {
return ConvertUtils.convert(obj); 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, public static Object convertAsType(String value, Class<?> entity,
String fieldName) throws SecurityException, NoSuchFieldException { String fieldName) throws SecurityException, NoSuchFieldException {
Field field = getField(fieldName, entity); Field field = getField(fieldName, entity);
return convertObject(value, field.getType()); 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, public static Object convertAsType(String value, Class<?> entity,
String[] fieldPath) throws SecurityException, NoSuchFieldException { String[] fieldPath) throws SecurityException, NoSuchFieldException {
Field f = getField(fieldPath[0], entity); Field f = getField(fieldPath[0], entity);
@ -109,7 +134,39 @@ public class ConvertUtil {
return convertObject(value, f.getType()); 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 { throws SecurityException, NoSuchFieldException {
Field rval; Field rval;
try { try {

View file

@ -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
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jun 11, 2013 bclement Initial creation
*
* </pre>
*
* @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);
}
}