Java Reflection Field Find findFields(Class c, boolean allowTransient, int max, Iterable> fieldClassesToFind, Iterable> exceptClasses)

Here you can find the source of findFields(Class c, boolean allowTransient, int max, Iterable> fieldClassesToFind, Iterable> exceptClasses)

Description

Find in the given class and all its superclasses all non-static fields assignable to the specified classes to find, except of the specified 'except' classes, and returns such fields as a list.

License

Apache License

Parameter

Parameter Description
c the class to check
allowTransient whether to check transient fields
max the max number of the fields to return, or 0 if unlimited
fieldClassesToFind the field classes to find
exceptClasses the field classes to skip from search

Return

all fields of the supposed field classes

Declaration

private static List<Field> findFields(Class<?> c, boolean allowTransient, int max,
        Iterable<Class<?>> fieldClassesToFind, Iterable<Class<?>> exceptClasses) 

Method Source Code

//package com.java2s;
/*//  ww w  .j  a v a  2 s .co m
 * Copyright (C) 2016 Andrey Mogilev
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.*;
import java.util.*;

public class Main {
    /**
     * Find in the given class and all its superclasses all non-static fields assignable to the specified
     * classes to find, except of the specified 'except' classes, and returns such fields as a list.
     *
     * @param c the class to check
     * @param allowTransient whether to check transient fields
     * @param max the max number of the fields to return, or 0 if unlimited
     * @param fieldClassesToFind the field classes to find
     * @param exceptClasses the field classes to skip from search
     *
     * @return all fields of the supposed field classes
     */
    private static List<Field> findFields(Class<?> c, boolean allowTransient, int max,
            Iterable<Class<?>> fieldClassesToFind, Iterable<Class<?>> exceptClasses) {
        List<Field> result = new ArrayList<Field>(max > 0 && max < 10 ? max : 10);
        while (c != Object.class && c != null) {
            for (Field field : c.getDeclaredFields()) {
                if (Modifier.isStatic(field.getModifiers())) {
                    continue;
                }
                if (!allowTransient && Modifier.isTransient(field.getModifiers())) {
                    continue;
                }
                findClassesLoop: for (Class<?> fc : fieldClassesToFind) {
                    if (fc.isAssignableFrom(field.getType())) {
                        if (exceptClasses != null) {
                            for (Class ec : exceptClasses) {
                                if (ec == field.getType()) {
                                    continue findClassesLoop;
                                }
                            }
                        }
                        result.add(field);
                        break;
                    }
                }
            }
            c = c.getSuperclass();

            // TODO: maybe also check enclosing class for non-static local classes, but it complicates reflective gets
        }
        return result;
    }

    /**
     * Find in the given class and all its superclasses all fields assignable to the specified
     * classes to find, and returns such fields as a list.
     *
     * @param c the class to check
     * @param fieldClassesToFind the field classes to find
     * @param exceptClasses the field classes to skip from search
     * @return all fields of the supposed field classes
     */
    public static List<Field> findFields(Class<?> c, boolean allowTransient, Iterable<Class<?>> fieldClassesToFind,
            Iterable<Class<?>> exceptClasses) {
        return findFields(c, allowTransient, 0, fieldClassesToFind, exceptClasses);
    }
}

Related

  1. findFieldIncludeSuperclass(String fieldName, Class clazz)
  2. findFieldInternal(Class currentClass, Class annotation, Set fields)
  3. findFieldOfBean(Object bean, String fieldName)
  4. findFieldOfTypeInClass(final Class source, final Class type)
  5. findFieldRecursively(Class c, String fieldName)
  6. findFields(Class type)
  7. findFields(final Class clazz, final Predicate filter)
  8. findFieldsAnnotatedWith(Class annotation, Class parentClass)
  9. findFieldsAnnotatedWith(final Class type, final Class annotation)