I have a class which is basically a copy of another class.
public class A {
int a;
String b;
}
public class CopyA {
int a;
String b;
}
What I am doing is putting values from class A
into CopyA
before sending CopyA
through a webservice call. Now I would like to create a reflection-method that basically copies all fields that are identical (by name and type) from class A
to class CopyA
.
How can I do this?
This is what I have so far, but it doesn't quite work. I think the problem here is that I am trying to set a field on the field I am looping through.
private <T extends Object, Y extends Object> void copyFields(T from, Y too) {
Class<? extends Object> fromClass = from.getClass();
Field[] fromFields = fromClass.getDeclaredFields();
Class<? extends Object> tooClass = too.getClass();
Field[] tooFields = tooClass.getDeclaredFields();
if (fromFields != null && tooFields != null) {
for (Field tooF : tooFields) {
logger.debug("toofield name #0 and type #1", tooF.getName(), tooF.getType().toString());
try {
// Check if that fields exists in the other method
Field fromF = fromClass.g开发者_如何学PythonetDeclaredField(tooF.getName());
if (fromF.getType().equals(tooF.getType())) {
tooF.set(tooF, fromF);
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I am sure there must be someone that has already done this somehow
If you don't mind using a third party library, BeanUtils from Apache Commons will handle this quite easily, using copyProperties(Object, Object)
.
Why don't you use gson library https://github.com/google/gson
you just convert the Class A to json string. Then convert jsonString to you subClass (CopyA) .using below code:
Gson gson= new Gson();
String tmp = gson.toJson(a);
CopyA myObject = gson.fromJson(tmp,CopyA.class);
BeanUtils will only copy public fields and is a bit slow. Instead go with getter and setter methods.
public Object loadData (RideHotelsService object_a) throws Exception{
Method[] gettersAndSetters = object_a.getClass().getMethods();
for (int i = 0; i < gettersAndSetters.length; i++) {
String methodName = gettersAndSetters[i].getName();
try{
if(methodName.startsWith("get")){
this.getClass().getMethod(methodName.replaceFirst("get", "set") , gettersAndSetters[i].getReturnType() ).invoke(this, gettersAndSetters[i].invoke(object_a, null));
}else if(methodName.startsWith("is") ){
this.getClass().getMethod(methodName.replaceFirst("is", "set") , gettersAndSetters[i].getReturnType() ).invoke(this, gettersAndSetters[i].invoke(object_a, null));
}
}catch (NoSuchMethodException e) {
// TODO: handle exception
}catch (IllegalArgumentException e) {
// TODO: handle exception
}
}
return null;
}
Here is a working and tested solution. You can control the depth of the mapping in the class hierarchy.
public class FieldMapper {
public static void copy(Object from, Object to) throws Exception {
FieldMapper.copy(from, to, Object.class);
}
public static void copy(Object from, Object to, Class depth) throws Exception {
Class fromClass = from.getClass();
Class toClass = to.getClass();
List<Field> fromFields = collectFields(fromClass, depth);
List<Field> toFields = collectFields(toClass, depth);
Field target;
for (Field source : fromFields) {
if ((target = findAndRemove(source, toFields)) != null) {
target.set(to, source.get(from));
}
}
}
private static List<Field> collectFields(Class c, Class depth) {
List<Field> accessibleFields = new ArrayList<>();
do {
int modifiers;
for (Field field : c.getDeclaredFields()) {
modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) {
accessibleFields.add(field);
}
}
c = c.getSuperclass();
} while (c != null && c != depth);
return accessibleFields;
}
private static Field findAndRemove(Field field, List<Field> fields) {
Field actual;
for (Iterator<Field> i = fields.iterator(); i.hasNext();) {
actual = i.next();
if (field.getName().equals(actual.getName())
&& field.getType().equals(actual.getType())) {
i.remove();
return actual;
}
}
return null;
}
}
Spring has a built in BeanUtils.copyProperties
method. But it doesn't work with classes without getters/setters. JSON serialization/deserialization can be another option for copying fields. Jackson can be used for this purpose. If you are using Spring In most cases Jackson is already in your dependency list.
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Clazz copyObject = mapper.readValue(mapper.writeValueAsString(sourceObject), Clazz.class);
My solution:
public static <T > void copyAllFields(T to, T from) {
Class<T> clazz = (Class<T>) from.getClass();
// OR:
// Class<T> clazz = (Class<T>) to.getClass();
List<Field> fields = getAllModelFields(clazz);
if (fields != null) {
for (Field field : fields) {
try {
field.setAccessible(true);
field.set(to,field.get(from));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
public static List<Field> getAllModelFields(Class aClass) {
List<Field> fields = new ArrayList<>();
do {
Collections.addAll(fields, aClass.getDeclaredFields());
aClass = aClass.getSuperclass();
} while (aClass != null);
return fields;
}
This is a late post, but can still be effective for people in future.
Spring provides a utility BeanUtils.copyProperties(srcObj, tarObj)
which copies values from source object to target object when the names of the member variables of both classes are the same.
If there is a date conversion, (eg, String to Date) 'null' would be copied to the target object. We can then, explicitly set the values of the date as required.
The BeanUtils from Apache Common
throws an error when there is a mismatch of data-types (esp. conversion to and from Date)
Hope this helps!
The first argument to tooF.set()
should be the target object (too
), not the field, and the second argument should be the value, not the field the value comes from. (To get the value, you need to call fromF.get()
-- again passing in a target object, in this case from
.)
Most of the reflection API works this way. You get Field
objects, Method
objects, and so on from the class, not from an instance, so to use them (except for statics) you generally need to pass them an instance.
Dozer
UPDATE Nov 19 2012: There's now a new ModelMapper project too.
I think you can try dozer. It has good support for bean to bean conversion. Its also easy to use. You can either inject it into your spring application or add the jar in class path and its done.
For an example of your case :
DozerMapper mapper = new DozerMapper();
A a= new A();
CopyA copyA = new CopyA();
a.set... // set fields of a.
mapper.map(a,copyOfA); // will copy all fields from a to copyA
Without using BeanUtils or Apache Commons
public static <T1 extends Object, T2 extends Object> void copy(T1 origEntity, T2 destEntity) throws IllegalAccessException, NoSuchFieldException { Field[] fields = origEntity.getClass().getDeclaredFields(); for (Field field : fields){ origFields.set(destEntity, field.get(origEntity)); } }
Orika's is simple faster bean mapping framework because it does through byte code generation. It does nested mappings and mappings with different names. For more details, please check here Sample mapping may look complex, but for complex scenarios it would be simple.
MapperFactory factory = new DefaultMapperFactory.Builder().build();
mapperFactory.registerClassMap(mapperFactory.classMap(Book.class,BookDto.class).byDefault().toClassMap());
MapperFacade mapper = factory.getMapperFacade();
BookDto bookDto = mapperFacade.map(book, BookDto.class);
If you have spring in the dependencies you can also use org.springframework.beans.BeanUtils.
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/BeanUtils.html
I solved the above problem in Kotlin that works fine for me for my Android Apps Development:
object FieldMapper {
fun <T:Any> copy(to: T, from: T) {
try {
val fromClass = from.javaClass
val fromFields = getAllFields(fromClass)
fromFields?.let {
for (field in fromFields) {
try {
field.isAccessible = true
field.set(to, field.get(from))
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun getAllFields(paramClass: Class<*>): List<Field> {
var theClass:Class<*>? = paramClass
val fields = ArrayList<Field>()
try {
while (theClass != null) {
Collections.addAll(fields, *theClass?.declaredFields)
theClass = theClass?.superclass
}
}catch (e:Exception){
e.printStackTrace()
}
return fields
}
}
public static <T> void copyAvalableFields(@NotNull T source, @NotNull T target) throws IllegalAccessException {
Field[] fields = source.getClass().getDeclaredFields();
for (Field field : fields) {
if (!Modifier.isStatic(field.getModifiers())
&& !Modifier.isFinal(field.getModifiers())) {
field.set(target, field.get(source));
}
}
}
We read all the fields of the class. Filter non-static and non-final fields from the result. But there may be an error accessing non-public fields. For example, if this function is in the same class, and the class being copied does not contain public fields, an access error will occur. The solution may be to place this function in the same package or change access to public or in this code inside the loop call field.setAccessible (true); what will make the fields available
I didn't want to add dependency to another JAR file because of this, so wrote something which would suit my needs. I follow the convention of fjorm https://code.google.com/p/fjorm/ which means that my generally accessible fields are public and that I don't bother to write setters and getters. (in my opinion code is easier to manage and more readable actually)
So I wrote something (it's not actually much difficult) which suits my needs (assumes that the class has public constructor without args) and it could be extracted into utility class
public Effect copyUsingReflection() {
Constructor constructorToUse = null;
for (Constructor constructor : this.getClass().getConstructors()) {
if (constructor.getParameterTypes().length == 0) {
constructorToUse = constructor;
constructorToUse.setAccessible(true);
}
}
if (constructorToUse != null) {
try {
Effect copyOfEffect = (Effect) constructorToUse.newInstance();
for (Field field : this.getClass().getFields()) {
try {
Object valueToCopy = field.get(this);
//if it has field of the same type (Effect in this case), call the method to copy it recursively
if (valueToCopy instanceof Effect) {
valueToCopy = ((Effect) valueToCopy).copyUsingReflection();
}
//TODO add here other special types of fields, like Maps, Lists, etc.
field.set(copyOfEffect, valueToCopy);
} catch (IllegalArgumentException | IllegalAccessException ex) {
Logger.getLogger(Effect.class.getName()).log(Level.SEVERE, null, ex);
}
}
return copyOfEffect;
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(Effect.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}
Mladen's basic idea worked (thanks), but needed a few changes to be robust, so I contributed them here.
The only place where this type of solution should be used is if you want to clone the object, but can't because it is a managed object. If you are lucky enough to have objects that all have 100% side-effect free setters for all fields, you should definitely use the BeanUtils option instead.
Here, I use lang3's utility methods to simplify the code, so if you paste it, you must first import Apache's lang3 library.
Copy code
static public <X extends Object> X copy(X object, String... skipFields) {
Constructor constructorToUse = null;
for (Constructor constructor : object.getClass().getConstructors()) {
if (constructor.getParameterTypes().length == 0) {
constructorToUse = constructor;
constructorToUse.setAccessible(true);
break;
}
}
if (constructorToUse == null) {
throw new IllegalStateException(object + " must have a zero arg constructor in order to be copied");
}
X copy;
try {
copy = (X) constructorToUse.newInstance();
for (Field field : FieldUtils.getAllFields(object.getClass())) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
//Avoid the fields that you don't want to copy. Note, if you pass in "id", it will skip any field with "id" in it. So be careful.
if (StringUtils.containsAny(field.getName(), skipFields)) {
continue;
}
field.setAccessible(true);
Object valueToCopy = field.get(object);
//TODO add here other special types of fields, like Maps, Lists, etc.
field.set(copy, valueToCopy);
}
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
throw new IllegalStateException("Could not copy " + object, e);
}
return copy;
}
public <T1 extends Object, T2 extends Object> void copy(T1 origEntity, T2 destEntity) {
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.map(origEntity,destEntity);
}
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.4.0</version>
</dependency>
Here is my solution, will cover the child class case:
/**
* This methods transfer the attributes from one class to another class if it
* has null values.
*
* @param fromClass from class
* @param toClass to class
*/
private void loadProperties(Object fromClass, Object toClass) {
if (Objects.isNull(fromClass) || Objects.isNull(toClass))
return;
Field[] fields = toClass.getClass().getDeclaredFields();
Field[] fieldsSuperClass = toClass.getClass().getSuperclass().getDeclaredFields();
Field[] fieldsFinal = new Field[fields.length + fieldsSuperClass.length];
Arrays.setAll(fieldsFinal, i -> (i < fields.length ? fields[i] : fieldsSuperClass[i - fields.length]));
for (Field field : fieldsFinal) {
field.setAccessible(true);
try {
String propertyKey = field.getName();
if (field.get(toClass) == null) {
Field defaultPropertyField = fromClass.getClass().getDeclaredField(propertyKey);
defaultPropertyField.setAccessible(true);
Object propertyValue = defaultPropertyField.get(fromClass);
if (propertyValue != null)
field.set(toClass, propertyValue);
}
} catch (IllegalAccessException e) {
logger.error(() -> "Error while loading properties from " + fromClass.getClass() +" and to " +toClass.getClass(), e);
} catch (NoSuchFieldException e) {
logger.error(() -> "Exception occurred while loading properties from " + fromClass.getClass()+" and to " +toClass.getClass(), e);
}
}
}
精彩评论