summaryrefslogtreecommitdiffstats
path: root/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader
diff options
context:
space:
mode:
Diffstat (limited to 'mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader')
-rw-r--r--mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/AnnotatableEntity.java68
-rw-r--r--mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/ClassWithOptions.java36
-rw-r--r--mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/IterableJarLoadingURLClassLoader.java76
-rw-r--r--mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/JarClassIterator.java105
4 files changed, 285 insertions, 0 deletions
diff --git a/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/AnnotatableEntity.java b/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/AnnotatableEntity.java
new file mode 100644
index 0000000000..b2df6587c1
--- /dev/null
+++ b/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/AnnotatableEntity.java
@@ -0,0 +1,68 @@
+/* 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.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import org.mozilla.gecko.annotationProcessors.AnnotationInfo;
+
+/**
+ * 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/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/ClassWithOptions.java b/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/ClassWithOptions.java
new file mode 100644
index 0000000000..d1e4e6cfe0
--- /dev/null
+++ b/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/ClassWithOptions.java
@@ -0,0 +1,36 @@
+/* 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.utils.GeneratableElementIterator;
+
+public class ClassWithOptions {
+ public final Class<?> wrappedClass;
+ public final String generatedName;
+ public final String ifdef;
+
+ public ClassWithOptions(Class<?> someClass, String name, String ifdef) {
+ wrappedClass = someClass;
+ generatedName = name;
+ this.ifdef = ifdef;
+ }
+
+ public boolean hasGenerated() {
+ final GeneratableElementIterator methodIterator = new GeneratableElementIterator(this);
+
+ if (methodIterator.hasNext()) {
+ return true;
+ }
+
+ final ClassWithOptions[] innerClasses = methodIterator.getInnerClasses();
+ for (ClassWithOptions innerClass : innerClasses) {
+ if (innerClass.hasGenerated()) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
diff --git a/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/IterableJarLoadingURLClassLoader.java b/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/IterableJarLoadingURLClassLoader.java
new file mode 100644
index 0000000000..50200ef3ec
--- /dev/null
+++ b/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/IterableJarLoadingURLClassLoader.java
@@ -0,0 +1,76 @@
+/* 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/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/JarClassIterator.java b/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/JarClassIterator.java
new file mode 100644
index 0000000000..f93667be8d
--- /dev/null
+++ b/mobile/android/annotations/src/main/java/org/mozilla/gecko/annotationProcessors/classloader/JarClassIterator.java
@@ -0,0 +1,105 @@
+/* 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.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.Iterator;
+
+/**
+ * Class for iterating over an IterableJarLoadingURLClassLoader's classes.
+ *
+ * <p>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();
+ }
+
+ String ifdef = "";
+ for (final Annotation annotation : ret.getDeclaredAnnotations()) {
+ Class<? extends Annotation> annotationType = annotation.annotationType();
+ if (!annotationType.getName().equals("org.mozilla.gecko.annotation.BuildFlag")) {
+ continue;
+ }
+
+ try {
+ final Method valueMethod = annotationType.getDeclaredMethod("value");
+ valueMethod.setAccessible(true);
+ ifdef = (String) valueMethod.invoke(annotation);
+ break;
+ } catch (final Exception e) {
+ System.err.println("Unable to read BuildFlag annotation.");
+ e.printStackTrace(System.err);
+ System.exit(1);
+ }
+ }
+
+ lookAhead = new ClassWithOptions(ret, ret.getSimpleName(), ifdef);
+ 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.");
+ }
+}