下面列出了怎么用org.springframework.util.ReflectionUtils.FieldCallback的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 解析对象为Model
* 自定义filter
* @param obj
* @param parseSuper 是否解析父类
* @return
*/
public static List<Model> parseByObject(Object obj,boolean parseSuper,FieldFilter ff){
List<Model> list = new ArrayList<>();
if (obj==null) {
return list;
}
//解析Field
FieldCallback fc = new FieldCallback() {
@Override
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
if (ff != null && !ff.matches(field)) {
return;
}
Model m = parseByField(obj, field);
if (m!=null) {
list.add(m);
}
}
};
if (parseSuper) {
ReflectionUtil.doWithFields(obj.getClass(),fc);
}else{
ReflectionUtil.doWithLocalFields(obj.getClass(),fc);
}
return list;
}
public DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl(final Class<T> domainType) {
super(domainType);
this.hashAndRangeKeyMethodExtractor = new DynamoDBHashAndRangeKeyMethodExtractorImpl<T>(getJavaType());
ReflectionUtils.doWithMethods(domainType, new MethodCallback() {
public void doWith(Method method) {
if (method.getAnnotation(DynamoDBHashKey.class) != null) {
String setterMethodName = toSetterMethodNameFromAccessorMethod(method);
if (setterMethodName != null) {
hashKeySetterMethod = ReflectionUtils.findMethod(domainType, setterMethodName, method.getReturnType());
}
}
}
});
ReflectionUtils.doWithFields(domainType, new FieldCallback() {
public void doWith(Field field) {
if (field.getAnnotation(DynamoDBHashKey.class) != null) {
hashKeyField = ReflectionUtils.findField(domainType, field.getName());
}
}
});
Assert.isTrue(hashKeySetterMethod != null || hashKeyField != null, "Unable to find hash key field or setter method on " + domainType + "!");
Assert.isTrue(hashKeySetterMethod == null || hashKeyField == null, "Found both hash key field and setter method on " + domainType + "!");
}
@Override
public Set<String> getIndexRangeKeyPropertyNames() {
final Set<String> propertyNames = new HashSet<String>();
ReflectionUtils.doWithMethods(getJavaType(), new MethodCallback() {
public void doWith(Method method) {
if (method.getAnnotation(DynamoDBIndexRangeKey.class) != null) {
if ((method.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName() != null && method
.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName().trim().length() > 0)
|| (method.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames() != null && method
.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames().length > 0)) {
propertyNames.add(getPropertyNameForAccessorMethod(method));
}
}
}
});
ReflectionUtils.doWithFields(getJavaType(), new FieldCallback() {
public void doWith(Field field) {
if (field.getAnnotation(DynamoDBIndexRangeKey.class) != null) {
if ((field.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName() != null && field
.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName().trim().length() > 0)
|| (field.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames() != null && field
.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames().length > 0)) {
propertyNames.add(getPropertyNameForField(field));
}
}
}
});
return propertyNames;
}
/**
* Creates a new {@link FieldAndGetterReflectionEntityInformation} inspecting the
* given domain class for a getter carrying the given annotation.
*
* @param domainClass
* must not be {@literal null}.
* @param annotation
* must not be {@literal null}.
*/
public FieldAndGetterReflectionEntityInformation(Class<T> domainClass, final Class<? extends Annotation> annotation) {
super(domainClass);
Assert.notNull(annotation);
ReflectionUtils.doWithMethods(domainClass, new MethodCallback() {
public void doWith(Method method) {
if (method.getAnnotation(annotation) != null) {
FieldAndGetterReflectionEntityInformation.this.method = method;
return;
}
}
});
if (method == null)
{
ReflectionUtils.doWithFields(domainClass, new FieldCallback() {
public void doWith(Field field) {
if (field.getAnnotation(annotation) != null) {
FieldAndGetterReflectionEntityInformation.this.field = field;
return;
}
}
});
}
Assert.isTrue(this.method != null || this.field != null, String.format("No field or method annotated with %s found!", annotation.toString()));
Assert.isTrue(this.method == null || this.field == null, String.format("Both field and method annotated with %s found!", annotation.toString()));
if (method != null)
{
ReflectionUtils.makeAccessible(method);
}
}
/**
* Find annotated elements on types
* @param ann annotation to search
* @param clazz class to search on.
* @return List with annotated elements
*/
public static List<AnnotatedElement> findAnnotatedElements(final Class<? extends Annotation> annotationType, Class<?> clazz) {
final ArrayList<AnnotatedElement> elements = new ArrayList<AnnotatedElement>();
// Lookup fields
ReflectionUtils.doWithFields(clazz, new FieldCallback() {
@Override
public void doWith(Field field) throws IllegalArgumentException,
IllegalAccessException {
if (field.getAnnotation(annotationType) != null) {
elements.add(field);
}
}
});
// Lookup methods
ReflectionUtils.doWithMethods(clazz, new MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException,
IllegalAccessException {
if (org.springframework.core.annotation.AnnotationUtils.getAnnotation(method, annotationType) != null)
elements.add(method);
}
});
return elements;
}
protected void scanDataAccessAnnotation(Object bean, String beanName) {
Class<?> managedBeanClass = bean.getClass();
FieldCallback fcb = new DataAccessFieldCallback(configurableListableBeanFactory, bean);
ReflectionUtils.doWithFields(managedBeanClass, fcb);
}