michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.annotationProcessors.classloader; michael@0: michael@0: import java.io.File; michael@0: import java.io.IOException; michael@0: import java.net.URL; michael@0: import java.net.URLClassLoader; michael@0: import java.util.Collections; michael@0: import java.util.Enumeration; michael@0: import java.util.Iterator; michael@0: import java.util.LinkedList; michael@0: import java.util.jar.JarEntry; michael@0: import java.util.jar.JarFile; michael@0: michael@0: /** michael@0: * A classloader which can be initialised with a list of jar files and which can provide an iterator michael@0: * over the top level classes in the jar files it was initialised with. michael@0: * classNames is kept sorted to ensure iteration order is consistent across program invocations. michael@0: * Otherwise, we'd forever be reporting the outdatedness of the generated code as we permute its michael@0: * contents. michael@0: */ michael@0: public class IterableJarLoadingURLClassLoader extends URLClassLoader { michael@0: LinkedList classNames = new LinkedList(); michael@0: michael@0: /** michael@0: * Create an instance and return its iterator. Provides an iterator over the classes in the jar michael@0: * files provided as arguments. michael@0: * Inner classes are not supported. michael@0: * michael@0: * @param args A list of jar file names an iterator over the classes of which is desired. michael@0: * @return An iterator over the top level classes in the jar files provided, in arbitrary order. michael@0: */ michael@0: public static Iterator getIteratorOverJars(String[] args) { michael@0: URL[] urlArray = new URL[args.length]; michael@0: LinkedList aClassNames = new LinkedList(); michael@0: michael@0: for (int i = 0; i < args.length; i++) { michael@0: try { michael@0: urlArray[i] = (new File(args[i])).toURI().toURL(); michael@0: michael@0: Enumeration entries = new JarFile(args[i]).entries(); michael@0: while (entries.hasMoreElements()) { michael@0: JarEntry e = entries.nextElement(); michael@0: String fName = e.getName(); michael@0: if (!fName.endsWith(".class")) { michael@0: continue; michael@0: } michael@0: final String className = fName.substring(0, fName.length() - 6).replace('/', '.'); michael@0: michael@0: aClassNames.add(className); michael@0: } michael@0: } catch (IOException e) { michael@0: System.err.println("Error loading jar file \"" + args[i] + '"'); michael@0: e.printStackTrace(System.err); michael@0: } michael@0: } michael@0: Collections.sort(aClassNames); michael@0: return new JarClassIterator(new IterableJarLoadingURLClassLoader(urlArray, aClassNames)); michael@0: } michael@0: michael@0: /** michael@0: * Constructs a classloader capable of loading all classes given as URLs in urls. Used by static michael@0: * method above. michael@0: * michael@0: * @param urls URLs for all classes the new instance shall be capable of loading. michael@0: * @param aClassNames A list of names of the classes this instance shall be capable of loading. michael@0: */ michael@0: protected IterableJarLoadingURLClassLoader(URL[] urls, LinkedList aClassNames) {// Array to populate with URLs for each class in the given jars. michael@0: super(urls); michael@0: classNames = aClassNames; michael@0: } michael@0: }