/** * Gets all fields of the given class and its parents (if any). * * @param cls * the {@link Class} to query * @return an array of Fields (possibly empty). * @throws IllegalArgumentException * if the class is {@code null} * @since 3.2 */ public static Field[] getAllFields(final Class<?> cls) { final List<Field> allFieldsList = getAllFieldsList(cls); return allFieldsList.toArray(new Field[allFieldsList.size()]); }
/** * Gets all fields of the given class and its parents (if any) that are annotated with the given annotation. * @param cls * the {@link Class} to query * @param annotationCls * the {@link Annotation} that must be present on a field to be matched * @return a list of Fields (possibly empty). * @throws IllegalArgumentException * if the class or annotation are {@code null} * @since 3.4 */ public static List<Field> getFieldsListWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) { Validate.isTrue(annotationCls != null, "The annotation class must not be null"); final List<Field> allFields = getAllFieldsList(cls); final List<Field> annotatedFields = new ArrayList<>(); for (final Field field : allFields) { if (field.getAnnotation(annotationCls) != null) { annotatedFields.add(field); } } return annotatedFields; }
@Test public void testGetAllFieldsList() { assertEquals(0, FieldUtils.getAllFieldsList(Object.class).size()); final List<Field> fieldsNumber = Arrays.asList(Number.class.getDeclaredFields()); assertEquals(fieldsNumber, FieldUtils.getAllFieldsList(Number.class)); final List<Field> fieldsInteger = Arrays.asList(Integer.class.getDeclaredFields()); final List<Field> allFieldsInteger = new ArrayList<>(fieldsInteger); allFieldsInteger.addAll(fieldsNumber); assertEquals(allFieldsInteger, FieldUtils.getAllFieldsList(Integer.class)); final List<Field> allFields = FieldUtils.getAllFieldsList(PublicChild.class); // Under Jacoco,0.8.1 and Java 10, the field count is 7. int expected = 5; for (Field field : allFields) { if (field.getName().equals(JACOCO_DATA_FIELD_NAME)) { expected++; } } assertEquals(allFields.toString(), expected, allFields.size()); }
List<AnnotatedFieldProperty> names = new ArrayList<>(); List<Field> fields = FieldUtils.getFieldsListWithAnnotation(clazz, annotation); List<Field> allFields = FieldUtils.getAllFieldsList(clazz); for (Field field : fields) { AnnotatedFieldProperty p = addFieldProperty(clazz, names, field);
/** * Gets all fields of the given class and its parents (if any). * * @param cls * the {@link Class} to query * @return an array of Fields (possibly empty). * @throws IllegalArgumentException * if the class is {@code null} * @since 3.2 */ public static Field[] getAllFields(final Class<?> cls) { final List<Field> allFieldsList = getAllFieldsList(cls); return allFieldsList.toArray(new Field[allFieldsList.size()]); }
/** * Gets all fields of the given class and its parents (if any). * * @param cls * the {@link Class} to query * @return an array of Fields (possibly empty). * @throws IllegalArgumentException * if the class is {@code null} * @since 3.2 */ public static Field[] getAllFields(final Class<?> cls) { final List<Field> allFieldsList = getAllFieldsList(cls); return allFieldsList.toArray(new Field[allFieldsList.size()]); }
/** * Gets all fields of the given class and its parents (if any). * * @param cls * the {@link Class} to query * @return an array of Fields (possibly empty). * @throws IllegalArgumentException * if the class is {@code null} * @since 3.2 */ public static Field[] getAllFields(final Class<?> cls) { final List<Field> allFieldsList = getAllFieldsList(cls); return allFieldsList.toArray(new Field[allFieldsList.size()]); }
/** * Gets all fields of the given class and its parents (if any) that are annotated with the given annotation. * @param cls * the {@link Class} to query * @param annotationCls * the {@link Annotation} that must be present on a field to be matched * @return a list of Fields (possibly empty). * @throws IllegalArgumentException * if the class or annotation are {@code null} * @since 3.4 */ public static List<Field> getFieldsListWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) { Validate.isTrue(annotationCls != null, "The annotation class must not be null"); final List<Field> allFields = getAllFieldsList(cls); final List<Field> annotatedFields = new ArrayList<>(); for (final Field field : allFields) { if (field.getAnnotation(annotationCls) != null) { annotatedFields.add(field); } } return annotatedFields; }
/** * Gets all fields of the given class and its parents (if any) that are annotated with the given annotation. * @param cls * the {@link Class} to query * @param annotationCls * the {@link Annotation} that must be present on a field to be matched * @return a list of Fields (possibly empty). * @throws IllegalArgumentException * if the class or annotation are {@code null} * @since 3.4 */ public static List<Field> getFieldsListWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) { Validate.isTrue(annotationCls != null, "The annotation class must not be null"); final List<Field> allFields = getAllFieldsList(cls); final List<Field> annotatedFields = new ArrayList<>(); for (final Field field : allFields) { if (field.getAnnotation(annotationCls) != null) { annotatedFields.add(field); } } return annotatedFields; }
/** * Gets all fields of the given class and its parents (if any) that are annotated with the given annotation. * @param cls * the {@link Class} to query * @param annotationCls * the {@link Annotation} that must be present on a field to be matched * @return a list of Fields (possibly empty). * @throws IllegalArgumentException * if the class or annotation are {@code null} * @since 3.4 */ public static List<Field> getFieldsListWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) { Validate.isTrue(annotationCls != null, "The annotation class must not be null"); final List<Field> allFields = getAllFieldsList(cls); final List<Field> annotatedFields = new ArrayList<>(); for (final Field field : allFields) { if (field.getAnnotation(annotationCls) != null) { annotatedFields.add(field); } } return annotatedFields; }
public static void serializeToMap(Map<String, Object> map, Object sourceObject) { if (sourceObject == null) { return; } FieldUtils.getAllFieldsList(sourceObject.getClass()).stream() .filter(IntrospectionUtil::isSimple) .forEach(f -> { try { Object value = FieldUtils.readField(f, sourceObject, true); if (value != null) { map.put(f.getName(), value); } } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ValueMapSerializer.class.getName()).log(Level.SEVERE, null, ex); } }); }
public ResourceModelMetaData(Class<?> modelType) { FieldMetadataCreator fc = new FieldMetadataCreator(modelType); getAllFieldsList(modelType).forEach(fc); MethodMetadataCreator mc = new MethodMetadataCreator(); methodsOf(modelType).stream().filter(m -> !m.getDeclaringClass().isInterface() || isAbstract(m.getModifiers())).forEach(mc); this.mappableFields = fc.getMappableFields(); this.afterMappingMethods = mc.getAfterMappingMethods(); this.typeName = modelType.getName(); }
List<AnnotatedFieldProperty> names = new ArrayList<>(); List<Field> fields = FieldUtils.getFieldsListWithAnnotation(clazz, annotation); List<Field> allFields = FieldUtils.getAllFieldsList(clazz); for (Field field : fields) { AnnotatedFieldProperty p = addFieldProperty(clazz, names, field);