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:de.javakaffee.kryoserializers.KryoTest.java

private static void assertEqualDeclaredFields(final Class<? extends Object> clazz, final Object one,
        final Object another, final Map<Object, Object> alreadyChecked)
        throws Exception, IllegalAccessException {
    for (final Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);/*from   ww w .j  ava2s  .  c  o m*/
        if (!Modifier.isTransient(field.getModifiers())) {
            assertDeepEquals(field.get(one), field.get(another), alreadyChecked);
        }
    }
}

From source file:com.vmware.qe.framework.datadriven.impl.injector.DataInjectorUsingAnnotations.java

@Override
public void inject(Object test, HierarchicalConfiguration data, HierarchicalConfiguration context) {
    Class<? extends Object> testClass = test.getClass();
    for (Field field : testClass.getDeclaredFields()) {
        for (Annotation annotation : field.getAnnotations()) {
            if (annotation.annotationType().isAssignableFrom(Data.class)) {
            }/*from w  ww.  ja v  a2  s.  co m*/
            Data dataAnnotation = (Data) annotation;
            field.setAccessible(true);
            try {
                if (dataAnnotation.name().equals("")) {
                    field.set(test, data);
                } else {
                    String value = data.getString(dataAnnotation.name());
                    field.set(test, value);
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                throw new DDException("Error in injecting data to field with name = " + field.getName(), e);
            }
        }
    }
}

From source file:com.github.helenusdriver.commons.lang3.reflect.ReflectionUtils.java

/**
 * Gets the declared members of the given type from the specified class.
 *
 * @author paouelle//from  w  ww.  ja  v  a2 s  .  c  o  m
 *
 * @param <T> the type of member
 *
 * @param  type the type of members to retrieve
 * @param  clazz the class from which to retrieve the members
 * @return the non-<code>null</code> members of the given type from the
 *         specified class
 * @throws NullPointerException if <code>type</code> or
 *         <code>clazz</code> is <code>null</code>
 * @throws IllegalArgumentException if <code>type</code> is not
 *         {@link Field}, {@link Method}, or {@link Constructor}
 */
@SuppressWarnings("unchecked")
public static <T extends Member> T[] getDeclaredMembers(Class<T> type, Class<?> clazz) {
    org.apache.commons.lang3.Validate.notNull(type, "invalid null member type");
    org.apache.commons.lang3.Validate.notNull(clazz, "invalid null class");
    if (type == Field.class) {
        return (T[]) clazz.getDeclaredFields();
    } else if (type == Method.class) {
        return (T[]) clazz.getDeclaredMethods();
    } else if (type == Constructor.class) {
        return (T[]) clazz.getDeclaredConstructors();
    } else {
        throw new IllegalArgumentException("invalid member class: " + type.getName());
    }
}

From source file:org.jasig.cas.util.annotation.AbstractAnnotationBeanPostProcessor.java

private final void addDeclaredFields(final Class<?> clazz, final List<Field> fields) {
    fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
}

From source file:mercury.DTO.java

@Override
public JSONObject toJSONObject(Properties properties) {
    JSONObject json = new JSONObject();
    try {//ww w .  j  av  a  2 s. c  o m
        Class clazz = this.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            String name = field.getName();
            Method getter = clazz.getDeclaredMethod("get" + StringUtils.capitalize(name));
            if (getter != null) {
                Object value = getter.invoke(this);
                if (value != null) {
                    if (value instanceof Collection) {
                        Collection col = (Collection) value;

                        for (Object item : col) {
                            if (item == null) {
                                continue;
                            }

                            if (item instanceof DTO) {
                                json.append(name, ((DTO) item).toJSONObject(null));
                            } else if (item instanceof String) {
                                json.append(name, ((String) item).trim());
                            } else {
                                json.append(name, item);
                            }
                        }
                    } else if (value instanceof DTO) {
                        json.putOpt(name, ((DTO) value).toJSONObject(properties));
                    } else if (value instanceof String) {
                        json.putOpt(name, ((String) value).trim());
                    } else {
                        json.putOpt(name, value);
                    }
                }
            }
        }
    } catch (Exception e) {
    }
    return json;
}

