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:natalia.dymnikova.configuration.ConfigBeanPostProcessor.java

@Override
public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {
    Class<?> currentClass = getUserClass(bean.getClass());

    while (currentClass != null && currentClass != Object.class) {
        stream(currentClass.getDeclaredFields()).filter(f -> f.getAnnotation(ConfigValue.class) != null)
                .map(this::makeAccessible).forEach(field -> injectConfiguration(bean, beanName, field));

        currentClass = currentClass.getSuperclass();
    }//from   ww  w  . j  ava2  s.c o  m

    return bean;
}

From source file:com.beginner.core.listener.ZookeeperListener.java

/**
 * ?JSON??//from  ww  w  .j  a  va 2 s .com
 */
private void setProperties(String result) throws ClassNotFoundException, IllegalAccessException {
    JSONObject json = JSONObject.fromObject(result);
    String className = json.getString("resourcesClass");
    if (json.containsKey("resourcesClass")) {
        Class<?> resourcesClass = Class.forName(className);
        Field[] fields = resourcesClass.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            field.setAccessible(true);
            String name = field.getName();
            Class<?> type = field.getType().getClass();
            if (json.containsKey(name)) {
                if (type.equals(String.class)) {
                    field.set(resourcesClass, json.get(name));
                }
            }
        }
    }
}

From source file:ea.compoment.db.util.sql.UpdateSqlBuilder.java

/**
 * Where?// w ww  .j  a v  a2s .  co  m
 * 
 * @param entity
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws DBException
 */
public NVArrayList buildWhere(Object entity)
        throws IllegalArgumentException, IllegalAccessException, DBException {
    Class<?> clazz = entity.getClass();
    NVArrayList whereArrayList = new NVArrayList();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!DBAnnoUtils.isTransient(field)) {
            if (DBAnnoUtils.isBaseDateType(field)) {
                Annotation annotation = field.getAnnotation(PrimaryKey.class);
                if (annotation != null) {
                    String columnName = DBAnnoUtils.getColumnByField(field);
                    whereArrayList.add(
                            (columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity).toString());
                }

            }
        }
    }
    if (whereArrayList.isEmpty()) {
        throw new DBException("?Where??");
    }
    return whereArrayList;
}

From source file:net.mindengine.blogix.db.readers.ObjectReader.java

private Map<String, Field> getAllFieldsOfObjectType(Class<?> objectType) {
    Map<String, Field> fields = convertOnlyNonStaticFieldsToMap(objectType.getDeclaredFields());

    Class<?> superClass = objectType.getSuperclass();
    if (superClass != null) {
        fields.putAll(getAllFieldsOfObjectType(superClass));
    }//from   w  ww. j ava 2  s. c  o m
    return fields;
}

From source file:annis.model.DataObject.java

private void forEachFieldDo(FieldCallBack fieldCallBack) {
    Class<?> clazz = this.getClass();

    while (true) {

        Field[] fields = clazz.getDeclaredFields();
        try {/*from  w w  w  .j a v  a2  s  .  com*/
            for (Field field : fields) {
                // FIXME: test skipped cases
                // skip Logger object
                if (field.getType() == Logger.class)
                    continue;
                // skip fields annotated with @Transient
                if (field.getAnnotation(Transient.class) != null)
                    continue;
                // skip serialVersionUID
                if ("serialVersionUID".equals(field.getName()))
                    continue;
                field.setAccessible(true);
                fieldCallBack.doForField(field);
            }
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        if (clazz.getName().equals(DataObject.class.getName()))
            break;

        clazz = clazz.getSuperclass();
    }
}

From source file:com.rosenvold.spring.SpringContextAnalyzer.java

boolean hasAutowiredField(Class clazz) {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (isAutowired(field))
            return true;
    }/*from   ww w .  java 2s .com*/
    return false;
}

From source file:com.flipkart.polyguice.dropwiz.DropConfigProvider.java

private Object getValueFromFields(String path, Class<?> type, Object inst) throws Exception {
    Field[] fields = type.getDeclaredFields();
    for (Field field : fields) {
        JsonProperty ann = field.getAnnotation(JsonProperty.class);
        if (ann != null) {
            String annName = ann.value();
            if (StringUtils.isBlank(annName)) {
                annName = ann.defaultValue();
            }//from  w w w  . j ava 2s. co  m
            if (StringUtils.isBlank(annName)) {
                annName = field.getName();
            }
            if (StringUtils.equals(path, annName)) {
                boolean accessible = field.isAccessible();
                if (!accessible) {
                    field.setAccessible(true);
                }
                Object value = field.get(inst);
                if (!accessible) {
                    field.setAccessible(false);
                }
                return value;
            }
        }
    }
    return null;
}

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

/**
 * Where?// www  .  j ava2 s. c  o  m
 * 
 * @param entity
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws DBException
 */
public ArrayListEx buildWhere(Object entity)
        throws IllegalArgumentException, IllegalAccessException, DBException {
    Class<?> clazz = entity.getClass();
    ArrayListEx whereArrayList = new ArrayListEx();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!DBUtils.isTransient(field)) {
            if (DBUtils.isBaseDateType(field)) {
                Annotation annotation = field.getAnnotation(PrimaryKey.class);
                if (annotation != null) {
                    String columnName = DBUtils.getColumnByField(field);
                    whereArrayList.add(
                            (columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity).toString());
                }

            }
        }
    }
    if (whereArrayList.isEmpty()) {
        throw new DBException("?Where??");
    }
    return whereArrayList;
}

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

/**
 * Where?//from w w  w  . j  a  va2  s  .  c  om
 * 
 * @param entity
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws TADBException
 */
public TAArrayList buildWhere(Object entity)
        throws IllegalArgumentException, IllegalAccessException, TADBException {
    Class<?> clazz = entity.getClass();
    TAArrayList whereArrayList = new TAArrayList();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!TADBUtils.isTransient(field)) {
            if (TADBUtils.isBaseDateType(field)) {
                Annotation annotation = field.getAnnotation(TAPrimaryKey.class);
                if (annotation != null) {
                    String columnName = TADBUtils.getColumnByField(field);
                    whereArrayList.add(
                            (columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity).toString());
                }

            }
        }
    }
    if (whereArrayList.isEmpty()) {
        throw new TADBException("?Where??");
    }
    return whereArrayList;
}

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

/**
 * Where?/*w  w w  . j av  a 2s  .  c o  m*/
 *
 * @param entity
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws TADBException
 */
public TAArrayList buildWhere(Object entity)
        throws IllegalArgumentException, IllegalAccessException, TADBException {
    Class<?> clazz = entity.getClass();
    TAArrayList whereArrayList = new TAArrayList();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!TADBUtils.isTransient(field)) {
            if (TADBUtils.isBaseDateType(field)) {
                Annotation annotation = field.getAnnotation(DBPrimaryKey.class);
                if (annotation != null) {
                    String columnName = TADBUtils.getColumnByField(field);
                    whereArrayList.add(
                            (columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity).toString());
                }

            }
        }
    }
    if (whereArrayList.isEmpty()) {
        throw new TADBException("?Where??");
    }
    return whereArrayList;
}