1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/build/annotationProcessors/classloader/IterableJarLoadingURLClassLoader.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,75 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.annotationProcessors.classloader; 1.9 + 1.10 +import java.io.File; 1.11 +import java.io.IOException; 1.12 +import java.net.URL; 1.13 +import java.net.URLClassLoader; 1.14 +import java.util.Collections; 1.15 +import java.util.Enumeration; 1.16 +import java.util.Iterator; 1.17 +import java.util.LinkedList; 1.18 +import java.util.jar.JarEntry; 1.19 +import java.util.jar.JarFile; 1.20 + 1.21 +/** 1.22 + * A classloader which can be initialised with a list of jar files and which can provide an iterator 1.23 + * over the top level classes in the jar files it was initialised with. 1.24 + * classNames is kept sorted to ensure iteration order is consistent across program invocations. 1.25 + * Otherwise, we'd forever be reporting the outdatedness of the generated code as we permute its 1.26 + * contents. 1.27 + */ 1.28 +public class IterableJarLoadingURLClassLoader extends URLClassLoader { 1.29 + LinkedList<String> classNames = new LinkedList<String>(); 1.30 + 1.31 + /** 1.32 + * Create an instance and return its iterator. Provides an iterator over the classes in the jar 1.33 + * files provided as arguments. 1.34 + * Inner classes are not supported. 1.35 + * 1.36 + * @param args A list of jar file names an iterator over the classes of which is desired. 1.37 + * @return An iterator over the top level classes in the jar files provided, in arbitrary order. 1.38 + */ 1.39 + public static Iterator<ClassWithOptions> getIteratorOverJars(String[] args) { 1.40 + URL[] urlArray = new URL[args.length]; 1.41 + LinkedList<String> aClassNames = new LinkedList<String>(); 1.42 + 1.43 + for (int i = 0; i < args.length; i++) { 1.44 + try { 1.45 + urlArray[i] = (new File(args[i])).toURI().toURL(); 1.46 + 1.47 + Enumeration<JarEntry> entries = new JarFile(args[i]).entries(); 1.48 + while (entries.hasMoreElements()) { 1.49 + JarEntry e = entries.nextElement(); 1.50 + String fName = e.getName(); 1.51 + if (!fName.endsWith(".class")) { 1.52 + continue; 1.53 + } 1.54 + final String className = fName.substring(0, fName.length() - 6).replace('/', '.'); 1.55 + 1.56 + aClassNames.add(className); 1.57 + } 1.58 + } catch (IOException e) { 1.59 + System.err.println("Error loading jar file \"" + args[i] + '"'); 1.60 + e.printStackTrace(System.err); 1.61 + } 1.62 + } 1.63 + Collections.sort(aClassNames); 1.64 + return new JarClassIterator(new IterableJarLoadingURLClassLoader(urlArray, aClassNames)); 1.65 + } 1.66 + 1.67 + /** 1.68 + * Constructs a classloader capable of loading all classes given as URLs in urls. Used by static 1.69 + * method above. 1.70 + * 1.71 + * @param urls URLs for all classes the new instance shall be capable of loading. 1.72 + * @param aClassNames A list of names of the classes this instance shall be capable of loading. 1.73 + */ 1.74 + protected IterableJarLoadingURLClassLoader(URL[] urls, LinkedList<String> aClassNames) {// Array to populate with URLs for each class in the given jars. 1.75 + super(urls); 1.76 + classNames = aClassNames; 1.77 + } 1.78 +}