use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project java-chassis by ServiceComb.
the class ProducerArgumentsMapperCreator method processBeanParameter.
@Override
protected void processBeanParameter(int producerParamIdx, Parameter producerParameter) {
ProducerBeanParamMapper mapper = new ProducerBeanParamMapper(providerMethod.getParameters()[producerParamIdx].getName(), producerParameter.getType());
JavaType producerType = TypeFactory.defaultInstance().constructType(producerParameter.getParameterizedType());
for (BeanPropertyDefinition propertyDefinition : serializationConfig.introspect(producerType).findProperties()) {
String parameterName = collectParameterName(providerMethod, propertyDefinition);
Integer swaggerIdx = findSwaggerParameterIndex(parameterName);
if (swaggerIdx == null) {
throw new IllegalStateException(String.format("failed to find producer parameter in contract, method=%s:%s, bean parameter name=%s.", providerMethod.getDeclaringClass().getName(), providerMethod.getName(), parameterName));
}
swaggerParameterTypes.put(parameterName, propertyDefinition.getPrimaryType());
mapper.addField(parameterName, LambdaMetafactoryUtils.createObjectSetter(propertyDefinition));
processedSwaggerParamters.add(parameterName);
}
mappers.add(mapper);
}
use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project alfresco-remote-api by Alfresco.
the class SerializerOfExecutionResult method serialize.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void serialize(ExecutionResult value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
SerializationConfig config = provider.getConfig();
Object rootObj = value.getRoot();
if (rootObj == null) {
provider.getDefaultNullValueSerializer().serialize(null, jgen, provider);
} else {
Class<?> cls = rootObj.getClass();
// create an untyped map, add the contents of the root + the embeds.
Map toBeSerialized = new HashMap();
BeanPropertiesFilter filter = value.getFilter();
if (filter == null)
filter = BeanPropertiesFilter.ALLOW_ALL;
if (Map.class.isAssignableFrom(cls)) {
// Its a map so
Map rootAsaMap = (Map) rootObj;
toBeSerialized.putAll(rootAsaMap);
} else {
JavaType classType = config.constructType(cls);
BeanDescription beanDesc = provider.getConfig().introspect(classType);
List<BeanPropertyDefinition> props = beanDesc.findProperties();
for (BeanPropertyDefinition beanProperty : props) {
if (beanProperty.couldSerialize() && filter.isAllowed(beanProperty.getName())) {
Object propertyValue = ResourceInspectorUtil.invokeMethod(beanProperty.getGetter().getAnnotated(), rootObj);
if (propertyValue != null) {
if ((propertyValue instanceof String)) {
if (((String) propertyValue).trim().length() > 0) {
toBeSerialized.put(beanProperty.getName(), propertyValue);
}
} else {
toBeSerialized.put(beanProperty.getName(), propertyValue);
}
}
}
}
}
// Add embedded
for (Entry<String, Object> embedded : value.getEmbedded().entrySet()) {
if (filter == null || filter.isAllowed(embedded.getKey())) {
toBeSerialized.put(embedded.getKey(), embedded.getValue());
}
}
// if its an embedded entity then render the properties (not as an "entry:")
if (value.isAnEmbeddedEntity()) {
jgen.writeObject(toBeSerialized);
} else {
jgen.writeStartObject();
jgen.writeObjectField("entry", toBeSerialized);
if (value.getRelated() != null && !value.getRelated().isEmpty()) {
jgen.writeObjectField("relations", value.getRelated());
}
jgen.writeEndObject();
}
}
}
use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project endpoints-java by cloudendpoints.
the class JacksonResourceSchemaProvider method getResourceSchema.
@Override
public ResourceSchema getResourceSchema(TypeToken<?> type, ApiConfig config) {
ResourceSchema schema = super.getResourceSchema(type, config);
if (schema != null) {
return schema;
}
ObjectMapper objectMapper = ObjectMapperUtil.createStandardObjectMapper(config.getSerializationConfig());
JavaType javaType = objectMapper.getTypeFactory().constructType(type.getRawType());
BeanDescription beanDescription = objectMapper.getSerializationConfig().introspect(javaType);
ResourceSchema.Builder schemaBuilder = ResourceSchema.builderForType(type.getRawType());
Set<String> genericDataFieldNames = getGenericDataFieldNames(type);
for (BeanPropertyDefinition definition : beanDescription.findProperties()) {
TypeToken<?> propertyType = getPropertyType(type, toMethod(definition.getGetter()), toMethod(definition.getSetter()), definition.getField(), config);
String name = definition.getName();
if (genericDataFieldNames == null || genericDataFieldNames.contains(name)) {
if (hasUnresolvedType(propertyType)) {
logger.atWarning().log("skipping field '%s' of type '%s' because it is unresolved.", name, propertyType);
continue;
}
if (propertyType != null) {
ResourcePropertySchema propertySchema = ResourcePropertySchema.of(propertyType);
propertySchema.setDescription(definition.getMetadata().getDescription());
schemaBuilder.addProperty(name, propertySchema);
} else {
logger.atWarning().log("No type found for property '%s' on class '%s'.", name, type);
}
} else {
logger.atFine().log("skipping field '%s' because it's not a Java client model field.", name);
}
}
return schemaBuilder.build();
}
use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project incubator-servicecomb-java-chassis by apache.
the class TestLambdaMetafactoryUtils method should_support_primitive_type.
@Test
public void should_support_primitive_type() {
Child child = new Child();
ObjectMapper mapper = JsonUtils.OBJ_MAPPER;
BeanDescription beanDescription = mapper.getSerializationConfig().introspect(mapper.constructType(Child.class));
List<BeanPropertyDefinition> properties = beanDescription.findProperties();
assertThat(properties).hasSize(2);
for (int idx = 0; idx < properties.size(); idx++) {
BeanPropertyDefinition property = properties.get(idx);
Setter<Object, Object> setter = createObjectSetter(property.getSetter().getAnnotated());
setter.set(child, idx);
Getter<Object, Object> getter = createObjectGetter(property.getGetter().getAnnotated());
Object value = getter.get(child);
assertThat(value).isEqualTo(idx);
}
}
use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project incubator-servicecomb-java-chassis by apache.
the class AbstractOperationGenerator method extractAggregatedParameterGenerators.
protected void extractAggregatedParameterGenerators(Map<String, List<Annotation>> methodAnnotationMap, java.lang.reflect.Parameter methodParameter) {
JavaType javaType = TypeFactory.defaultInstance().constructType(methodParameter.getParameterizedType());
BeanDescription beanDescription = Json.mapper().getSerializationConfig().introspect(javaType);
for (BeanPropertyDefinition propertyDefinition : beanDescription.findProperties()) {
if (!propertyDefinition.couldSerialize()) {
continue;
}
Annotation[] annotations = collectAnnotations(propertyDefinition);
ParameterGenerator propertyParameterGenerator = new ParameterGenerator(method, methodAnnotationMap, propertyDefinition.getName(), annotations, propertyDefinition.getPrimaryType());
parameterGenerators.add(propertyParameterGenerator);
}
}
Aggregations