Example usage for java.lang Class getDeclaredFields

List of usage examples for java.lang Class getDeclaredFields

Introduction

In this page you can find the example usage for java.lang Class getDeclaredFields.

Prototype

@CallerSensitive
public Field[] getDeclaredFields() throws SecurityException 

Source Link

Document

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

Usage

From source file:com.sm.query.utils.QueryUtils.java

/**
 * @param object//from   w w  w  .j  a  v a2s .c o  m
 * @param metaData  - hashmap of ClassInfo
 * @return ClassInfo which represent
 * use getSimpleName() instead of getName()
 */
public static ClassInfo findClassInfo(Object object, Map<String, ClassInfo> metaData) {
    String className = object.getClass().getName();
    //getSimpleName without package name space
    String keyName = object.getClass().getSimpleName();
    ClassInfo info = metaData.get(keyName);
    if (info == null) {
        Type type = getType(className);
        Object obj;
        Collection values;
        switch (type) {
        case ARRAY:
            obj = Array.get(object, 0);
            //info = findClassInfo(object, cacheMetaData);
            assert (obj != null);
            info = findClassInfo(obj, metaData);
            break;
        case MAP:
            values = ((Map) object).values();
            obj = values.iterator().next();
            assert (obj != null);
            info = findClassInfo(obj, metaData);
            break;
        case HASHSET:
        case LIST:
            Iterator v = ((Collection) object).iterator();
            obj = v.next();
            info = findClassInfo(obj, metaData);
            break;
        case OBJECT:
            List<FieldInfo> list = new ArrayList<FieldInfo>();
            Class<?> cls = object.getClass();
            while (cls != null && cls != Object.class) {
                Field[] fields = cls.getDeclaredFields();
                for (Field field : fields) {
                    if (!isSkipField(field)) {
                        field.setAccessible(true);
                        String fieldClsName = field.getType().getName();
                        Type fieldType = getType(fieldClsName);
                        list.add(new FieldInfo(field, fieldType, fieldClsName));
                    }
                }
                cls = cls.getSuperclass();
            }
            if (list.size() > 0) {
                FieldInfo[] fieldArray = new FieldInfo[list.size()];
                fieldArray = list.toArray(fieldArray);
                info = new ClassInfo(className, type, fieldArray);
            }
            break;
        default:
            logger.error("Wrong type =" + type + " in createClassInfo object className " + className
                    + " simple name " + keyName);
        } // switch

        metaData.put(keyName, info);
    }
    return info;

}

From source file:com.zlfun.framework.excel.ExcelUtils.java

