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.feilong.commons.core.lang.reflect.FieldUtil.java

/**
 *  {@link java.lang.reflect.Field}(? private, inherited ).
 * // w w  w. j  av a  2  s. co m
 * @param obj
 *            the obj
 * @return the declared fields
 * @see java.lang.Class#getDeclaredFields()
 * @see java.lang.Class#getSuperclass()
 * @see java.lang.reflect.Field
 * @see org.apache.commons.lang3.ArrayUtils#addAll(boolean[], boolean...)
 */
private static Field[] getDeclaredFields(Object obj) {
    Class<?> klass = obj.getClass();
    Class<?> superClass = klass.getSuperclass();

    //Class????(??public)
    Field[] fields = klass.getDeclaredFields();
    do {
        if (log.isDebugEnabled()) {
            log.debug("current class:[{}],super class:[{}]", klass.getName(), superClass.getName());
        }
        fields = ArrayUtils.addAll(fields, superClass.getDeclaredFields());
        superClass = superClass.getSuperclass();

    } while (null != superClass && superClass != Object.class);

    return fields;
}

From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java

/**
 * http://stackoverflow.com/questions/1042798/retrieving-the-inherited-attribute-names-values-using-java-reflection
 * @param fields/*w w  w.jav  a 2  s  . c o m*/
 * @param c
 * @return
 */
public static List<Field> getAllFields(List<Field> fields, Class<?> c) {
    fields.addAll(Arrays.asList(c.getDeclaredFields()));

    if (c.getSuperclass() != null) {
        fields = getAllFields(fields, c.getSuperclass());
    }

    return fields;
}

From source file:org.nuxeo.apidoc.introspection.ServerInfo.java

protected static List<Class<?>> getSPI(Class<?> klass) {
    List<Class<?>> spi = new ArrayList<>();
    for (Field field : klass.getDeclaredFields()) {
        String cName = field.getType().getCanonicalName();
        if (cName.startsWith("org.nuxeo")) {
            // remove XObjects
            Class<?> fieldClass = field.getType();
            Annotation[] annotations = fieldClass.getDeclaredAnnotations();
            if (annotations.length == 0) {
                spi.add(fieldClass);/*from ww w.  j ava  2s . c o  m*/
            }
        }
    }
    return spi;
}

From source file:com.github.gekoh.yagen.util.FieldInfo.java

private static List<FieldInfo> convertFields(List<FieldInfo> fields, Class baseEntity) {

    for (Field field : baseEntity.getDeclaredFields()) {
        FieldInfo fi;/*from   w  w w .j  a va 2 s  . c om*/
        Class type = field.getType();
        String name = field.getName();
        Column column = field.getAnnotation(Column.class);
        if (field.isAnnotationPresent(Embedded.class)) {
            if (field.isAnnotationPresent(AttributeOverride.class)) {
                fi = new FieldInfo(type, name, field.getAnnotation(AttributeOverride.class));
            } else {
                fi = new FieldInfo(type, name, field.getAnnotation(AttributeOverrides.class));
            }
        } else if (field.isAnnotationPresent(Enumerated.class)) {
            fi = new FieldInfo(type, name, true, column);
        } else if (column != null && !field.isAnnotationPresent(CollectionTable.class)) {
            if (type.isPrimitive()) {
                if (type.equals(Boolean.TYPE)) {
                    type = Boolean.class;
                } else if (type.equals(Long.TYPE)) {
                    type = Long.class;
                } else if (type.equals(Integer.TYPE)) {
                    type = Integer.class;
                } else if (type.equals(Short.TYPE)) {
                    type = Short.class;
                } else if (type.equals(Byte.TYPE)) {
                    type = Byte.class;
                } else if (type.equals(Double.TYPE)) {
                    type = Double.class;
                } else if (type.equals(Float.TYPE)) {
                    type = Float.class;
                } else if (type.equals(Character.TYPE)) {
                    type = Character.class;
                }
            }
            fi = new FieldInfo(type, name, false, column);
        } else if ((field.isAnnotationPresent(ManyToOne.class) && !field.isAnnotationPresent(JoinTable.class))
                || (field.isAnnotationPresent(OneToOne.class)
                        && StringUtils.isEmpty(field.getAnnotation(OneToOne.class).mappedBy()))) {
            String columnName = field.isAnnotationPresent(JoinColumn.class)
                    ? field.getAnnotation(JoinColumn.class).name()
                    : field.getName();
            fi = getIdFieldInfo(type, name, columnName);
        } else if (!field.isAnnotationPresent(Transient.class)
                && (Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type))
                && (field.isAnnotationPresent(JoinColumn.class) || field.isAnnotationPresent(JoinTable.class)
                        || field.isAnnotationPresent(CollectionTable.class))) {
            fi = new FieldInfo(type, name);
        } else {
            continue;
        }
        if (field.isAnnotationPresent(Type.class)) {
            fi.addAnnotation(field.getAnnotation(Type.class));
        }
        fi.setField(field);
        fields.add(fi);
    }

    return fields;
}

From source file:net.sf.excelutils.ExcelParser.java

/**
 * get properties of the JavaBean//from w  w w . j a  v  a2s.c  om
 * 
 * @param clazz JavaBean
 * @return fields in the javabean
 */
