org.springframework.util.ReflectionUtils.FieldCallback Java Examples

The following examples show how to use org.springframework.util.ReflectionUtils.FieldCallback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MyBatisUtil.java    From phone with Apache License 2.0 6 votes vote down vote up
/**
 * 解析对象为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;
}
 
Example #2
Source File: DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl.java    From spring-data-dynamodb with Apache License 2.0 6 votes vote down vote up
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 + "!");

}
 
Example #3
Source File: DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl.java    From spring-data-dynamodb with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #4
Source File: FieldAndGetterReflectionEntityInformation.java    From spring-data-dynamodb with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example #5
Source File: AnnotatedElementAccessor.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #6
Source File: DataAccessAnnotationProcessor.java    From tutorials with MIT License 4 votes vote down vote up
protected void scanDataAccessAnnotation(Object bean, String beanName) {
    Class<?> managedBeanClass = bean.getClass();
    FieldCallback fcb = new DataAccessFieldCallback(configurableListableBeanFactory, bean);
    ReflectionUtils.doWithFields(managedBeanClass, fcb);
}