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:ea.compoment.db.util.sql.InsertSqlBuilder.java

/**
 * ,?/*www. j av a 2s  . c om*/
 * 
 * @return
 * @throws DBException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static NVArrayList getFieldsAndValue(Object entity)
        throws DBException, IllegalArgumentException, IllegalAccessException {
    NVArrayList arrayList = new NVArrayList();
    if (entity == null) {
        throw new DBException("?");
    }
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {

        if (!DBAnnoUtils.isTransient(field)) {
            if (DBAnnoUtils.isBaseDateType(field)) {
                PrimaryKey annotation = field.getAnnotation(PrimaryKey.class);
                if (annotation != null && annotation.autoIncrement()) {

                } else {
                    String columnName = DBAnnoUtils.getColumnByField(field);
                    field.setAccessible(true);
                    arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity) == null ? null : field.get(entity).toString());
                }

            }
        }
    }
    return arrayList;
}

From source file:com.changhong.util.db.util.sql.InsertSqlBuilder.java

/**
 * ,?/*www  . j  a  v a  2 s .  co m*/
 * 
 * @return
 * @throws CHDBException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static CHArrayList getFieldsAndValue(Object entity)
        throws CHDBException, IllegalArgumentException, IllegalAccessException {
    // TODO Auto-generated method stub
    CHArrayList arrayList = new CHArrayList();
    if (entity == null) {
        throw new CHDBException("?");
    }
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {

        if (!DBUtils.isTransient(field)) {
            if (DBUtils.isBaseDateType(field)) {
                CHPrimaryKey annotation = field.getAnnotation(CHPrimaryKey.class);
                if (annotation == null || !annotation.autoIncrement()) {
                    String columnName = DBUtils.getColumnByField(field);
                    columnName = (columnName != null && !columnName.equals("")) ? columnName : field.getName();
                    field.setAccessible(true);
                    String val = DBUtils.toString(field.get(entity));
                    arrayList.add(columnName, val);
                }

            }
        }
    }
    return arrayList;
}

From source file:cn.xdf.thinkutils.db2.util.sql.InsertSqlBuilder.java

/**
 * ,?//from   ww  w  .  j  a v  a  2  s .c  o  m
 * 
 * @return
 * @throws DBException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static ArrayListEx getFieldsAndValue(Object entity)
        throws DBException, IllegalArgumentException, IllegalAccessException {
    // TODO Auto-generated method stub
    ArrayListEx arrayList = new ArrayListEx();
    if (entity == null) {
        throw new DBException("?");
    }
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {

        if (!DBUtils.isTransient(field)) {
            if (DBUtils.isBaseDateType(field)) {
                PrimaryKey annotation = field.getAnnotation(PrimaryKey.class);
                if (annotation != null && annotation.autoIncrement()) {

                } else {
                    String columnName = DBUtils.getColumnByField(field);
                    field.setAccessible(true);
                    arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity) == null ? null : field.get(entity).toString());
                }

            }
        }
    }
    return arrayList;
}

From source file:com.alading.library.util.db.util.sql.TAInsertSqlBuilder.java

/**
 * ,?//from w ww .jav  a 2 s  . c om
 * 
 * @return
 * @throws TADBException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static TAArrayList getFieldsAndValue(Object entity)
        throws TADBException, IllegalArgumentException, IllegalAccessException {
    // TODO Auto-generated method stub
    TAArrayList arrayList = new TAArrayList();
    if (entity == null) {
        throw new TADBException("?");
    }
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {

        if (!TADBUtils.isTransient(field)) {
            if (TADBUtils.isBaseDateType(field)) {
                TAPrimaryKey annotation = field.getAnnotation(TAPrimaryKey.class);
                if (annotation != null && annotation.autoIncrement()) {

                } else {
                    String columnName = TADBUtils.getColumnByField(field);
                    field.setAccessible(true);
                    arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity) == null ? null : field.get(entity).toString());
                }

            }
        }
    }
    return arrayList;
}

From source file:com.witness.utils.db.util.sql.TAInsertSqlBuilder.java

/**
 * ,?/*from   w  ww  . j  a  va 2 s .  co m*/
 * 
 * @return
 * @throws TADBException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static TAArrayList getFieldsAndValue(Object entity)
        throws TADBException, IllegalArgumentException, IllegalAccessException {
    // TODO Auto-generated method stub
    TAArrayList arrayList = new TAArrayList();
    if (entity == null) {
        throw new TADBException("?");
    }
    Class<?> clazz = entity.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {

        if (!TADBUtils.isTransient(field)) {
            if (TADBUtils.isBaseDateType(field)) {
                DBPrimaryKey annotation = field.getAnnotation(DBPrimaryKey.class);
                if (annotation != null && annotation.autoIncrement()) {

                } else {
                    String columnName = TADBUtils.getColumnByField(field);
                    field.setAccessible(true);
                    arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity) == null ? null : field.get(entity).toString());
                }

            }
        }
    }
    return arrayList;
}

