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.zlfun.framework.excel.ExcelUtils.java

private static <T> void parent(T t, Class<?> parentClass, Map<String, String> map) {
    Field[] fs = parentClass.getDeclaredFields();

    for (Field f : fs) {
        String data = get(t, f);//  ww w  . j  ava2s .c  om
        if (data == null) {
            continue;
        }
        WebParam itemField = f.getAnnotation(WebParam.class);
        if (itemField == null) {
            continue;
        }
        if ("".equals(itemField.value())) {
            String fname = f.getName();
            if (!map.containsKey(fname)) {
                map.put(fname, data);
            }
        } else {
            String fname = itemField.value();
            if (!map.containsKey(fname)) {
                map.put(fname, data);
            }
        }

    }
}

From source file:ch.flashcard.HibernateDetachUtility.java

private static void nullOutFieldsByFieldAccess(Object object, Map<Integer, Object> checkedObjects,
        Map<Integer, List<Object>> checkedObjectCollisionMap, int depth, SerializationType serializationType)
        throws Exception {

    Class tmpClass = object.getClass();
    List<Field> fieldsToClean = new ArrayList<Field>();
    while (tmpClass != null && tmpClass != Object.class) {
        Field[] declaredFields = tmpClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            // do not process static final or transient fields since they won't be serialized anyway
            int modifiers = declaredField.getModifiers();
            if (!((Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers))
                    || Modifier.isTransient(modifiers))) {
                fieldsToClean.add(declaredField);
            }/*from  ww  w .  j a v  a 2  s  . c  om*/
        }
        tmpClass = tmpClass.getSuperclass();
    }

    nullOutFieldsByFieldAccess(object, fieldsToClean, checkedObjects, checkedObjectCollisionMap, depth,
            serializationType);
}

From source file:edu.usu.sdl.openstorefront.doc.JaxrsProcessor.java

private static void mapParameters(List<APIParamModel> parameterList, Field fields[]) {
    for (Field field : fields) {
        APIParamModel paramModel = new APIParamModel();
        paramModel.setFieldName(field.getName());

        QueryParam queryParam = (QueryParam) field.getAnnotation(QueryParam.class);
        FormParam formParam = (FormParam) field.getAnnotation(FormParam.class);
        MatrixParam matrixParam = (MatrixParam) field.getAnnotation(MatrixParam.class);
        HeaderParam headerParam = (HeaderParam) field.getAnnotation(HeaderParam.class);
        CookieParam cookieParam = (CookieParam) field.getAnnotation(CookieParam.class);
        PathParam pathParam = (PathParam) field.getAnnotation(PathParam.class);
        BeanParam beanParam = (BeanParam) field.getAnnotation(BeanParam.class);

        if (queryParam != null) {
            paramModel.setParameterType(QueryParam.class.getSimpleName());
            paramModel.setParameterName(queryParam.value());
        }// w w  w . j av  a 2  s.c  o  m
        if (formParam != null) {
            paramModel.setParameterType(FormParam.class.getSimpleName());
            paramModel.setParameterName(formParam.value());
        }
        if (matrixParam != null) {
            paramModel.setParameterType(MatrixParam.class.getSimpleName());
            paramModel.setParameterName(matrixParam.value());
        }
        if (pathParam != null) {
            paramModel.setParameterType(PathParam.class.getSimpleName());
            paramModel.setParameterName(pathParam.value());
        }
        if (headerParam != null) {
            paramModel.setParameterType(HeaderParam.class.getSimpleName());
            paramModel.setParameterName(headerParam.value());
        }
        if (cookieParam != null) {
            paramModel.setParameterType(CookieParam.class.getSimpleName());
            paramModel.setParameterName(cookieParam.value());
        }

        if (beanParam != null) {
            Class fieldClass = field.getDeclaringClass();
            mapParameters(parameterList, fieldClass.getDeclaredFields());
        }

        if (StringUtils.isNotBlank(paramModel.getParameterType())) {

            APIDescription aPIDescription = (APIDescription) field.getAnnotation(APIDescription.class);
            if (aPIDescription != null) {
                paramModel.setParameterDescription(aPIDescription.value());
            }

            ParameterRestrictions restrictions = (ParameterRestrictions) field
                    .getAnnotation(ParameterRestrictions.class);
            if (restrictions != null) {
                paramModel.setRestrictions(restrictions.value());
            }

            RequiredParam requiredParam = (RequiredParam) field.getAnnotation(RequiredParam.class);
            if (requiredParam != null) {
                paramModel.setRequired(true);
            }

            DefaultValue defaultValue = (DefaultValue) field.getAnnotation(DefaultValue.class);
            if (defaultValue != null) {
                paramModel.setDefaultValue(defaultValue.value());
            }

            parameterList.add(paramModel);
        }
    }
}

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

private static <T> void parent(Class<T> parentClass, Set<String> set, List<String> result) {
    Field[] fs = parentClass.getDeclaredFields();

    for (Field f : fs) {

        WebParam itemField = f.getAnnotation(WebParam.class);
        if (itemField == null) {
            continue;
        }/*from  w  w  w  .  j a v  a 2 s . co m*/
        if ("".equals(itemField.value())) {
            String fname = f.getName();
            if (!set.contains(fname)) {
                set.add(fname);
                result.add(fname);
            }
        } else {
            String fname = itemField.value();
            if (!set.contains(fname)) {
                set.add(fname);
                result.add(fname);
            }
        }

    }
}

