com.fasterxml.jackson.databind.cfg.HandlerInstantiator Java Examples

The following examples show how to use com.fasterxml.jackson.databind.cfg.HandlerInstantiator. 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: ResourceDeserializerTest.java    From bowman with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
	typeResolver = mock(TypeResolver.class);
	configuration = Configuration.build();
	
	instantiator = mock(HandlerInstantiator.class);
	
	doReturn(new ResourceDeserializer(Object.class, typeResolver, configuration))
		.when(instantiator).deserializerInstance(any(), any(), eq(ResourceDeserializer.class));
			
	mapper = new ObjectMapper();
	mapper.setHandlerInstantiator(instantiator);
	mapper.registerModule(new Jackson2HalModule());
	mapper.registerModule(new TestModule());
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	
	doReturn(Object.class).when(typeResolver).resolveType(any(), any(), any());
}
 
Example #2
Source File: RestOperationsFactoryTest.java    From bowman with Apache License 2.0 6 votes vote down vote up
@Test
public void createInstantiatesObjectMapperWithInlineAssociationDeserializerAwareHandlerInstantiator() {
	ObjectMapper mapper = new ObjectMapper();
	RestTemplate restTemplate = new RestTemplate();
	
	when(mapperFactory.create(any())).thenReturn(mapper);
	when(restTemplateFactory.create(any(), any())).thenReturn(restTemplate);
	
	factory.create();

	ArgumentCaptor<HandlerInstantiator> handlerInstantiator = ArgumentCaptor.forClass(HandlerInstantiator.class);
	verify(mapperFactory).create(handlerInstantiator.capture());
	
	JsonDeserializer<?> result = handlerInstantiator.getValue()
		.deserializerInstance(null, null, InlineAssociationDeserializer.class);
	
	assertThat(result, is(anInlineAssociationDeserializerMatching(
		aRestOperationsMatching(is(restTemplate), is(mapper)), is(proxyFactory))));
}
 
Example #3
Source File: DefaultDeserializationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public JsonDeserializer<Object> deserializerInstance(Annotated ann, Object deserDef)
    throws JsonMappingException
{
    if (deserDef == null) {
        return null;
    }
    JsonDeserializer<?> deser;
    
    if (deserDef instanceof JsonDeserializer) {
        deser = (JsonDeserializer<?>) deserDef;
    } 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 (!(deserDef instanceof Class)) {
            throw new IllegalStateException("AnnotationIntrospector returned deserializer definition of type "+deserDef.getClass().getName()+"; expected type JsonDeserializer or Class<JsonDeserializer> instead");
        }
        Class<?> deserClass = (Class<?>)deserDef;
        // there are some known "no class" markers to consider too:
        if (deserClass == JsonDeserializer.None.class || ClassUtil.isBogusClass(deserClass)) {
            return null;
        }
        if (!JsonDeserializer.class.isAssignableFrom(deserClass)) {
            throw new IllegalStateException("AnnotationIntrospector returned Class "+deserClass.getName()+"; expected Class<JsonDeserializer>");
        }
        HandlerInstantiator hi = _config.getHandlerInstantiator();
        deser = (hi == null) ? null : hi.deserializerInstance(_config, ann, deserClass);
        if (deser == null) {
            deser = (JsonDeserializer<?>) ClassUtil.createInstance(deserClass,
                    _config.canOverrideAccessModifiers());
        }
    }
    // First: need to resolve
    if (deser instanceof ResolvableDeserializer) {
        ((ResolvableDeserializer) deser).resolve(this);
    }
    return (JsonDeserializer<Object>) deser;
}
 
Example #4
Source File: RestOperationsTest.java    From bowman with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	HandlerInstantiator instantiator = mock(HandlerInstantiator.class);
	
	doReturn(declaredTypeResourceDeserializer()).when(instantiator)
		.deserializerInstance(any(), any(), eq(ResourceDeserializer.class));
	
	restTemplate = mock(RestTemplate.class);
	objectMapper = new DefaultObjectMapperFactory().create(instantiator);

	restOperations = new RestOperations(restTemplate, objectMapper);
}
 
