Skip to content

Commit afea85c

Browse files
committed
IndexItem: give more information for all CCEs
It is nice to know the original object type, and the expected target type, regardless of exactly what went wrong in the adaptation.
1 parent f15a313 commit afea85c

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/main/java/org/scijava/annotations/IndexItem.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ else if (expectedType == Class.class) {
191191
return loader.loadClass((String) o);
192192
}
193193
catch (Throwable t) {
194-
throw cce(t);
194+
throw cce(o, expectedType, t);
195195
}
196196
}
197197
else if (expectedType.isArray()) {
@@ -214,7 +214,7 @@ else if (Enum.class.isAssignableFrom(expectedType)) {
214214
return loader.loadClass(enumName).getField(constName).get(null);
215215
}
216216
catch (Throwable t) {
217-
throw cce(t);
217+
throw cce(o, expectedType, t);
218218
}
219219
}
220220
else if (Annotation.class.isAssignableFrom(expectedType)) {
@@ -224,13 +224,20 @@ else if (Annotation.class.isAssignableFrom(expectedType)) {
224224
final Map<Object, Object> map = (Map<Object, Object>) o;
225225
return proxy(annotation, loader, map);
226226
}
227-
throw new ClassCastException("Cannot cast object of type " +
228-
o.getClass().getName() + " to " + expectedType.getName());
227+
throw cce(o, expectedType, null);
229228
}
230229

231-
private static ClassCastException cce(final Throwable cause) {
232-
final ClassCastException cce = new ClassCastException();
233-
cce.initCause(cause);
230+
private static ClassCastException cce(final Object o,
231+
final Class<?> expectedType, final Throwable cause)
232+
{
233+
final String oType = o == null ? "<null>" : o.getClass().getName();
234+
final String eType =
235+
expectedType == null ? "<null>" : expectedType.getName();
236+
final ClassCastException cce =
237+
new ClassCastException("Cannot cast object of type " + oType + " to " +
238+
eType);
239+
if (cause != null) cce.initCause(cause);
234240
return cce;
235241
}
242+
236243
}

0 commit comments

Comments
 (0)