?? bruteforcepropertydescriptor.java
字號:
protected void writeDeepDestinationValue(Object destObj, Object destFieldValue, Hint destHint, ClassMap classMap)
throws IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException,
NoSuchMethodException, NoSuchFieldException {
// follow deep field hierarchy. If any values are null along the way, then create a new instance
PropertyDescriptor[] hierarchy = getHierarchy(destObj);
// first, iteratate through hierarchy and instantiate any objects that are null
Object parentObj = destObj;
int hierarchyLength = hierarchy.length - 1;
for (int i = 0; i < hierarchyLength; i++) {
PropertyDescriptor pd = hierarchy[i];
Object value = pd.getReadMethod().invoke(parentObj, null);
Class clazz = null;
if (value == null) {
clazz = pd.getPropertyType();
if (clazz.isInterface()) {
// before setting the property on the destination object we should check for a destination hint. need to know
// that we are at the end of the line
if ((i + 1) == hierarchyLength) {
// determine the property type
if (destHint != null) {
clazz = destHint.getHint();
}
}
}
Object o;
try {
o = clazz.newInstance();
pd.getWriteMethod().invoke(parentObj, new Object[] { o });
} catch (InstantiationException e) {
// lets see if they have a factory we can try. If not...throw the exception:
if (classMap.getDestClass().getBeanFactory() != null) {
o = destBeanCreator.createFromFactory(null, classMap.getSourceClass().getClassToMap(), classMap
.getDestClass().getBeanFactory(), classMap.getDestClass().getFactoryBeanId(), clazz);
pd.getWriteMethod().invoke(parentObj, new Object[] { o });
} else {
throw e;
}
}
value = pd.getReadMethod().invoke(parentObj, null);
}
parentObj = value;
}
// second, set deep field value
PropertyDescriptor pd = hierarchy[hierarchy.length - 1];
if (pd.getReadMethod().getReturnType().isPrimitive() && destFieldValue == null) {
} else {
if (!field.isIndexed()) {
Method method = pd.getWriteMethod();
if(method == null && field.getTheSetMethod() != null) {
// lets see if we can find a custom method
method = findAMethod(parentObj.getClass(), field.getTheSetMethod());
}
method.invoke(parentObj, new Object[] { destFieldValue });
} else {
writeIndexedValue(pd, parentObj, destFieldValue);
}
}
}
protected PropertyDescriptor[] getHierarchy(Object obj) {
if (hierarchy == null) {
hierarchy = reflectionUtils.getDeepFieldHierarchy(obj.getClass(), field.getName());
}
return hierarchy;
}
public String getReadMethodName() {
if (readMethod != null) {
return readMethod.getName();
} else {
try {
readMethod = getReadMethod(clazz);
} catch (MappingException e) {
log.debug("Can't find read method for class:" + clazz + " and field:" + field.getName());
return "NoReadMethodFound";
}
return readMethod.getName();
}
}
public String getWriteMethodName() {
if (writeMethod != null) {
return writeMethod.getName();
} else {
writeMethod = getWriteMethod();
return writeMethod.getName();
}
}
public Method getReadMethod() {
return getReadMethod(clazz);
}
protected Method getReadMethod(Class bean) {
if (readMethod == null || !readMethod.getDeclaringClass().isAssignableFrom(bean)) {
if (field.getTheGetMethod() == null) {
readMethod = getReadMethod(clazz, field.getName());
} else {
try {
readMethod = findAMethod(clazz, field.getTheGetMethod());
} catch (Exception e) {
throw new MappingException(e);
}
}
}
return readMethod;
}
public Method getWriteMethod() {
if (writeMethod == null || !writeMethod.getDeclaringClass().isAssignableFrom(clazz)) {
if (field.getTheSetMethod() == null || field.getName().indexOf(MapperConstants.DEEP_FIELD_DELIMITOR) > 0) {
writeMethod = getWriteMethod(clazz, field.getName());
} else {
try {
writeMethod = findAMethod(clazz, field.getTheSetMethod());
} catch (Exception e) {
throw new MappingException(e);
}
}
}
return writeMethod;
}
protected Object getValueOfIndexedField(Object object, int index) {
if (object instanceof Collection) {
Collection c = (Collection) object;
if ((c != null) && (index > -1) && (index < c.size())) {
Iterator iter = c.iterator();
for (int x = 0; x < index; x++) {
iter.next();
}
return iter.next();
} else {
return null;
}
} else if (object instanceof Object[]) {
Object[] objs = (Object[]) object;
if ((objs != null) && (index > -1) && (index < objs.length)) {
return objs[index];
} else {
return null;
}
} else {
return null;
}
}
protected void writeIndexedValue(PropertyDescriptor pd, Object destObj, Object destFieldValue)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException,
NoSuchFieldException {
int index = field.getIndex();
Object o = getReadMethod().invoke(destObj, null);
if (o == null) {
Class returnType = getPropertyType();
if (returnType.isArray()) {
o = Array.newInstance(returnType.getComponentType(), 1);
} else if (collectionUtils.isSet(returnType)) {
o = new HashSet();
} else { // default
o = new ArrayList();
}
}
if (o instanceof Collection) {
Collection newCollection;
if (o instanceof Set) {
newCollection = new HashSet();
} else {
newCollection = new ArrayList();
}
Collection c = (Collection) o;
Iterator i = c.iterator();
int x = 0;
while (i.hasNext()) {
if (x != index) {
newCollection.add(i.next());
} else {
newCollection.add(destFieldValue);
}
x++;
}
if (newCollection.size() <= index) {
while (newCollection.size() < index) {
newCollection.add(null);
}
newCollection.add(destFieldValue);
}
getWriteMethod().invoke(destObj, new Object[] { newCollection });
} else if (o.getClass().isArray()) {
Object[] objs = (Object[]) o;
Object[] x = (Object[]) Array.newInstance(objs.getClass().getComponentType(),
objs.length > index ? objs.length + 1 : index + 1);
for (int i = 0; i < objs.length; i++) {
x[i] = objs[i];
}
x[index] = destFieldValue;
if (pd == null) {
getWriteMethod().invoke(destObj, new Object[] { x });
} else {
pd.getWriteMethod().invoke(destObj, new Object[] { x });
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -