@@ -127,16 +127,43 @@ protected JavaType _narrow(Class<?> subclass)
127
127
}
128
128
// Should we check that there is a sub-class relationship?
129
129
// 15-Jan-2016, tatu: Almost yes, but there are some complications with
130
- // placeholder values, so no .
131
- /*
130
+ // placeholder values (`Void`, `NoClass`), so can not quite do yet .
131
+ // TODO: fix in 2.8
132
132
if (!_class .isAssignableFrom (subclass )) {
133
+ /*
133
134
throw new IllegalArgumentException("Class "+subclass.getName()+" not sub-type of "
134
135
+_class.getName());
136
+ */
137
+ return new SimpleType (subclass , _bindings , this , _superInterfaces ,
138
+ _valueHandler , _typeHandler , _asStatic );
135
139
}
136
- */
137
- // 15-Jan-2015, tatu: Not correct; should really re-resolve...
138
- return new SimpleType (subclass , _bindings , this , _superInterfaces ,
139
- _valueHandler , _typeHandler , _asStatic );
140
+ // Otherwise, stitch together the hierarchy. First, super-class
141
+ Class <?> next = subclass .getSuperclass ();
142
+ if (next == _class ) { // straight up parent class? Great.
143
+ return new SimpleType (subclass , _bindings , this ,
144
+ _superInterfaces , _valueHandler , _typeHandler , _asStatic );
145
+ }
146
+ if ((next != null ) && _class .isAssignableFrom (next )) {
147
+ JavaType superb = _narrow (next );
148
+ return new SimpleType (subclass , _bindings , superb ,
149
+ null , _valueHandler , _typeHandler , _asStatic );
150
+ }
151
+ // if not found, try a super-interface
152
+ Class <?>[] nextI = subclass .getInterfaces ();
153
+ for (Class <?> iface : nextI ) {
154
+ if (iface == _class ) { // directly implemented
155
+ return new SimpleType (subclass , _bindings , null ,
156
+ new JavaType [] { this }, _valueHandler , _typeHandler , _asStatic );
157
+ }
158
+ if (_class .isAssignableFrom (iface )) { // indirect, so recurse
159
+ JavaType superb = _narrow (iface );
160
+ return new SimpleType (subclass , _bindings , null ,
161
+ new JavaType [] { superb }, _valueHandler , _typeHandler , _asStatic );
162
+ }
163
+ }
164
+ // should not get here but...
165
+ throw new IllegalArgumentException ("Internal error: Can not resolve sub-type for Class " +subclass .getName ()+" to "
166
+ +_class .getName ());
140
167
}
141
168
142
169
@ Override
0 commit comments