package org.springframework.data.util;
import static org.springframework.util.ReflectionUtils.*;
import java.lang.reflect.Field;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.lang.Nullable;
public class DirectFieldAccessFallbackBeanWrapper extends BeanWrapperImpl {
public DirectFieldAccessFallbackBeanWrapper(Object entity) {
super(entity);
}
public DirectFieldAccessFallbackBeanWrapper(Class<?> type) {
super(type);
}
@Override
@Nullable
public Object getPropertyValue(String propertyName) {
try {
return super.getPropertyValue(propertyName);
} catch (NotReadablePropertyException e) {
Field field = findField(getWrappedClass(), propertyName);
if (field == null) {
throw new NotReadablePropertyException(getWrappedClass(), propertyName,
"Could not find field for property during fallback access!");
}
makeAccessible(field);
return getField(field, getWrappedInstance());
}
}
@Override
public void setPropertyValue(String propertyName, @Nullable Object value) {
try {
super.setPropertyValue(propertyName, value);
} catch (NotWritablePropertyException e) {
Field field = findField(getWrappedClass(), propertyName);
if (field == null) {
throw new NotWritablePropertyException(getWrappedClass(), propertyName,
"Could not find field for property during fallback access!", e);
}
makeAccessible(field);
setField(field, getWrappedInstance(), value);
}
}
}