Enable turning off look-up optimisation in TypeFactory
See original GitHub issueWhile building a library for generating JSON Schemas (Draft 7) from Java types (which is not limited to JSON but can also be used to document the Java type itself in the form of a JSON Schema), I ended up “re-inventing the wheel” for the whole type resolution topic, which works fine but is a whole new API for something that already exists here in jackson-databind
.
Resolution: I’m throwing away my own type resolution implementation in favour of the jackson
JavaType
, TypeFactory
etc.
However, some limitations (such as ignoring static
fields and methods) are hard-coded in final
classes such as AnnotatedFieldCollector
and AnnotatedMethodCollector
, which I had to copy-paste and then adjust instead of being able to simply create a sub-type and override accordingly.
While those two classes were simple/small enough for that kind of approach (even though ugly), doing the same for the TypeFactory
is impractical (where Enum
and Comparable
are “optimised” in a way, that their self-references are being ignored – which I want to have in order to properly resolve the parameter and return types of respective methods).
protected JavaType _fromParamType(ClassStack context, ParameterizedType ptype,
TypeBindings parentBindings)
{
// Assumption here is we'll always get Class, not one of other Types
Class<?> rawType = (Class<?>) ptype.getRawType();
// 29-Oct-2015, tatu: For performance reasons, let's streamline handling of
// couple of not-so-useful parametric types
if (rawType == CLS_ENUM) {
return CORE_TYPE_ENUM;
}
if (rawType == CLS_COMPARABLE) {
return CORE_TYPE_COMPARABLE;
}
Long story short: my use-case is a special one that arises from using this type resolution logic outside of the limitations of JSON, i.e. I don’t expect jackson-databind
to support those directly, but removing the final
modifiers from (at least) the TypeFactory
class would go a long way in making all that re-usable. The affected methods are already protected
(oddly enough in a final
class).
An alternative way of removing the unwanted optimisation would, of course, be appreciated as well.
I’m currently basing this on jackson
(-databind
) version 2.9.9.
I can appreciate that Joshua Bloch’s recommendation in “Effective Java” is being followed here (“Design and document for inheritance or else prohibit it”) and looking at the code of the TypeFactory
it really does not look like it was designed for inheritance (apart from the existence of numerous protected
methods).
With that in mind, adding a (by default enabled) option for including those “optimisations” would be my preferred choice, but introducing this (albeit small) additional complexity for my non-JSON use-case seems wrong.
What do you reckon?
- Simply remove the
final
keyword on this one class, - introduce an interface implemented by the standard
TypeFactory
which can be implemented by an alternate wrapper/proxy, and that is used throughout various places instead of the specific class, - add a setting for turning the
Enum
/Comparable
optimisation on/off?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Ok, first one quick suggestion: depending on exactly what parts you find useful, you might want to have a look at ClassMate library instead:
https://github.com/FasterXML/java-classmate
because it handles generic type resolution very well (I wrote it based on findings from Jackson
TypeFactory
). It does not have POJO property inference pieces, but core type resolution for types, subtypes, fields and methods is rock solid. It also lacks any Jackson-use-case-specific business logic.@CarstenWickner excellent!
I’ll create another issue so I remember to add a link to this tool, as a replacement for Jackson’s obsolete JSON schema module.