1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.sensoredabmigration.application;
import java.lang.reflect.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
*
*/
public class Converter<S, D> {
public Converter() {
}
/**
* Returns the getter method
* @param attribute
* @return
*/
private String getGetterMethod(final Field attribute) {
final String first = attribute.getName().substring(0, 1).toUpperCase();
final String next = attribute.getName().substring(1, attribute.getName().length()).toLowerCase();
return "get" + first + next;
}
/**
* Returns the getter method
* @param attribute
* @return
*/
private String getSetterMethod(final Field attribute) {
final String first = attribute.getName().substring(0, 1).toUpperCase();
final String next = attribute.getName().substring(1, attribute.getName().length()).toLowerCase();
return "set" + first + next;
}
public List<D> convert(final Class sourceClass, final Class destinationClass, final List<S> listOfSource) {
final List<D> listOfDestinations = new ArrayList<D>();
final Field[] attributesSource = sourceClass.getDeclaredFields();
try {
for (Iterator<S> it = listOfSource.iterator(); it.hasNext(); ) {
final S currentSource = it.next();
final Object currentDestination = destinationClass.newInstance();
for (int i = 0; i < attributesSource.length; i++) {
final Field currentAttribute = attributesSource[i];
final String getter = getGetterMethod(currentAttribute);
final String setter = getSetterMethod(currentAttribute);
try {
final Method getterMethod = sourceClass.getMethod(getter, null);
final S resultGetter = (S) getterMethod.invoke(currentSource, null);
Class[] parametres = new Class[1];
parametres[0] = (Class) currentAttribute.getGenericType();
final Method setterMethod = destinationClass.getMethod(setter, parametres);
final D resultSetter = (D) setterMethod.invoke(currentDestination, resultGetter);
} catch (NoSuchMethodException exception) {
continue;
}
}
listOfDestinations.add((D) currentDestination);
}
} catch (IllegalArgumentException ex) {
Logger.getLogger(Converter.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(Converter.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(Converter.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Converter.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Converter.class.getName()).log(Level.SEVERE, null, ex);
}
return listOfDestinations;
}
} |
Partager