com.fasterxml.jackson.databind.introspect.Annotated Java Examples

The following examples show how to use com.fasterxml.jackson.databind.introspect.Annotated. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JacksonValueMapper.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: CSJacksonAnnotationIntrospector.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
@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());

}
 
Example #3
Source File: SwaggerJacksonAnnotationIntrospector.java    From netty-rest with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #4
Source File: FriendlyIdAnnotationIntrospector.java    From friendly-id with Apache License 2.0 6 votes vote down vote up
@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;
	}
}
 
Example #5
Source File: FriendlyIdAnnotationIntrospector.java    From friendly-id with Apache License 2.0 6 votes vote down vote up
@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;
	}
}
 
Example #6
Source File: RosettaAnnotationIntrospector.java    From Rosetta with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #7
Source File: KvSupportModule.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #8
Source File: JtModule.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #9
Source File: ParanamerAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #10
Source File: JacksonLombokAnnotationIntrospector.java    From jackson-lombok with MIT License 6 votes vote down vote up
@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;
        }
    }
}
 
Example #11
Source File: SwaggerJacksonAnnotationIntrospector.java    From netty-rest with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #12
Source File: KvSupportModule.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
private Class<? extends PropertyInterceptor>[] getInterceptors(Annotated a) {
    KvMapping ann = a.getAnnotation(KvMapping.class);
    if(ann == null) {
        return null;
    }
    Class<? extends PropertyInterceptor>[] interceptors = ann.interceptors();
    if(ArrayUtils.isEmpty(interceptors)) {
        return null;
    }
    return interceptors;
}
 
Example #13
Source File: CustomAnnotationIntrospector.java    From elepy with Apache License 2.0 5 votes vote down vote up
private String findPropertyName(Annotated annotated) {

        if (isId(annotated)) {
            return "_id";
        }
        return null;
    }
 
Example #14
Source File: DeserializerCache.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Converter<Object,Object> findConverter(DeserializationContext ctxt,
        Annotated a)
    throws JsonMappingException
{
    Object convDef = ctxt.getAnnotationIntrospector().findDeserializationConverter(a);
    if (convDef == null) {
        return null;
    }
    return ctxt.converterInstance(a, convDef);
}
 
Example #15
Source File: CommonObjectMappers.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public Object findFilterId(Annotated ann) {
    Object id = super.findFilterId(ann);
    if (id == null) {
        String name = ann.getRawType().getName();
        // TODO We hardcode here two packages. It should be configurable.
        if (name.startsWith("com.netflix.titus") || name.startsWith("com.netflix.managedbatch")) {
            id = "titusFilter";
        }
    }
    return id;
}
 
Example #16
Source File: SpringHandlerInstantiator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
 
Example #17
Source File: SwaggerJacksonAnnotationIntrospector.java    From netty-rest with Apache License 2.0 5 votes vote down vote up
@Override
public Integer findPropertyIndex(Annotated a)
{
    ApiModelProperty prop = a.getAnnotation(ApiModelProperty.class);
    if (prop != null && prop.position() != 0) {
        return prop.position();
    }
    return null;
}
 
Example #18
Source File: SpringHandlerInstantiator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/** @since 4.3 */
@Override
public PropertyNamingStrategy namingStrategyInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (PropertyNamingStrategy) this.beanFactory.createBean(implClass);
}
 
Example #19
Source File: CustomAnnotationIntrospector.java    From elepy with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(Annotated a) {
    String rawName = findPropertyName(a);
    if (rawName != null) {
        return new PropertyName(rawName);
    }
    return null;
}
 
Example #20
Source File: DeserializerCache.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Helper method called to check if a class or method
 * has annotation that tells which class to use for deserialization.
 * Returns null if no such annotation found.
 */
protected JsonDeserializer<Object> findDeserializerFromAnnotation(DeserializationContext ctxt,
        Annotated ann)
    throws JsonMappingException
{
    Object deserDef = ctxt.getAnnotationIntrospector().findDeserializer(ann);
    if (deserDef == null) {
        return null;
    }
    JsonDeserializer<Object> deser = ctxt.deserializerInstance(ann, deserDef);
    // One more thing however: may need to also apply a converter:
    return findConvertingDeserializer(ctxt, ann, deser);
}
 
Example #21
Source File: DatabindContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Helper method to use to construct a {@link Converter}, given a definition
 * that may be either actual converter instance, or Class for instantiating one.
 * 
 * @since 2.2
 */