private static <T> T fill(Map<String, String> map, T t) {
    T bean = t;// w w  w  . ja  va 2s  . co  m
    try {

        if (t.getClass().getAnnotation(WebParam.class) == null) {

            Field[] fs = t.getClass().getDeclaredFields();
            for (Field f : fs) {

                set(bean, f, map.get(f.getName()));

            }
        } else {
            Field[] fs = t.getClass().getDeclaredFields();
            for (Field f : fs) {
                WebParam param = f.getAnnotation(WebParam.class);
                if (param != null) {
                    String fname;
                    if ("".equals(param.value())) {
                        fname = f.getName();
                    } else {
                        fname = param.value();
                    }

                    set(bean, f, map.get(fname));
                }

            }

            // ?
            Class<?> parent = t.getClass().getSuperclass();
            while (parent != Object.class) {

                if (parent.getAnnotation(ItemField.class) != null) {

                    Field[] pfs = parent.getDeclaredFields();
                    for (Field f : pfs) {
                        WebParam param = f.getAnnotation(WebParam.class);
                        if (param != null) {
                            String fname;
                            if ("".equals(param.value())) {
                                fname = f.getName();
                            } else {
                                fname = param.value();
                            }

                            set(bean, f, map.get(fname));
                        }

                    }

                    parent = parent.getSuperclass();
                } else {
                    break;
                }

            }

        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bean;
}

From source file:org.opentides.util.CrudUtil.java

/**
 * Helper method to retrieve all fields of a class including
 * fields declared in its superclass./* w ww.  j a v  a 2s.co  m*/
 * @param clazz
 * @param includeParent
 * @return
 */
@SuppressWarnings({ "rawtypes" })
public static List<Field> getAllFields(Class clazz, boolean includeParent) {
    List<Field> fields = new ArrayList<Field>();
    if (BaseEntity.class.isAssignableFrom(clazz) && includeParent)
        fields.addAll(getAllFields(clazz.getSuperclass(), includeParent));
    for (Field field : clazz.getDeclaredFields())
        fields.add(field);
    return fields;
}

From source file:de.javakaffee.web.msm.integration.TestUtils.java

public static void assertEqualDeclaredFields(final Class<? extends Object> clazz, final Object one,
        final Object another, final Map<Object, Object> alreadyChecked) {
    for (final Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);/*from w  w w. j  a  v a2  s.  c o m*/
        if (!Modifier.isTransient(field.getModifiers())) {
            try {
                assertDeepEquals(field.get(one), field.get(another), alreadyChecked);
            } catch (final IllegalArgumentException e) {
                throw new RuntimeException(e);
            } catch (final IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

From source file:jp.co.golorp.emarf.model.Models.java

/**
 * ??????????/* ww w  .  j a v a  2s . c  o m*/
 *
 * @param <T>
 *            
 * @param bean
 *            ??bean
 */
private static <T> void cp2org(final T bean) {
    Class<?> clazz = bean.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (field.getName().startsWith(BeanGenerator.ORG_PREFIX)) {
            try {
                String orgName = field.getName();
                String propertyName = orgName.replaceFirst(BeanGenerator.ORG_PREFIX, "");
                Field property = clazz.getDeclaredField(propertyName);
                field.setAccessible(true);
                property.setAccessible(true);
                Object value = property.get(bean);
                field.set(bean, value);
            } catch (Exception e) {
                throw new SystemError(e);
            }
        }
    }
}

From source file:com.microsoft.windowsazure.mobileservices.MobileServiceTableBase.java

/**
* Returns the id property class defined or annotated in the entity class
* @param   clazz   Target entity class/*from  w ww  .  j  av  a 2s .c  o  m*/
* @return          Property class
*/
protected static <F> Class<?> getIdPropertyClass(Class<F> clazz) {
    // Search for id properties annotations, regardless case
    for (Field field : clazz.getDeclaredFields()) {
        SerializedName serializedName = field.getAnnotation(SerializedName.class);

        if (serializedName != null) {
            if (IdProperties.contains(serializedName.value())) {
                return field.getType();
            }
        } else {
            if (IdProperties.contains(field.getName())) {
                return field.getType();
            }
        }
    }

    return null;
}

From source file:com.alibaba.fastjson.parser.ParserConfig.java

/**
 * fieldName,field ?fieldName??findField
 * /*w  w w  .j  a  v a 2  s  .  com*/
 * @param clazz
 * @param fieldCacheMap :map&lt;fieldName ,Field&gt;
 */
public static void parserAllFieldToCache(Class<?> clazz, Map</**fieldName*/
        String, Field> fieldCacheMap) {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        String fieldName = field.getName();
        if (!fieldCacheMap.containsKey(fieldName)) {
            fieldCacheMap.put(fieldName, field);
        }
    }
    if (clazz.getSuperclass() != null && clazz.getSuperclass() != Object.class) {
        parserAllFieldToCache(clazz.getSuperclass(), fieldCacheMap);
    }
}

From source file:com.github.dactiv.common.utils.ReflectionUtils.java

/**
 * //from w ww  . j  a  v a2  s .c  om
 * ?o??
 * 
 * @param targetClass
 *            Class
 * @param type
 *            ????
 * 
 * @return List
 */
public static List<String> getAccessibleFieldNames(final Class targetClass, Class type) {

    Assert.notNull(targetClass, "targetClass?");
    Assert.notNull(type, "type?");

    List<String> list = new ArrayList<String>();

    for (Field field : targetClass.getDeclaredFields()) {
        if (field.getType().equals(type)) {
            list.add(field.getName());
        }
    }

    return list;
}

From source file:cn.aposoft.util.spring.ReflectionUtils.java

/**
 * This variant retrieves {@link Class#getDeclaredFields()} from a local
 * cache in order to avoid the JVM's SecurityManager check and defensive
 * array copying.//from w  w w. j  a  v a 2s.  c om
 * 
 * @param clazz
 *            the class to introspect
 * @return the cached array of fields
 * @see Class#getDeclaredFields()
 */
private static Field[] getDeclaredFields(Class<?> clazz) {
    Field[] result = declaredFieldsCache.get(clazz);
    if (result == null) {
        result = clazz.getDeclaredFields();
        declaredFieldsCache.put(clazz, (result.length == 0 ? NO_FIELDS : result));
    }
    return result;
}

From source file:com.zc.util.refelect.Reflector.java

/**
 *
 * ?annotationClass//from   w ww .  j a v  a  2s .  c o  m
 *
 * @param targetClass
 *            Class
 * @param annotationClass
 *            Class
 *
 * @return List
 */
public static <T extends Annotation> List<T> getAnnotations(Class targetClass, Class annotationClass) {

    List<T> result = new ArrayList<T>();
    Annotation annotation = targetClass.getAnnotation(annotationClass);
    if (annotation != null) {
        result.add((T) annotation);
    }
    Constructor[] constructors = targetClass.getDeclaredConstructors();
    // ?
    CollectionUtils.addAll(result, getAnnotations(constructors, annotationClass).iterator());

    Field[] fields = targetClass.getDeclaredFields();
    // ?
    CollectionUtils.addAll(result, getAnnotations(fields, annotationClass).iterator());

    Method[] methods = targetClass.getDeclaredMethods();
    // ?
    CollectionUtils.addAll(result, getAnnotations(methods, annotationClass).iterator());

    for (Class<?> superClass = targetClass.getSuperclass(); superClass == null
            || superClass == Object.class; superClass = superClass.getSuperclass()) {
        List<T> temp = (List<T>) getAnnotations(superClass, annotationClass);
        if (CollectionUtils.isNotEmpty(temp)) {
            CollectionUtils.addAll(result, temp.iterator());
        }
    }

    return result;
}