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:ml.shifu.shifu.util.ClassUtils.java

@SuppressWarnings("unchecked")
public static List<Field> getAllFields(Class<?> clazz) {
    if (clazz == null || clazz.equals(Object.class)) {
        return Collections.EMPTY_LIST;
    }/*  www.ja va 2s .c o m*/

    List<Field> result = new ArrayList<Field>();
    for (Field field : clazz.getDeclaredFields()) {
        result.add(field);
    }
    Class<?> tmpClazz = clazz.getSuperclass();
    while (!Object.class.equals(tmpClazz)) {
        result.addAll(getAllFields(tmpClazz));
        tmpClazz = tmpClazz.getSuperclass();
    }

    return result;
}

From source file:br.gov.frameworkdemoiselle.ldap.internal.ClazzUtils.java

/**
 * Build a array of super classes fields
 * //from  w  w  w . ja va2s  . c o m
 * @return Array of Super Classes Fields
 */
public static Field[] getSuperClassesFields(Class<?> entryClass) {
    Field[] fieldArray = entryClass.getDeclaredFields();
    Class<?> superClazz = entryClass.getSuperclass();
    while (superClazz != null && !"java.lang.Object".equals(superClazz.getName())) {
        fieldArray = (Field[]) ArrayUtils.addAll(fieldArray, superClazz.getDeclaredFields());
        superClazz = superClazz.getSuperclass();
    }
    return fieldArray;
}

From source file:com.dungnv.vfw5.base.utils.StringUtils.java