Example #5
Source File: InlineAssociationDeserializerTest.java    From bowman with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	RestOperations restOperations = mock(RestOperations.class);
	ClientProxyFactory proxyFactory = new JavassistClientProxyFactory();
	
	instantiator = mock(HandlerInstantiator.class);
	
	doReturn(new InlineAssociationDeserializer<>(Object.class, restOperations, proxyFactory))
		.when(instantiator).deserializerInstance(any(), any(), eq(InlineAssociationDeserializer.class));
	
	mapper = new ObjectMapper();
	mapper.setHandlerInstantiator(instantiator);
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
 
Example #6
Source File: RestOperationsFactoryTest.java    From bowman with Apache License 2.0 5 votes vote down vote up
@Test
public void createInstantiatesObjectMapperWithResourceDeserializerAwareHandlerInstantiator() {
	factory.create();
	
	ArgumentCaptor<HandlerInstantiator> handlerInstantiator = ArgumentCaptor.forClass(HandlerInstantiator.class);
	verify(mapperFactory).create(handlerInstantiator.capture());
	
	JsonDeserializer<?> result = handlerInstantiator.getValue()
		.deserializerInstance(null, null, ResourceDeserializer.class);
	
	assertThat(result, is(aResourceDeserializerMatching(instanceOf(DefaultTypeResolver.class),
		is(configuration))));
}
 
Example #7
Source File: DefaultObjectMapperFactory.java    From bowman with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectMapper create(HandlerInstantiator instantiator) {
	ObjectMapper mapper = new ObjectMapper();
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	mapper.registerModule(new Jackson2HalModule());
	mapper.registerModule(new JacksonClientModule());
	mapper.setHandlerInstantiator(instantiator);
	return mapper;
}
 
Example #8
Source File: DefaultDeserializationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final KeyDeserializer keyDeserializerInstance(Annotated ann, Object deserDef)
    throws JsonMappingException
{
    if (deserDef == null) {
        return null;
    }

    KeyDeserializer deser;
    
    if (deserDef instanceof KeyDeserializer) {
        deser = (KeyDeserializer) deserDef;
    } else {
        if (!(deserDef instanceof Class)) {
            throw new IllegalStateException("AnnotationIntrospector returned key deserializer definition of type "
                    +deserDef.getClass().getName()
                    +"; expected type KeyDeserializer or Class<KeyDeserializer> instead");
        }
        Class<?> deserClass = (Class<?>)deserDef;
        // there are some known "no class" markers to consider too:
        if (deserClass == KeyDeserializer.None.class || ClassUtil.isBogusClass(deserClass)) {
            return null;
        }
        if (!KeyDeserializer.class.isAssignableFrom(deserClass)) {
            throw new IllegalStateException("AnnotationIntrospector returned Class "+deserClass.getName()
                    +"; expected Class<KeyDeserializer>");
        }
        HandlerInstantiator hi = _config.getHandlerInstantiator();
        deser = (hi == null) ? null : hi.keyDeserializerInstance(_config, ann, deserClass);
        if (deser == null) {
            deser = (KeyDeserializer) ClassUtil.createInstance(deserClass,
                    _config.canOverrideAccessModifiers());
        }
    }
    // First: need to resolve
    if (deser instanceof ResolvableDeserializer) {
        ((ResolvableDeserializer) deser).resolve(this);
    }
    return deser;
}
 
Example #9
Source File: BasicDeserializerFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig config,
        Annotated annotated, Object instDef)
    throws JsonMappingException
{
    if (instDef == null) {
        return null;
    }

    ValueInstantiator inst;
    
    if (instDef instanceof ValueInstantiator) {
        return (ValueInstantiator) instDef;
    }
    if (!(instDef instanceof Class)) {
        throw new IllegalStateException("AnnotationIntrospector returned key deserializer definition of type "
                +instDef.getClass().getName()
                +"; expected type KeyDeserializer or Class<KeyDeserializer> instead");
    }
    Class<?> instClass = (Class<?>)instDef;
    if (ClassUtil.isBogusClass(instClass)) {
        return null;
    }
    if (!ValueInstantiator.class.isAssignableFrom(instClass)) {
        throw new IllegalStateException("AnnotationIntrospector returned Class "+instClass.getName()
                +"; expected Class<ValueInstantiator>");
    }
    HandlerInstantiator hi = config.getHandlerInstantiator();
    if (hi != null) {
        inst = hi.valueInstantiatorInstance(config, annotated, instClass);
        if (inst != null) {
            return inst;
        }
    }
    return (ValueInstantiator) ClassUtil.createInstance(instClass,
            config.canOverrideAccessModifiers());
}
 
