Spring Data Core

org.springframework.data.util
Class ReflectionUtils

java.lang.Object
  extended by org.springframework.data.util.ReflectionUtils

public class ReflectionUtils
extends Object

Spring Data specific reflection utility methods and classes.

Since:
1.5
Author:
Oliver Gierke

Nested Class Summary
static class ReflectionUtils.AnnotationFieldFilter
          A ReflectionUtils.FieldFilter for a given annotation.
static interface ReflectionUtils.DescribedFieldFilter
          A ReflectionUtils.FieldFilter that has a description.
 
Constructor Summary
ReflectionUtils()
           
 
Method Summary
static
<T> T
createInstanceIfPresent(String classname, T defaultInstance)
          Creates an instance of the class with the given fully qualified name or returns the given default instance if the class cannot be loaded or instantiated.
static Field findField(Class<?> type, ReflectionUtils.DescribedFieldFilter filter)
          Finds the field matching the given ReflectionUtils.DescribedFieldFilter.
static Field findField(Class<?> type, ReflectionUtils.DescribedFieldFilter filter, boolean enforceUniqueness)
          Finds the field matching the given ReflectionUtils.DescribedFieldFilter.
static Field findField(Class<?> type, ReflectionUtils.FieldFilter filter)
          Finds the first field on the given class matching the given ReflectionUtils.DescribedFieldFilter.
static void setField(Field field, Object target, Object value)
          Sets the given field on the given object to the given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtils

public ReflectionUtils()
Method Detail

createInstanceIfPresent

public static <T> T createInstanceIfPresent(String classname,
                                            T defaultInstance)
Creates an instance of the class with the given fully qualified name or returns the given default instance if the class cannot be loaded or instantiated.

Parameters:
classname - the fully qualified class name to create an instance for.
defaultInstance - the instance to fall back to in case the given class cannot be loaded or instantiated.
Returns:

findField

public static Field findField(Class<?> type,
                              ReflectionUtils.FieldFilter filter)
Finds the first field on the given class matching the given ReflectionUtils.DescribedFieldFilter.

Parameters:
type - must not be null.
filter - must not be null.
Returns:
the field matching the filter or null in case no field could be found.

findField

public static Field findField(Class<?> type,
                              ReflectionUtils.DescribedFieldFilter filter)
Finds the field matching the given ReflectionUtils.DescribedFieldFilter. Will make sure there's only one field matching the filter.

Parameters:
type - must not be null.
filter - must not be null.
Returns:
the field matching the given ReflectionUtils.DescribedFieldFilter or null if none found.
Throws:
IllegalStateException - in case more than one matching field is found
See Also:
findField(Class, DescribedFieldFilter, boolean)

findField

public static Field findField(Class<?> type,
                              ReflectionUtils.DescribedFieldFilter filter,
                              boolean enforceUniqueness)
Finds the field matching the given ReflectionUtils.DescribedFieldFilter. Will make sure there's only one field matching the filter in case enforceUniqueness is true.

Parameters:
type - must not be null.
filter - must not be null.
enforceUniqueness - whether to enforce uniqueness of the field
Returns:
the field matching the given ReflectionUtils.DescribedFieldFilter or null if none found.
Throws:
IllegalStateException - if enforceUniqueness is true and more than one matching field is found

setField

public static void setField(Field field,
                            Object target,
                            Object value)
Sets the given field on the given object to the given value. Will make sure the given field is accessible.

Parameters:
field - must not be null.
target - must not be null.
value -

Spring Data Core

Copyright © 2011-2013-2013 Pivotal. All Rights Reserved.