Java Code Examples for com.fasterxml.jackson.databind.util.ClassUtil#createInstance()

The following examples show how to use com.fasterxml.jackson.databind.util.ClassUtil#createInstance() . 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: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
/**
 * @param ignoreXmlIDREF Whether {@link XmlIDREF} annotation should be processed
 *   JAXB style (meaning that references are always serialized using id), or
 *   not (first reference as full POJO, others as ids)
 */
public JaxbAnnotationIntrospector(boolean ignoreXmlIDREF)
{
    _ignoreXmlIDREF = ignoreXmlIDREF;
    _jaxbPackageName = XmlElement.class.getPackage().getName();

    JsonSerializer<?> dataHandlerSerializer = null;
    JsonDeserializer<?> dataHandlerDeserializer = null;
    // Data handlers included dynamically, to try to prevent issues on platforms
    // with less than complete support for JAXB API
    try {
        dataHandlerSerializer = ClassUtil.createInstance(DataHandlerJsonSerializer.class, false);
        dataHandlerDeserializer = ClassUtil.createInstance(DataHandlerJsonDeserializer.class, false);
    } catch (Throwable e) {
        //dataHandlers not supported...
    }
    _dataHandlerSerializer = dataHandlerSerializer;
    _dataHandlerDeserializer = dataHandlerDeserializer;
}
 
Example 2
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private XmlAdapter<Object,Object> findAdapterForClass(AnnotatedClass ac, boolean forSerialization)
{
    /* As per [JACKSON-411], XmlJavaTypeAdapter should not be inherited from super-class.
     * It would still be nice to be able to use mix-ins; but unfortunately we seem to lose
     * knowledge of class that actually declared the annotation. Thus, we'll only accept
     * declaration from specific class itself.
     */
    XmlJavaTypeAdapter adapterInfo = ac.getAnnotated().getAnnotation(XmlJavaTypeAdapter.class);
    if (adapterInfo != null) { // should we try caching this?
        @SuppressWarnings("rawtypes")
        Class<? extends XmlAdapter> cls = adapterInfo.value();
        // true -> yes, force access if need be
        return ClassUtil.createInstance(cls, true);
    }
    return null;
}
 
Example 3
Source File: TestSimpleMaterializedInterfaces.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
/**
 * First test verifies that bean builder works as expected
 */
public void testLowLevelMaterializer() throws Exception
{
    AbstractTypeMaterializer mat = new AbstractTypeMaterializer();
    DeserializationConfig config = new ObjectMapper().deserializationConfig();
    Class<?> impl = _materializeRawType(mat, config, Bean.class);
    assertNotNull(impl);
    assertTrue(Bean.class.isAssignableFrom(impl));
    // also, let's instantiate to make sure:
    Object ob = ClassUtil.createInstance(impl, false);
    // and just for good measure do actual cast
    Bean bean = (Bean) ob;
    // call something to ensure generation worked...
    assertNull(bean.getA());

    // Also: let's verify that we can handle dup calls:
    Class<?> impl2 = _materializeRawType(mat, config, Bean.class);
    assertNotNull(impl2);
    assertSame(impl, impl2);
}
 
Example 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 14
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 15
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 16
Source File: RosettaAnnotationIntrospector.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public JsonDeserializer<?> findDeserializer(Annotated a) {
  StoredAsJson storedAsJson = a.getAnnotation(StoredAsJson.class);
  RosettaDeserialize rosettaDeserialize = a.getAnnotation(RosettaDeserialize.class);
  if (storedAsJson != null && rosettaDeserialize != null) {
    throw new IllegalArgumentException("Cannot have @StoredAsJson as well as @RosettaDeserialize annotations on the same entry");
  }
  if (storedAsJson != null) {
    if (a instanceof AnnotatedMethod) {
      a = getAnnotatedTypeFromAnnotatedMethod((AnnotatedMethod) a);
    }

    String empty = StoredAsJson.NULL.equals(storedAsJson.empty()) ? "null" : storedAsJson.empty();
    return new StoredAsJsonDeserializer(a.getRawType(), getType(a), empty, objectMapper);
  }

  if (rosettaDeserialize != null) {
    Class<? extends JsonDeserializer> klass = rosettaDeserialize.using();
    if (klass != JsonDeserializer.None.class) {
      return ClassUtil.createInstance(
          klass,
          objectMapper.getDeserializationConfig().canOverrideAccessModifiers());
    }
  }

  return null;
}