public static Field[] getBeanProperties(Class clazz) {
    Field[] fields = clazz.getDeclaredFields();
    Method[] methods = clazz.getMethods();
    String m = "";

    for (int i = 0; i < methods.length; i++) {
        m += methods[i].getName() + ",";
    }

    List flist = new ArrayList();
    for (int i = 0; i < fields.length; i++) {
        if (m.indexOf("get" + fields[i].getName().substring(0, 1).toUpperCase()
                + fields[i].getName().substring(1, fields[i].getName().length())) >= 0) {
            flist.add(fields[i]);
        }
    }
    Field[] result = new Field[flist.size()];
    flist.toArray(result);
    return result;
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Gets all fields from a class. Keys are the field names as strings.
 *
 * @param type the type//from w w w .  j  av a2 s  .co  m
 * @return the all fields
 */
//CHECKSTYLE.OFF FinalParameter Precondition cast.
public static Map<String, Field> getAllFields(final Class<?> type) {

    Map<String, Field> fieldMap = new HashMap<>();
    for (Field field : type.getDeclaredFields()) {
        fieldMap.put(field.getName(), field);
    }
    Class<?> nextType = type.getSuperclass();

    while (nextType != Object.class) {
        for (Field field : nextType.getDeclaredFields()) {
            fieldMap.put(field.getName(), field);
        }
        nextType = nextType.getSuperclass();
    }
    //CHECKSTYLE.ON
    return fieldMap;
}

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

/**
 * Adds schema property to dictionary.//from www .  ja va 2s .  c  om
 *
 * @param type              Schema type.
 * @param propDefDictionary The property definition dictionary.
 */
protected static void addSchemaPropertiesToDictionary(Class<?> type,
        Map<String, PropertyDefinitionBase> propDefDictionary) {
    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;
                    // Some property definitions descend from
                    // ServiceObjectPropertyDefinition but don't have
                    // a Uri, like ExtendedProperties. Ignore them.
                    if (null != propertyDefinition.getUri() && !propertyDefinition.getUri().isEmpty()) {
                        PropertyDefinitionBase existingPropertyDefinition;
                        if (propDefDictionary.containsKey(propertyDefinition.getUri())) {
                            existingPropertyDefinition = propDefDictionary.get(propertyDefinition.getUri());
                            EwsUtilities.ewsAssert(existingPropertyDefinition == propertyDefinition,
                                    "Schema.allSchemaProperties." + "delegate",
                                    String.format(
                                            "There are at least " + "two distinct property "
                                                    + "definitions with the" + " following URI: %s",
                                            propertyDefinition.getUri()));
                        } else {
                            propDefDictionary.put(propertyDefinition.getUri(), propertyDefinition);
                            // The following is a "generic hack" to register
                            // property that are not public and
                            // thus not returned by the above GetFields
                            // call. It is currently solely used to register
                            // the MeetingTimeZone property.
                            List<PropertyDefinition> associatedInternalProperties = propertyDefinition
                                    .getAssociatedInternalProperties();
                            for (PropertyDefinition associatedInternalProperty : associatedInternalProperties) {
                                propDefDictionary.put(associatedInternalProperty.getUri(),
                                        associatedInternalProperty);
                            }

                        }
                    }
                }
            } catch (IllegalArgumentException e) {
                LOG.error(e);

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

                // Skip the field
            }

        }
    }
}

From source file:com.iisigroup.cap.utils.CapBeanUtil.java

/**
 * ?Clazz?(?super class)/*from   ww w . ja va2 s . c  o  m*/
 * 
 * @param clazz
 *            class
 * @return List<Field>
 */
@SuppressWarnings({ "rawtypes" })
public static Map<Class, Field[]> getAllFields(Class clazz) {
    Map<Class, Field[]> map = new LinkedHashMap<Class, Field[]>();
    Class searchClazz = clazz;
    while (!Object.class.equals(searchClazz) && searchClazz != null) {
        Field[] fields = searchClazz.getDeclaredFields();
        map.put(searchClazz, fields);
        searchClazz = searchClazz.getSuperclass();
    }
    return map;
}

From source file:com.iisigroup.cap.utils.CapBeanUtil.java

/**
 * ?Clazz?(?super class)/*from  w  ww.j a v  a2s  . co m*/
 * 
 * @param clazz
 *            class
 * @param containSuperClazz
 *            ??Class?
 * @return String[]
 */
@SuppressWarnings({ "rawtypes" })
public static String[] getFieldName(Class clazz, boolean containSuperClazz) {
    Set<String> cols = new LinkedHashSet<String>();
    Class searchClazz = clazz;
    while (!Object.class.equals(searchClazz) && searchClazz != null) {
        Field[] fields = searchClazz.getDeclaredFields();
        for (Field f : fields) {
            if ("serialVersionUID".equals(f.getName()))
                continue;
            cols.add(f.getName());
        }
        searchClazz = containSuperClazz ? searchClazz.getSuperclass() : null;
    }
    return cols.toArray(new String[cols.size()]);
}

From source file:com.iisigroup.cap.utils.CapBeanUtil.java

/**
 * ?Clazz?(?super class)//from  w ww .  jav a 2 s .co m
 * 
 * @param clazz
 *            class
 * @param containSuperClazz
 *            ??Class?
 * @return Field[]
 */
@SuppressWarnings({ "rawtypes" })
public static Field[] getField(Class clazz, boolean containSuperClazz) {
    Set<Field> cols = new LinkedHashSet<Field>();
    Class searchClazz = clazz;
    while (!Object.class.equals(searchClazz) && searchClazz != null) {
        Field[] fields = searchClazz.getDeclaredFields();
        for (Field f : fields) {
            if ("serialVersionUID".equals(f.getName()))
                continue;
            cols.add(f);
        }
        searchClazz = containSuperClazz ? searchClazz.getSuperclass() : null;
    }
    return cols.toArray(new Field[] {});
}