use of cn.taketoday.core.reflect.PropertyAccessor in project today-infrastructure by TAKETODAY.
the class ReflectionUtilsTest method testNewPropertyAccessor.
@Test
public void testNewPropertyAccessor() throws NoSuchFieldException {
final PropertyBean propertyBean = new PropertyBean();
final Field declaredField = PropertyBean.class.getDeclaredField("static_pro");
final PropertyAccessor staticProAccessor = PropertyAccessor.fromField(declaredField);
assertEquals(staticProAccessor.get(null), 0);
staticProAccessor.set(null, 2);
assertEquals(staticProAccessor.get(null), 2);
final Field boolField = PropertyBean.class.getDeclaredField("bool");
final PropertyAccessor boolAccessor = PropertyAccessor.fromField(boolField);
assertEquals(boolAccessor.get(propertyBean), false);
boolAccessor.set(propertyBean, true);
assertEquals(boolAccessor.get(propertyBean), true);
final Field finalProField = PropertyBean.class.getDeclaredField("finalPro");
final PropertyAccessor finalProAccessor = PropertyAccessor.fromField(finalProField);
assertEquals(finalProAccessor.get(propertyBean), 10L);
try {
finalProAccessor.set(null, 101);
} catch (ReflectionException e) {
assertEquals(finalProAccessor.get(propertyBean), 10L);
}
final Field staticFinalProField = PropertyBean.class.getDeclaredField("staticFinalPro");
final PropertyAccessor staticFinalProAccessor = PropertyAccessor.fromField(staticFinalProField);
assertEquals(staticFinalProAccessor.get(propertyBean), (short) 100);
try {
staticFinalProAccessor.set(null, 101);
} catch (ReflectionException e) {
assertEquals(staticFinalProAccessor.get(propertyBean), (short) 100);
}
}
Aggregations