Experience has shown that refactoring complex exception detail message
constructions to a separate method tends to improve performance
with JDK's runtime compilers.
--- /u/martin/ws/dolphin/src/share/classes/java/lang/Class.java 2007-06-28 14:57:58.260063000 -0700
+++ /u/martin/ws/checked/src/share/classes/java/lang/Class.java 2007-07-26 16:55:10.570075000 -0700
@@ -3005,11 +3005,14 @@
*/
public T cast(Object obj) {
if (obj != null && !isInstance(obj))
- throw new ClassCastException("Cannot cast " + obj.getClass().getName()
- + " to " + getName());
+ throw new ClassCastException(cannotCastMsg(obj));
return (T) obj;
}
+ private String cannotCastMsg(Object obj) {
+ return "Cannot cast " + obj.getClass().getName() + " to " + getName();
+ }
+
/**
* Casts this {@code Class} object to represent a subclass of the class
* represented by the specified class object. Checks that that the cast