下面列出了怎么用com.fasterxml.jackson.databind.util.SimpleBeanPropertyDefinition的API类实例代码及写法,或者点击链接到github查看源代码。
public void testSingleIntAccessorGeneration() throws Exception
{
Method method = Bean1.class.getDeclaredMethod("getX");
AnnotatedMethod annMethod = new AnnotatedMethod(null, method, null, null);
PropertyAccessorCollector coll = new PropertyAccessorCollector(Bean1.class);
BeanPropertyWriter bpw = new BeanPropertyWriter(SimpleBeanPropertyDefinition
.construct(MAPPER_CONFIG, annMethod, new PropertyName("x")),
annMethod, null,
null,
null, null, null,
false, null, null);
coll.addIntGetter(bpw);
BeanPropertyAccessor acc = coll.findAccessor(null);
Bean1 bean = new Bean1();
int value = acc.intGetter(bean, 0);
assertEquals(bean.getX(), value);
}
@Test
public void withConfigReturnsConfiguredWriter() {
// given
ObjectMapper objectMapper = new ObjectMapper();
SerializationConfig config = objectMapper.getSerializationConfig();
VirtualPropertiesWriter writer = spy(new VirtualPropertiesWriter(
new VirtualProperty[0],
mock(ValueResolver.class),
new VirtualPropertyFilter[0]
));
JavaType javaType = config.constructType(LogEvent.class);
AnnotatedClass annotatedClass = createTestAnnotatedClass(config, javaType);
SimpleBeanPropertyDefinition simpleBeanPropertyDefinition =
getTestBeanPropertyDefinition(config, javaType, annotatedClass);
VirtualPropertiesWriter result = writer.withConfig(
config,
annotatedClass,
simpleBeanPropertyDefinition,
config.constructType(VirtualProperty.class)
);
// then
assertArrayEquals(writer.virtualProperties, result.virtualProperties);
assertEquals(writer.valueResolver, result.valueResolver);
assertEquals(writer.filters, result.filters);
}
@Test
public void writerCreatedWithDeprecatedConstructorWritesGivenProperties() throws Exception {
// given
ObjectMapper objectMapper = new ObjectMapper();
SerializationConfig config = objectMapper.getSerializationConfig();
JavaType javaType = config.constructType(LogEvent.class);
AnnotatedClass annotatedClass = createTestAnnotatedClass(config, javaType);
SimpleBeanPropertyDefinition simpleBeanPropertyDefinition =
getTestBeanPropertyDefinition(config, javaType, annotatedClass);
String expectedName = UUID.randomUUID().toString();
String expectedValue = UUID.randomUUID().toString();
VirtualProperty virtualProperty = spy(createNonDynamicVirtualProperty(expectedName, expectedValue));
ValueResolver valueResolver = createTestValueResolver(virtualProperty, expectedValue);
VirtualPropertiesWriter writer = new VirtualPropertiesWriter(
simpleBeanPropertyDefinition,
new AnnotationCollector.OneAnnotation(
annotatedClass.getRawType(),
annotatedClass.getAnnotations().get(JsonAppend.class)
),
javaType,
new VirtualProperty[] { virtualProperty },
valueResolver
);
JsonGenerator jsonGenerator = mock(JsonGenerator.class);
// when
writer.serializeAsField(new Object(), jsonGenerator, mock(SerializerProvider.class));
// then
verify(jsonGenerator).writeFieldName(eq(expectedName));
verify(jsonGenerator).writeString(eq(expectedValue));
}
private SimpleBeanPropertyDefinition getTestBeanPropertyDefinition(SerializationConfig config, JavaType javaType, AnnotatedClass annotatedClass) {
return SimpleBeanPropertyDefinition.construct(
config,
new VirtualAnnotatedMember(
annotatedClass,
LogEvent.class,
"virtualProperties",
javaType
)
);
}
public void testDualIntAccessorGeneration() throws Exception
{
PropertyAccessorCollector coll = new PropertyAccessorCollector(Bean3.class);
String[] methodNames = new String[] {
"getX", "getY", "get3"
};
/*
public BeanPropertyWriter(BeanPropertyDefinition propDef,
AnnotatedMember member, Annotations contextAnnotations,
JavaType declaredType,
JsonSerializer<Object> ser, TypeSerializer typeSer, JavaType serType,
boolean suppressNulls, Object suppressableValue)
*/
for (String methodName : methodNames) {
Method method = Bean3.class.getDeclaredMethod(methodName);
AnnotatedMethod annMethod = new AnnotatedMethod(null, method, null, null);
// should we translate from method name to property name?
coll.addIntGetter(new BeanPropertyWriter(SimpleBeanPropertyDefinition
.construct(MAPPER_CONFIG, annMethod, new PropertyName(methodName)),
annMethod, null,
null,
null, null, null,
false, null, null));
}
BeanPropertyAccessor acc = coll.findAccessor(null);
Bean3 bean = new Bean3();
assertEquals(bean.getX(), acc.intGetter(bean, 0));
assertEquals(bean.getY(), acc.intGetter(bean, 1));
assertEquals(bean.get3(), acc.intGetter(bean, 2));
}
public void testLotsaIntAccessorGeneration() throws Exception
{
PropertyAccessorCollector coll = new PropertyAccessorCollector(BeanN.class);
String[] methodNames = new String[] {
"getX", "getY", "get3", "get4", "get5", "get6", "get7"
};
for (String methodName : methodNames) {
Method method = BeanN.class.getDeclaredMethod(methodName);
AnnotatedMethod annMethod = new AnnotatedMethod(null, method, null, null);
coll.addIntGetter(new BeanPropertyWriter(SimpleBeanPropertyDefinition.construct(
MAPPER_CONFIG, annMethod, new PropertyName(methodName)),
annMethod, null,
null,
null, null, null,
false, null, null));
}
BeanPropertyAccessor acc = coll.findAccessor(null);
BeanN bean = new BeanN();
assertEquals(bean.getX(), acc.intGetter(bean, 0));
assertEquals(bean.getY(), acc.intGetter(bean, 1));
assertEquals(bean.get3(), acc.intGetter(bean, 2));
assertEquals(bean.get4(), acc.intGetter(bean, 3));
assertEquals(bean.get5(), acc.intGetter(bean, 4));
assertEquals(bean.get6(), acc.intGetter(bean, 5));
assertEquals(bean.get7(), acc.intGetter(bean, 6));
}