@SuppressWarnings("unchecked")
public Converter<Object,Object> converterInstance(Annotated annotated,
        Object converterDef)
    throws JsonMappingException
{
    if (converterDef == null) {
        return null;
    }
    if (converterDef instanceof Converter<?,?>) {
        return (Converter<Object,Object>) converterDef;
    }
    if (!(converterDef instanceof Class)) {
        throw new IllegalStateException("AnnotationIntrospector returned Converter definition of type "
                +converterDef.getClass().getName()+"; expected type Converter or Class<Converter> instead");
    }
    Class<?> converterClass = (Class<?>)converterDef;
    // there are some known "no class" markers to consider too:
    if (converterClass == Converter.None.class || ClassUtil.isBogusClass(converterClass)) {
        return null;
    }
    if (!Converter.class.isAssignableFrom(converterClass)) {
        throw new IllegalStateException("AnnotationIntrospector returned Class "
                +converterClass.getName()+"; expected Class<Converter>");
    }
    final MapperConfig<?> config = getConfig();
    HandlerInstantiator hi = config.getHandlerInstantiator();
    Converter<?,?> conv = (hi == null) ? null : hi.converterInstance(config, annotated, converterClass);
    if (conv == null) {
        conv = (Converter<?,?>) ClassUtil.createInstance(converterClass,
                config.canOverrideAccessModifiers());
    }
    return (Converter<Object,Object>) conv;
}
 
Example #22
Source File: JtModule.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@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()));
}
 
Example #23
Source File: DatabindContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ObjectIdResolver objectIdResolverInstance(Annotated annotated, ObjectIdInfo objectIdInfo)
{
    Class<? extends ObjectIdResolver> implClass = objectIdInfo.getResolverType();
    final MapperConfig<?> config = getConfig();
    HandlerInstantiator hi = config.getHandlerInstantiator();
    ObjectIdResolver resolver = (hi == null) ? null : hi.resolverIdGeneratorInstance(config, annotated, implClass);
    if (resolver == null) {
        resolver = ClassUtil.createInstance(implClass, config.canOverrideAccessModifiers());
    }

    return resolver;
}
 
Example #24
Source File: RosettaAnnotationIntrospector.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
private Type getType(Annotated a) {
  try {
    // Jackson 2.7+
    return a.getType();
  } catch (Throwable t) {
    return a.getGenericType();
  }
}
 
Example #25
Source File: ReferenceTypeSerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected boolean _useStatic(SerializerProvider provider, BeanProperty property,
        JavaType referredType)
{
    // First: no serializer for `Object.class`, must be dynamic
    if (referredType.isJavaLangObject()) {
        return false;
    }
    // but if type is final, might as well fetch
    if (referredType.isFinal()) { // or should we allow annotation override? (only if requested...)
        return true;
    }
    // also: if indicated by typing, should be considered static
    if (referredType.useStaticType()) {
        return true;
    }
    // if neither, maybe explicit annotation?
    AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    if ((intr != null) && (property != null)) {
        Annotated ann = property.getMember();
        if (ann != null) {
            JsonSerialize.Typing t = intr.findSerializationTyping(property.getMember());
            if (t == JsonSerialize.Typing.STATIC) {
                return true;
            }
            if (t == JsonSerialize.Typing.DYNAMIC) {
                return false;
            }
        }
    }
    // and finally, may be forced by global static typing (unlikely...)
    return provider.isEnabled(MapperFeature.USE_STATIC_TYPING);
}
 
Example #26
Source File: DefaultSerializerProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JsonSerializer<Object> serializerInstance(Annotated annotated, Object serDef)
        throws JsonMappingException
{
    if (serDef == null) {
        return null;
    }
    JsonSerializer<?> ser;
    
    if (serDef instanceof JsonSerializer) {
        ser = (JsonSerializer<?>) serDef;
    } else {
        // Alas, there's no way to force return type of "either class
        // X or Y" -- need to throw an exception after the fact
        if (!(serDef instanceof Class)) {
            reportBadDefinition(annotated.getType(),
                    "AnnotationIntrospector returned serializer definition of type "
                    +serDef.getClass().getName()+"; expected type JsonSerializer or Class<JsonSerializer> instead");
        }
        Class<?> serClass = (Class<?>)serDef;
        // there are some known "no class" markers to consider too:
        if (serClass == JsonSerializer.None.class || ClassUtil.isBogusClass(serClass)) {
            return null;
        }
        if (!JsonSerializer.class.isAssignableFrom(serClass)) {
            reportBadDefinition(annotated.getType(),
                    "AnnotationIntrospector returned Class "
                    +serClass.getName()+"; expected Class<JsonSerializer>");
        }
        HandlerInstantiator hi = _config.getHandlerInstantiator();
        ser = (hi == null) ? null : hi.serializerInstance(_config, annotated, serClass);
        if (ser == null) {
            ser = (JsonSerializer<?>) ClassUtil.createInstance(serClass,
                    _config.canOverrideAccessModifiers());
        }
    }
    return (JsonSerializer<Object>) _handleResolvable(ser);
}
 
Example #27
Source File: MapperConfig.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #28
Source File: MapperConfig.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #29
Source File: RosettaAnnotationIntrospector.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(Annotated a) {
  PropertyName propertyName = findRosettaPropertyName(a);
  if (propertyName == null) {
    propertyName = super.findNameForDeserialization(a);
  }
  return propertyName;
}
 
Example #30
Source File: ServerInstanceListSerializerTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
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;
}