?? reflectionutils.java
字號(hào):
/*
* 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.util;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.StringTokenizer;
import net.sf.dozer.util.mapping.MappingException;
import org.apache.commons.beanutils.PropertyUtils;
/**
* @author tierney.matt
* @author garsombke.franz
*/
public class ReflectionUtils {
public PropertyDescriptor getPropertyDescriptor(Class objectClass, String fieldName) {
PropertyDescriptor result = null;
if (fieldName.indexOf(MapperConstants.DEEP_FIELD_DELIMITOR) >= 0) {
PropertyDescriptor[] hierarchy = getDeepFieldHierarchy(objectClass, fieldName);
result = hierarchy[hierarchy.length - 1];
} else {
PropertyDescriptor[] descriptors = getPropertyDescriptors(objectClass);
if (descriptors != null) {
for (int i = 0; i < descriptors.length; i++) {
if (fieldName.equalsIgnoreCase(descriptors[i].getName())) {
result = descriptors[i];
break;
}
}
}
}
return result;
}
public PropertyDescriptor[] getDeepFieldHierarchy(Class parentClass, String field) {
if (field.indexOf(MapperConstants.DEEP_FIELD_DELIMITOR) < 0) {
throw new MappingException("Field does not contain deep field delimitor");
}
StringTokenizer toks = new StringTokenizer(field, MapperConstants.DEEP_FIELD_DELIMITOR);
Class latestClass = parentClass;
PropertyDescriptor[] hierarchy = new PropertyDescriptor[toks.countTokens()];
int index = 0;
while (toks.hasMoreTokens()) {
String aFieldName = toks.nextToken();
PropertyDescriptor propDescriptor = getPropertyDescriptor(latestClass, aFieldName);
if (propDescriptor == null) {
throw new MappingException("Exception occurred determining deep field hierarchy for Class --> "
+ parentClass.getName() + ", Field --> " + field
+ ". Unable to determine property descriptor for Class --> " + latestClass.getName() + ", Field Name: "
+ aFieldName);
}
latestClass = propDescriptor.getPropertyType();
hierarchy[index++] = propDescriptor;
}
return hierarchy;
}
public Method getMethod(Object field, String methodName) {
Method[] methods = field.getClass().getMethods();
Method resultMethod = null;
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.getName().equals(methodName)) {
resultMethod = method;
}
}
if (resultMethod == null) {
throw new MappingException("No method found for class:" + field.getClass() + " and method name:" + methodName);
}
return resultMethod;
}
protected PropertyDescriptor[] getPropertyDescriptors(Class objectClass) {
return PropertyUtils.getPropertyDescriptors(objectClass);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -