/** * @return The type of the of an object. */ public static Class<?> getType(Object object) { return WhiteboxImpl.getType(object); }
/** * Gets the types. * * @param arguments the arguments * @return the types */ private static Class<?>[] getTypes(Object[] arguments) { Class<?>[] classes = new Class<?>[arguments.length]; for (int i = 0; i < arguments.length; i++) { classes[i] = getType(arguments[i]); } return classes; }
/** * @return The type of the of an object. */ public static Class<?> getType(Object object) { return WhiteboxImpl.getType(object); }
/** * Gets the types. * * @param arguments * the arguments * @return the types */ private static Class<?>[] getTypes(Object[] arguments) { Class<?>[] classes = new Class<?>[arguments.length]; for (int i = 0; i < arguments.length; i++) { classes[i] = getType(arguments[i]); } return classes; }
/** * Convert argument types to primitive. * * @param paramTypes * the param types * @param arguments * the arguments * @return the class[] */ private static Class<?>[] convertArgumentTypesToPrimitive(Class<?>[] paramTypes, Object[] arguments) { Class<?>[] types = new Class<?>[arguments.length]; for (int i = 0; i < arguments.length; i++) { Class<?> argumentType = null; if (arguments[i] == null) { argumentType = paramTypes[i]; } else { argumentType = getType(arguments[i]); } Class<?> primitiveWrapperType = PrimitiveWrapper.getPrimitiveFromWrapperType(argumentType); if (primitiveWrapperType == null) { types[i] = argumentType; } else { types[i] = primitiveWrapperType; } } return types; }
/** * Find field in hierarchy. * * @param object the object * @param strategy the strategy * @return the field */ private static Field findFieldInHierarchy(Object object, FieldMatcherStrategy strategy) { assertObjectInGetInternalStateIsNotNull(object); return findSingleFieldUsingStrategy(strategy, object, true, getType(object)); }
/** * Find field in hierarchy. * * @param object * the object * @param strategy * the strategy * @return the field */ private static Field findFieldInHierarchy(Object object, FieldMatcherStrategy strategy) { assertObjectInGetInternalStateIsNotNull(object); return findSingleFieldUsingStrategy(strategy, object, true, getType(object)); }
/** * Get all instance fields for a particular object. It returns all fields * regardless of the field modifier and regardless of where in the class * hierarchy a field is located. * * @param object * The object whose instance fields to get. * @return All instance fields in the hierarchy. All fields are set to * accessible */ public static Set<Field> getAllInstanceFields(Object object) { return findAllFieldsUsingStrategy(new AllFieldsMatcherStrategy(), object, true, getType(object)); }
/** * Get an array of {@link Field}'s that matches the supplied list of field * names. Both instance and static fields are taken into account. * * @param clazz * The class that should contain the fields. * @param fieldNames * Names of the fields that will be returned. * @return An array of Field's. May be of length 0 but not . */ public static Field[] getFields(Class<?> clazz, String... fieldNames) { final List<Field> fields = new LinkedList<Field>(); for (Field field : getAllFields(clazz)) { for (String fieldName : fieldNames) { if (field.getName().equals(fieldName)) { fields.add(field); } } } final Field[] fieldArray = fields.toArray(new Field[fields.size()]); if (fieldArray.length == 0) { throw new FieldNotFoundException(String.format( "No fields matching the name(s) %s were found in the class hierarchy of %s.", concatenateStrings(fieldNames), getType(clazz))); } return fieldArray; }
/** * Get all fields assignable from a particular type. This method traverses * the class hierarchy when checking for the type. * * @param object * The object to look for type. Note that if're you're passing an * object only instance fields are checked, passing a class will * only check static fields. * @param type * The type to look for. * @return A set of all fields of the particular type. */ public static Set<Field> getFieldsOfType(Object object, Class<?> type) { return findAllFieldsUsingStrategy(new AssignableFromFieldTypeMatcherStrategy(type), object, true, getType(object)); }
/** * Get all fields annotated with a particular annotation. This method * traverses the class hierarchy when checking for the annotation. * * @param object The object to look for annotations. Note that if're you're * passing an object only instance fields are checked, passing a * class will only check static fields. * @param annotationTypes The annotation types to look for * @return A set of all fields containing the particular annotation(s). * @since 1.3 */ public static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation>[] annotationTypes) { return findAllFieldsUsingStrategy(new FieldAnnotationMatcherStrategy(annotationTypes), object, true, getType(object)); }
/** * Get all fields assignable from a particular type. This method traverses * the class hierarchy when checking for the type. * * @param object The object to look for type. Note that if're you're passing an * object only instance fields are checked, passing a class will * only check static fields. * @param type The type to look for. * @return A set of all fields of the particular type. */ public static Set<Field> getFieldsOfType(Object object, Class<?> type) { return findAllFieldsUsingStrategy(new AssignableFromFieldTypeMatcherStrategy(type), object, true, getType(object)); }
/** * Get all fields annotated with a particular annotation. This method * traverses the class hierarchy when checking for the annotation. * * @param object * The object to look for annotations. Note that if're you're * passing an object only instance fields are checked, passing a * class will only check static fields. * @param annotationTypes * The annotation types to look for * @return A set of all fields containing the particular annotation(s). * @since 1.3 */ public static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation>[] annotationTypes) { return findAllFieldsUsingStrategy(new FieldAnnotationMatcherStrategy(annotationTypes), object, true, getType(object)); }
/** * Get the type of an object and convert it to primitive if the type has a * primitive counter-part. E.g. if object is an instance of * <code>java.lang.Integer</code> this method will return * <code>int.class</code>. * * @param object * The object whose type to get. * @return the type as primitive if wrapped */ private static Class<?> getTypeAsPrimitiveIfWrapped(Object object) { if (object != null) { final Class<?> firstArgumentType = getType(object); final Class<?> firstArgumentTypeAsPrimitive = PrimitiveWrapper.hasPrimitiveCounterPart(firstArgumentType) ? PrimitiveWrapper .getPrimitiveFromWrapperType(firstArgumentType) : firstArgumentType; return firstArgumentTypeAsPrimitive; } return null; }
/** * Get the type of an object and convert it to primitive if the type has a * primitive counter-part. E.g. if object is an instance of * {@code java.lang.Integer} this method will return * {@code int.class}. * * @param object The object whose type to get. * @return the type as primitive if wrapped */ static Class<?> getTypeAsPrimitiveIfWrapped(Object object) { if (object != null) { final Class<?> firstArgumentType = getType(object); final Class<?> firstArgumentTypeAsPrimitive = PrimitiveWrapper.hasPrimitiveCounterPart(firstArgumentType) ? PrimitiveWrapper .getPrimitiveFromWrapperType(firstArgumentType) : firstArgumentType; return firstArgumentTypeAsPrimitive; } return null; }
public static void setInternalStateFromContext(Object object, Object context, FieldMatchingStrategy strategy) { if (isClass(context)) { copyState(object, getType(context), strategy); } else { copyState(object, context, strategy); } }
/** * Set the value of a field using reflection at at specific place in the * class hierarchy (<tt>where</tt>). This first field assignable to * <tt>object</tt> will then be set to <tt>value</tt>. * * @param object the object to modify * @param value the new value of the field * @param where the class in the hierarchy where the field is defined */ public static void setInternalState(Object object, Object value, Class<?> where) { setField(object, value, findField(object, new AssignableFromFieldTypeMatcherStrategy(getType(value)), where)); }
public static void setInternalStateFromContext(Object object, Object context, FieldMatchingStrategy strategy) { if (isClass(context)) { copyState(object, getType(context), strategy); } else { copyState(object, context, strategy); } }
/** * Set the value of a field using reflection at at specific place in the * class hierarchy (<tt>where</tt>). This first field assignable to * <tt>object</tt> will then be set to <tt>value</tt>. * * @param object * the object to modify * @param value * the new value of the field * @param where * the class in the hierarchy where the field is defined */ public static void setInternalState(Object object, Object value, Class<?> where) { setField(object, value, findField(object, new AssignableFromFieldTypeMatcherStrategy(getType(value)), where)); }
/** * Set the value of a field using reflection. This method will traverse the * super class hierarchy until the first field assignable to the * <tt>value</tt> type is found. The <tt>value</tt> (or * <tt>additionaValues</tt> if present) will then be assigned to this field. * * @param object the object to modify * @param value the new value of the field * @param additionalValues Additional values to set on the object */ public static void setInternalState(Object object, Object value, Object... additionalValues) { setField(object, value, findFieldInHierarchy(object, new AssignableFromFieldTypeMatcherStrategy(getType(value)))); if (additionalValues != null && additionalValues.length > 0) { for (Object additionalValue : additionalValues) { setField( object, additionalValue, findFieldInHierarchy(object, new AssignableFromFieldTypeMatcherStrategy( getType(additionalValue)))); } } }