?? bruteforcepropertydescriptor.java
字號:
/*
* Copyright 2005-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sf.dozer.util.mapping.propertydescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import net.sf.dozer.util.mapping.MappingException;
import net.sf.dozer.util.mapping.fieldmap.ClassMap;
import net.sf.dozer.util.mapping.fieldmap.Field;
import net.sf.dozer.util.mapping.fieldmap.Hint;
import net.sf.dozer.util.mapping.util.CollectionUtils;
import net.sf.dozer.util.mapping.util.DestBeanCreator;
import net.sf.dozer.util.mapping.util.MapperConstants;
import net.sf.dozer.util.mapping.util.MappingUtils;
import net.sf.dozer.util.mapping.util.ReflectionUtils;
import org.apache.log4j.Logger;
/**
* @author garsombke.franz
*/
public class BruteForcePropertyDescriptor implements DozerPropertyDescriptorIF {
private static final Logger log = Logger.getLogger(BruteForcePropertyDescriptor.class);
private Method readMethod;
private Method writeMethod;
private PropertyDescriptor[] hierarchy;
private final Class clazz;
private final Field field;
private Class returnType;
private final ReflectionUtils reflectionUtils = new ReflectionUtils();
private final CollectionUtils collectionUtils = new CollectionUtils();
private final DestBeanCreator destBeanCreator = new DestBeanCreator(MappingUtils.storedFactories);//only temp use the public static factories. The factories data needs to be relocated to a better place
public BruteForcePropertyDescriptor(Class clazz, Field field) {
this.field = field;
this.clazz = clazz;
}
public Class getPropertyType() {
if (returnType == null) {
try {
returnType = getReadMethod().getReturnType();
} catch (Exception e) {
log.debug("Read method exception:" + e);
// let us try the set method - the field might not have a 'get' method
returnType = getWriteMethod().getParameterTypes()[0];
}
}
return returnType;
}
public void setPropertyValue(Object bean, Object value, Hint hint, ClassMap classMap) {
try {
if (field.getName().indexOf(MapperConstants.DEEP_FIELD_DELIMITOR) < 0) {
if (getPropertyType().isPrimitive() && value == null) {
// do nothing
} else {
//Check if dest value is already set and is equal to src value. If true, no need to rewrite the dest value
try {
if (getPropertyValue(bean) == value) {
return;
}
} catch (Exception e) {
//if we failed to read the value, assume we must write, and continue...
}
if (!field.isIndexed()) {
getWriteMethod().invoke(bean, new Object[] { value });
} else {
writeIndexedValue(null, bean, value);
}
}
} else {
writeDeepDestinationValue(bean, value, hint, classMap);
}
} catch (Exception e) {
throw new MappingException(e);
}
}
public Object getPropertyValue(Object bean) {
Object o = null;
try {
if (field.getName().indexOf(MapperConstants.DEEP_FIELD_DELIMITOR) < 0) {
o = getReadMethod(clazz).invoke(bean, null);
if (field.isIndexed()) {
return getValueOfIndexedField(o, field.getIndex());
} else {
return o;
}
} else {
return getDeepSrcFieldValue(bean);
}
} catch (Exception e) {
throw new MappingException(e);
}
}
protected Method findAMethod(Class parentDestClass, String methodName) throws NoSuchMethodException,
ClassNotFoundException {
// TODO USE HELPER from bean utils to find method w/ params
StringTokenizer tokenizer = new StringTokenizer(methodName, "(");
String m = tokenizer.nextToken();
// If tokenizer has more elements, it mean that parameters may have been specified
if (tokenizer.hasMoreElements()) {
StringTokenizer tokens = new StringTokenizer(tokenizer.nextToken(), ")");
String params = (tokens.hasMoreTokens() ? tokens.nextToken() : null);
return findMethodWithParam(parentDestClass, m, params);
}
Method[] methods = parentDestClass.getMethods();
Method result = null;
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.getName().equals(methodName)) {
// Return the first method find
return method;
}
}
return result;
}
private Method findMethodWithParam(Class parentDestClass, String methodName, String params)
throws NoSuchMethodException, ClassNotFoundException {
// TODO USE HELPER from bean utils to find method w/ params
List list = new ArrayList();
if (params != null) {
StringTokenizer tokenizer = new StringTokenizer(params, ",");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
list.add(Class.forName(token));
}
}
return parentDestClass.getMethod(methodName, (Class[]) list.toArray(new Class[0]));
}
protected Object getDeepSrcFieldValue(Object srcObj) throws InvocationTargetException, IllegalAccessException {
// follow deep field hierarchy. If any values are null along the way, then return null
Object parentObj = srcObj;
Object hierarchyValue = null;
PropertyDescriptor[] hierarchy = getHierarchy(srcObj);
for (int i = 0; i < hierarchy.length; i++) {
PropertyDescriptor pd = hierarchy[i];
hierarchyValue = pd.getReadMethod().invoke(parentObj, null);
parentObj = hierarchyValue;
if (hierarchyValue == null) {
break;
}
}
return hierarchyValue;
}
protected Method getReadMethod(Class objectClass, String fieldName) {
PropertyDescriptor pd = reflectionUtils.getPropertyDescriptor(objectClass, fieldName);
if ((pd == null || pd.getReadMethod() == null)) {
throw new MappingException("Unable to determine read method for field: " + fieldName + " class: " + objectClass);
}
return pd.getReadMethod();
}
protected Method getWriteMethod(Class objectClass, String fieldName) {
PropertyDescriptor pd = reflectionUtils.getPropertyDescriptor(objectClass, fieldName);
if ((pd == null || pd.getWriteMethod() == null)) {
throw new MappingException("Unable to determine write method for field: " + fieldName + " class: " + objectClass);
}
return pd.getWriteMethod();
}
protected Object getDeepDestinationValue(Object destObj) throws IllegalAccessException, InvocationTargetException,
InstantiationException {
// 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;
for (int i = 0; i < hierarchy.length - 1; i++) {
PropertyDescriptor pd = hierarchy[i];
Object value = pd.getReadMethod().invoke(parentObj, null);
if (value == null) {
pd.getWriteMethod().invoke(parentObj, new Object[] { pd.getPropertyType().newInstance() });
value = pd.getReadMethod().invoke(parentObj, null);
}
parentObj = value;
}
return parentObj;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -