Java Reflection Field Find findFields(final Class clazz, final Predicate filter)

Here you can find the source of findFields(final Class clazz, final Predicate filter)

Description

find Fields

License

Open Source License

Declaration

private static List<Field> findFields(final Class<?> clazz, final Predicate<Field> filter) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.annotation.Nullable;

import java.lang.reflect.Field;

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

import java.util.function.Predicate;

public class Main {
    private static List<Field> findFields(final Class<?> clazz, final Predicate<Field> filter) {
        final List<Field> fields = new ArrayList<>();
        collectFields(clazz, filter, fields);
        fields.forEach(field -> field.setAccessible(true));
        return fields;
    }// ww  w  . j av a2  s. c o m

    private static void collectFields(@Nullable final Class<?> clazz, final Predicate<Field> filter,
            final List<Field> output) {
        if (clazz == null)
            return;
        Arrays.stream(clazz.getDeclaredFields()).filter(filter).forEach(output::add);
        collectFields(clazz.getSuperclass(), filter, output);
    }
}

Related

  1. findFieldOfBean(Object bean, String fieldName)
  2. findFieldOfTypeInClass(final Class source, final Class type)
  3. findFieldRecursively(Class c, String fieldName)
  4. findFields(Class c, boolean allowTransient, int max, Iterable> fieldClassesToFind, Iterable> exceptClasses)
  5. findFields(Class type)
  6. findFieldsAnnotatedWith(Class annotation, Class parentClass)
  7. findFieldsAnnotatedWith(final Class type, final Class annotation)
  8. findFieldsOfClass(Class target, Object o, String path, Logger log, Set done)
  9. findFieldToInject(Class target, String name, Class source)