org.springframework.data.util.ReflectionUtils Java Examples

The following examples show how to use org.springframework.data.util.ReflectionUtils. 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: PessimisticLockingInterceptor.java    From spring-content with Apache License 2.0 6 votes vote down vote up
private Object invokeWithIntecept(MethodInvocation invocation) throws Throwable {
    Object entity = invocation.getArguments()[0];
    Field idField = ReflectionUtils.findField(entity.getClass(), ID_FILTER);
    if (idField == null) {
        idField = ReflectionUtils.findField(entity.getClass(), DATA_ID_FILTER);
    }
    if (idField == null) {
        return invocation.proceed();
    }

    org.springframework.util.ReflectionUtils.makeAccessible(idField);
    Object id = org.springframework.util.ReflectionUtils.getField(idField, entity);

    if (locker.lockOwner(id) == null || locker.isLockOwner(id, auth.getAuthentication())) {
        return invocation.proceed();
    } else {
        throw new LockOwnerException("Not lock owner");
    }
}
 
Example #2
Source File: AnnotationAuditingMetadata.java    From spring-data-mybatis with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@link AnnotationAuditingMetadata} instance for the given type.
 * @param type must not be {@literal null}.
 */
private AnnotationAuditingMetadata(Class<?> type) {

	Assert.notNull(type, "Given type must not be null!");

	this.createdByField = Optional
			.ofNullable(ReflectionUtils.findField(type, CREATED_BY_FILTER));
	this.createdDateField = Optional
			.ofNullable(ReflectionUtils.findField(type, CREATED_DATE_FILTER));
	this.lastModifiedByField = Optional
			.ofNullable(ReflectionUtils.findField(type, LAST_MODIFIED_BY_FILTER));
	this.lastModifiedDateField = Optional
			.ofNullable(ReflectionUtils.findField(type, LAST_MODIFIED_DATE_FILTER));

	assertValidDateFieldType(createdDateField);
	assertValidDateFieldType(lastModifiedDateField);
}
 
Example #3
Source File: DefaultAuditableBeanWrapperFactory.java    From spring-data-mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<TemporalAccessor> getLastModifiedDate() {

	return getAsTemporalAccessor(
			metadata.getLastModifiedDateField().map(field -> {

				Object value = org.springframework.util.ReflectionUtils
						.getField(field, target);
				return value instanceof Optional
						? ((Optional<?>) value).orElse(null) : value;

			}), TemporalAccessor.class);
}
 
Example #4
Source File: DefaultAuditableBeanWrapperFactory.java    From spring-data-mybatis with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the given field to the given value if the field is not {@literal null}.
 * @param field
 * @param value
 */
private TemporalAccessor setDateField(Optional<Field> field,
		TemporalAccessor value) {

	field.ifPresent(it -> ReflectionUtils.setField(it, target,
			getDateValueToSet(value, it.getType(), it)));

	return value;
}
 
Example #5
Source File: SequenceOption.java    From HA-DB with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void purperSequence(MongoTemplate template, String collectionName, Object entity) {
	Optional<Field> noField = Optional.ofNullable(ReflectionUtils.findField(entity.getClass(), IDNO_FILTER));
	noField.ifPresent(f -> {
		try {
			String fieldName = noField.get().getName();
			Long increment = generate(template, collectionName, fieldName, 1l);
			f.setAccessible(true);
			f.set(entity, increment);
		} catch (Exception e) {
			e.printStackTrace();
		}
	});
}
 
Example #6
Source File: MongeezProperties.java    From mongeez-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
private MongoAuth instantiateMongoAuth(String username, String password, String authDb) {
    Optional<Constructor<?>> constructor = ReflectionUtils.findConstructor(MongoAuth.class, username, password, authDb);
    if (constructor.isPresent()) {
        return (MongoAuth) BeanUtils.instantiateClass(constructor.get(), username, password, authDb);
    }

    constructor = ReflectionUtils.findConstructor(MongoAuth.class, username, password);
    if (constructor.isPresent()) {
        return (MongoAuth) BeanUtils.instantiateClass(constructor.get(), username, password);
    }

    throw new IllegalStateException("No suitable constructor found to instantiate MongoAuth. " +
            "Are you using a supported Mongeez version?");
}
 
Example #7
Source File: DefaultAuditableBeanWrapperFactory.java    From spring-data-mybatis with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the given field to the given value if present.
 * @param field
 * @param value
 */
private <S> S setField(Optional<Field> field, S value) {

	field.ifPresent(it -> ReflectionUtils.setField(it, target, value));

	return value;
}