summaryrefslogtreecommitdiffstats
path: root/build/annotationProcessors/classloader
diff options
context:
space:
mode:
Diffstat (limited to 'build/annotationProcessors/classloader')
-rw-r--r--build/annotationProcessors/classloader/AnnotatableEntity.java62
-rw-r--r--build/annotationProcessors/classloader/ClassWithOptions.java15
-rw-r--r--build/annotationProcessors/classloader/IterableJarLoadingURLClassLoader.java75
-rw-r--r--build/annotationProcessors/classloader/JarClassIterator.java84
4 files changed, 236 insertions, 0 deletions
diff --git a/build/annotationProcessors/classloader/AnnotatableEntity.java b/build/annotationProcessors/classloader/AnnotatableEntity.java
new file mode 100644
index 000000000..b11a6c49a
--- /dev/null
+++ b/build/annotationProcessors/classloader/AnnotatableEntity.java
@@ -0,0 +1,62 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.annotationProcessors.classloader;
+
+import org.mozilla.gecko.annotationProcessors.AnnotationInfo;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+/**
+ * Union type to hold either a method, field, or ctor. Allows us to iterate "The generatable stuff", despite
+ * the fact that such things can be of either flavour.
+ */
+public class AnnotatableEntity {
+ public enum ENTITY_TYPE {METHOD, NATIVE, FIELD, CONSTRUCTOR}
+
+ private final Member mMember;
+ public final ENTITY_TYPE mEntityType;
+
+ public final AnnotationInfo mAnnotationInfo;
+
+ public AnnotatableEntity(Member aObject, AnnotationInfo aAnnotationInfo) {
+ mMember = aObject;
+ mAnnotationInfo = aAnnotationInfo;
+
+ if (aObject instanceof Method) {
+ if (Modifier.isNative(aObject.getModifiers())) {
+ mEntityType = ENTITY_TYPE.NATIVE;
+ } else {
+ mEntityType = ENTITY_TYPE.METHOD;
+ }
+ } else if (aObject instanceof Field) {
+ mEntityType = ENTITY_TYPE.FIELD;
+ } else {
+ mEntityType = ENTITY_TYPE.CONSTRUCTOR;
+ }
+ }
+
+ public Method getMethod() {
+ if (mEntityType != ENTITY_TYPE.METHOD && mEntityType != ENTITY_TYPE.NATIVE) {
+ throw new UnsupportedOperationException("Attempt to cast to unsupported member type.");
+ }
+ return (Method) mMember;
+ }
+ public Field getField() {
+ if (mEntityType != ENTITY_TYPE.FIELD) {
+ throw new UnsupportedOperationException("Attempt to cast to unsupported member type.");
+ }
+ return (Field) mMember;
+ }
+ public Constructor<?> getConstructor() {
+ if (mEntityType != ENTITY_TYPE.CONSTRUCTOR) {
+ throw new UnsupportedOperationException("Attempt to cast to unsupported member type.");
+ }
+ return (Constructor<?>) mMember;
+ }
+}
diff --git a/build/annotationProcessors/classloader/ClassWithOptions.java b/build/annotationProcessors/classloader/ClassWithOptions.java
new file mode 100644
index 000000000..070cff8b6
--- /dev/null
+++ b/build/annotationProcessors/classloader/ClassWithOptions.java
@@ -0,0 +1,15 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.annotationProcessors.classloader;
+
+public class ClassWithOptions {
+ public final Class<?> wrappedClass;
+ public final String generatedName;
+
+ public ClassWithOptions(Class<?> someClass, String name) {
+ wrappedClass = someClass;
+ generatedName = name;
+ }
+}
diff --git a/build/annotationProcessors/classloader/IterableJarLoadingURLClassLoader.java b/build/annotationProcessors/classloader/IterableJarLoadingURLClassLoader.java
new file mode 100644
index 000000000..7e74399ca
--- /dev/null
+++ b/build/annotationProcessors/classloader/IterableJarLoadingURLClassLoader.java
@@ -0,0 +1,75 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.annotationProcessors.classloader;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+/**
+ * A classloader which can be initialised with a list of jar files and which can provide an iterator
+ * over the top level classes in the jar files it was initialised with.
+ * classNames is kept sorted to ensure iteration order is consistent across program invocations.
+ * Otherwise, we'd forever be reporting the outdatedness of the generated code as we permute its
+ * contents.
+ */
+public class IterableJarLoadingURLClassLoader extends URLClassLoader {
+ LinkedList<String> classNames = new LinkedList<String>();
+
+ /**
+ * Create an instance and return its iterator. Provides an iterator over the classes in the jar
+ * files provided as arguments.
+ * Inner classes are not supported.
+ *
+ * @param args A list of jar file names an iterator over the classes of which is desired.
+ * @return An iterator over the top level classes in the jar files provided, in arbitrary order.
+ */
+ public static Iterator<ClassWithOptions> getIteratorOverJars(String[] args) {
+ URL[] urlArray = new URL[args.length];
+ LinkedList<String> aClassNames = new LinkedList<String>();
+
+ for (int i = 0; i < args.length; i++) {
+ try {
+ urlArray[i] = (new File(args[i])).toURI().toURL();
+
+ Enumeration<JarEntry> entries = new JarFile(args[i]).entries();
+ while (entries.hasMoreElements()) {
+ JarEntry e = entries.nextElement();
+ String fName = e.getName();
+ if (!fName.endsWith(".class")) {
+ continue;
+ }
+ final String className = fName.substring(0, fName.length() - 6).replace('/', '.');
+
+ aClassNames.add(className);
+ }
+ } catch (IOException e) {
+ System.err.println("Error loading jar file \"" + args[i] + '"');
+ e.printStackTrace(System.err);
+ }
+ }
+ Collections.sort(aClassNames);
+ return new JarClassIterator(new IterableJarLoadingURLClassLoader(urlArray, aClassNames));
+ }
+
+ /**
+ * Constructs a classloader capable of loading all classes given as URLs in urls. Used by static
+ * method above.
+ *
+ * @param urls URLs for all classes the new instance shall be capable of loading.
+ * @param aClassNames A list of names of the classes this instance shall be capable of loading.
+ */
+ protected IterableJarLoadingURLClassLoader(URL[] urls, LinkedList<String> aClassNames) {// Array to populate with URLs for each class in the given jars.
+ super(urls);
+ classNames = aClassNames;
+ }
+}
diff --git a/build/annotationProcessors/classloader/JarClassIterator.java b/build/annotationProcessors/classloader/JarClassIterator.java
new file mode 100644
index 000000000..452de8131
--- /dev/null
+++ b/build/annotationProcessors/classloader/JarClassIterator.java
@@ -0,0 +1,84 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.annotationProcessors.classloader;
+
+import java.util.Iterator;
+
+/**
+ * Class for iterating over an IterableJarLoadingURLClassLoader's classes.
+ *
+ * This class is not thread safe: use it only from a single thread.
+ */
+public class JarClassIterator implements Iterator<ClassWithOptions> {
+ private IterableJarLoadingURLClassLoader mTarget;
+ private Iterator<String> mTargetClassListIterator;
+
+ private ClassWithOptions lookAhead;
+
+ public JarClassIterator(IterableJarLoadingURLClassLoader aTarget) {
+ mTarget = aTarget;
+ mTargetClassListIterator = aTarget.classNames.iterator();
+ }
+
+ @Override
+ public boolean hasNext() {
+ return fillLookAheadIfPossible();
+ }
+
+ @Override
+ public ClassWithOptions next() {
+ if (!fillLookAheadIfPossible()) {
+ throw new IllegalStateException("Failed to look ahead in next()!");
+ }
+ ClassWithOptions next = lookAhead;
+ lookAhead = null;
+ return next;
+ }
+
+ private boolean fillLookAheadIfPossible() {
+ if (lookAhead != null) {
+ return true;
+ }
+
+ if (!mTargetClassListIterator.hasNext()) {
+ return false;
+ }
+
+ String className = mTargetClassListIterator.next();
+ try {
+ Class<?> ret = mTarget.loadClass(className);
+
+ // Incremental builds can leave stale classfiles in the jar. Such classfiles will cause
+ // an exception at this point. We can safely ignore these classes - they cannot possibly
+ // ever be loaded as they conflict with their parent class and will be killed by
+ // Proguard later on anyway.
+ final Class<?> enclosingClass;
+ try {
+ enclosingClass = ret.getEnclosingClass();
+ } catch (IncompatibleClassChangeError e) {
+ return fillLookAheadIfPossible();
+ }
+
+ if (enclosingClass != null) {
+ // Anonymous inner class - unsupported.
+ // Or named inner class, which will be processed when we process the outer class.
+ return fillLookAheadIfPossible();
+ }
+
+ lookAhead = new ClassWithOptions(ret, ret.getSimpleName());
+ return true;
+ } catch (ClassNotFoundException e) {
+ System.err.println("Unable to enumerate class: " + className + ". Corrupted jar file?");
+ e.printStackTrace();
+ System.exit(2);
+ }
+ return false;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException("Removal of classes from iterator not supported.");
+ }
+}