Skip to content

Commit 798da68

Browse files
committed
Fix #1768
1 parent 78761f1 commit 798da68

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/main/java/com/fasterxml/jackson/databind/type/TypeParser.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public TypeParser withFactory(TypeFactory f) {
2828

2929
public JavaType parse(String canonical) throws IllegalArgumentException
3030
{
31-
canonical = canonical.trim();
32-
MyTokenizer tokens = new MyTokenizer(canonical);
31+
MyTokenizer tokens = new MyTokenizer(canonical.trim());
3332
JavaType type = parseType(tokens);
3433
// must be end, now
3534
if (tokens.hasMoreTokens()) {
@@ -90,19 +89,18 @@ protected Class<?> findClass(String className, MyTokenizer tokens)
9089

9190
protected IllegalArgumentException _problem(MyTokenizer tokens, String msg)
9291
{
93-
return new IllegalArgumentException("Failed to parse type '"+tokens.getAllInput()
94-
+"' (remaining: '"+tokens.getRemainingInput()+"'): "+msg);
92+
return new IllegalArgumentException(String.format("Failed to parse type '%s' (remaining: '%s'): %s",
93+
tokens.getAllInput(), tokens.getRemainingInput(), msg));
9594
}
9695

97-
final static class MyTokenizer
98-
extends StringTokenizer
96+
final static class MyTokenizer extends StringTokenizer
9997
{
10098
protected final String _input;
10199

102100
protected int _index;
103101

104102
protected String _pushbackToken;
105-
103+
106104
public MyTokenizer(String str) {
107105
super(str, "<,>", true);
108106
_input = str;
@@ -121,18 +119,19 @@ public String nextToken() {
121119
_pushbackToken = null;
122120
} else {
123121
token = super.nextToken();
122+
_index += token.length();
123+
token = token.trim();
124124
}
125-
_index += token.length();
126125
return token;
127126
}
128127

129128
public void pushBack(String token) {
130129
_pushbackToken = token;
131-
_index -= token.length();
130+
// let's NOT change index for now, since token may have been trim()ed
132131
}
133-
132+
134133
public String getAllInput() { return _input; }
135-
public String getUsedInput() { return _input.substring(0, _index); }
134+
// public String getUsedInput() { return _input.substring(0, _index); }
136135
public String getRemainingInput() { return _input.substring(_index); }
137136
}
138137
}

src/test/java/com/fasterxml/jackson/databind/type/TestTypeFactory.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,20 @@ public void testCanonicalNames()
231231
assertEquals("java.util.EnumMap<com.fasterxml.jackson.databind.type.TestTypeFactory$EnumForCanonical,java.lang.String>",
232232
can);
233233
assertEquals(t, tf.constructFromCanonical(can));
234-
234+
}
235+
236+
// [databind#1768]
237+
@SuppressWarnings("serial")
238+
public void testCanonicalWithSpaces()
239+
{
240+
TypeFactory tf = TypeFactory.defaultInstance();
241+
Object objects = new TreeMap<Object, Object>() { }; // to get subtype
242+
String reflectTypeName = objects.getClass().getGenericSuperclass().getTypeName();
243+
JavaType t1 = tf.constructType(objects.getClass().getGenericSuperclass());
244+
// This will throw an Exception if you don't remove all white spaces from the String.
245+
JavaType t2 = tf.constructFromCanonical(reflectTypeName);
246+
assertNotNull(t2);
247+
assertEquals(t2, t1);
235248
}
236249

237250
/*

0 commit comments

Comments
 (0)