public static void addValueToList(List list, String[] str, Object pointer)throws Exception
{
Field field = null;
String type = null;
for (int i = 0; i < str.length; i++)
{
field = pointer.getClass().getDeclaredField(str[i]);
type = field.getType().getName();
String methodName = null;
StringBuffer buffer = new StringBuffer();
String substr1 = str[i].substring(0, 1);
String substr2 = str[i].substring(1, str[i].length());
buffer.append(substr1.toUpperCase(Locale.getDefault()));
buffer.append(substr2);
methodName = new String(buffer);
Method method = null;
try
{
method = pointer.getClass().getDeclaredMethod(methodName, null);
}
catch (NoSuchMethodException e)
{
e.toString();
}
if (type.compareToIgnoreCase("short") == 0)
{
short value = ((Short)method.invoke(pointer, null)).shortValue();
if (-1!= value)
{
list.add(value);
continue;
}
}
else if (type.compareToIgnoreCase("int") == 0)
{
int value = ((Integer)method.invoke(pointer, null)).intValue();
if (-1!= value)
{
list.add(value);
continue;
}
}
else if (type.indexOf("java.lang.String") >= 0)
{
String value = (String)method.invoke(pointer, null);
if (null != value)
{
list.add(value);
}
}
}
}
反射案例
最新推荐文章于 2022-10-09 15:14:04 发布