question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable turning off look-up optimisation in TypeFactory

See original GitHub issue

While 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?

  1. Simply remove the final keyword on this one class,
  2. 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,
  3. add a setting for turning the Enum/Comparable optimisation on/off?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, May 30, 2019

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.

0reactions
cowtowncodercommented, Jun 7, 2019

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

LocalizationProvider/ConfigurationContext.cs at master - GitHub
Gets or sets the callback for enabling or disabling localization. ... Gets or sets callback whether lookup resource by requested key. /// Use...
Read more >
SUNSAVER DUOTM - Morningstar Corporation
2) Turn off any other charging sources on the battery. If the noise stops, an alternate charging source is producing noise. 3) Remove...
Read more >
Chapter 16. Configuration - Index of / - EsperTech
The API to use to enable debug logging and disable timer event output is shown here: Configuration config = new Configuration(); config.getEngineDefaults().
Read more >
Hibernate Search 6.1.7.Final: Reference Documentation
To disable annotation mapping, set hibernate.search.mapping.process_annotations to false . 5.2.2. Mapping Map -based models. " ...
Read more >
SUSE-SU-2022:1678-1: important: Security update for jackson ...
writeString(Reader, int)' + Allow "optional-padding" for 'Base64Variant' ... Type' + Disabling 'FAIL_ON_INVALID_SUBTYPE' breaks polymorphic ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found