/** * This method must be provided by the vm vendor, as it is used by other provided class * implementations. For example, java.io.ObjectInputStream.readObject() and * java.io.ObjectInputStream.resolveProxyClass(). It is also useful for other classes, such as * java.rmi.server.RMIClassLoader. Walk the stack and answer the most recent non-null and * non-bootstrap ClassLoader on the stack of the calling thread. If no such ClassLoader is found, * null is returned. Notes: 1) This method operates on the defining classes of methods on stack. * NOT the classes of receivers. * * @return the first non-bootstrap ClassLoader on the stack */ public static final ClassLoader getNonBootstrapClassLoader() { StackBrowser browser = new StackBrowser(); browser.init(); while (browser.hasMoreFrames()) { ClassLoader cl = browser.getClassLoader(); if (cl != BootstrapClassLoader.getBootstrapClassLoader() && cl != null) { return cl; } browser.up(); } return null; }
// TODO: Harmony static Class<?>[] getStackClasses(int maxDepth, boolean stopAtPrivileged) { StackBrowser browser = new StackBrowser(); if (maxDepth == -1) { browser.init(); maxDepth = 0; while (browser.hasMoreFrames()) { maxDepth++; browser.up(); } } if (maxDepth == 0) return new Class[0]; else if (maxDepth < 0) { throw new Error("Unexpected negative call stack size" + maxDepth); } Class<?>[] result = new Class[maxDepth]; browser.init(); for (int i = 0; i < maxDepth; i++) { result[i] = browser.getCurrentClass().getClassForType(); browser.up(); } return result; }