com.json.ignore.util.AnnotationUtil Maven / Gradle / Ivy
package com.json.ignore.util;
import com.json.ignore.filter.field.FieldFilterSetting;
import com.json.ignore.filter.field.FieldFilterSettings;
import com.json.ignore.filter.file.FileConfig;
import com.json.ignore.filter.strategy.SessionStrategies;
import com.json.ignore.filter.strategy.SessionStrategy;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Annotation util class
*
* This is util class used to help find annotations in class or method
*/
public final class AnnotationUtil {
protected AnnotationUtil() {
}
/**
* Search for specified type list of annotation
*
* @param method {@link Method} object's method which may have annotations
* @param annotationClass {@link Annotation} name of annotation to search
* @param {@link Annotation} generic class
* @return {@link Annotation} list of found annotations if found, else an array of length zero
*/
public static T[] getDeclaredAnnotations(Method method, Class annotationClass) {
return method.getDeclaredAnnotationsByType(annotationClass);
}
/**
* Search for specified type of annotation
*
* @param method {@link Method} object's method which may have annotation
* @param annotationClass {@link Annotation} name of annotation to search
* @param {@link Annotation} generic class
* @return {@link Annotation} annotation if found, else null
*/
public static T getDeclaredAnnotation(Method method, Class annotationClass) {
return method.getDeclaredAnnotation(annotationClass);
}
/**
* Check for annotations id declared in method
*
* @param method {@link Method} object's method which may have annotation
* @param annotationClasses {@link Annotation} name of annotation to search
* @param {@link Annotation}
* @return {@link Annotation} if one of specified annotation is found, else returns false
*/
public static boolean isAnnotationExists(Method method, List> annotationClasses) {
if (annotationClasses != null)
for (Class clazz : annotationClasses) {
if (getDeclaredAnnotations(method, clazz).length > 0)
return true;
}
return false;
}
/**
* Search for {@link FieldFilterSetting} in method
*
* @param method {@link Method} object's method which may have annotation
* @return list of {@link FieldFilterSetting} if this type of annotation declared in method
*/
public static FieldFilterSetting[] getSettingAnnotations(Method method) {
FieldFilterSettings settings = AnnotationUtil.getDeclaredAnnotation(method, FieldFilterSettings.class);
if (settings != null) {
return settings.value();
} else
return AnnotationUtil.getDeclaredAnnotations(method, FieldFilterSetting.class);
}
/**
* Search for {@link SessionStrategy} in method
*
* @param method {@link Method} object's method which may have annotation
* @return list of {@link SessionStrategy} if this type of annotation declared in method
*/
public static SessionStrategy[] getStrategyAnnotations(Method method) {
SessionStrategies strategies = AnnotationUtil.getDeclaredAnnotation(method, SessionStrategies.class);
if (strategies != null) {
return strategies.value();
} else
return AnnotationUtil.getDeclaredAnnotations(method, SessionStrategy.class);
}
/**
* Convert strategy class in Map
*
* @param strategy {@link FileConfig.Strategy} filter strategy
* @return {@link HashMap} map of fields which should be filtered/excluded
*/
public static Map> getStrategyFields(FileConfig.Strategy strategy) {
Map> fields = new HashMap<>();
if (strategy != null) {
strategy.getFilters().forEach(filter -> {
Class clazz = FileUtil.getClassByName(filter.getClassName());
List items;
if (fields.containsKey(clazz)) {
items = fields.get(clazz);
} else
items = new ArrayList<>();
filter.getFields().forEach(field -> {
//filter duplicates of field names
if (!items.contains(field.getName()))
items.add(field.getName());
});
fields.put(clazz, items);
});
}
return fields;
}
}