Loading... (original) (raw)
Currently, Class.getClassLoader() is a relatively expensive operation,
presumably because of jni overhead.
Some core library code would like to use it to optimize some operations.
E.g. when using a Charset to decode a byte stream into a char array,
one might have to make a defensive copy of the output char array because
a malicious Charset might keep a reference to it, thereby
making it possible for a String to change its value in the future,
which is a big no-no. Being able to ask whether a Charset is
provided as part of the JDK, cheaply, would be a big help to optimizing this
for "builtin" classes.
boolean isTrusted(Object x) {
return System.getSecurityManager() == null ||
x.getClass().getClassLoader() == null;
}
One suspects that getClassLoader() can be trivially optimized to a field access,
just like getClass() was.
This bug is filed against compiler2, but of course we encourage a fix in compiler1 as well.