From source file:com.arvato.thoroughly.service.tmall.impl.APIServiceImpl.java

private String startSearch(final Class<?> classType, String prefix) {
    final Field[] objectFields = classType.getDeclaredFields();

    final StringBuffer sb = new StringBuffer();
    String fieldsSeparatedByCommas = null;

    prefix = prefix == null ? "" : prefix + ".";

    for (final Field item : objectFields) {
        if ("serialVersionUID".equals(item.getName())) {
            continue;
        }//from   www  .jav a  2 s. com
        final ApiListField apiListField = item.getAnnotation(ApiListField.class);
        if (apiListField != null) {
            continue;
        }
        final ApiField annotation = item.getAnnotation(ApiField.class);

        sb.append(prefix + annotation.value() + ",");
    }
    if (StringUtils.isNotEmpty(sb)) {
        fieldsSeparatedByCommas = sb.toString().substring(0, sb.toString().length() - 1);

        fields.put(classType.getTypeName(), fieldsSeparatedByCommas);
    }
    return fieldsSeparatedByCommas;
}

From source file:mercury.RootJsonHandler.java

protected <T extends DTO> T populateDtoFromJson(String jsonData, T dto) {
    try {//from w w w  . j  av  a2 s.  c  o m
        JSONObject json = new JSONObject(jsonData);
        Class clazz = dto.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            try {
                String name = field.getName();
                Class fieldClass = field.getType();
                Object value = null;
                if (fieldClass.equals(String.class)) {
                    value = json.has(name) ? json.getString(name) : null;
                } else if (fieldClass.equals(Integer.class)) {
                    try {
                        value = json.has(name) ? json.getInt(name) : null;
                    } catch (Exception e) {
                        value = -1;
                    }
                } else if (fieldClass.equals(Float.class)) {
                    String sValue = json.has(name) ? json.getString(name).replaceAll(",", ".") : null;
                    value = sValue != null ? Float.valueOf(sValue) : null;
                } else if (fieldClass.equals(Date.class)) {
                    value = json.has(name) ? json.getString(name) : null;
                    value = value != null ? this.dateFormatter.parse((String) value) : null;
                }

                if (value == null) {
                    continue;
                }

                Method setter = clazz.getDeclaredMethod("set" + StringUtils.capitalize(name), fieldClass);
                if (setter != null) {
                    setter.invoke(dto, value);
                }
            } catch (Exception e) {
                continue;
            }
        }
    } catch (JSONException je) {
    }
    return dto;
}

From source file:com.comcast.video.dawg.service.house.AbstractDawgService.java

/**
 * Helper method to get a DBObject with all the fields of an object and then
 * specify the _id of the object/*from   ww w . j a v  a 2s . co m*/
 * @param obj The object to get a DBObject for
 * @param idField The name of the field on the object that will server as the id
 * @return
 */
public DBObject getDBObject(Object obj, String idField) {
    try {
        DBObject dbObj = new BasicDBObject();
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String fName = field.getName();
            Object fVal = field.get(obj);
            dbObj.put(fName, fVal);
            if (fName.equals(idField)) {
                dbObj.put("_id", fVal);
            }
        }

        return dbObj;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ar.com.zauber.commons.web.uri.UriJspFunctionsTest.java

/** recargar jspFunctions en cada test*/
@Before/*w  w  w. j  a v a2  s. co m*/
public final void resetJspFunctions() throws Exception {
    Class<UriJspFunctions> a = UriJspFunctions.class;
    Field[] fs = a.getDeclaredFields();
    for (int i = 0; i < fs.length; i++) {
        Field field = fs[i];
        if (field.getName().contains("initialized")) {
            field.setAccessible(true);
            ((AtomicBoolean) field.get(null)).set(false);
            field.setAccessible(false);
        }
    }

}

From source file:org.apache.aries.blueprint.plugin.model.Bean.java

private void resolveProperties(Matcher matcher, Class<?> curClass) {
    for (Field field : curClass.getDeclaredFields()) {
        Property prop = Property.create(matcher, field);
        if (prop != null) {
            properties.add(prop);/* w  w  w.  j  a v a2 s .  c  om*/
        }
    }
}