Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve TypeFactory.constructFromCanonical() to work with `java.lang.reflect.Type.getTypeName()' format #1768

Closed
luiscleto opened this issue Sep 18, 2017 · 3 comments
Milestone

Comments

@luiscleto
Copy link

Java's Type#getTypeName() for parameterized types with multiple type parameters returns the inner types in a comma and space separated list. (e.g. "java.util.Map<java.lang.Integer, java.lang.Double>").

Attempting to construct the type using TypeFactory's constructFromCanonical will result in an exception as the inner tokens are not trimmed (although the top level String is). For the example above it would be

java.lang.IllegalArgumentException: Failed to parse type 'java.util.Map<java.lang.Integer, java.lang.Double>' (remaining: '>'): Can not locate class ' java.lang.Double', problem:  java.lang.Double
	at com.fasterxml.jackson.databind.type.TypeParser._problem(TypeParser.java:87)
	at com.fasterxml.jackson.databind.type.TypeParser.findClass(TypeParser.java:81)
	at com.fasterxml.jackson.databind.type.TypeParser.parseType(TypeParser.java:44)
	at com.fasterxml.jackson.databind.type.TypeParser.parseTypes(TypeParser.java:62)
	at com.fasterxml.jackson.databind.type.TypeParser.parseType(TypeParser.java:49)
	at com.fasterxml.jackson.databind.type.TypeParser.parse(TypeParser.java:30)
	at com.fasterxml.jackson.databind.type.TypeFactory.constructFromCanonical(TypeFactory.java:236)

Since using Jackson's own JavaTypes to obtain the canonical form does not yield inner spaces, no error will occur for that scenario, hence I see this as a feature I'd like to have rather than a bug. This could be easily achieved if findClass trimmed the String it receives (or received an already trimmed token).

@luiscleto
Copy link
Author

Replicating the above issue:

        objects = new TreeMap<Object, Object>() { };
        t = tf.constructType(java.util.TreeMap.class);
        reflectTypeName = objects.getClass().getGenericSuperclass().getTypeName();
        System.out.println(reflectTypeName);
        // This will throw an Exception if you don't remove all white spaces from the String.
        System.out.println(t.equals(tf.constructFromCanonical(reflectTypeName)));

@cowtowncoder
Copy link
Member

Sounds reasonable enough improvement.

@cowtowncoder cowtowncoder added this to the 2.9.2 milestone Sep 19, 2017
@cowtowncoder cowtowncoder changed the title FEATURE: TypeFactory#constructFromCanonical(String) could support java.lang.reflect.Type#getTypeName()'s format Improve TypeFactory.constructFromCanonical() to work with `java.lang.reflect.Type.getTypeName()' format Sep 19, 2017
@luiscleto
Copy link
Author

Awesome! Thanks for the quick work on this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants