use of cn.taketoday.core.reflect.PropertyAccessor in project today-framework by TAKETODAY.
the class TypeDescriptorTests method property.
@Test
public void property() throws Exception {
final BeanProperty property = BeanProperty.valueOf(getClass(), "property");
TypeDescriptor desc = property.getTypeDescriptor();
assertThat(desc.getType()).isEqualTo(Map.class);
assertThat(desc.getMapKeyDescriptor().getElementDescriptor().getType()).isEqualTo(Integer.class);
assertThat(desc.getMapValueDescriptor().getElementDescriptor().getType()).isEqualTo(Long.class);
PropertyAccessor propertyAccessor = property.obtainAccessor();
Method writeMethod = propertyAccessor.getWriteMethod();
Method readMethod = propertyAccessor.getReadMethod();
assertThat(writeMethod).isNotNull();
assertThat(readMethod).isNotNull();
assertThat(readMethod.getAnnotation(MethodAnnotation1.class)).isNotNull();
assertThat(writeMethod.getAnnotation(MethodAnnotation2.class)).isNotNull();
assertThat(desc.getAnnotation(MethodAnnotation3.class)).isNotNull();
}
use of cn.taketoday.core.reflect.PropertyAccessor in project today-framework by TAKETODAY.
the class BenchmarkTests method testProperty.
@Test
void testProperty() throws IllegalAccessException {
Field field = ReflectionUtils.findField(PropertyTestBean.class, "value");
PropertyAccessor propertyAccessor = PropertyAccessor.fromField(field);
field.trySetAccessible();
PropertyTestBean propertyTestBean = new PropertyTestBean();
propertyAccessor.set(propertyTestBean, "TODAY");
Object value = propertyAccessor.get(propertyTestBean);
assertThat(value).isEqualTo(propertyTestBean.value).isEqualTo("TODAY");
// set
long start = System.currentTimeMillis();
int times = 1_0000_0000;
for (int i = 0; i < times; i++) {
field.set(propertyTestBean, "reflect");
}
System.out.println("reflect set used: " + (System.currentTimeMillis() - start) + "ms");
start = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
propertyAccessor.set(propertyTestBean, "propertyAccessor");
}
System.out.println("Accessor set used: " + (System.currentTimeMillis() - start) + "ms");
// get
start = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
field.get(propertyTestBean);
}
System.out.println("reflect get used: " + (System.currentTimeMillis() - start) + "ms");
start = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
propertyAccessor.get(propertyTestBean);
}
System.out.println("Accessor get used: " + (System.currentTimeMillis() - start) + "ms");
}
use of cn.taketoday.core.reflect.PropertyAccessor in project today-framework 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);
}
}
use of cn.taketoday.core.reflect.PropertyAccessor in project today-infrastructure by TAKETODAY.
the class TypeDescriptorTests method property.
@Test
public void property() throws Exception {
final BeanProperty property = BeanProperty.valueOf(getClass(), "property");
TypeDescriptor desc = property.getTypeDescriptor();
assertThat(desc.getType()).isEqualTo(Map.class);
assertThat(desc.getMapKeyDescriptor().getElementDescriptor().getType()).isEqualTo(Integer.class);
assertThat(desc.getMapValueDescriptor().getElementDescriptor().getType()).isEqualTo(Long.class);
PropertyAccessor propertyAccessor = property.obtainAccessor();
Method writeMethod = propertyAccessor.getWriteMethod();
Method readMethod = propertyAccessor.getReadMethod();
assertThat(writeMethod).isNotNull();
assertThat(readMethod).isNotNull();
assertThat(readMethod.getAnnotation(MethodAnnotation1.class)).isNotNull();
assertThat(writeMethod.getAnnotation(MethodAnnotation2.class)).isNotNull();
assertThat(desc.getAnnotation(MethodAnnotation3.class)).isNotNull();
}
use of cn.taketoday.core.reflect.PropertyAccessor in project today-infrastructure by TAKETODAY.
the class BenchmarkTests method testProperty.
@Test
void testProperty() throws IllegalAccessException {
Field field = ReflectionUtils.findField(PropertyTestBean.class, "value");
PropertyAccessor propertyAccessor = PropertyAccessor.fromField(field);
field.trySetAccessible();
PropertyTestBean propertyTestBean = new PropertyTestBean();
propertyAccessor.set(propertyTestBean, "TODAY");
Object value = propertyAccessor.get(propertyTestBean);
assertThat(value).isEqualTo(propertyTestBean.value).isEqualTo("TODAY");
// set
long start = System.currentTimeMillis();
int times = 1_0000_0000;
for (int i = 0; i < times; i++) {
field.set(propertyTestBean, "reflect");
}
System.out.println("reflect set used: " + (System.currentTimeMillis() - start) + "ms");
start = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
propertyAccessor.set(propertyTestBean, "propertyAccessor");
}
System.out.println("Accessor set used: " + (System.currentTimeMillis() - start) + "ms");
// get
start = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
field.get(propertyTestBean);
}
System.out.println("reflect get used: " + (System.currentTimeMillis() - start) + "ms");
start = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
propertyAccessor.get(propertyTestBean);
}
System.out.println("Accessor get used: " + (System.currentTimeMillis() - start) + "ms");
}
Aggregations