Open Source Repository

Home /sojo/sojo-1.0.0 | Repository Home



net/sf/sojo/core/reflect/ReflectionFieldHelper.java
/*
 * Copyright 2002-2005 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.sojo.core.reflect;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

import net.sf.sojo.util.Util;

/**
 * Find all fields of the search classes (go upward in the class hierachy, how to the <code>java.lang.Object</code>).
 
 @author linke
 *
 */
public final class ReflectionFieldHelper {

  private static final ClassPropertiesCache classFieldCache = new ClassPropertiesCache();
  
  public static Field[] getAllFieldsByClass (Class pvClass) {
    Map lvFieldMap = getAllFieldMapsByClassIntern(pvClass, null);
    return (Field[]) lvFieldMap.values().toArray(new Field [lvFieldMap.size()]);
  }

  public static Map getAllGetFieldMapsByClass (Class pvClass, String pvFilter[]) {
    Map lvFieldMap = new TreeMap(getAllFieldMapsByClassIntern (pvClass, pvFilter));
    lvFieldMap.put(Util.getKeyWordClass(), pvClass.getName());
    return Collections.unmodifiableMap(lvFieldMap);
  }

  public static Map getAllSetFieldMapsByClass (Class pvClass, String pvFilter[]) {
    Map lvFieldMap = getAllFieldMapsByClassIntern (pvClass, pvFilter);
    return Collections.unmodifiableMap(lvFieldMap);
  }
  
  private static Map getAllFieldMapsByClassIntern (Class pvClass, String pvFilter[]) {
    Map lvFieldMap = classFieldCache.getClassPropertiesMapByClass(pvClass);
    if (lvFieldMap == null) {
      lvFieldMap = getAllFieldsByClassIntern(pvClass, new TreeMap());
    }
    lvFieldMap = Util.filterMapByKeys(lvFieldMap, pvFilter);
    return lvFieldMap;    
  }
  
  public static void addAllFields2MapByClass (Class pvClass, String pvFilter[]) {
    if (classFieldCache.containsClass(pvClass== false) {
      Map lvFieldMap = getAllSetFieldMapsByClass(pvClass, pvFilter);
      classFieldCache.addClassPropertiesMap(pvClass, lvFieldMap);
    
  }
  
  public static void removePropertiesByClass (Class pvClass) {
    classFieldCache.removePropertiesByClass(pvClass);
  }
  
  public static boolean containsClass(Class pvClass) {
    return classFieldCache.containsClass(pvClass);
  }
  

  /**
   * Recursive search alle method from the Class in the Class Hierarchy to Object.class.
   @param pvClass Search class.
   @param pvFieldsMap Method map (key=property name, value=method).
   @return All finded fields.
   */
  private static Map getAllFieldsByClassIntern (Class pvClass, Map pvFieldsMap) {
    putAllFieldsInternpvClass.getFields(), pvFieldsMap;    
    putAllFieldsInternpvClass.getDeclaredFields(), pvFieldsMap);
    
    if (!(pvClass.getSuperclass().equals(Object.class))) {
      getAllFieldsByClassIntern(pvClass.getSuperclass(), pvFieldsMap);
    }
    return pvFieldsMap;
  }
  
  private static void putAllFieldsIntern (Field pvAllFields[], Map pvFieldsMap) {
    for (int i = 0; i < pvAllFields.length; i++) {
      pvFieldsMap.put(pvAllFields[i].getName(), pvAllFields[i]);
    }    
  }

}