IterableJarLoadingURLClassLoader.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. package org.mozilla.gecko.annotationProcessors.classloader;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.net.URL;
  8. import java.net.URLClassLoader;
  9. import java.util.Collections;
  10. import java.util.Enumeration;
  11. import java.util.Iterator;
  12. import java.util.LinkedList;
  13. import java.util.jar.JarEntry;
  14. import java.util.jar.JarFile;
  15. /**
  16. * A classloader which can be initialised with a list of jar files and which can provide an iterator
  17. * over the top level classes in the jar files it was initialised with.
  18. * classNames is kept sorted to ensure iteration order is consistent across program invocations.
  19. * Otherwise, we'd forever be reporting the outdatedness of the generated code as we permute its
  20. * contents.
  21. */
  22. public class IterableJarLoadingURLClassLoader extends URLClassLoader {
  23. LinkedList<String> classNames = new LinkedList<String>();
  24. /**
  25. * Create an instance and return its iterator. Provides an iterator over the classes in the jar
  26. * files provided as arguments.
  27. * Inner classes are not supported.
  28. *
  29. * @param args A list of jar file names an iterator over the classes of which is desired.
  30. * @return An iterator over the top level classes in the jar files provided, in arbitrary order.
  31. */
  32. public static Iterator<ClassWithOptions> getIteratorOverJars(String[] args) {
  33. URL[] urlArray = new URL[args.length];
  34. LinkedList<String> aClassNames = new LinkedList<String>();
  35. for (int i = 0; i < args.length; i++) {
  36. try {
  37. urlArray[i] = (new File(args[i])).toURI().toURL();
  38. Enumeration<JarEntry> entries = new JarFile(args[i]).entries();
  39. while (entries.hasMoreElements()) {
  40. JarEntry e = entries.nextElement();
  41. String fName = e.getName();
  42. if (!fName.endsWith(".class")) {
  43. continue;
  44. }
  45. final String className = fName.substring(0, fName.length() - 6).replace('/', '.');
  46. aClassNames.add(className);
  47. }
  48. } catch (IOException e) {
  49. System.err.println("Error loading jar file \"" + args[i] + '"');
  50. e.printStackTrace(System.err);
  51. }
  52. }
  53. Collections.sort(aClassNames);
  54. return new JarClassIterator(new IterableJarLoadingURLClassLoader(urlArray, aClassNames));
  55. }
  56. /**
  57. * Constructs a classloader capable of loading all classes given as URLs in urls. Used by static
  58. * method above.
  59. *
  60. * @param urls URLs for all classes the new instance shall be capable of loading.
  61. * @param aClassNames A list of names of the classes this instance shall be capable of loading.
  62. */
  63. protected IterableJarLoadingURLClassLoader(URL[] urls, LinkedList<String> aClassNames) {// Array to populate with URLs for each class in the given jars.
  64. super(urls);
  65. classNames = aClassNames;
  66. }
  67. }