/**
   * Helper method called to construct and initialize instance of {@link TypeResolverBuilder} if
   * given annotated element indicates one is needed.
   */
  @SuppressWarnings("deprecation")
  protected TypeResolverBuilder<?> _findTypeResolver(
      MapperConfig<?> config, Annotated ann, JavaType baseType) {
    // First: maybe we have explicit type resolver?
    TypeResolverBuilder<?> b;
    JsonTypeInfo info = _findAnnotation(ann, JsonTypeInfo.class);
    JsonTypeResolver resAnn = _findAnnotation(ann, JsonTypeResolver.class);

    if (resAnn != null) {
      if (info == null) {
        return null;
      }
      /* let's not try to force access override (would need to pass
       * settings through if we did, since that's not doable on some
       * platforms)
       */
      b = config.typeResolverBuilderInstance(ann, resAnn.value());
    } else { // if not, use standard one, if indicated by annotations
      if (info == null) {
        return null;
      }
      // bit special; must return 'marker' to block use of default typing:
      if (info.use() == JsonTypeInfo.Id.NONE) {
        return _constructNoTypeResolverBuilder();
      }
      b = _constructStdTypeResolverBuilder();
    }
    // Does it define a custom type id resolver?
    JsonTypeIdResolver idResInfo = _findAnnotation(ann, JsonTypeIdResolver.class);
    TypeIdResolver idRes =
        (idResInfo == null) ? null : config.typeIdResolverInstance(ann, idResInfo.value());
    if (idRes != null) { // [JACKSON-359]
      idRes.init(baseType);
    }
    b = b.init(info.use(), idRes);
    /* 13-Aug-2011, tatu: One complication wrt [JACKSON-453]; external id
     *   only works for properties; so if declared for a Class, we will need
     *   to map it to "PROPERTY" instead of "EXTERNAL_PROPERTY"
     */
    JsonTypeInfo.As inclusion = info.include();
    if (inclusion == JsonTypeInfo.As.EXTERNAL_PROPERTY && (ann instanceof AnnotatedClass)) {
      inclusion = JsonTypeInfo.As.PROPERTY;
    }
    b = b.inclusion(inclusion);
    b = b.typeProperty(info.property());
    Class<?> defaultImpl = info.defaultImpl();

    // 08-Dec-2014, tatu: To deprecated `JsonTypeInfo.None` we need to use other placeholder(s);
    //   and since `java.util.Void` has other purpose (to indicate "deser as null"), we'll instead
    //   use `JsonTypeInfo.class` itself. But any annotation type will actually do, as they have no
    //   valid use (can not instantiate as default)
    if (defaultImpl != JsonTypeInfo.None.class && !defaultImpl.isAnnotation()) {
      b = b.defaultImpl(defaultImpl);
    }
    b = b.typeIdVisibility(info.visible());
    return b;
  }
Ejemplo n.º 2
0
  /**
   * @return Null if class might be a bean; type String (that identifies why it's not a bean) if not
   */
  public static String canBeABeanType(Class<?> type) {
    // First: language constructs that ain't beans:
    if (type.isAnnotation()) {
      return "annotation";
    }
    if (type.isArray()) {
      return "array";
    }
    if (type.isEnum()) {
      return "enum";
    }
    if (type.isPrimitive()) {
      return "primitive";
    }

    // Anything else? Seems valid, then
    return null;
  }