From source file:org.dataconservancy.packaging.tool.ser.SerializationAnnotationUtil.java

/**
 * Answers a {@code Map} of {@link PropertyDescriptor} instances, which are used to reflectively access the
 * {@link Serialize serializable} streams on {@code annotatedClass} instances.
 * <p>/*from   ww  w  .  j  av a  2 s.  c  o  m*/
 * Use of {@code PropertyDescriptor} is simply a convenience in lieu of the use of underlying Java reflection.
 * </p>
 * <p>
 * This method looks for fields annotated by the {@code Serialize} annotation on the {@code annotatedClass}.
 * A {@code PropertyDescriptor} is created for each field, and is keyed by the {@code StreamId} in the returned
 * {@code Map}.
 * </p>
 *
 * @param annotatedClass the class to scan for the presense of {@code @Serialize}
 * @return a Map of PropertyDescriptors keyed by their StreamId.
 */
public static Map<StreamId, PropertyDescriptor> getStreamDescriptors(Class annotatedClass) {
    HashMap<StreamId, PropertyDescriptor> results = new HashMap<>();

    Arrays.stream(annotatedClass.getDeclaredFields())
            .filter(candidateField -> AnnotationUtils.getAnnotation(candidateField, Serialize.class) != null)
            .forEach(annotatedField -> {
                AnnotationAttributes attributes = AnnotationUtils.getAnnotationAttributes(annotatedField,
                        AnnotationUtils.getAnnotation(annotatedField, Serialize.class));
                StreamId streamId = (StreamId) attributes.get("streamId");
                PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(PackageState.class,
                        annotatedField.getName());
                results.put(streamId, descriptor);
            });

    return results;
}

From source file:Main.java

/**
 * This method will iterate over all the fields on an object searching for declared fields defined as
 * Collection, List, Set, Map type.  If those fields are null they will be set to empty unmodifiable
 * instances.  If those fields are not null then it will force them to be unmodifiable.
 *
 * This method does not handle nested types.
 *
 * @param o the object to modify.  a null object will do nothing.
 *///w ww . j  a v  a2 s  .com
public static void makeUnmodifiableAndNullSafe(Object o) throws IllegalAccessException {
    if (o == null) {
        return;
    }

    Class<?> targetClass = o.getClass();
    for (Field f : targetClass.getDeclaredFields()) {
        f.setAccessible(true);
        try {
            if (f.getType().isAssignableFrom(List.class)) {
                f.set(o, unmodifiableListNullSafe((List<?>) f.get(o)));
            } else if (f.getType().isAssignableFrom(Set.class)) {
                f.set(o, unmodifiableSetNullSafe((Set<?>) f.get(o)));
            } else if (f.getType().isAssignableFrom(Collection.class)) {
                f.set(o, unmodifiableCollectionNullSafe((Collection<?>) f.get(o)));
            } else if (f.getType().isAssignableFrom(Map.class)) {
                f.set(o, unmodifiableMapNullSafe((Map<?, ?>) f.get(o)));
            }
        } finally {
            f.setAccessible(false);
        }
    }
}

From source file:Main.java

private static void processAttachAllProperties(Class<? extends Object> clazz, List<Field> fields) {
    if (!isEffectiveClass(clazz)) {
        return;//from w  w w  .ja v  a 2s  . co  m
    }

    Field[] declaredFields = clazz.getDeclaredFields();
    for (Field field : declaredFields) {
        fields.add(field);
    }

    processAttachAllProperties(clazz.getSuperclass(), fields);
}

From source file:cz.cuni.mff.d3s.spl.example.newton.app.Main.java

private static void inspectClass(Object obj) {
    if (!INSPECT) {
        return;//w  ww .jav a 2 s . c  o m
    }

    System.out.printf("Inspecting %s:\n", obj);
    Class<?> klass = obj.getClass();
    System.out.printf("  Class: %s\n", klass.getName());
    for (Field f : klass.getDeclaredFields()) {
        Object value;
        boolean accessible = f.isAccessible();
        try {
            f.setAccessible(true);
            value = f.get(obj);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            value = String.format("<failed to read: %s>", e.getMessage());
        }
        f.setAccessible(accessible);
        System.out.printf("  Field %s: %s\n", f.getName(), value);
    }
    for (Method m : klass.getDeclaredMethods()) {
        System.out.printf("  Method %s\n", m.getName());
    }
    System.out.printf("-------\n");
    System.out.flush();
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static Map<String, Class> getClassFields(Class clazz, boolean includeParentClass) {

    Map<String, Class> map = new HashMap<String, Class>();

    Field[] fields = clazz.getDeclaredFields();

    for (Field field : fields) {

        map.put(clazz.getName() + "." + field.getName(), field.getType());// field.getType()

    }/*w  w w . j a  v a  2 s.  c o  m*/

    if (includeParentClass)

        getParentClassFields(map, clazz.getSuperclass());

    return map;

}