public static void trimString(Object obj, boolean isLower) {
    String oldData = "";
    String newData = "";
    try {/*  ww  w  .j a  v  a  2  s . c  o m*/
        if (obj != null) {
            Class escapeClass = obj.getClass();
            Field fields[] = escapeClass.getDeclaredFields();
            Field superFields[] = escapeClass.getSuperclass().getDeclaredFields();
            Field allField[] = new Field[fields.length + superFields.length];
            System.arraycopy(fields, 0, allField, 0, fields.length);
            System.arraycopy(superFields, 0, allField, fields.length, superFields.length);
            for (Field f : allField) {
                f.setAccessible(true);
                if (f.getType().equals(java.lang.String.class) && !Modifier.isFinal(f.getModifiers())) {
                    if (f.get(obj) != null) {
                        oldData = f.get(obj).toString();
                        newData = isLower ? oldData.trim().toLowerCase() : oldData.trim();
                        f.set(obj, newData);
                    }
                } else if (f.getType().isArray()) {
                    if (f.getType().getComponentType().equals(java.lang.String.class)) {
                        String[] tmpArr = (String[]) f.get(obj);
                        if (tmpArr != null) {
                            for (int i = 0; i < tmpArr.length; i++) {
                                tmpArr[i] = isLower ? tmpArr[i].trim().toLowerCase() : tmpArr[i].trim();
                            }
                            f.set(obj, tmpArr);
                        }
                    }
                } else if (f.get(obj) instanceof List) {
                    List<Object> tmpList = (List<Object>) f.get(obj);
                    for (int i = 0; i < tmpList.size(); i++) {
                        if (tmpList.get(i) instanceof java.lang.String) {
                            tmpList.set(i, isLower ? tmpList.get(i).toString().trim().toLowerCase()
                                    : tmpList.get(i).toString().trim());
                        }
                    }
                    f.set(obj, tmpList);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:kina.utils.Utils.java

private static Field[] getAllFieldsRec(Class clazz, List<Field> fields) {
    Class superClazz = clazz.getSuperclass();
    if (superClazz != null) {
        getAllFieldsRec(superClazz, fields);
    }// ww  w.  jav a2  s  .c o m

    fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    return fields.toArray(new Field[fields.size()]);
}

From source file:com.mine.core.util.ReflectUtils.java

/**
 * ???/*from   w w w .j a  va  2s  .  c om*/
 */
public static String[] getFiledNames(Class<?> clazz) {
    Field[] fields = clazz.getDeclaredFields();
    String[] fieldNames = new String[fields.length];
    for (int i = 0; i < fields.length; i++) {
        fieldNames[i] = fields[i].getName();
    }
    return fieldNames;
}

From source file:com.mine.core.util.ReflectUtils.java

/**
 * ??????/*  w w  w.ja  va2s.c  o  m*/
 */
public static String[] getStaticFiledNames(Class<?> clazz) {
    Field[] fields = clazz.getDeclaredFields();
    String[] fieldNames = new String[fields.length];
    for (int i = 0; i < fields.length; i++) {
        fieldNames[i] = fields[i].getName();
    }
    return fieldNames;
}

From source file:microsoft.exchange.webservices.data.core.service.schema.ServiceObjectSchema.java

/**
 * Adds the schema property names to dictionary.
 *
 * @param type                   The type.
 * @param propertyNameDictionary The property name dictionary.
 *///ww w  . j a  v a 2  s. c o m
protected static void addSchemaPropertyNamesToDictionary(Class<?> type,
        Map<PropertyDefinition, String> propertyNameDictionary) {

    Field[] fields = type.getDeclaredFields();
    for (Field field : fields) {
        int modifier = field.getModifiers();
        if (Modifier.isPublic(modifier) && Modifier.isStatic(modifier)) {
            Object o;
            try {
                o = field.get(null);
                if (o instanceof PropertyDefinition) {
                    PropertyDefinition propertyDefinition = (PropertyDefinition) o;
                    propertyNameDictionary.put(propertyDefinition, field.getName());
                }
            } catch (IllegalArgumentException e) {
                LOG.error(e);

                // Skip the field
            } catch (IllegalAccessException e) {
                LOG.error(e);

                // Skip the field
            }
        }
    }
}

From source file:org.oncoblocks.centromere.web.controller.RequestUtils.java

/**
 * Inspects a {@link Model} class and returns all of the available and acceptable query parameter
 *   definitions, as a map of parameter names and {@link QueryParameterDescriptor} objects.
 * /*from w  ww  . java  2  s. c o m*/
 * @param model
 * @return
 */
public static Map<String, QueryParameterDescriptor> getAvailableQueryParameters(Class<? extends Model<?>> model,
        boolean recursive) {
    Map<String, QueryParameterDescriptor> paramMap = new HashMap<>();
    for (Field field : model.getDeclaredFields()) {
        String fieldName = field.getName();
        Class<?> type = field.getType();
        if (Collection.class.isAssignableFrom(field.getType())) {
            ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
            type = (Class<?>) parameterizedType.getActualTypeArguments()[0];
        }
        if (field.isAnnotationPresent(Ignored.class)) {
            continue;
        } else {
            paramMap.put(fieldName,
                    new QueryParameterDescriptor(fieldName, fieldName, type, Evaluation.EQUALS));
        }
        if (field.isAnnotationPresent(ForeignKey.class)) {
            if (!recursive)
                continue;
            ForeignKey foreignKey = field.getAnnotation(ForeignKey.class);
            String relField = !"".equals(foreignKey.rel()) ? foreignKey.rel() : fieldName;
            Map<String, QueryParameterDescriptor> foreignModelMap = getAvailableQueryParameters(
                    foreignKey.model(), false);
            for (QueryParameterDescriptor descriptor : foreignModelMap.values()) {
                String newParamName = relField + "." + descriptor.getParamName();
                descriptor.setParamName(newParamName);
                paramMap.put(newParamName, descriptor);
            }
        }
        if (field.isAnnotationPresent(Aliases.class)) {
            Aliases aliases = field.getAnnotation(Aliases.class);
            for (Alias alias : aliases.value()) {
                paramMap.put(alias.value(),
                        new QueryParameterDescriptor(alias.value(),
                                alias.fieldName().equals("") ? fieldName : alias.fieldName(), type,
                                alias.evaluation()));
            }
        } else if (field.isAnnotationPresent(Alias.class)) {
            Alias alias = field.getAnnotation(Alias.class);
            paramMap.put(alias.value(), new QueryParameterDescriptor(alias.value(),
                    alias.fieldName().equals("") ? fieldName : alias.fieldName(), type, alias.evaluation()));
        }
    }
    return paramMap;
}

From source file:com.dungnv.vfw5.base.utils.StringUtils.java

public static void escapeHTMLString(Object escapeObject) {
    String oldData = "";
    String newData = "";
    try {/*  www .j a  v  a 2s . c o  m*/
        if (escapeObject != null) {
            Class escapeClass = escapeObject.getClass();
            Field fields[] = escapeClass.getDeclaredFields();
            Field superFields[] = escapeClass.getSuperclass().getDeclaredFields();
            Field allField[] = new Field[fields.length + superFields.length];
            System.arraycopy(fields, 0, allField, 0, fields.length);
            System.arraycopy(superFields, 0, allField, fields.length, superFields.length);
            for (Field f : allField) {
                f.setAccessible(true);
                if (f.getType().equals(java.lang.String.class) && !Modifier.isFinal(f.getModifiers())) {
                    if (f.get(escapeObject) != null) {
                        oldData = f.get(escapeObject).toString();
                        newData = StringEscapeUtils.escapeSql(oldData);
                        f.set(escapeObject, newData);
                    }
                } else if (f.getType().isArray()) {
                    if (f.getType().getComponentType().equals(java.lang.String.class)) {
                        String[] tmpArr = (String[]) f.get(escapeObject);
                        if (tmpArr != null) {
                            for (int i = 0; i < tmpArr.length; i++) {
                                tmpArr[i] = StringEscapeUtils.escapeSql(tmpArr[i]);
                            }
                            f.set(escapeObject, tmpArr);
                        }
                    }
                } else if (f.get(escapeObject) instanceof List) {
                    List<Object> tmpList = (List<Object>) f.get(escapeObject);
                    for (int i = 0; i < tmpList.size(); i++) {
                        if (tmpList.get(i) instanceof java.lang.String) {
                            tmpList.set(i, StringEscapeUtils.escapeSql(tmpList.get(i).toString()));
                        }
                    }
                    f.set(escapeObject, tmpList);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.hihframework.core.utils.ReflectUtil.java

/**
 * @param obj//from w  w w . j  a v  a  2  s  .  c  o m
 * @param cmpModifier
 * @return
 */
private static String[] getFieldsName(Object obj, int cmpModifier) {
    String[] retValue;
    Class<?> objClass = obj.getClass();
    Field[] fields = objClass.getDeclaredFields();

    if (fields != null && !(fields.length < 1)) {
        int j = 0;

        for (int i = 0; i < fields.length; i++) {
            int modifier = fields[i].getModifiers();
            if (modifier == cmpModifier)
                j++;
        }

        retValue = new String[j];

        j = 0;
        for (int i = 0; i < fields.length; i++) {
            int modifier = fields[i].getModifiers();
            if (modifier == cmpModifier) {
                retValue[j] = fields[i].getName();
                j++;
            }
        }

        return retValue;
    } else
        return null;
}