📄 mappingprocessor.java
字号:
// Array to List
} else if (collectionUtils.isArray(sourceFieldType) && (collectionUtils.isList(destCollectionType))) {
result = mapArrayToList(sourceCollectionValue, classMap, fieldMap, destObj);
}
// List to Array
else if (collectionUtils.isList(sourceFieldType) && (collectionUtils.isArray(destCollectionType))) {
result = mapListToArray((List) sourceCollectionValue, classMap, fieldMap, destObj);
// List to List
} else if (collectionUtils.isList(sourceFieldType) && (collectionUtils.isList(destCollectionType))) {
result = mapListToList((List) sourceCollectionValue, classMap, fieldMap, destObj);
}
// Set to Set
else if (collectionUtils.isSet(sourceFieldType) && collectionUtils.isSet(destCollectionType)) {
result = mapSetToSet((Set) sourceCollectionValue, classMap, fieldMap, destObj, destCollectionType);
}
// Set to Array
else if (collectionUtils.isSet(sourceFieldType) && collectionUtils.isArray(destCollectionType)) {
result = mapSetToArray((Set) sourceCollectionValue, classMap, fieldMap, destObj);
}
// Array to Set
else if (collectionUtils.isArray(sourceFieldType) && collectionUtils.isSet(destCollectionType)) {
result = addToSet(fieldMap, Arrays.asList((Object[]) sourceCollectionValue), classMap, destObj);
}
// Set to List
else if (collectionUtils.isSet(sourceFieldType) && collectionUtils.isList(destCollectionType)) {
result = mapListToList((Set) sourceCollectionValue, classMap, fieldMap, destObj);
}
// List to Set
else if (collectionUtils.isList(sourceFieldType) && collectionUtils.isSet(destCollectionType)) {
result = addToSet(fieldMap, (List) sourceCollectionValue, classMap, destObj);
}
return result;
}
private Object mapMap(Object sourceMapValue, ClassMap classMap, FieldMap fieldMap, Object destObj, Class destFieldType)
throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
ClassNotFoundException, NoSuchFieldException {
Map result = null;
Object field = fieldMap.doesFieldExist(destObj, destFieldType);
if (field == null) {
// no destination map exists
result = (Map) sourceMapValue.getClass().newInstance();
} else {
result = (Map) field;
}
Map sourceMap = (Map) sourceMapValue;
Set sourceEntrySet = sourceMap.entrySet();
Iterator iter = sourceEntrySet.iterator();
while (iter.hasNext()) {
Map.Entry sourceEntry = (Map.Entry) iter.next();
Object sourceEntryValue = sourceEntry.getValue();
Object destEntryValue = mapOrRecurseObject(sourceEntryValue, sourceEntryValue.getClass(), classMap, fieldMap,
destObj);
result.put(sourceEntry.getKey(), destEntryValue);
}
return result;
}
private Object mapMapToProperty(Object sourceValue, Class sourceFieldClass, FieldMap fieldMap, Object destObj,
Class destType, ClassMap classMap) throws IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {
Object result = null;
String key = null;
// determine which field is the Map
if (mappingUtils.isSupportedMap(destType)) {
Object field = fieldMap.doesFieldExist(destObj, destType);
// make sure we don't null out the whole Map
if (sourceFieldClass == null) {
return field;
}
if (field == null) {
if (fieldMap.getDestinationTypeHint() != null) {
// if we have a hint we can instantiate correct object
destType = fieldMap.getDestinationTypeHint().getHint();
} else if (destType.isInterface()) {
// if there is no hint we assume it is a HashMap
destType = HashMap.class;
}
// destType could also be a concrete implementation
result = destType.newInstance();
} else {
result = field;
}
key = fieldMap.getDestKey();
((Map) result).put(key, sourceValue);
} else {
key = fieldMap.getSourceKey();
result = ((Map) sourceValue).get(key);
}
return mapOrRecurseObject(result, destType, classMap, fieldMap, destObj);
}
private Object mapCustomMapToProperty(Object sourceValue, Class sourceFieldClass, FieldMap fieldMap, Object destObj,
Class destType) throws IllegalAccessException, InstantiationException, InvocationTargetException,
NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {
Object result = null;
String key = null;
Object field = null;
// determine which field is the Map
if (fieldMap.getDestField().getMapGetMethod() != null && fieldMap.getDestField().getMapSetMethod() != null) {
field = fieldMap.doesFieldExist(destObj, destType);
// make sure we don't null out the whole Map
if (sourceFieldClass == null) {
return field;
}
if (field == null) {
if (fieldMap.getDestinationTypeHint() != null) {
// if we have a hint we can instantiate correct object
destType = fieldMap.getDestinationTypeHint().getHint();
}
// destType could also be a concrete implementation
result = destType.newInstance();
} else {
result = field;
}
key = fieldMap.getDestKey();
Method resultMethod = reflectionUtils.getMethod(result, fieldMap.getDestField().getMapSetMethod());
resultMethod.invoke(result, new Object[] { key, sourceValue });
} else {
key = fieldMap.getSourceKey();
Method resultMethod = reflectionUtils.getMethod(sourceValue, fieldMap.getSourceField().getMapGetMethod());
result = resultMethod.invoke(sourceValue, new Object[] { key });
}
return result;
}
private Object mapArrayToArray(Object sourceCollectionValue, ClassMap classMap, FieldMap fieldMap, Object destObj)
throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
ClassNotFoundException, NoSuchFieldException {
Class destEntryType = fieldMap.getDestFieldType(classMap.getDestClass().getClassToMap()).getComponentType();
int size = Array.getLength(sourceCollectionValue);
if (mappingUtils.isPrimitiveArray(sourceCollectionValue.getClass())) {
return addToPrimitiveArray(fieldMap, size, sourceCollectionValue, classMap, destObj, destEntryType);
} else {
List list = Arrays.asList((Object[]) sourceCollectionValue);
List returnList = null;
if (!destEntryType.getName().equals("java.lang.Object")) {
returnList = addOrUpdateToList(fieldMap, list, classMap, destObj, destEntryType);
} else {
returnList = addOrUpdateToList(fieldMap, list, classMap, destObj, null);
}
return collectionUtils.convertListToArray(returnList, destEntryType);
}
}
private Object addToPrimitiveArray(FieldMap fieldMap, int size, Object sourceCollectionValue, ClassMap classMap,
Object destObj, Class destEntryType) throws IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {
Object outArray = null;
Object field = fieldMap.doesFieldExist(destObj, destEntryType);
int arraySize = 0;
if (field == null) {
outArray = Array.newInstance(destEntryType, size);
} else {
outArray = Array.newInstance(destEntryType, size + Array.getLength(field));
arraySize = Array.getLength(field);
for (int i = 0; i < Array.getLength(field); i++) {
Array.set(outArray, i, Array.get(field, i));
}
}
// primitive arrays are ALWAYS cumulative
for (int i = 0; i < size; i++) {
Object toValue = mapOrRecurseObject(Array.get(sourceCollectionValue, i), destEntryType, classMap, fieldMap,
destObj);
Array.set(outArray, arraySize, toValue);
arraySize++;
}
return outArray;
}
private Object mapListToArray(Collection sourceCollectionValue, ClassMap classMap, FieldMap fieldMap, Object destObj)
throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
ClassNotFoundException, NoSuchFieldException {
List list = null;
Class destEntryType = fieldMap.getDestFieldType(classMap.getDestClass().getClassToMap()).getComponentType();
if (!destEntryType.getName().equals("java.lang.Object")) {
list = addOrUpdateToList(fieldMap, sourceCollectionValue, classMap, destObj, destEntryType);
} else {
list = addOrUpdateToList(fieldMap, sourceCollectionValue, classMap, destObj);
}
return collectionUtils.convertListToArray(list, destEntryType);
}
private List mapListToList(Collection sourceCollectionValue, ClassMap classMap, FieldMap fieldMap, Object destObj)
throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
ClassNotFoundException, NoSuchFieldException {
return addOrUpdateToList(fieldMap, sourceCollectionValue, classMap, destObj);
}
private Set addToSet(FieldMap fieldMap, Collection sourceCollectionValue, ClassMap classMap, Object destObj)
throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
ClassNotFoundException, NoSuchFieldException {
Class destEntryType = null;
ListOrderedSet result = new ListOrderedSet();
// don't want to create the set if it already exists.
Object field = fieldMap.doesFieldExist(destObj, null);
if (field != null) {
result.addAll((Collection) field);
}
Object destValue = null;
Iterator iter = sourceCollectionValue.iterator();
Object sourceValue = null;
while (iter.hasNext()) {
sourceValue = iter.next();
if (destEntryType == null
|| (fieldMap.getDestinationTypeHint() != null && fieldMap.getDestinationTypeHint().hasMoreThanOneHint())) {
destEntryType = fieldMap.getDestHintType(sourceValue.getClass());
}
destValue = mapOrRecurseObject(sourceValue, destEntryType, classMap, fieldMap, destObj);
if (fieldMap.isGenericFieldMap()) {
GenericFieldMap gfm = (GenericFieldMap) fieldMap;
if (gfm.getRelationshipType() == null
|| gfm.getRelationshipType().equals(MapperConstants.RELATIONSHIP_CUMULATIVE)) {
result.add(destValue);
} else {
if (result.contains(destValue)) {
int index = result.indexOf(destValue);
// perform an update if complex type - can't map strings
Object obj = result.get(index);
// make sure it is not a String
if (!obj.getClass().isAssignableFrom(String.class)) {
map(null, obj, destValue, null, fieldMap);
}
result.add(destValue);
} else {
result.add(destValue);
}
}
} else {
result.add(destValue);
}
}
if (field == null) {
Class destSetType = fieldMap.getDestFieldType(destObj.getClass());
return collectionUtils.createNewSet(destSetType, result);
} else {
((Set) field).addAll(result);
return (Set) field;
}
}
private List addOrUpdateToList(FieldMap fieldMap, Collection sourceCollectionValue, ClassMap classMap,
Object destObj, Class destEntryType) throws IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {
List result = null;
// don't want to create the list if it already exists.
// these maps are special cases which do not fall under what we are
// looking for
Object field = fieldMap.doesFieldExist(destObj, destEntryType);
if (field == null) {
result = new ArrayList(sourceCollectionValue.size());
} else {
if (collectionUtils.isList(field.getClass())) {
result = (List) field;
} else if (collectionUtils.isArray(field.getClass())) {// must be array
result = new ArrayList(Arrays.asList((Object[]) field));
} else { // assume it is neither - safest way is to create new
// List
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -