Here you can find the source of findFields(Class> c, boolean allowTransient, int max, Iterable
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 |
private static List<Field> findFields(Class<?> c, boolean allowTransient, int max, Iterable<Class<?>> fieldClassesToFind, Iterable<Class<?>> exceptClasses)
//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); } }