⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reflectionutils.java

📁 一个javabean的转换与copy非常的好用希望大家好好研究一下
💻 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.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);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -