Java 类javax.json.bind.annotation.JsonbTypeAdapter 实例源码

项目:yasson    文件:AnnotationIntrospector.java   
/**
 * Checks for {@link JsonbAdapter} on a property.
 * @param property property not null
 * @return components info
 */
public AdapterBinding getAdapterBinding(Property property) {
    Objects.requireNonNull(property);
    JsonbTypeAdapter adapterAnnotation = getAnnotationFromProperty(JsonbTypeAdapter.class, property)
            .orElseGet(()-> getAnnotationFromPropertyType(property, JsonbTypeAdapter.class));
    if (adapterAnnotation == null) {
        return null;
    }

    return getAdapterBindingFromAnnotation(adapterAnnotation, ReflectionUtils.getOptionalRawType(property.getPropertyType()));
}
项目:yasson    文件:AnnotationIntrospector.java   
private AdapterBinding getAdapterBindingFromAnnotation(JsonbTypeAdapter adapterAnnotation, Optional<Class<?>> expectedClass) {
    final Class<? extends JsonbAdapter> adapterClass = adapterAnnotation.value();
    final AdapterBinding adapterBinding = jsonbContext.getComponentMatcher().introspectAdapterBinding(adapterClass, null);

    if (expectedClass.isPresent() && !(ReflectionUtils.getRawType(adapterBinding.getBindingType()).equals(expectedClass.get()))) {
        throw new JsonbException(Messages.getMessage(MessageKeys.ADAPTER_INCOMPATIBLE, adapterBinding.getBindingType(), expectedClass.get()));
    }
    return adapterBinding;
}
项目:johnzon    文件:JsonbAccessMode.java   
private void validateAnnotations(final Object parameter,
                                 final JsonbTypeAdapter adapter, final JsonbDateFormat dateFormat,
                                 final JsonbNumberFormat numberFormat,
                                 final JohnzonConverter johnzonConverter) {
    int notNull = adapter != null ? 1 : 0;
    notNull += dateFormat != null ? 1 : 0;
    notNull += numberFormat != null ? 1 : 0;
    notNull += johnzonConverter != null ? 1 : 0;
    if (notNull > 1) {
        throw new IllegalArgumentException("Conflicting @JsonbXXX/@JohnzonConverter on " + parameter);
    }
}
项目:johnzon    文件:JsonbAccessMode.java   
private Adapter<?, ?> toConverter(final Type type,
                                  final JsonbTypeAdapter adapter, final JsonbDateFormat dateFormat,
                                  final JsonbNumberFormat numberFormat) throws InstantiationException, IllegalAccessException {
    final Adapter converter;
    if (adapter != null) {
        final Class<? extends JsonbAdapter> value = adapter.value();
        final ParameterizedType pt = findPt(value, JsonbAdapter.class);
        if (pt == null) {
            throw new IllegalArgumentException(value + " doesn't implement JsonbAdapter");
        }
        final JohnzonAdapterFactory.Instance<? extends JsonbAdapter> instance = newInstance(value);
        toRelease.add(instance);
        final Type[] actualTypeArguments = pt.getActualTypeArguments();
        converter = new JohnzonJsonbAdapter(instance.getValue(), actualTypeArguments[0], actualTypeArguments[1]);
    } else if (dateFormat != null) { // TODO: support lists, LocalDate?
        if (Date.class == type) {
            converter = new ConverterAdapter<>(new JsonbDateConverter(dateFormat));
        } else if (LocalDateTime.class == type) {
            converter = new ConverterAdapter<>(new JsonbLocalDateTimeConverter(dateFormat));
        } else if (LocalDate.class == type) {
            converter = new ConverterAdapter<>(new JsonbLocalDateConverter(dateFormat));
        } else if (ZonedDateTime.class == type) {
            converter = new ConverterAdapter<>(new JsonbZonedDateTimeConverter(dateFormat));
        } else {
            throw new IllegalArgumentException(type + " not a supported date type");
        }
    } else if (numberFormat != null) {  // TODO: support lists?
        converter = new ConverterAdapter<>(new JsonbNumberConverter(numberFormat));
    } else {
        converter = new ConverterAdapter<>(new JsonbValueConverter());
    }
    return converter;
}