Example #10
Source File: POJOPropertiesCollector.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private PropertyNamingStrategy _findNamingStrategy()
{
    Object namingDef = _annotationIntrospector.findNamingStrategy(_classDef);
    if (namingDef == null) {
        return _config.getPropertyNamingStrategy();
    }
    if (namingDef instanceof PropertyNamingStrategy) {
        return (PropertyNamingStrategy) namingDef;
    }
    /* Alas, there's no way to force return type of "either class
     * X or Y" -- need to throw an exception after the fact
     */
    if (!(namingDef instanceof Class)) {
        throw new IllegalStateException("AnnotationIntrospector returned PropertyNamingStrategy definition of type "
                +namingDef.getClass().getName()+"; expected type PropertyNamingStrategy or Class<PropertyNamingStrategy> instead");
    }
    Class<?> namingClass = (Class<?>)namingDef;
    // 09-Nov-2015, tatu: Need to consider pseudo-value of STD, which means "use default"
    if (namingClass == PropertyNamingStrategy.class) {
        return null;
    }
    
    if (!PropertyNamingStrategy.class.isAssignableFrom(namingClass)) {
        throw new IllegalStateException("AnnotationIntrospector returned Class "
                +namingClass.getName()+"; expected Class<PropertyNamingStrategy>");
    }
    HandlerInstantiator hi = _config.getHandlerInstantiator();
    if (hi != null) {
        PropertyNamingStrategy pns = hi.namingStrategyInstance(_config, _classDef, namingClass);
        if (pns != null) {
            return pns;
        }
    }
    return (PropertyNamingStrategy) ClassUtil.createInstance(namingClass,
                _config.canOverrideAccessModifiers());
}
 
Example #11
Source File: BasicBeanDescription.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected Converter<Object,Object> _createConverter(Object converterDef)
{
    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>");
    }
    HandlerInstantiator hi = _config.getHandlerInstantiator();
    Converter<?,?> conv = (hi == null) ? null : hi.converterInstance(_config, _classInfo, converterClass);
    if (conv == null) {
        conv = (Converter<?,?>) ClassUtil.createInstance(converterClass,
                _config.canOverrideAccessModifiers());
    }
    return (Converter<Object,Object>) conv;
}
 
Example #12
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 #13
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 #14
Source File: DatabindContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ObjectIdGenerator<?> objectIdGeneratorInstance(Annotated annotated,
        ObjectIdInfo objectIdInfo)
    throws JsonMappingException
{
    Class<?> implClass = objectIdInfo.getGeneratorType();
    final MapperConfig<?> config = getConfig();
    HandlerInstantiator hi = config.getHandlerInstantiator();
    ObjectIdGenerator<?> gen = (hi == null) ? null : hi.objectIdGeneratorInstance(config, annotated, implClass);
    if (gen == null) {
        gen = (ObjectIdGenerator<?>) ClassUtil.createInstance(implClass,
                config.canOverrideAccessModifiers());
    }
    return gen.forScope(objectIdInfo.getScope());
}
 
Example #15
Source File: DefaultSerializerProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object includeFilterInstance(BeanPropertyDefinition forProperty,
        Class<?> filterClass)
{
    if (filterClass == null) {
        return null;
    }
    HandlerInstantiator hi = _config.getHandlerInstantiator();
    Object filter = (hi == null) ? null : hi.includeFilterInstance(_config, forProperty, filterClass);
    if (filter == null) {
        filter = ClassUtil.createInstance(filterClass,
                _config.canOverrideAccessModifiers());
    }
    return filter;
}
 
Example #16
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 #17
Source File: JacksonJsonLayoutTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void builderBuildsMapperWithCustomHandlerInstantiator() {

    // given
    JacksonJsonLayout.Builder builder = spy(createDefaultTestBuilder());

    ObjectMapper objectMapper = spy(ObjectMapper.class);

    when(builder.createDefaultObjectMapper()).thenReturn(objectMapper);

    // when
    builder.build();

    // then
    ArgumentCaptor<SerializationConfig> captor = ArgumentCaptor.forClass(SerializationConfig.class);
    verify(objectMapper).setConfig(captor.capture());

    HandlerInstantiator handlerInstantiator = captor.getValue().getHandlerInstantiator();
    assertTrue(handlerInstantiator instanceof JacksonHandlerInstantiator);

}
 
