/** * Chain together a ClassReader than will read the given class and a ClassWriter to write out a * new class with an adapter in the middle to add annotations * * @param fromName the name of the class we're coming from */ private static void adaptClass(String fromName) { if (VERBOSE) System.out.println("Adding annotations to class: " + fromName); // gets an input stream to read the bytecode of the class String resource = fromName.replace('.', '/') + ".class"; InputStream is = BootstrapClassLoader.getBootstrapClassLoader().getResourceAsStream(resource); byte[] b; // adapts the class on the fly try { ClassReader cr = new ClassReader(is); ClassWriter cw = new ClassWriter(0); ClassVisitor cv = new AddAnnotationClassAdapter(cw, fromName); cr.accept(cv, 0); b = cw.toByteArray(); } catch (Exception e) { throw new Error("Couldn't find class " + fromName + " (" + resource + ")", e); } // store the adapted class on disk try { File file = new File(destinationDir + resource); new File(file.getParent()).mkdirs(); // ensure we have a directory to write to FileOutputStream fos = new FileOutputStream(file); fos.write(b); fos.close(); } catch (Exception e) { throw new Error("Error writing to " + destinationDir + resource + " to disk", e); } }
public ClassLoader getClassLoader() { SecurityManager security = System.getSecurityManager(); if (security != null) { ClassLoader parentCL = RVMClass.getClassLoaderFromStackFrame(1); if (parentCL != null) { security.checkPermission(new RuntimePermission("getClassLoader")); } } ClassLoader cl = type.getClassLoader(); return cl == BootstrapClassLoader.getBootstrapClassLoader() ? null : cl; }
/** * 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; }
/** * This method must be provided by the vm vendor, as it is used by * org.apache.harmony.kernel.vm.MsgHelp.setLocale() to get the bootstrap ClassLoader. MsgHelp uses * the bootstrap ClassLoader to find the resource bundle of messages packaged with the bootstrap * classes. Returns the ClassLoader of the method (including natives) at the specified depth on * the stack of the calling thread. Frames representing the VM implementation of java.lang.reflect * are not included in the list. This is not a public method as it can return the bootstrap class * loader, which should not be accessed by non-bootstrap classes. Notes: * * <ul> * <li>This method operates on the defining classes of methods on stack. NOT the classes of * receivers. * <li>The item at depth zero is the caller of this method * </ul> * * @param depth the stack depth of the requested ClassLoader * @return the ClassLoader at the specified depth * @see java.lang.ClassLoader#getStackClassLoader */ static final ClassLoader getStackClassLoader(int depth) { if (org.jikesrvm.VM.runningVM) { ClassLoader ans = RVMClass.getClassLoaderFromStackFrame(depth); if (ans == BootstrapClassLoader.getBootstrapClassLoader()) { return null; } else { return ans; } } else { return null; } };
/** * This method must be included, as it is used by ResourceBundle.getBundle(), and other places as * well. The reference implementation of this method uses the getStackClassLoader() method. * Returns the ClassLoader of the method that called the caller. i.e. A.x() calls B.y() calls * callerClassLoader(), A's ClassLoader will be returned. Returns null for the bootstrap * ClassLoader. * * @return a ClassLoader or null for the bootstrap ClassLoader * @throws SecurityException when called from a non-bootstrap Class */ public static ClassLoader callerClassLoader() { if (org.jikesrvm.VM.runningVM) { ClassLoader ans = RVMClass.getClassLoaderFromStackFrame(2); if (ans == BootstrapClassLoader.getBootstrapClassLoader()) { return null; } else { return ans; } } else { return null; } }
public static Class<?> forName(String className, boolean initialize, ClassLoader classLoader) throws ClassNotFoundException, LinkageError, ExceptionInInitializerError { if (classLoader == null) { SecurityManager security = System.getSecurityManager(); if (security != null) { ClassLoader parentCL = RVMClass.getClassLoaderFromStackFrame(1); if (parentCL != null) { try { security.checkPermission(new RuntimePermission("getClassLoader")); } catch (SecurityException e) { throw new ClassNotFoundException( "Security exception when" + " trying to get a classloader so we can load the" + " class named \"" + className + "\"", e); } } } classLoader = BootstrapClassLoader.getBootstrapClassLoader(); } return forNameInternal(className, initialize, classLoader); }
/** * This method must be provided by the vm vendor, as it is used by * org.apache.harmony.luni.util.MsgHelp.setLocale() to get the bootstrap ClassLoader. MsgHelp uses * the bootstrap ClassLoader to find the resource bundle of messages packaged with the bootstrap * classes. The reference implementation of this method uses the getStackClassLoader() method. * * <p>Returns the ClassLoader of the method that called the caller. i.e. A.x() calls B.y() calls * callerClassLoader(), A's ClassLoader will be returned. Returns null for the bootstrap * ClassLoader. * * @return a ClassLoader * @throws SecurityException when called from a non-bootstrap Class */ public static ClassLoader bootCallerClassLoader() { return BootstrapClassLoader.getBootstrapClassLoader(); }
public InputStream getResourceAsStream(String resName) { ClassLoader loader = type.getClassLoader(); if (loader == BootstrapClassLoader.getBootstrapClassLoader()) return ClassLoader.getSystemResourceAsStream(toResourceName(resName)); else return loader.getResourceAsStream(toResourceName(resName)); }