下面列出了怎么用com.fasterxml.jackson.databind.introspect.AnnotatedMember的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Helper method that can be used to see if specified property is annotated
* to indicate use of a converter for property value (in case of container types,
* it is container type itself, not key or content type).
*
* @since 2.2
*/
protected JsonSerializer<Object> findConvertingSerializer(SerializerProvider provider,
BeanPropertyWriter prop)
throws JsonMappingException
{
final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
if (intr != null) {
AnnotatedMember m = prop.getMember();
if (m != null) {
Object convDef = intr.findSerializationConverter(m);
if (convDef != null) {
Converter<Object,Object> conv = provider.converterInstance(prop.getMember(), convDef);
JavaType delegateType = conv.getOutputType(provider.getTypeFactory());
// [databind#731]: Should skip if nominally java.lang.Object
JsonSerializer<?> ser = delegateType.isJavaLangObject() ? null
: provider.findValueSerializer(delegateType, prop);
return new StdDelegatingSerializer(conv, delegateType, ser);
}
}
}
return null;
}
/**
* @deprecated Since 2.9 use {link {@link #findContextualConvertingSerializer} instead
*/
@Deprecated
protected JsonSerializer<?> findConvertingContentSerializer(SerializerProvider provider,
BeanProperty prop, JsonSerializer<?> existingSerializer)
throws JsonMappingException
{
final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
if (_neitherNull(intr, prop)) {
AnnotatedMember m = prop.getMember();
if (m != null) {
Object convDef = intr.findSerializationContentConverter(m);
if (convDef != null) {
Converter<Object,Object> conv = provider.converterInstance(prop.getMember(), convDef);
JavaType delegateType = conv.getOutputType(provider.getTypeFactory());
// [databind#731]: Should skip if nominally java.lang.Object
if ((existingSerializer == null) && !delegateType.isJavaLangObject()) {
existingSerializer = provider.findValueSerializer(delegateType);
}
return new StdDelegatingSerializer(conv, delegateType, existingSerializer);
}
}
}
return existingSerializer;
}
/**
* Convenience method for finding out possibly configured content value serializer.
*
* @since 2.7.4
*/
protected JsonSerializer<?> findAnnotatedContentSerializer(SerializerProvider serializers,
BeanProperty property)
throws JsonMappingException
{
if (property != null) {
// First: if we have a property, may have property-annotation overrides
AnnotatedMember m = property.getMember();
final AnnotationIntrospector intr = serializers.getAnnotationIntrospector();
if (m != null) {
Object serDef = intr.findContentSerializer(m);
if (serDef != null) {
return serializers.serializerInstance(m, serDef);
}
}
}
return null;
}
/**
* Helper method that can be used to see if specified property has annotation
* indicating that a converter is to be used for contained values (contents
* of structured types; array/List/Map values)
*
* @param existingDeserializer (optional) configured content
* serializer if one already exists.
*
* @since 2.2
*/
protected JsonDeserializer<?> findConvertingContentDeserializer(DeserializationContext ctxt,
BeanProperty prop, JsonDeserializer<?> existingDeserializer)
throws JsonMappingException
{
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
if (_neitherNull(intr, prop)) {
AnnotatedMember member = prop.getMember();
if (member != null) {
Object convDef = intr.findDeserializationContentConverter(member);
if (convDef != null) {
Converter<Object,Object> conv = ctxt.converterInstance(prop.getMember(), convDef);
JavaType delegateType = conv.getInputType(ctxt.getTypeFactory());
if (existingDeserializer == null) {
existingDeserializer = ctxt.findContextualValueDeserializer(delegateType, prop);
}
return new StdDelegatingDeserializer<Object>(conv, delegateType, existingDeserializer);
}
}
}
return existingDeserializer;
}
/**
* @since 2.9
*/
public static EnumResolver constructUsingMethod(Class<Enum<?>> enumCls,
AnnotatedMember accessor,
AnnotationIntrospector ai)
{
Enum<?>[] enumValues = enumCls.getEnumConstants();
HashMap<String, Enum<?>> map = new HashMap<String, Enum<?>>();
// from last to first, so that in case of duplicate values, first wins
for (int i = enumValues.length; --i >= 0; ) {
Enum<?> en = enumValues[i];
try {
Object o = accessor.getValue(en);
if (o != null) {
map.put(o.toString(), en);
}
} catch (Exception e) {
throw new IllegalArgumentException("Failed to access @JsonValue of Enum value "+en+": "+e.getMessage());
}
}
Enum<?> defaultEnum = (ai != null) ? ai.findDefaultEnumValue(enumCls) : null;
return new EnumResolver(enumCls, enumValues, map, defaultEnum);
}
@Override
protected StackManipulation invocationOperation(
AnnotatedMember annotatedMember, TypeDefinition beanClassDescription) {
final String methodName = annotatedMember.getName();
@SuppressWarnings("unchecked")
final MethodList<MethodDescription> matchingMethods =
(MethodList<MethodDescription>) beanClassDescription.getDeclaredMethods().filter(named(methodName));
if (matchingMethods.size() == 1) { //method was declared on class
return MethodInvocation.invoke(matchingMethods.getOnly());
}
if (matchingMethods.isEmpty()) { //method was not found on class, try super class
return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
}
else { //should never happen
throw new IllegalStateException("Could not find definition of method: " + methodName);
}
}
@Override
protected StackManipulation invocationOperation(
AnnotatedMember annotatedMember, TypeDefinition beanClassDescription) {
final String fieldName = annotatedMember.getName();
@SuppressWarnings("unchecked")
final FieldList<FieldDescription> matchingFields =
(FieldList<FieldDescription>) beanClassDescription.getDeclaredFields().filter(named(fieldName));
if (matchingFields.size() == 1) { //method was declared on class
return FieldAccess.forField(matchingFields.getOnly()).read();
}
if (matchingFields.isEmpty()) { //method was not found on class, try super class
return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
}
else { //should never happen
throw new IllegalStateException("Could not find definition of field: " + fieldName);
}
}
@Override
public Size apply(MethodVisitor methodVisitor,
Implementation.Context implementationContext) {
final boolean mustCast = (beanValueAccess == MethodVariableAccess.REFERENCE);
final List<StackManipulation> operations = new ArrayList<StackManipulation>();
operations.add(loadLocalVar()); // load local for cast bean
operations.add(loadBeanValueArg());
final AnnotatedMember member = prop.getMember();
if (mustCast) {
operations.add(TypeCasting.to(new ForLoadedType(getClassToCastBeanValueTo(member))));
}
operations.add(invocationOperation(member, beanClassDescription));
operations.add(MethodReturn.VOID);
final StackManipulation.Compound compound = new StackManipulation.Compound(operations);
return compound.apply(methodVisitor, implementationContext);
}
@SuppressWarnings("unchecked")
@Override
protected StackManipulation invocationOperation(AnnotatedMember annotatedMember,
TypeDefinition beanClassDescription) {
final String fieldName = annotatedMember.getName();
final FieldList<FieldDescription> matchingFields =
(FieldList<FieldDescription>) beanClassDescription.getDeclaredFields().filter(named(fieldName));
if (matchingFields.size() == 1) { //method was declared on class
return FieldAccess.forField(matchingFields.getOnly()).write();
}
if (matchingFields.isEmpty()) { //method was not found on class, try super class
return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
}
else { //should never happen
throw new IllegalStateException("Could not find definition of field: " + fieldName);
}
}
@Override
protected StackManipulation invocationOperation(AnnotatedMember annotatedMember,
TypeDefinition beanClassDescription) {
final String methodName = annotatedMember.getName();
@SuppressWarnings("unchecked")
final MethodList<MethodDescription> matchingMethods =
(MethodList<MethodDescription>) beanClassDescription.getDeclaredMethods().filter(named(methodName));
if (matchingMethods.size() == 1) { //method was declared on class
return MethodInvocation.invoke(matchingMethods.getOnly());
}
if (matchingMethods.isEmpty()) { //method was not found on class, try super class
return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
}
else { //should never happen
throw new IllegalStateException("Could not find definition of method: " + methodName);
}
}
@Override
public void setupModule(SetupContext context) {
context.insertAnnotationIntrospector(new NopAnnotationIntrospector() {
private static final long serialVersionUID = 479313244908256455L;
@Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
if (!(m instanceof AnnotatedMethod)) {
return super.hasIgnoreMarker(m);
}
AnnotatedMethod method = (AnnotatedMethod) m;
return method.getName().startsWith("lambda$") ? true : super.hasIgnoreMarker(m);
}
});
}
@Override
public Boolean hasRequiredMarker(AnnotatedMember m)
{
ApiParam apiParam = m.getAnnotation(ApiParam.class);
if (apiParam != null) {
return apiParam.required();
}
ApiModelProperty ann = m.getAnnotation(ApiModelProperty.class);
if (ann != null) {
return ann.required();
}
XmlElement elem = m.getAnnotation(XmlElement.class);
if (elem != null) {
if (elem.required()) {
return true;
}
}
return null;
}
private static Object readValue(final AnnotatedMember member, final Object object) {
if (object == null) {
return null;
}
final Object res;
final AccessibleObject accessor = (AccessibleObject) member.getMember();
// case: private field
if (!accessor.isAccessible()) {
accessor.setAccessible(true);
try {
res = member.getValue(object);
} finally {
accessor.setAccessible(false);
}
} else {
// public access (most likely getter)
res = member.getValue(object);
}
return res;
}
@Bean
public ObjectMapper firebaseObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
@Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
return _findAnnotation(m, FirebaseId.class) != null;
}
});
return objectMapper;
}
@Override
public void serialize(HalEntityWrapper wrapper, JsonGenerator generator, SerializerProvider serializers)
throws IOException {
generator.writeStartObject();
for (BeanPropertyDefinition property : getPropertyDefinitions(serializers, wrapper.getEntity().getClass())) {
AnnotatedMember accessor = property.getAccessor();
if (accessor != null) {
writeValue(property.getName(), accessor.getValue(wrapper.getEntity()), generator);
}
}
writeLinks(wrapper.getEntity(), generator);
generator.writeEndObject();
}
@Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
// This is a somewhat hacky way of having ObjectMapper only serialize the fields in our
// model classes instead of the base class that comes from the SDK. In the future, we will
// remove the SDK dependency itself and the base classes and this hack will go away.
if (m.getDeclaringClass() == AmazonWebServiceRequest.class ||
m.getDeclaringClass() == AmazonWebServiceResult.class) {
return true;
}
return super.hasIgnoreMarker(m);
}
public Std(PropertyName name, JavaType type, PropertyName wrapperName,
AnnotatedMember member, PropertyMetadata metadata)
{
_name = name;
_type = type;
_wrapperName = wrapperName;
_metadata = metadata;
_member = member;
}
/**
* @deprecated Since 2.9
*/
@Deprecated
public Std(PropertyName name, JavaType type, PropertyName wrapperName,
Annotations contextAnnotations,
AnnotatedMember member, PropertyMetadata metadata)
{
this(name, type, wrapperName, member, metadata);
}
public void setTypeId(AnnotatedMember idProp) {
// Not legal to use multiple ones...
if (_typeId != null) {
throw new IllegalArgumentException("Multiple type ids specified with "+_typeId+" and "+idProp);
}
_typeId = idProp;
}
/**
* @param ser Explicit serializer to use, if caller knows it (which
* occurs if and only if the "value method" was annotated with
* {@link com.fasterxml.jackson.databind.annotation.JsonSerialize#using}), otherwise
* null
*
* @since 2.8 Earlier method took "raw" Method, but that does not work with access
* to information we need
*/
@SuppressWarnings("unchecked")
public JsonValueSerializer(AnnotatedMember accessor, JsonSerializer<?> ser)
{
super(accessor.getType());
_accessor = accessor;
_valueSerializer = (JsonSerializer<Object>) ser;
_property = null;
_forceTypeInformation = true; // gets reconsidered when we are contextualized
}
@SuppressWarnings("unchecked")
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer<?> ser = null;
Boolean unwrapSingle = null;
if (property != null) {
final AnnotationIntrospector intr = serializers.getAnnotationIntrospector();
AnnotatedMember m = property.getMember();
if (m != null) {
Object serDef = intr.findContentSerializer(m);
if (serDef != null) {
ser = serializers.serializerInstance(m, serDef);
}
}
}
JsonFormat.Value format = findFormatOverrides(serializers, property, handledType());
if (format != null) {
unwrapSingle = format.getFeature(JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
}
// [databind#124]: May have a content converter
ser = findContextualConvertingSerializer(serializers, property, ser);
if (ser == null) {
ser = serializers.findValueSerializer(String.class, property);
}
// Optimization: default serializer just writes String, so we can avoid a call:
if (isDefaultSerializer(ser)) {
if (unwrapSingle == _unwrapSingle) {
return this;
}
return _withResolved(property, unwrapSingle);
}
// otherwise...
// note: will never have TypeSerializer, because Strings are "natural" type
return new CollectionSerializer(serializers.constructType(String.class),
true, /*TypeSerializer*/ null, (JsonSerializer<Object>) ser);
}
@SuppressWarnings("unchecked")
public AnyGetterWriter(BeanProperty property,
AnnotatedMember accessor, JsonSerializer<?> serializer)
{
_accessor = accessor;
_property = property;
_serializer = (JsonSerializer<Object>) serializer;
if (serializer instanceof MapSerializer) {
_mapSerializer = (MapSerializer) serializer;
}
}
public SettableAnyProperty(BeanProperty property, AnnotatedMember setter, JavaType type,
KeyDeserializer keyDeser,
JsonDeserializer<Object> valueDeser, TypeDeserializer typeDeser)
{
_property = property;
_setter = setter;
_type = type;
_valueDeserializer = valueDeser;
_valueTypeDeserializer = typeDeser;
_keyDeserializer = keyDeser;
_setterIsField = setter instanceof AnnotatedField;
}
/**
* @deprecated in 2.9 (remove from 3.0)
*/
@Deprecated // see [databind#1835]
public ValueInjector(PropertyName propName, JavaType type,
com.fasterxml.jackson.databind.util.Annotations contextAnnotations, // removed from later versions
AnnotatedMember mutator, Object valueId)
{
this(propName, type, mutator, valueId);
}
/**
* Method used when actual String serialization is indicated using @JsonValue
* on a method.
*
* @since 2.9
*/
@SuppressWarnings({ "unchecked" })
public static EnumResolver constructUnsafeUsingMethod(Class<?> rawEnumCls,
AnnotatedMember accessor,
AnnotationIntrospector ai)
{
// wrong as ever but:
Class<Enum<?>> enumCls = (Class<Enum<?>>) rawEnumCls;
return constructUsingMethod(enumCls, accessor, ai);
}
protected PropertyNamingStrategy judgeStrategy(AnnotatedMember member) {
Class<?> clazz = member.getDeclaringClass();
if (Config.class.isAssignableFrom(clazz)) {
return PropertyNamingStrategy.UPPER_CAMEL_CASE;
} else if (clazz.getCanonicalName().startsWith("net.bis5.mattermost.model.config.")) {
return PropertyNamingStrategy.UPPER_CAMEL_CASE;
} else {
return PropertyNamingStrategy.SNAKE_CASE;
}
}
@Override
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc, BeanDeserializerBuilder builder) {
for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
if (!propDef.hasGetter() || propDef.hasSetter()) {
continue;
}
AnnotatedMember getter = propDef.getAccessor();
if (!Keeper.class.equals(getter.getRawType())) {
continue;
}
builder.addOrReplaceProperty(new CustomGetterBeanProperty(propDef, getter), true);
}
return builder;
}
@Override
public String findImplicitPropertyName(AnnotatedMember param) {
if (param instanceof AnnotatedParameter) {
return _paranamer.findParameterName((AnnotatedParameter) param);
}
return null;
}
@Override
public Size apply(MethodVisitor methodVisitor,
Implementation.Context implementationContext) {
final List<StackManipulation> operations = new ArrayList<StackManipulation>();
operations.add(loadLocalVar()); // load local for cast bean
final AnnotatedMember member = prop.getMember();
operations.add(invocationOperation(member, beanClassDescription));
operations.add(methodReturn);
final StackManipulation.Compound compound = new StackManipulation.Compound(operations);
return compound.apply(methodVisitor, implementationContext);
}
@Override
public boolean hasIgnoreMarker(AnnotatedMember m)
{
ApiModelProperty ann = m.getAnnotation(ApiModelProperty.class);
if (ann != null && ann.hidden()) {
return true;
}
return false;
}