Class FieldUtils
java.lang.Object
org.apache.felix.scr.impl.inject.field.FieldUtils
Utility methods for handling field injection.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classReturn type forsearchField(Class, String, ComponentLogger) -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static FieldUtils.FieldSearchResultaccept(Class<?> componentClass, Field field, boolean acceptPrivate, boolean acceptPackage, ComponentLogger logger) This method checks whether the found field is acceptable (= usable) for the component instance.private static FieldUtils.FieldSearchResultgetField(Class<?> componentClass, Class<?> targetClass, String fieldName, boolean acceptPrivate, boolean acceptPackage, ComponentLogger logger) Finds the field namedfieldNamefield in the giventargetClass.static FieldUtils.FieldSearchResultsearchField(Class<?> componentClass, String fieldName, ComponentLogger logger) Searches the field namedfieldNamein the giventargetClass.private static voidsetAccessible(Field field) Make the field accessiblestatic voidsetField(Field f, Object component, Object value, ComponentLogger logger) Set the field, type etc.static StringReturn a string representation of the field
-
Constructor Details
-
FieldUtils
public FieldUtils()
-
-
Method Details
-
searchField
public static FieldUtils.FieldSearchResult searchField(Class<?> componentClass, String fieldName, ComponentLogger logger) Searches the field namedfieldNamein the giventargetClass. If the target class has no acceptable field the class hierarchy is traversed until a field is found or the root of the class hierarchy is reached without finding a field.If an unexpected error occurs while searching,
nullis returned. In all other cases aFieldSearchResultis returned. If no field is found,FieldSearchResult#fieldis set tonull. If the field is found, but not usable (e.g. due to visibility restrictions),FieldSearchResult#usableis set tofalse.- Parameters:
fieldName- The name of the fieldlogger- A logger to log errors / problemstargetClass- The class of the component- Returns:
- A field search result or
nullif an unexpected error occurred.
-
getField
private static FieldUtils.FieldSearchResult getField(Class<?> componentClass, Class<?> targetClass, String fieldName, boolean acceptPrivate, boolean acceptPackage, ComponentLogger logger) throws InvocationTargetException Finds the field namedfieldNamefield in the giventargetClass. If the target class has no acceptable field the class hierarchy is traversed until a field is found or the root of the class hierarchy is reached without finding a field.- Parameters:
componentClass- The class of the component (for logging)targetClass- The class in which to look for the methodfieldName- The name of the fieldacceptPrivate-trueif private fields should be considered.acceptPackage-trueif package private fields should be considered.logger- For error logging- Returns:
- If the field is found a
FieldSearchResultis returned. If the field is not found,nullis returned. - Throws:
InvocationTargetException- If an unexpected Throwable is caught trying to find the requested field.
-
accept
private static FieldUtils.FieldSearchResult accept(Class<?> componentClass, Field field, boolean acceptPrivate, boolean acceptPackage, ComponentLogger logger) This method checks whether the found field is acceptable (= usable) for the component instance. It returns aFieldSearchResultwith the usable flag set totrueif the field is not static and- Is public or protected
- Is private and
acceptPrivateistrue - Is package private and
acceptPackageistrue
If the field is usable, this method makes the field accessible.
If the field is not usable, a
FieldSearchResultwith the usable flag set tofalseis returned and an error is logged with the provided logger.- Parameters:
componentClass- The class of the component,.field- The field to checkacceptPrivate- Whether a private field is acceptableacceptPackage- Whether a package private field is acceptablelogger- The logger for error logging- Returns:
- A field search result, this is never
null
-
toString
-
setAccessible
Make the field accessible- Parameters:
field- The field
-
setField
Set the field, type etc.- Parameters:
f- The fieldcomponent- The component instancevalue- The value to setlogger- The logger
-