Skip to content

Commit

Permalink
Groundwork for #123: remove Annotations enum from 3.0, take `Annota…
Browse files Browse the repository at this point in the history
…tionIntrospector` directly.

Does not yet remove JAXB dependency (leaving JAXB-provider subtypes in place for now).
  • Loading branch information
cowtowncoder committed Mar 12, 2021
1 parent d3a2747 commit 2f00fec
Show file tree
Hide file tree
Showing 20 changed files with 301 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ public void addUntouchable(Class<?> type)
/**
* Method for removing definition of specified type as untouchable:
* usually only
*
* @since 2.2
*/
public void removeUntouchable(Class<?> type)
{
Expand All @@ -243,17 +241,17 @@ public void removeUntouchable(Class<?> type)
}

/**
* Method for configuring which annotation sets to use (including none).
* Annotation sets are defined in order decreasing precedence; that is,
* first one has the priority over following ones.
* Method for overriding {@link AnnotationIntrospector} to use instead of
* default {@code JacksonAnnotationIntrospector}: often used to add
* JAXB-backed introspector.
*
* @param annotationsToUse Ordered list of annotation sets to use; if null,
* default
* @param aiOverride AnnotationIntrospector to configure mapper to use
* ({@code null} to leave it as default one)
*/
public void setAnnotationsToUse(Annotations[] annotationsToUse) {
_mapperConfig.setAnnotationsToUse(annotationsToUse);
public void setAnnotationsToUse(AnnotationIntrospector aiOverride) {
_mapperConfig.setAnnotationIntrospector(aiOverride);
}

/**
* Method that can be used to directly define {@link ObjectMapper} to use
* for serialization and deserialization; if null, will use the standard
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.fasterxml.jackson.jaxrs.cfg;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.Map;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;

/**
* Helper class used to encapsulate details of configuring an
Expand Down Expand Up @@ -45,11 +43,13 @@ public abstract class MapperConfiguratorBase<IMPL extends MapperConfiguratorBase
*/

/**
* Annotations set to use by default; overridden by explicit call
* to {@link #setAnnotationsToUse}
* {@code AnnotationIntrospector} to use as an override over default
* {@code JacksonAnnotationIntrospector}, if any.
*
* @since 3.0
*/
protected Annotations[] _annotationsToUse;
protected AnnotationIntrospector _instropectorOverride;

/*
/**********************************************************************
/* Lazily constructed Mapper instance(s)
Expand Down Expand Up @@ -77,11 +77,12 @@ public abstract class MapperConfiguratorBase<IMPL extends MapperConfiguratorBase
/* Life-cycle
/**********************************************************************
*/

public MapperConfiguratorBase(MAPPER mapper, Annotations[] defaultAnnotations)

public MapperConfiguratorBase(MAPPER mapper,
AnnotationIntrospector instropectorOverride)
{
_mapper = mapper;
_annotationsToUse = defaultAnnotations;
_instropectorOverride = instropectorOverride;
}

public synchronized MAPPER getDefaultMapper() {
Expand Down Expand Up @@ -134,8 +135,8 @@ public synchronized final void setMapper(MAPPER m) {
_mapper = m;
}

public synchronized final void setAnnotationsToUse(Annotations[] annotationsToUse) {
_annotationsToUse = annotationsToUse;
public synchronized final void setAnnotationIntrospector(AnnotationIntrospector aiOverride) {
_instropectorOverride = aiOverride;
}

public final void configure(DeserializationFeature f, boolean state) {
Expand Down Expand Up @@ -174,13 +175,9 @@ protected MAPPER _mapperWithConfiguration(MapperBuilder<?,?> mapperBuilder)
protected MapperBuilder<?,?> _builderWithConfiguration(MapperBuilder<?,?> mapperBuilder)
{
// First, AnnotationIntrospector settings
AnnotationIntrospector intr;
if ((_annotationsToUse == null) || (_annotationsToUse.length == 0)) {
intr = AnnotationIntrospector.nopInstance();
} else {
intr = _resolveIntrospectors(_annotationsToUse);
if (_instropectorOverride != null) {
mapperBuilder = mapperBuilder.annotationIntrospector(_instropectorOverride);
}
mapperBuilder = mapperBuilder.annotationIntrospector(intr);

// Features?
if (_mapperFeatures != null) {
Expand All @@ -203,6 +200,7 @@ protected MapperBuilder<?,?> _builderWithConfiguration(MapperBuilder<?,?> mapper
return mapperBuilder;
}

/*
protected AnnotationIntrospector _resolveIntrospectors(Annotations[] annotationsToUse)
{
// Let's ensure there are no dups there first, filter out nulls
Expand All @@ -222,23 +220,5 @@ protected AnnotationIntrospector _resolveIntrospectors(Annotations[] annotations
}
return curr;
}

protected AnnotationIntrospector _resolveIntrospector(Annotations ann)
{
switch (ann) {
case JACKSON:
return _jacksonIntrospector();
case JAXB:
return _jaxbIntrospector();
default:
throw new IllegalStateException();
}
}

// Separate method to allow overriding
protected AnnotationIntrospector _jacksonIntrospector() {
return new JacksonAnnotationIntrospector();
}

protected abstract AnnotationIntrospector _jaxbIntrospector();
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper;
import com.fasterxml.jackson.jaxrs.cfg.Annotations;

import com.fasterxml.jackson.jaxrs.cfg.MapperConfiguratorBase;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;

/**
* Helper class used to encapsulate details of configuring an
* {@link ObjectMapper} instance to be used for data binding, as
* well as accessing it.
*/
public class CBORMapperConfigurator
extends MapperConfiguratorBase<CBORMapperConfigurator, ObjectMapper>
extends MapperConfiguratorBase<CBORMapperConfigurator, CBORMapper>
{
public CBORMapperConfigurator(ObjectMapper mapper, Annotations[] defAnnotations)
public CBORMapperConfigurator(CBORMapper mapper,
AnnotationIntrospector aiOverride)
{
super(mapper, defAnnotations);
super(mapper, aiOverride);
}

/*
Expand All @@ -32,18 +32,4 @@ public CBORMapperConfigurator(ObjectMapper mapper, Annotations[] defAnnotations)
protected MapperBuilder<?,?> mapperBuilder() {
return CBORMapper.builder(new CBORFactory());
}

@Override
protected AnnotationIntrospector _jaxbIntrospector() {
return JaxbHolder.get();
}

// Silly class to encapsulate reference to JAXB introspector class so that
// loading of parent class does not require it; only happens if and when
// introspector needed
private static class JaxbHolder {
public static AnnotationIntrospector get() {
return new JaxbAnnotationIntrospector();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import javax.ws.rs.ext.*;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;

import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper;

import com.fasterxml.jackson.jaxrs.base.ProviderBase;
import com.fasterxml.jackson.jaxrs.cfg.Annotations;

/**
* Basic implementation of JAX-RS abstractions ({@link MessageBodyReader},
Expand Down Expand Up @@ -47,24 +49,15 @@
@Produces(MediaType.WILDCARD)
public class JacksonCBORProvider
extends ProviderBase<JacksonCBORProvider,
ObjectMapper,
CBORMapper,
CBOREndpointConfig,
CBORMapperConfigurator
>
{
/**
* Default annotation sets to use, if not explicitly defined during
* construction: only Jackson annotations are used for the base
* class. Sub-classes can use other settings.
*/
public final static Annotations[] BASIC_ANNOTATIONS = {
Annotations.JACKSON
};

/*
/**********************************************************
/**********************************************************************
/* Context configuration
/**********************************************************
/**********************************************************************
*/

/**
Expand All @@ -76,44 +69,35 @@ public class JacksonCBORProvider
protected Providers _providers;

/*
/**********************************************************
/**********************************************************************
/* Construction
/**********************************************************
/**********************************************************************
*/

/**
* Default constructor, usually used when provider is automatically
* configured to be used with JAX-RS implementation.
*/
public JacksonCBORProvider() {
this(null, BASIC_ANNOTATIONS);
this(null, null);
}

/**
* @param annotationsToUse Annotation set(s) to use for configuring
* data binding
*/
public JacksonCBORProvider(Annotations... annotationsToUse)
public JacksonCBORProvider(CBORMapper mapper)
{
this(null, annotationsToUse);
this(mapper, null);
}

public JacksonCBORProvider(ObjectMapper mapper)
{
this(mapper, BASIC_ANNOTATIONS);
}

/**
* Constructor to use when a custom mapper (usually components
* like serializer/deserializer factories that have been configured)
* is to be used.
*
* @param annotationsToUse Sets of annotations (Jackson, JAXB) that provider should
* support
*
* @param aiOverride AnnotationIntrospector to override default with, if any
*/
public JacksonCBORProvider(ObjectMapper mapper, Annotations[] annotationsToUse)
public JacksonCBORProvider(CBORMapper mapper,
AnnotationIntrospector aiOverride)
{
super(new CBORMapperConfigurator(mapper, annotationsToUse));
super(new CBORMapperConfigurator(mapper, aiOverride));
}

/**
Expand All @@ -124,11 +108,11 @@ public JacksonCBORProvider(ObjectMapper mapper, Annotations[] annotationsToUse)
public Version version() {
return PackageVersion.VERSION;
}

/*
/**********************************************************
/**********************************************************************
/* Abstract method impls
/**********************************************************
/**********************************************************************
*/

/**
Expand Down Expand Up @@ -180,26 +164,21 @@ protected boolean hasMatchingMediaType(MediaType mediaType)
* but will be passed to {@link ContextResolver} as is.
*/
@Override
protected ObjectMapper _locateMapperViaProvider(Class<?> type, MediaType mediaType)
protected CBORMapper _locateMapperViaProvider(Class<?> type, MediaType mediaType)
{
// First: were we configured with a specific instance?
ObjectMapper m = _mapperConfig.getConfiguredMapper();
CBORMapper m = _mapperConfig.getConfiguredMapper();
if (m == null) {
// If not, maybe we can get one configured via context?
if (_providers != null) {
ContextResolver<ObjectMapper> resolver = _providers.getContextResolver(ObjectMapper.class, mediaType);
ContextResolver<CBORMapper> resolver = _providers.getContextResolver(CBORMapper.class, mediaType);
// Above should work as is, but due to this bug
// [https://jersey.dev.java.net/issues/show_bug.cgi?id=288]
// in Jersey, it doesn't. But this works until resolution of the issue:
// in Jersey, it doesn't. But this works until resolution of
// the issue:
if (resolver == null) {
resolver = _providers.getContextResolver(ObjectMapper.class, null);
resolver = _providers.getContextResolver(CBORMapper.class, null);
}
if (resolver != null) {
m = resolver.getContext(type);
// 07-Feb-2014, tatu: just in case, ensure we have correct type
if (m.tokenStreamFactory() instanceof CBORFactory) {
return m;
}
}
}
if (m == null) {
Expand Down
Loading

0 comments on commit 2f00fec

Please sign in to comment.