use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project cxf by apache.
the class JaxRs2Extension method extractParameters.
@SuppressWarnings("deprecation")
@Override
public List<Parameter> extractParameters(final List<Annotation> annotations, final Type type, final Set<Type> typesToSkip, final Iterator<SwaggerExtension> chain) {
if (shouldIgnoreType(type, typesToSkip)) {
return new ArrayList<>();
}
List<Parameter> parameters = new ArrayList<>();
for (Annotation annotation : annotations) {
if (annotation instanceof MatrixParam) {
MatrixParam param = (MatrixParam) annotation;
MatrixParameter mp = new MatrixParameter().name(param.value());
Property schema = createProperty(type);
if (schema != null) {
mp.setProperty(schema);
}
applyBeanValidatorAnnotations(mp, annotations);
parameters.add(mp);
} else if (annotation instanceof BeanParam) {
// Use Jackson's logic for processing Beans
final BeanDescription beanDesc = mapper.getSerializationConfig().introspect(constructType(type));
final List<BeanPropertyDefinition> properties = beanDesc.findProperties();
for (final BeanPropertyDefinition propDef : properties) {
final AnnotatedField field = propDef.getField();
final AnnotatedMethod setter = propDef.getSetter();
final List<Annotation> paramAnnotations = new ArrayList<>();
final Iterator<SwaggerExtension> extensions = SwaggerExtensions.chain();
Type paramType = null;
// Gather the field's details
if (field != null) {
paramType = field.getAnnotated().getType();
for (final Annotation fieldAnnotation : field.annotations()) {
if (!paramAnnotations.contains(fieldAnnotation)) {
paramAnnotations.add(fieldAnnotation);
}
}
}
// Gather the setter's details but only the ones we need
if (setter != null) {
// Do not set the param class/type from the setter if the values are already identified
if (paramType == null && setter.getMember().getGenericParameterTypes() != null) {
paramType = setter.getMember().getGenericParameterTypes()[0];
}
for (final Annotation fieldAnnotation : setter.annotations()) {
if (!paramAnnotations.contains(fieldAnnotation)) {
paramAnnotations.add(fieldAnnotation);
}
}
}
// Re-process all Bean fields and let the default swagger-jaxrs processor do its thing
List<Parameter> extracted = extensions.next().extractParameters(paramAnnotations, paramType, typesToSkip, extensions);
// since downstream processors won't know how to introspect @BeanParam, process here
for (Parameter param : extracted) {
if (ParameterProcessor.applyAnnotations(null, param, paramType, paramAnnotations) != null) {
applyBeanValidatorAnnotations(param, paramAnnotations);
parameters.add(param);
}
}
}
}
}
// Only call down to the other items in the chain if no parameters were produced
if (parameters.isEmpty()) {
parameters = super.extractParameters(annotations, type, typesToSkip, chain);
}
return parameters;
}
use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project requery by requery.
the class DeserializerModifier method updateProperties.
@Override
public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config, BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs) {
List<BeanPropertyDefinition> definitions = super.updateProperties(config, beanDesc, propDefs);
List<BeanPropertyDefinition> remove = new ArrayList<>();
List<BeanPropertyDefinition> add = new ArrayList<>();
for (BeanPropertyDefinition definition : definitions) {
if (definition.hasGetter() && Collection.class.isAssignableFrom(definition.getGetter().getRawType())) {
if (definition instanceof POJOPropertyBuilder) {
POJOPropertyBuilder builder = (POJOPropertyBuilder) definition;
builder = new POJOPropertyBuilder(builder, builder.getFullName()) {
@Override
public boolean hasField() {
// forces the getter to be used on the collection
return false;
}
};
remove.add(definition);
add.add(builder);
}
}
}
definitions.removeAll(remove);
definitions.addAll(add);
return definitions;
}
use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project Gaffer by gchq.
the class JsonSerialisationUtil method getSerialisedFieldClasses.
/**
* Gets all the fields and their classes for a given class.
*
* @param className the class name to find the fields for.
* @return a map of field name to class name
*/
public static Map<String, String> getSerialisedFieldClasses(final String className) {
final Map<String, String> cachedResult = cache.get(className);
if (null != cachedResult) {
return cachedResult;
}
final Class<?> clazz;
try {
clazz = Class.forName(SimpleClassNameIdResolver.getClassName(className));
} catch (final Exception e) {
throw new IllegalArgumentException("Class name was not recognised: " + className, e);
}
final ObjectMapper mapper = new ObjectMapper();
final JavaType type = mapper.getTypeFactory().constructType(clazz);
final BeanDescription introspection = mapper.getSerializationConfig().introspect(type);
final Class<?> builder = introspection.findPOJOBuilder();
String buildMethodPrefix = "with";
if (null != builder) {
JsonPOJOBuilder anno = findAnnotation(builder, JsonPOJOBuilder.class);
if (null != anno) {
buildMethodPrefix = anno.withPrefix();
}
}
Constructor<?> creator = null;
for (final Constructor<?> constructor : type.getRawClass().getDeclaredConstructors()) {
final JsonCreator anno = constructor.getAnnotation(JsonCreator.class);
if (null != anno) {
creator = constructor;
break;
}
}
final List<BeanPropertyDefinition> properties = introspection.findProperties();
final Map<String, String> fieldMap = new HashMap<>();
for (final BeanPropertyDefinition property : properties) {
final String propName = property.getName();
final String propClass;
if ("class".equals(propName)) {
propClass = Class.class.getName();
} else {
Type genericType = null;
if (null != builder) {
final String methodName = buildMethodPrefix + propName;
Method matchedMethod = null;
for (final Method method : builder.getMethods()) {
if (methodName.equalsIgnoreCase(method.getName())) {
final Type[] params = method.getGenericParameterTypes();
if (null != params && 1 == params.length) {
final JsonSetter jsonSetter = method.getAnnotation(JsonSetter.class);
if (null != jsonSetter && propName.equals(jsonSetter.value())) {
matchedMethod = method;
break;
}
final JsonProperty jsonProperty = method.getAnnotation(JsonProperty.class);
if (null != jsonProperty && propName.equals(jsonProperty.value())) {
matchedMethod = method;
break;
}
if (null == matchedMethod) {
matchedMethod = method;
} else if (builder.equals(method.getReturnType())) {
// Checks for overridden methods
matchedMethod = method;
}
}
}
}
if (null != matchedMethod) {
genericType = matchedMethod.getGenericParameterTypes()[0];
}
}
if (null == genericType && null != creator) {
for (final Parameter parameter : creator.getParameters()) {
final JsonProperty anno = parameter.getAnnotation(JsonProperty.class);
if (null != anno && propName.equals(anno.value())) {
if (null != parameter.getParameterizedType()) {
genericType = parameter.getParameterizedType();
} else {
genericType = parameter.getType();
}
break;
}
}
}
if (null == genericType && null != property.getSetter() && null != property.getSetter().getGenericParameterTypes() && 1 == property.getSetter().getGenericParameterTypes().length) {
genericType = property.getSetter().getGenericParameterTypes()[0];
}
if (null != genericType && genericType instanceof Class && ((Class) genericType).isEnum()) {
genericType = String.class;
}
if (null == genericType) {
propClass = Object.class.getName();
} else {
propClass = getFieldTypeString(clazz, genericType);
}
}
fieldMap.put(propName, propClass);
}
final Map<String, Map<String, String>> newCache = new HashMap<>(cache);
newCache.put(className, Collections.unmodifiableMap(fieldMap));
cache = Collections.unmodifiableMap(newCache);
return fieldMap;
}
use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project Gaffer by gchq.
the class GraphConfigurationServiceV2 method getSerialisedFields.
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Need to wrap all runtime exceptions before they are given to the user")
@Override
public Response getSerialisedFields(final String className) {
final Class<?> clazz;
try {
clazz = Class.forName(SimpleClassNameIdResolver.getClassName(className));
} catch (final Exception e) {
throw new IllegalArgumentException("Class name was not recognised: " + className, e);
}
final ObjectMapper mapper = new ObjectMapper();
final JavaType type = mapper.getTypeFactory().constructType(clazz);
final BeanDescription introspection = mapper.getSerializationConfig().introspect(type);
final List<BeanPropertyDefinition> properties = introspection.findProperties();
final Set<String> fields = new HashSet<>();
for (final BeanPropertyDefinition property : properties) {
fields.add(property.getName());
}
return Response.ok(fields).header(GAFFER_MEDIA_TYPE_HEADER, GAFFER_MEDIA_TYPE).build();
}
use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project Gaffer by gchq.
the class GraphConfigurationService method getSerialisedFields.
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Need to wrap all runtime exceptions before they are given to the user")
@Override
public Set<String> getSerialisedFields(final String className) {
final Class<?> clazz;
try {
clazz = Class.forName(SimpleClassNameIdResolver.getClassName(className));
} catch (final Exception e) {
throw new IllegalArgumentException("Class name was not recognised: " + className, e);
}
final ObjectMapper mapper = new ObjectMapper();
final JavaType type = mapper.getTypeFactory().constructType(clazz);
final BeanDescription introspection = mapper.getSerializationConfig().introspect(type);
final List<BeanPropertyDefinition> properties = introspection.findProperties();
final Set<String> fields = new HashSet<>();
for (final BeanPropertyDefinition property : properties) {
fields.add(property.getName());
}
return fields;
}
Aggregations