下面列出了怎么用com.fasterxml.jackson.databind.introspect.Annotated的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public PropertyName findNameForDeserialization(MapperConfig<?> config, Annotated a)
{
/* 14-Apr-2014, tatu: Important -- we should NOT introspect name here,
* since we are not using annotations; instead it needs to be done
* in {@link #findParameterSourceName(AnnotatedParameter)}.
*/
/*
PropertyName name = super.findNameForDeserialization(a);
if (name == null) {
if (a instanceof AnnotatedParameter) {
String rawName _paranamer.findParameterName((AnnotatedParameter) a);
if (rawName != null) {
return new PropertyName(rawName);
}
}
}
*/
return null;
}
@Override
public boolean hasCreatorAnnotation(Annotated annotated) {
if (super.hasCreatorAnnotation(annotated)) {
return true;
} else if (!(annotated instanceof AnnotatedConstructor)) {
return false;
} else {
AnnotatedConstructor annotatedConstructor = (AnnotatedConstructor) annotated;
ConstructorProperties properties = getConstructorPropertiesAnnotation(annotatedConstructor);
if (properties == null) {
return false;
} else {
addJacksonAnnotationsToContructorParameters(annotatedConstructor);
return true;
}
}
}
@Override
@SuppressWarnings("unchecked")
public JsonSerializer<?> findSerializer(Annotated a) {
StoredAsJson storedAsJson = a.getAnnotation(StoredAsJson.class);
RosettaSerialize rosettaSerialize = a.getAnnotation(RosettaSerialize.class);
if (storedAsJson != null && rosettaSerialize != null) {
throw new IllegalArgumentException("Cannot have @StoredAsJson as well as @RosettaSerialize annotations on the same entry");
}
if (storedAsJson != null) {
Class<?> type = a.getRawType();
return storedAsJson.binary() ? new StoredAsJsonBinarySerializer(type) : new StoredAsJsonSerializer(type);
}
if (rosettaSerialize != null) {
Class<? extends JsonSerializer> klass = rosettaSerialize.using();
if (klass != JsonSerializer.None.class) {
return ClassUtil.createInstance(
klass,
objectMapper.getSerializationConfig().canOverrideAccessModifiers());
}
}
return null;
}
@Override
public Object findDeserializationConverter(Annotated a) {
Class<? extends PropertyInterceptor>[] interceptors = getInterceptors(a);
if (interceptors == null) {
return null;
}
JavaType javaType = a.getType();
if(a instanceof AnnotatedMethod) {
AnnotatedMethod am = (AnnotatedMethod) a;
if(am.getParameterCount() == 1) {
javaType = am.getParameterType(0);
} else {
throw new RuntimeException("Invalid property setter: " + am.getAnnotated());
}
}
return new KvInterceptorsDeserializationConverter(interceptors, new KvPropertyContextImpl(a, javaType));
}
@Override
public List<NamedType> findSubtypes(Annotated a)
{
final ApiModel api = a.getAnnotation(ApiModel.class);
if (api != null) {
final Class<?>[] classes = api.subTypes();
final List<NamedType> names = new ArrayList<>(classes.length);
for (Class<?> subType : classes) {
names.add(new NamedType(subType));
}
if (!names.isEmpty()) {
return names;
}
}
return Collections.emptyList();
}
@Override
public String findPropertyDescription(Annotated a)
{
ApiParam apiParam = a.getAnnotation(ApiParam.class);
if (apiParam != null) {
return apiParam.description();
}
ApiModel model = a.getAnnotation(ApiModel.class);
if (model != null && !"".equals(model.description())) {
return model.description();
}
ApiModelProperty prop = a.getAnnotation(ApiModelProperty.class);
if (prop != null) {
return prop.value();
}
return null;
}
@Override
public Object findSerializer(Annotated a) {
AnnotatedElement ae = a.getAnnotated();
Url an = ae.getAnnotation(Url.class);
if (an == null) {
return null;
}
if (an.type() == String.class) {
return new UriSerializer(an);
} else if (an.type() == List.class) {
return new UrisSerializer(an);
}
throw new UnsupportedOperationException("Unsupported type " + an.type());
}
private boolean isIncluded(AnnotatedType declaringType, BeanPropertyDefinition prop, boolean deserializableInSubType, InclusionStrategy inclusionStrategy) {
List<AnnotatedElement> elements = Utils.flatten(
Optional.ofNullable(prop.getConstructorParameter()).map(ElementFactory::getParameter),
Optional.ofNullable(prop.getSetter()).map(Annotated::getAnnotated),
Optional.ofNullable(prop.getGetter()).map(Annotated::getAnnotated),
Optional.ofNullable(prop.getField()).map(Annotated::getAnnotated))
.collect(Collectors.toList());
InputFieldInclusionParams params = InputFieldInclusionParams.builder()
.withType(declaringType)
.withElementDeclaringClass(prop.getPrimaryMember().getDeclaringClass())
.withElements(elements)
.withDeserializationInfo(prop.couldDeserialize(), deserializableInSubType)
.build();
return inclusionStrategy.includeInputField(params);
}
@Override
public Object findDeserializationConverter(Annotated a) {
JtToMap ann = a.getAnnotation(JtToMap.class);
if (ann == null) {
return null;
}
JavaType javaType = a.getType();
if(a instanceof AnnotatedMethod) {
AnnotatedMethod am = (AnnotatedMethod) a;
if(am.getParameterCount() == 1) {
javaType = am.getParameterType(0);
} else {
throw new RuntimeException("Invalid property setter: " + am.getAnnotated());
}
}
return new DeserializationConverterImpl(ann, new Ctx(a, javaType));
}
@Override
public Object findSerializer(Annotated annotatedMethod) {
IdFormat annotation = _findAnnotation(annotatedMethod, IdFormat.class);
if (annotatedMethod.getRawType() == UUID.class) {
if (annotation != null) {
switch (annotation.value()) {
case RAW:
return UUIDSerializer.class;
case URL62:
return FriendlyIdSerializer.class;
}
}
return FriendlyIdSerializer.class;
} else {
return null;
}
}
@Override
public Object findDeserializer(Annotated annotatedMethod) {
IdFormat annotation = _findAnnotation(annotatedMethod, IdFormat.class);
if (rawDeserializationType(annotatedMethod) == UUID.class) {
if (annotation != null) {
switch (annotation.value()) {
case RAW:
return UUIDDeserializer.class;
case URL62:
return FriendlyIdDeserializer.class;
}
}
return FriendlyIdDeserializer.class;
} else {
return null;
}
}
@Override
public Object findSerializationConverter(Annotated a) {
JtToMap ann = a.getAnnotation(JtToMap.class);
if (ann == null) {
return null;
}
return new SerializationConverterImpl(ann, new Ctx(a, a.getType()));
}
@Override
public PropertyName findNameForDeserialization(Annotated a)
{
ApiParam model = a.getAnnotation(ApiParam.class);
if (model != null && !"".equals(model.value())) {
return new PropertyName(model.value());
}
return null;
}
/** @since 4.3 */
@Override
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (ValueInstantiator) this.beanFactory.createBean(implClass);
}
private PropertyName findRosettaPropertyName(Annotated a) {
RosettaProperty ann = a.getAnnotation(RosettaProperty.class);
if (ann != null) {
return ann.value().isEmpty() ? PropertyName.USE_DEFAULT : new PropertyName(ann.value());
}
return null;
}
/** @since 4.3 */
@Override
public ObjectIdResolver resolverIdGeneratorInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (ObjectIdResolver) this.beanFactory.createBean(implClass);
}
/** @since 4.3 */
@Override
public PropertyNamingStrategy namingStrategyInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (PropertyNamingStrategy) this.beanFactory.createBean(implClass);
}
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
@Override
public Object findSerializationConverter(Annotated a) {
Class<? extends PropertyInterceptor>[] interceptors = getInterceptors(a);
if (interceptors == null) {
return null;
}
return new KvInterceptorsSerializationConverter(interceptors, new KvPropertyContextImpl(a, a.getType()));
}
/** @since 4.3 */
@Override
public PropertyNamingStrategy namingStrategyInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (PropertyNamingStrategy) this.beanFactory.createBean(implClass);
}
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
@SuppressWarnings("unchecked")
@Override
public Object findSerializer(Annotated am) {
if (am.hasAnnotation(ManyToOne.class)) {
return new ManyToOneSerializer((Class<AbstractEntity>) am.getRawType());
} else {
return super.findDeserializer(am);
}
}
@Override
public PropertyName findNameForDeserialization(Annotated annotatedEntity) {
// This logic relies on the fact that Immutables generates setters annotated with @JsonProperty.
// It thus becomes obsolete whenever we move away from Immutables and the deserialization target no longer
// carries those annotations.
JsonProperty propertyAnnotation = _findAnnotation(annotatedEntity, JsonProperty.class);
if (propertyAnnotation != null) {
String jsonFieldName = propertyAnnotation.value();
Preconditions.checkArgument(
KEBAB_CASE_PATTERN.matcher(jsonFieldName).matches(),
"Conjure grammar requires kebab-case field names: %s",
jsonFieldName);
}
if (annotatedEntity instanceof AnnotatedMethod) {
AnnotatedMethod maybeSetter = (AnnotatedMethod) annotatedEntity;
if (maybeSetter.getName().startsWith("set")) {
// As a pre-caution, require that all setters have a JsonProperty annotation.
Preconditions.checkArgument(
_findAnnotation(annotatedEntity, JsonProperty.class) != null,
"All setter ({@code set*}) deserialization targets require @JsonProperty annotations: %s",
maybeSetter.getName());
}
}
return null; // delegate to the next introspector in an AnnotationIntrospectorPair.
}
@Override
public ObjectIdResolver resolverIdGeneratorInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
if (implClass.isAssignableFrom(EntityStoreResolver.class)) {
return new EntityStoreResolver(store);
}
return null;
}
@Override
public PropertyName findNameForDeserialization(Annotated a) {
String rawName = findPropertyName(a);
if (rawName != null) {
return new PropertyName(rawName);
}
return null;
}
private String findPropertyName(Annotated annotated) {
if (isId(annotated)) {
return "_id";
}
return null;
}
@Override
public PropertyName findNameForDeserialization(Annotated a) {
PropertyName propertyName = findRosettaPropertyName(a);
if (propertyName == null) {
propertyName = super.findNameForDeserialization(a);
}
return propertyName;
}
public JsonSerializer<?> serializerInstance(SerializationConfig config, Annotated annotated, Class<?> keyDeserClass) {
if (annotated.getName().equals("com.navercorp.pinpoint.web.applicationmap.nodes.ServerInstance")) {
final ServiceTypeRegistryService serviceTypeRegistryService = mockServiceTypeRegistryService();
final AgentLifeCycleStateSerializer agentLifeCycleStateSerializer = new AgentLifeCycleStateSerializer();
return new ServerInstanceSerializer(serviceTypeRegistryService, agentLifeCycleStateSerializer);
}
return null;
}
/**
* Method that can be called to obtain an instance of <code>TypeIdResolver</code> of
* specified type.
*/
public TypeResolverBuilder<?> typeResolverBuilderInstance(Annotated annotated,
Class<? extends TypeResolverBuilder<?>> builderClass)
{
HandlerInstantiator hi = getHandlerInstantiator();
if (hi != null) {
TypeResolverBuilder<?> builder = hi.typeResolverBuilderInstance(this, annotated, builderClass);
if (builder != null) {
return builder;
}
}
return (TypeResolverBuilder<?>) ClassUtil.createInstance(builderClass, canOverrideAccessModifiers());
}
/**
* Method that can be called to obtain an instance of <code>TypeIdResolver</code> of
* specified type.
*/
public TypeIdResolver typeIdResolverInstance(Annotated annotated,
Class<? extends TypeIdResolver> resolverClass)
{
HandlerInstantiator hi = getHandlerInstantiator();
if (hi != null) {
TypeIdResolver builder = hi.typeIdResolverInstance(this, annotated, resolverClass);
if (builder != null) {
return builder;
}
}
return (TypeIdResolver) ClassUtil.createInstance(resolverClass, canOverrideAccessModifiers());
}