Package uk.co.jemos.podam.api
Class AbstractClassInfoStrategy
- java.lang.Object
-
- uk.co.jemos.podam.api.AbstractClassInfoStrategy
-
- All Implemented Interfaces:
ClassAttributeApprover,ClassInfoStrategy
- Direct Known Subclasses:
DefaultClassInfoStrategy
public abstract class AbstractClassInfoStrategy extends Object implements ClassInfoStrategy, ClassAttributeApprover
Default abstract implementation of aClassInfoStrategyThis default implementation is based on field introspection.
- Since:
- 5.1.0
- Author:
- daivanov
-
-
Field Summary
Fields Modifier and Type Field Description private Set<Class<? extends Annotation>>excludedAnnotationsSet of annotations, which mark fields to be skipped from populating.private Map<Class<?>,Set<String>>excludedFieldsSet of fields, which mark fields to be skipped from populating.private Map<Class<?>,List<Method>>extraMethodsSet of extra methods to execute.private PatternGETTER_PATTERNprivate static org.slf4j.LoggerLOGThe application logger.private PatternSETTER_PATTERN
-
Constructor Summary
Constructors Constructor Description AbstractClassInfoStrategy()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AbstractClassInfoStrategyaddExcludedAnnotation(Class<? extends Annotation> annotation)Adds the specifiedAnnotationto set of excluded annotations, if it is not already present.AbstractClassInfoStrategyaddExcludedField(Class<?> pojoClass, String fieldName)Adds the specified field to set of excluded fields, if it is not already present.AbstractClassInfoStrategyaddExtraMethod(Class<?> pojoClass, String methodName, Class<?>... methodArgs)It adds an extra method to executebooleanapprove(ClassAttribute attribute)Override this method to select or reject class attributesprivate booleancontainsAnyAnnotation(Field field, Set<Class<? extends Annotation>> annotations)Checks if the given field has any one of the annotationsprivate booleancontainsAnyAnnotation(Method method, Set<Class<? extends Annotation>> annotations)Checks if the given method has any one of the annotationsprotected StringextractFieldNameFromMethod(String methodName, Pattern pattern)Given a accessor's name, it extracts the field name, according to JavaBean standardsprotected voidfindPojoAttributes(Class<?> clazz, Map<String,ClassAttribute> attributeMap, Set<Class<? extends Annotation>> excludeAnnotations, Set<String> excludedFields)Given a class and a set of class declared fields it returns a map of setters, getters and fields defined for this classClassAttributeApprovergetClassAttributeApprover(Class<?> pojoClass)ClassInfogetClassInfo(Class<?> pojoClass)ClassInfogetClassInfo(Class<?> clazz, Set<Class<? extends Annotation>> excludeFieldAnnotations, Set<String> excludedFields, ClassAttributeApprover attributeApprover, Collection<Method> extraMethods)It returns aClassInfoobject for the given classSet<Class<? extends Annotation>>getExcludedAnnotations()IdentifiesAnnotations for fields to be skipped.Set<String>getExcludedFields(Class<?> pojoClass)Identifies fields to be skipped.Collection<Method>getExtraMethods(Class<?> pojoClass)It returns the collection of extra methods to execute.protected PatterngetGetterPattern()Defines a regular expression for a getter's nameprotected PatterngetSetterPattern()Defines a regular expression for a setters nameAbstractClassInfoStrategyremoveExcludedAnnotation(Class<? extends Annotation> annotation)Removes the specifiedAnnotationfrom set of excluded annotations.AbstractClassInfoStrategyremoveExcludedField(Class<?> pojoClass, String fieldName)Removes the field name from set of excluded fields.
-
-
-
Field Detail
-
GETTER_PATTERN
private final Pattern GETTER_PATTERN
-
SETTER_PATTERN
private final Pattern SETTER_PATTERN
-
LOG
private static final org.slf4j.Logger LOG
The application logger.
-
excludedAnnotations
private final Set<Class<? extends Annotation>> excludedAnnotations
Set of annotations, which mark fields to be skipped from populating.
-
excludedFields
private Map<Class<?>,Set<String>> excludedFields
Set of fields, which mark fields to be skipped from populating.
-
-
Method Detail
-
addExcludedAnnotation
public AbstractClassInfoStrategy addExcludedAnnotation(Class<? extends Annotation> annotation)
Adds the specifiedAnnotationto set of excluded annotations, if it is not already present.- Parameters:
annotation- the annotation to use as an exclusion mark- Returns:
- itself
-
addExtraMethod
public AbstractClassInfoStrategy addExtraMethod(Class<?> pojoClass, String methodName, Class<?>... methodArgs) throws NoSuchMethodException, SecurityException
It adds an extra method to execute- Parameters:
pojoClass- The pojo class where to execute the methodmethodName- name to be scheduled for executionmethodArgs- list of method arguments- Returns:
- this object
- Throws:
SecurityException- If a security exception occurred while retrieving the methodNoSuchMethodException- If pojoClass doesn't declare the required method- Since:
- 5.3.0
-
removeExcludedAnnotation
public AbstractClassInfoStrategy removeExcludedAnnotation(Class<? extends Annotation> annotation)
Removes the specifiedAnnotationfrom set of excluded annotations.- Parameters:
annotation- the annotation used as an exclusion mark- Returns:
- itself
-
addExcludedField
public AbstractClassInfoStrategy addExcludedField(Class<?> pojoClass, String fieldName)
Adds the specified field to set of excluded fields, if it is not already present.- Parameters:
pojoClass- a class for which fields should be skippedfieldName- the field name to use as an exclusion mark- Returns:
- itself
-
removeExcludedField
public AbstractClassInfoStrategy removeExcludedField(Class<?> pojoClass, String fieldName)
Removes the field name from set of excluded fields.- Parameters:
pojoClass- a class for which fields should be skippedfieldName- the field name used as an exlusion mark- Returns:
- itself
-
approve
public boolean approve(ClassAttribute attribute)
Override this method to select or reject class attributes- Specified by:
approvein interfaceClassAttributeApprover- Parameters:
attribute- class attribute to analyze for further processing or skipping- Returns:
- true, if attribute should be kept, false, if it should be skipped
-
getExcludedAnnotations
public Set<Class<? extends Annotation>> getExcludedAnnotations()
IdentifiesAnnotations for fields to be skipped.Should return set of annotations, which will be treated as notion for
PodamFactoryto skip production of these particular fields.- Specified by:
getExcludedAnnotationsin interfaceClassInfoStrategy- Returns:
- set of annotations, which mark fields to be skipped from populating.
-
getExcludedFields
public Set<String> getExcludedFields(Class<?> pojoClass)
Identifies fields to be skipped.Should return set of field names as case-sensitive string, which will be treated as notion for
PodamFactoryto skip production of these particular fields.- Specified by:
getExcludedFieldsin interfaceClassInfoStrategy- Parameters:
pojoClass- a class for which fields should be skipped- Returns:
- set of field name, which mark fields to be skipped from populating.
-
getClassInfo
public ClassInfo getClassInfo(Class<?> pojoClass)
- Specified by:
getClassInfoin interfaceClassInfoStrategy- Parameters:
pojoClass- a class to introspect and fetch attributes- Returns:
- information about class internal structure
ClassInfo
-
getClassAttributeApprover
public ClassAttributeApprover getClassAttributeApprover(Class<?> pojoClass)
- Specified by:
getClassAttributeApproverin interfaceClassInfoStrategy
-
getExtraMethods
public Collection<Method> getExtraMethods(Class<?> pojoClass)
Description copied from interface:ClassInfoStrategyIt returns the collection of extra methods to execute.- Specified by:
getExtraMethodsin interfaceClassInfoStrategy- Parameters:
pojoClass- The pojo class- Returns:
- the collection of extra methods to execute.
-
getClassInfo
public ClassInfo getClassInfo(Class<?> clazz, Set<Class<? extends Annotation>> excludeFieldAnnotations, Set<String> excludedFields, ClassAttributeApprover attributeApprover, Collection<Method> extraMethods)
It returns aClassInfoobject for the given class- Parameters:
clazz- The class to retrieve info fromexcludeFieldAnnotations- the fields marked with any of these annotations will not be included in the class infoexcludedFields- the fields matching the given names will not be included in the class infoattributeApprover- aClassAttributeApproverimplementation, which defines which attributes to skip and which to processextraMethods- extra methods to call after object initialization- Returns:
- a
ClassInfoobject for the given class
-
containsAnyAnnotation
private boolean containsAnyAnnotation(Method method, Set<Class<? extends Annotation>> annotations)
Checks if the given method has any one of the annotations- Parameters:
method- the method to check forannotations- the set of annotations to look for in the field- Returns:
- true if the field is marked with any of the given annotations
-
containsAnyAnnotation
private boolean containsAnyAnnotation(Field field, Set<Class<? extends Annotation>> annotations)
Checks if the given field has any one of the annotations- Parameters:
field- the field to check forannotations- the set of annotations to look for in the field- Returns:
- true if the field is marked with any of the given annotations
-
findPojoAttributes
protected void findPojoAttributes(Class<?> clazz, Map<String,ClassAttribute> attributeMap, Set<Class<? extends Annotation>> excludeAnnotations, Set<String> excludedFields)
Given a class and a set of class declared fields it returns a map of setters, getters and fields defined for this class
-
extractFieldNameFromMethod
protected String extractFieldNameFromMethod(String methodName, Pattern pattern)
Given a accessor's name, it extracts the field name, according to JavaBean standardsThis method, given a accessor method's name, it returns the corresponding attribute name. For example: given setIntField the method would return intField. given getIntField the method would return intField; given isBoolField the method would return boolField.The correctness of the return value depends on the adherence to JavaBean standards.
- Parameters:
methodName- The accessor method from which the field name is requiredpattern- The pattern to strip from the method name- Returns:
- The field name corresponding to the setter
-
getGetterPattern
protected Pattern getGetterPattern()
Defines a regular expression for a getter's name- Returns:
- a compiled pattern for the getter's name
-
getSetterPattern
protected Pattern getSetterPattern()
Defines a regular expression for a setters name- Returns:
- a compiled pattern for the setter's name
-
-