Some more reflection util content.

This commit is contained in:
Olof Larsson 2015-01-05 13:29:22 +01:00
parent 160d908a5b
commit 47086252b0

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore.util;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class ReflectionUtil
{
@ -32,4 +34,45 @@ public class ReflectionUtil
return false;
}
}
public static boolean transferField(Class<?> clazz, Object from, Object to, String fieldName)
{
try
{
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
Object value = field.get(from);
field.set(to, value);
return true;
}
catch (Exception e)
{
return false;
}
}
public static boolean transferFields(Class<?> clazz, Object from, Object to, List<String> fieldNames)
{
if (fieldNames == null)
{
fieldNames = new ArrayList<String>();
for (Field field : clazz.getDeclaredFields())
{
fieldNames.add(field.getName());
}
}
for (String fieldName : fieldNames)
{
if ( ! transferField(clazz, from, to, fieldName)) return false;
}
return true;
}
public static boolean transferFields(Class<?> clazz, Object from, Object to)
{
return transferFields(clazz, from, to, null);
}
}