From source file:com.github.abel533.mapperhelper.EntityHelper.java

/**
 * ?Field/* w  ww  . j  a v a2 s  .c  om*/
 *
 * @param entityClass
 * @param fieldList
 * @return
 */
private static List<Field> getAllField(Class<?> entityClass, List<Field> fieldList) {
    if (fieldList == null) {
        fieldList = new ArrayList<Field>();
    }
    if (entityClass.equals(Object.class)) {
        return fieldList;
    }
    Field[] fields = entityClass.getDeclaredFields();
    for (Field field : fields) {
        //??bug#2
        if (!Modifier.isStatic(field.getModifiers())) {
            fieldList.add(field);
        }
    }
    Class<?> superClass = entityClass.getSuperclass();
    if (superClass != null && !superClass.equals(Object.class) && (superClass.isAnnotationPresent(Entity.class)
            || (!Map.class.isAssignableFrom(superClass) && !Collection.class.isAssignableFrom(superClass)))) {
        return getAllField(entityClass.getSuperclass(), fieldList);
    }
    return fieldList;
}

From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java

/**
 * Attempt to find a {@link Field field} on the supplied {@link Class} with the
 * supplied {@code name} and/or {@link Class type}. Searches all superclasses
 * up to {@link Object}.//from w ww  . j  a v a  2  s. co m
 * @param clazz the class to introspect
 * @param name the name of the field (may be {@code null} if type is specified)
 * @param type the type of the field (may be {@code null} if name is specified)
 * @return the corresponding Field object, or {@code null} if not found
 */
public static Field findField(Class<?> clazz, String name, Class<?> type) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.isTrue(name != null || type != null, "Either name or type of the field must be specified");
    Class<?> searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
        Field[] fields = searchType.getDeclaredFields();
        for (Field field : fields) {
            if ((name == null || name.equals(field.getName()))
                    && (type == null || type.equals(field.getType()))) {
                return field;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:edu.umass.cs.gigapaxos.paxospackets.PaxosPacket.java

static void checkFields(Class<?> clazz, GetType[] expFields) {
    if (!STRICT_ADDRESS_CHECKS)
        return;/*ww  w .j av  a 2  s. co  m*/
    Field[] fields = clazz.getDeclaredFields();
    boolean print = edu.umass.cs.gigapaxos.PaxosManager.getLogger().isLoggable(Level.FINE);
    if (print)
        System.out.println("Checking fields for " + clazz);
    for (int i = 0, j = 0; i < fields.length; j += (isStatic(fields[i]) ? 0 : 1), i++) {
        if (isStatic(fields[i]))
            continue;
        if (j >= expFields.length)
            break;
        if (print)
            System.out.println("    " + fields[j]);
        boolean match = true;
        String field = fields[i].getName();
        Class<?> type = fields[i].getType();
        match = match && field.equals(expFields[j].toString()) && type.equals(expFields[j].getType());
        if (!match)
            throw new RuntimeException("Field " + i + " = " + type + " " + field + " does not match expected "
                    + expFields[j].getType() + " " + expFields[j].toString());
    }
}

From source file:com.github.drinkjava2.jbeanbox.springsrc.ReflectionUtils.java

/**
 * Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared
 * fields./*  w w  w .j a  v  a  2 s  . com*/
 * 
 * @param clazz
 *            the target class to analyze
 * @param fc
 *            the callback to invoke for each field
 * @param ff
 *            the filter that determines the fields to apply the callback to
 */
public static void doWithFields(Class<?> clazz, FieldCallback fc, FieldFilter ff) {
    // Keep backing up the inheritance hierarchy.
    Class<?> targetClass = clazz;
    do {
        Field[] fields = targetClass.getDeclaredFields();
        for (Field field : fields) {
            if (ff != null && !ff.matches(field)) {
                continue;
            }
            try {
                fc.doWith(field);
            } catch (IllegalAccessException ex) {
                throw new IllegalStateException("Not allowed to access field '" + field.getName() + "': " + ex);
            }
        }
        targetClass = targetClass.getSuperclass();
    } while (targetClass != null && targetClass != Object.class);
}

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

/**
 * ?DeclaredField,?.//from   w  w  w . j  a  v a  2 s . c o  m
 * 
 * @param targetClass
 *            Class
 * @param ignoreParent
 *            ??,?Field
 * 
 * @return List
 */
public static List<Field> getAccessibleFields(final Class targetClass, final boolean ignoreParent) {
    Assert.notNull(targetClass, "targetClass?");
    List<Field> fields = new ArrayList<Field>();

    Class<?> sc = targetClass;

    do {
        Field[] result = sc.getDeclaredFields();

        if (!ArrayUtils.isEmpty(result)) {

            for (Field field : result) {
                field.setAccessible(true);
            }

            CollectionUtils.addAll(fields, result);
        }

        sc = sc.getSuperclass();

    } while (sc != Object.class && !ignoreParent);

    return fields;
}

From source file:framework.GlobalHelpers.java

public static Field findInheritedField(Class<?> clazz, String name) {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (field.getName().equals(name)) {
            return field;
        }/*from  w ww. java2  s . c o m*/
    }
    if (!clazz.equals(Object.class)) {
        return findInheritedField(clazz.getSuperclass(), name);
    }
    return null;
}