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.changhong.util.db.util.sql.UpdateSqlBuilder.java

/**
 * ,?/*  w  w  w  .j  a  va  2  s  .  c  o  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:br.gov.frameworkdemoiselle.util.contrib.Reflections.java

/**
 * Build a array of super classes fields
 * /* w w w.  j a  v  a  2  s .  c  o  m*/
 * @return Array of Super Classes Fields
 */
public static Field[] getSuperClassesFields(Class<?> entryClass) {
    Field[] fieldArray = null;
    fieldArray = (Field[]) ArrayUtils.addAll(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:se.berazy.api.examples.App.java

/**
 * Writes out all public fields./*from ww  w. ja v a 2  s  .  c o  m*/
 * @param T response
 */
static <T> void outPutResponse(T response) {
    String retval = "\nResponse: \n";
    Class<?> clazz = response.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!field.getName().equalsIgnoreCase("ExtensionData")) {
            try {
                retval += String.format("%s: %s\n", field.getName(), field.get(response));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    System.out.println(retval);
}

From source file:utils.ReflectionService.java

public static Map createClassModel(Class clazz, int maxDeep) {
    Map map = new HashMap();
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);/* w ww.j  a v a 2  s  .  c  o m*/
        String key = field.getName();
        try {
            Class<?> type;
            if (Collection.class.isAssignableFrom(field.getType())) {
                ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
                type = (Class<?>) collectionType.getActualTypeArguments()[0];
            } else {
                type = field.getType();
            }

            //            System.out.println(key + " ReflectionResource.createClassModel ==== putting type: " + field.getType().getName() + ", static: "
            //            + Modifier.isStatic(field.getModifiers()) + ", enum: " + type.isEnum() + ", java.*: " + type.getName().startsWith("java"));

            if (Modifier.isStatic(field.getModifiers())) {
                continue;
            } else if (type.isEnum()) {
                map.put(key, Enum.class.getName());
            } else if (!type.getName().startsWith("java") && !key.startsWith("_") && maxDeep > 0) {
                map.put(key, createClassModel(type, maxDeep - 1));
            } else {
                map.put(key, type.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return map;
}

From source file:com.vivekpanyam.evolve.Utils.java

private static Field getField(Class<?> cls, String name) {
    for (Field field : cls.getDeclaredFields()) {
        if (!field.isAccessible()) {
            field.setAccessible(true);/*from   w  ww  . j  a v  a2  s  .  co  m*/
        }
        if (field.getName().equals(name)) {
            return field;
        }
    }
    return null;
}

From source file:com.acuityph.commons.jpa.JpaUtils.java

/**
 * @param entityClass//  ww w  .j  a v a 2  s. c o m
 *        the entity class to inspect
 * @return the {@link Field} annotated with @Id, or null if no such field
 *         was found
 */
public static Field determineIdField(final Class<?> entityClass) {
    Field idField = null;
    Class<?> cl = entityClass;
    while (idField == null && cl != null && cl != Object.class) {
        for (final Field field : cl.getDeclaredFields()) {
            if (field.getAnnotation(Id.class) != null) {
                idField = field;
                break;
            }
        }
        if (idField == null) {
            cl = cl.getSuperclass();
        }
    }
    return idField;
}

From source file:Main.java

public static Field[] getAllFiedFromClassAndSuper(Class clazz, boolean needStatic) {
    ArrayList<Field> fields = new ArrayList<>();
    if (clazz != null) {
        Field[] classFields = clazz.getDeclaredFields();
        if (classFields != null) {
            for (Field field : classFields) {
                boolean isStatic = Modifier.isStatic(field.getModifiers());
                if (isStatic && !needStatic) {
                    continue;
                }//from  ww  w.j  a va2s.  c o m
                fields.add(field);
            }
        }

        Field[] superFields = getAllFiedFromClassAndSuper(clazz.getSuperclass(), needStatic);
        if (superFields != null) {
            for (Field field : superFields) {
                boolean isStatic = Modifier.isStatic(field.getModifiers());
                if (isStatic && !needStatic) {
                    continue;
                }
                fields.add(field);
            }
        }
    }
    return fields.toArray(new Field[fields.size()]);
}

From source file:Main.java

@SuppressLint("UseSparseArrays")
public static ArrayList<String> extractor(Notification notification) {
    ArrayList<String> notifText = new ArrayList<String>();
    RemoteViews views = notification.contentView;
    @SuppressWarnings("rawtypes")
    Class secretClass = views.getClass();

    try {/*from  www  .  j a v  a2  s . com*/

        Field outerFields[] = secretClass.getDeclaredFields();
        for (int i = 0; i < outerFields.length; i++) {

            if (!outerFields[i].getName().equals("mActions"))
                continue;

            outerFields[i].setAccessible(true);

            @SuppressWarnings("unchecked")
            ArrayList<Object> actions = (ArrayList<Object>) outerFields[i].get(views);
            for (Object action : actions) {

                Field innerFields[] = action.getClass().getDeclaredFields();

                Object value = null;
                Integer type = null;
                @SuppressWarnings("unused")
                Integer viewId = null;
                for (Field field : innerFields) {

                    field.setAccessible(true);
                    if (field.getName().equals("value")) {
                        value = field.get(action);
                    } else if (field.getName().equals("type")) {
                        type = field.getInt(action);
                    } else if (field.getName().equals("viewId")) {
                        viewId = field.getInt(action);
                    }
                }

                if (type != null && (type == 9 || type == 10) && value != null) {
                    // System.out.println("Type: " + Integer.toString(type)
                    // + " Value: " + value.toString());
                    if (!notifText.contains(value.toString()))
                        notifText.add(value.toString());
                }

            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return notifText;
}

From source file:com.dragome.callbackevictor.serverside.utils.ReflectionUtils.java

public static Map<String, Object> discoverFields(final Class<?> pClazz, final Matcher pMatcher,
        final Indexer pIndexer) {

    log.debug("discovering fields on " + pClazz.getName());

    final Map<String, Object> result = new HashMap<String, Object>();

    Class<?> current = pClazz;
    do {//ww w . j  a  v  a2  s.c  o  m
        final Field[] fields = current.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            final String fname = fields[i].getName();
            if (pMatcher.matches(fname)) {
                pIndexer.put(result, fname, fields[i]);

                log.debug("discovered field " + fname + " -> " + fields[i]);
            }
        }
        current = current.getSuperclass();
    } while (current != null);

    return result;
}

From source file:Main.java

/**
 * Returns a {@code Class} object that identifies the
 * declared class for the field represented by the given {@code String name} parameter inside
 * the invoked {@code Class<?> clazz} parameter.
 *
 * @param clazz the {@code Class} object whose declared fields to be
 *       checked for a certain field.//from   www.  j a  v  a  2  s. c  om
 * @param name the field name as {@code String} to be
 *       compared with {@link Field#getName()}
 * @return the {@code Class} object representing the type of given field name.
 *
 * @see {@link Class#getDeclaredFields()}
 * @see {@link Field#getType()}
 */
public static Class<?> getFieldClass(Class<?> clazz, String name) {
    if (clazz == null || name == null || name.isEmpty()) {
        return null;
    }

    Class<?> propertyClass = null;

    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        if (field.getName().equalsIgnoreCase(name)) {
            propertyClass = field.getType();
            break;
        }
    }

    return propertyClass;
}