Example #18
Source File: RestOperationsFactoryTest.java    From bowman with Apache License 2.0 4 votes vote down vote up
HandlerInstantiatorTestParams(Class<?> clazz,
	BiFunction<HandlerInstantiator, Class<?>, Object> instantiationMethod) {
	
	this.clazz = clazz;
	this.instantiationMethod = instantiationMethod;
}
 
Example #19
Source File: RestConfiguration.java    From taskana with Apache License 2.0 4 votes vote down vote up
@Bean
public HandlerInstantiator handlerInstantiator(ApplicationContext context) {
  return new SpringHandlerInstantiator(context.getAutowireCapableBeanFactory());
}
 
Example #20
Source File: RestOperationsFactoryTest.java    From bowman with Apache License 2.0 3 votes vote down vote up
@Theory
public void createInstantiatesObjectMapperWithNonLibraryHandlerAwareHandlerInstantiator(
	@ParametersSuppliedBy(NonLibraryHandlerTestParams.class) HandlerInstantiatorTestParams params) {

	factory.create();
	
	ArgumentCaptor<HandlerInstantiator> handlerInstantiator = ArgumentCaptor.forClass(HandlerInstantiator.class);
	verify(mapperFactory).create(handlerInstantiator.capture());
	
	Object result = params.instantiationMethod.apply(handlerInstantiator.getValue(), params.clazz);
	
	assertThat(result, instanceOf(params.clazz));
}
 
Example #21
Source File: Jackson2ObjectMapperFactoryBean.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Customize the construction of Jackson handlers
 * ({@link JsonSerializer}, {@link JsonDeserializer}, {@link KeyDeserializer},
 * {@code TypeResolverBuilder} and {@code TypeIdResolver}).
 * @since 4.1.3
 * @see Jackson2ObjectMapperFactoryBean#setApplicationContext(ApplicationContext)
 */
public void setHandlerInstantiator(HandlerInstantiator handlerInstantiator) {
	this.builder.handlerInstantiator(handlerInstantiator);
}
 
Example #22
Source File: Jackson2ObjectMapperFactoryBean.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Customize the construction of Jackson handlers
 * ({@link JsonSerializer}, {@link JsonDeserializer}, {@link KeyDeserializer},
 * {@code TypeResolverBuilder} and {@code TypeIdResolver}).
 * @since 4.1.3
 * @see Jackson2ObjectMapperFactoryBean#setApplicationContext(ApplicationContext)
 */
public void setHandlerInstantiator(HandlerInstantiator handlerInstantiator) {
	this.builder.handlerInstantiator(handlerInstantiator);
}
 
Example #23
Source File: Jackson2ObjectMapperFactoryBean.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Customize the construction of Jackson handlers
 * ({@link JsonSerializer}, {@link JsonDeserializer}, {@link KeyDeserializer},
 * {@code TypeResolverBuilder} and {@code TypeIdResolver}).
 * @since 4.1.3
 * @see Jackson2ObjectMapperFactoryBean#setApplicationContext(ApplicationContext)
 */
public void setHandlerInstantiator(HandlerInstantiator handlerInstantiator) {
	this.builder.handlerInstantiator(handlerInstantiator);
}
 
Example #24
Source File: Jackson2ObjectMapperFactoryBean.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Customize the construction of Jackson handlers
 * ({@link JsonSerializer}, {@link JsonDeserializer}, {@link KeyDeserializer},
 * {@code TypeResolverBuilder} and {@code TypeIdResolver}).
 * @since 4.1.3
 * @see Jackson2ObjectMapperFactoryBean#setApplicationContext(ApplicationContext)
 */
public void setHandlerInstantiator(HandlerInstantiator handlerInstantiator) {
	this.builder.handlerInstantiator(handlerInstantiator);
}
 
Example #25
Source File: ObjectMapperFactory.java    From bowman with Apache License 2.0 votes vote down vote up
ObjectMapper create(HandlerInstantiator instantiator);