| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ReflectionUtils |
|
| 0.0;0 | ||||
| ReflectionUtils$1 |
|
| 0.0;0 | ||||
| ReflectionUtils$2 |
|
| 0.0;0 | ||||
| ReflectionUtils$FieldProperty |
|
| 0.0;0 |
| 1 | /* | |
| 2 | This file is part of Combean. | |
| 3 | ||
| 4 | Combean is free software; you can redistribute it and/or modify | |
| 5 | it under the terms of the GNU General Public License as published by | |
| 6 | the Free Software Foundation; either version 2 of the License, or | |
| 7 | (at your option) any later version. | |
| 8 | ||
| 9 | Combean is distributed in the hope that it will be useful, | |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | GNU General Public License for more details. | |
| 13 | ||
| 14 | You should have received a copy of the GNU General Public License | |
| 15 | along with Combean; if not, write to the Free Software | |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
| 17 | */ | |
| 18 | /* | |
| 19 | * Created on 19.03.2005 | |
| 20 | * | |
| 21 | */ | |
| 22 | package net.sourceforge.combean.util; | |
| 23 | ||
| 24 | import java.lang.reflect.Field; | |
| 25 | ||
| 26 | /** | |
| 27 | * Utility class containing methods for convenient use of java.lang.reflect. | |
| 28 | * | |
| 29 | * @author schickin | |
| 30 | * | |
| 31 | */ | |
| 32 | 0 | public final class ReflectionUtils { |
| 33 | ||
| 34 | /** | |
| 35 | * Retrieve all fields of a class, declared in the class itself OR | |
| 36 | * in a superclass | |
| 37 | * | |
| 38 | * @param clazz class for which all fields shall be retrieved | |
| 39 | * @return the fields declared in clazz or in a superclass of clazz | |
| 40 | */ | |
| 41 | public static Field[] getAllFields(Class clazz) { | |
| 42 | 6 | return getAllFieldsMatching(clazz, new FieldProperty() { |
| 43 | public boolean fieldMatches(Field e) { | |
| 44 | 54 | return true; |
| 45 | } | |
| 46 | }); | |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Similar to getAllFields(Class) but returns only those fields which | |
| 51 | * are assignable to pattern | |
| 52 | * | |
| 53 | * @param clazz class for which all matching fields shall be retrieved | |
| 54 | * @param pattern the class to which matching fields shall be assignable | |
| 55 | * @return the matching fields declared in clazz or in a superclass of clazz | |
| 56 | */ | |
| 57 | public static Field[] getAllFieldsAssignableTo(final Class clazz, | |
| 58 | final Class<?> pattern) { | |
| 59 | 384 | return getAllFieldsMatching(clazz, new FieldProperty() { |
| 60 | public boolean fieldMatches(Field e) { | |
| 61 | 7020 | return pattern.isAssignableFrom(e.getType()); |
| 62 | } | |
| 63 | }); | |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Similar to getAllFields(Class) but returns only those fields for which | |
| 68 | * the given FieldProperty returns true | |
| 69 | * | |
| 70 | * @param clazz class for which all matching fields shall be retrieved | |
| 71 | * @param prop the FieldProperty which determines whether a field shall | |
| 72 | * be returned or not | |
| 73 | * @return the matching fields declared in clazz or in a superclass of clazz | |
| 74 | */ | |
| 75 | public static Field[] getAllFieldsMatching(Class clazz, FieldProperty prop) { | |
| 76 | 390 | int numFields = getNumAllFields(clazz, prop); |
| 77 | 390 | Field result[] = new Field[numFields]; |
| 78 | 390 | retrieveAllFields(clazz, result, 0, prop); |
| 79 | 390 | return result; |
| 80 | } | |
| 81 | ||
| 82 | 0 | private interface FieldProperty { |
| 83 | public boolean fieldMatches(Field e); | |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Private helper method for getAllFieldsMatching which returns the | |
| 88 | * number of matching fields. Used for properly allocating memory for | |
| 89 | * the result of getAllFieldsMatching | |
| 90 | * | |
| 91 | * @param clazz | |
| 92 | * @param prop | |
| 93 | * @return the number of matching fields | |
| 94 | */ | |
| 95 | private static int getNumAllFields(Class clazz, FieldProperty prop) { | |
| 96 | 1308 | int result = 0; |
| 97 | 1308 | Field[] declaredFields = clazz.getDeclaredFields(); |
| 98 | 4845 | for (int i = 0; i < declaredFields.length; i++) { |
| 99 | 3537 | if (prop.fieldMatches(declaredFields[i])) { |
| 100 | 1314 | result++; |
| 101 | } | |
| 102 | } | |
| 103 | 1308 | Class superClass = clazz.getSuperclass(); |
| 104 | 1308 | if (superClass != Object.class) { |
| 105 | 918 | result += getNumAllFields(superClass, prop); |
| 106 | } | |
| 107 | 1308 | return result; |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * Private helper method for getAllFieldsMatching which recursively | |
| 112 | * collects the matching fields. | |
| 113 | * | |
| 114 | * @param clazz | |
| 115 | * @param result the array where the result shall be stored | |
| 116 | * @param startPos the position in result where the next matching | |
| 117 | * field shall be stored | |
| 118 | * @param prop | |
| 119 | */ | |
| 120 | private static void retrieveAllFields(Class clazz, | |
| 121 | Field[] result, int startPos, FieldProperty prop) { | |
| 122 | 1308 | Field[] declaredFields = clazz.getDeclaredFields(); |
| 123 | 4845 | for (int i = 0; i < declaredFields.length; i++) { |
| 124 | 3537 | if (prop.fieldMatches(declaredFields[i])) { |
| 125 | 1314 | result[startPos++] = declaredFields[i]; |
| 126 | } | |
| 127 | } | |
| 128 | 1308 | Class superClass = clazz.getSuperclass(); |
| 129 | 1308 | if (superClass != Object.class) { |
| 130 | 918 | retrieveAllFields(superClass, result, startPos, prop); |
| 131 | } | |
| 132 | 1308 | } |
| 133 | } |